File size: 2,775 Bytes
d9494a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { compareArraysOfObjectsByProperty } from '@/utils/array/compareArraysOfObjectsByProperty';

type TestObject = {
  id: string;
  name: string;
};

describe('compareArraysOfObjectsByProperty', () => {
  it('should return false when both arrays are empty', () => {
    expect(compareArraysOfObjectsByProperty([], [], 'id')).toBe(false);
  });

  it('should return false when arrays have same objects by property', () => {
    const arrayA: TestObject[] = [
      { id: '1', name: 'Test 1' },
      { id: '2', name: 'Test 2' },
    ];
    const arrayB: TestObject[] = [
      { id: '1', name: 'Different Name' },
      { id: '2', name: 'Another Name' },
    ];

    expect(compareArraysOfObjectsByProperty(arrayA, arrayB, 'id')).toBe(false);
  });

  it('should return true when arrays have different lengths', () => {
    const arrayA: TestObject[] = [
      { id: '1', name: 'Test 1' },
      { id: '2', name: 'Test 2' },
    ];
    const arrayB: TestObject[] = [{ id: '1', name: 'Test 1' }];

    expect(compareArraysOfObjectsByProperty(arrayA, arrayB, 'id')).toBe(true);
  });

  it('should return true when arrayA has items not in arrayB', () => {
    const arrayA: TestObject[] = [
      { id: '1', name: 'Test 1' },
      { id: '2', name: 'Test 2' },
    ];
    const arrayB: TestObject[] = [
      { id: '1', name: 'Test 1' },
      { id: '3', name: 'Test 3' },
    ];

    expect(compareArraysOfObjectsByProperty(arrayA, arrayB, 'id')).toBe(true);
  });

  it('should return true when arrayB has items not in arrayA', () => {
    const arrayA: TestObject[] = [
      { id: '1', name: 'Test 1' },
      { id: '3', name: 'Test 3' },
    ];
    const arrayB: TestObject[] = [
      { id: '1', name: 'Test 1' },
      { id: '2', name: 'Test 2' },
    ];

    expect(compareArraysOfObjectsByProperty(arrayA, arrayB, 'id')).toBe(true);
  });

  it('should return false when arrays have same items in different order', () => {
    const arrayA: TestObject[] = [
      { id: '1', name: 'Test 1' },
      { id: '2', name: 'Test 2' },
      { id: '3', name: 'Test 3' },
    ];
    const arrayB: TestObject[] = [
      { id: '3', name: 'Test 3' },
      { id: '1', name: 'Test 1' },
      { id: '2', name: 'Test 2' },
    ];

    expect(compareArraysOfObjectsByProperty(arrayA, arrayB, 'id')).toBe(false);
  });

  it('should compare by the specified property', () => {
    const arrayA: TestObject[] = [
      { id: '1', name: 'Alpha' },
      { id: '2', name: 'Beta' },
    ];
    const arrayB: TestObject[] = [
      { id: '3', name: 'Alpha' },
      { id: '4', name: 'Beta' },
    ];

    expect(compareArraysOfObjectsByProperty(arrayA, arrayB, 'name')).toBe(
      false,
    );
    expect(compareArraysOfObjectsByProperty(arrayA, arrayB, 'id')).toBe(true);
  });
});