|
1 | 1 | import test from 'ava'; |
2 | | -import cloneArrayObjects from '.'; |
| 2 | +import cloneArrayObjects from './index.js'; |
3 | 3 |
|
4 | | -test('Return typeerror when arrayInput is not an array.', t => { |
5 | | - const err = t.throws(() => { |
6 | | - cloneArrayObjects(23); |
7 | | - }, TypeError); |
8 | | - t.is(err.message, 'Expected an array, got number'); |
| 4 | +test('should return a new array with cloned objects', t => { |
| 5 | + const input = [ |
| 6 | + {name: 'John', age: 30}, |
| 7 | + {name: 'Jane', age: 25}, |
| 8 | + ]; |
| 9 | + const output = cloneArrayObjects(input); |
| 10 | + |
| 11 | + t.not(output, input); // Should not modify the original array |
| 12 | + t.deepEqual(output, input); // Should have the same values as the input |
| 13 | +}); |
| 14 | + |
| 15 | +test('should throw a TypeError if the input is not an array', t => { |
| 16 | + const input = 'not an array'; |
| 17 | + |
| 18 | + const error = t.throws(() => cloneArrayObjects(input)); |
| 19 | + |
| 20 | + t.is(error.message, `Expected an array, got ${typeof input}`); |
| 21 | +}); |
| 22 | + |
| 23 | +test('should throw a TypeError if any object in the array is null', t => { |
| 24 | + const input = [{name: 'John', age: 30}, null]; |
| 25 | + |
| 26 | + const error = t.throws(() => cloneArrayObjects(input)); |
| 27 | + |
| 28 | + t.is(error.message, 'Expected an object, got object'); |
9 | 29 | }); |
10 | 30 |
|
11 | | -test('Return empty array when no valid input passed.', t => { |
12 | | - t.deepEqual(cloneArrayObjects(), []); |
13 | | - t.deepEqual(cloneArrayObjects([]), []); |
| 31 | +test('should throw a TypeError if any object in the array is not an object', t => { |
| 32 | + const input = [{name: 'John', age: 30}, 'not an object']; |
| 33 | + |
| 34 | + const error = t.throws(() => cloneArrayObjects(input)); |
| 35 | + |
| 36 | + t.is(error.message, 'Expected an object, got string'); |
14 | 37 | }); |
15 | 38 |
|
16 | | -test('Test clone', t => { |
17 | | - const actual = [{a: 1}, {b: 2}]; |
18 | | - const expected = [{a: 1}, {b: 2}]; |
19 | | - const modified = [{a: 1}, {b: 3}]; |
20 | | - t.deepEqual(cloneArrayObjects(actual), expected); |
21 | | - expected[1].b = 3; |
22 | | - t.notDeepEqual(cloneArrayObjects(actual), expected); |
23 | | - t.deepEqual(modified, expected); |
| 39 | +test('should return an empty array if the input is an empty array', t => { |
| 40 | + const input = []; |
| 41 | + const output = cloneArrayObjects(input); |
| 42 | + |
| 43 | + t.deepEqual(output, []); |
24 | 44 | }); |
0 commit comments