File size: 1,452 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
import move from '../../utils/move';

describe('move', () => {
  it('should be able to move element of array', () => {
    const data = [
      {
        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',
      },
    ];
    move(data, 0, 2);
    expect(data).toEqual([
      {
        firstName: '2',
        lastName: 'Luo',
        id: '75309979-e340-49eb-8016-5f67bfb56c1c',
      },
      {
        firstName: '3',
        lastName: 'Luo',
        id: '75309979-e340-49eb-8016-5f67bfb56c1c',
      },
      {
        firstName: '1',
        lastName: 'Luo',
        id: '75309979-e340-49eb-8016-5f67bfb56c1c',
      },
    ]);
  });

  it('should return empty array when data passed was not an array', () => {
    // @ts-expect-error we want to test function on non-array input
    expect(move({}, 0, 3)).toEqual([]);
  });

  it('should move nested item with empty slot', () => {
    expect(move([{ subFields: [{ test: '1' }] }], 0, 1)).toEqual([
      undefined,
      { subFields: [{ test: '1' }] },
    ]);

    expect(move([{ subFields: [{ test: '1' }] }], 0, 2)).toEqual([
      undefined,
      undefined,
      { subFields: [{ test: '1' }] },
    ]);
  });
});