File size: 2,039 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import insert from '../../utils/insert';

describe('insert', () => {
  it('should insert value at specific index in array', () => {
    expect(insert([1, 3, 4], 1, 2)).toEqual([1, 2, 3, 4]);
    expect(
      insert(
        [
          {
            firstName: '1',
            lastName: 'Luo',
            id: '75309979-e340-49eb-8016-5f67bfb56c1c',
          },
          {
            firstName: '2',
            lastName: 'Luo',
            id: '75309979-e340-49eb-8016-5f67bfb56c1c',
          },
          {
            firstName: '4',
            lastName: 'Luo',
            id: '75309979-e340-49eb-8016-5f67bfb56c1c',
          },
        ],
        2,
        {
          firstName: '3',
          lastName: 'Luo',
          id: '75309979-e340-49eb-8016-5f67bfb56c1c',
        },
      ),
    ).toEqual([
      {
        firstName: '1',
        lastName: 'Luo',
        id: '75309979-e340-49eb-8016-5f67bfb56c1c',
      },
      {
        firstName: '2',
        lastName: 'Luo',
        id: '75309979-e340-49eb-8016-5f67bfb56c1c',
      },
      {
        firstName: '3',
        lastName: 'Luo',
        id: '75309979-e340-49eb-8016-5f67bfb56c1c',
      },
      {
        firstName: '4',
        lastName: 'Luo',
        id: '75309979-e340-49eb-8016-5f67bfb56c1c',
      },
    ]);
  });

  it('should insert undefined as value when value to be inserted is falsy', () => {
    expect(insert([1, 2, 4], 2)).toEqual([1, 2, undefined, 4]);
    expect(insert([1, 2, 4], 2, 0)).toEqual([1, 2, 0, 4]);
    expect(insert([1, 2, 4] as (boolean | number)[], 2, false)).toEqual([
      1,
      2,
      false,
      4,
    ]);
    expect(insert([1, 2, 4] as (number | string)[], 2, '')).toEqual([
      1,
      2,
      '',
      4,
    ]);
    expect(insert([1, 2, 4], 2, undefined)).toEqual([1, 2, undefined, 4]);
  });

  it('should spread value when it is an array at one deep-level', () => {
    expect(insert([1, 2], 2, [3, 4])).toEqual([1, 2, 3, 4]);
    expect(insert([1, 2], 2, [3, [4]])).toEqual([1, 2, 3, [4]]);
  });
});