summaryrefslogtreecommitdiffhomepage
path: root/gui/test/list-diff.spec.ts
blob: bca3e72d84eecfba6a8fe61a2bd018bdf6db05d7 (plain)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { expect } from 'chai';
import { it, describe } from 'mocha';
import { calculateItemList, RowDisplayData } from '../src/renderer/components/List';

const prevItems: Array<RowDisplayData<undefined>> = [
  { key: 'a', data: undefined, removing: false },
  { key: 'b', data: undefined, removing: false },
  { key: 'c', data: undefined, removing: false },
  { key: 'd', data: undefined, removing: false },
];

describe('List diff', () => {
  it('Should add item to the beginning', () => {
    const nextItems: Array<RowDisplayData<undefined>> = [
      { key: '1', data: undefined, removing: false },
      ...prevItems,
    ];
    const combinedItems = calculateItemList(prevItems, nextItems);

    expect(combinedItems).to.have.length(5);
    expect(combinedItems.slice(1)).to.deep.equal(prevItems);
    expect(combinedItems).to.deep.equal(nextItems);
  });

  it('Should add item to the end', () => {
    const nextItems: Array<RowDisplayData<undefined>> = [
      ...prevItems,
      { key: '1', data: undefined, removing: false },
    ];
    const combinedItems = calculateItemList(prevItems, nextItems);

    expect(combinedItems).to.have.length(5);
    expect(combinedItems.slice(0, 4)).to.deep.equal(prevItems);
    expect(combinedItems).to.deep.equal(nextItems);
  });

  it('Should add item to the middle', () => {
    const nextItems: Array<RowDisplayData<undefined>> = [
      ...prevItems.slice(0, 2),
      { key: '1', data: undefined, removing: false },
      ...prevItems.slice(2),
    ];
    const combinedItems = calculateItemList(prevItems, nextItems);

    expect(combinedItems).to.have.length(5);
    expect([...combinedItems.slice(0, 2), ...combinedItems.slice(3)]).to.deep.equal(prevItems);
    expect(combinedItems).to.deep.equal(nextItems);
  });

  it('Should remove first item', () => {
    const nextItems = prevItems.slice(1);
    const combinedItems = calculateItemList(prevItems, nextItems);

    expect(combinedItems).to.have.length(4);
    expect(combinedItems.slice(1, 4)).to.deep.equal(prevItems.slice(1, 4));
    expect(combinedItems[0]).to.deep.equal({ ...prevItems[0], removing: true });
  });

  it('Should remove last item', () => {
    const nextItems = prevItems.slice(0, -1);
    const combinedItems = calculateItemList(prevItems, nextItems);

    expect(combinedItems).to.have.length(4);
    expect(combinedItems.slice(0, -1)).to.deep.equal(prevItems.slice(0, -1));
    expect(combinedItems.at(-1)).to.deep.equal({ ...prevItems.at(-1), removing: true });
  });

  it('Should remove middle item', () => {
    const nextItems = [...prevItems.slice(0, 1), ...prevItems.slice(2)];
    const combinedItems = calculateItemList(prevItems, nextItems);

    expect(combinedItems).to.have.length(4);
    expect(combinedItems.slice(0, 1)).to.deep.equal(prevItems.slice(0, 1));
    expect(combinedItems.slice(2)).to.deep.equal(prevItems.slice(2));
    expect(combinedItems[1]).to.deep.equal({ ...prevItems[1], removing: true });
  });

  it('should both add and remove items', () => {
    const nextItems = [
      { key: '1', data: undefined, removing: false },
      ...prevItems.slice(1, -1),
      { key: '2', data: undefined, removing: false },
    ];
    const combinedItems = calculateItemList(prevItems, nextItems);

    expect(combinedItems).to.have.length(6);
    expect(combinedItems[0]).to.deep.equal({ ...prevItems[0], removing: true });
    expect(combinedItems[1]).to.deep.equal(nextItems[0]);
    expect(combinedItems.slice(2, -2)).to.deep.equal(prevItems.slice(1, -1));
    expect(combinedItems.at(-2)).to.deep.equal({ ...prevItems.at(-1), removing: true });
    expect(combinedItems.at(-1)).to.deep.equal(nextItems.at(-1));
  });

  it('should remove item being removed', () => {
    const prevItems: Array<RowDisplayData<undefined>> = [
      { key: '1', data: undefined, removing: true },
    ];
    const nextItems: Array<RowDisplayData<undefined>> = [];
    const combinedItems = calculateItemList(prevItems, nextItems);

    expect(combinedItems).to.deep.equal(prevItems);
  });
});