Skip to content

Commit 22a8260

Browse files
committed
refactor: add unit test for removeRedundantPartialPairs
1 parent b878676 commit 22a8260

File tree

2 files changed

+185
-1
lines changed

2 files changed

+185
-1
lines changed

src/components/compare/compare.utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ function dedupeTuples<T extends [object | undefined, object | undefined]>(
115115
return result
116116
}
117117

118-
function removeRedundantPartialPairs<T extends [object | undefined, object | undefined]>(
118+
export function removeRedundantPartialPairs<T extends [object | undefined, object | undefined]>(
119119
tuples: T[],
120120
): T[] {
121121
const completeAtPosition = [new Set<object>(), new Set<object>()] as const

test/compare.utils.test.ts

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
/**
2+
* Copyright 2024-2025 NetCracker Technology Corporation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { removeRedundantPartialPairs } from '../src/components/compare/compare.utils'
18+
19+
describe('removeRedundantPartialPairs', () => {
20+
test('should return empty array when input is empty', () => {
21+
const result = removeRedundantPartialPairs([])
22+
expect(result).toEqual([])
23+
})
24+
25+
test('should keep all complete pairs when no partial pairs exist', () => {
26+
const objA = { id: 'A' }
27+
const objB = { id: 'B' }
28+
const objC = { id: 'C' }
29+
const objD = { id: 'D' }
30+
31+
const input: [object, object][] = [
32+
[objA, objB],
33+
[objC, objD],
34+
]
35+
36+
const result = removeRedundantPartialPairs(input)
37+
expect(result).toEqual(input)
38+
})
39+
40+
test('should keep all partial pairs when no complete pairs exist', () => {
41+
const objA = { id: 'A' }
42+
const objB = { id: 'B' }
43+
const objC = { id: 'C' }
44+
45+
const input: [object | undefined, object | undefined][] = [
46+
[objA, undefined],
47+
[undefined, objB],
48+
[objC, undefined],
49+
]
50+
51+
const result = removeRedundantPartialPairs(input)
52+
expect(result).toEqual(input)
53+
})
54+
55+
test('should remove partial pair when complete pair exists with same value at position 1 (right)', () => {
56+
const objA = { id: 'A' }
57+
const objB = { id: 'B' }
58+
59+
const input: [object | undefined, object | undefined][] = [
60+
[objA, objB], // Complete pair
61+
[undefined, objB], // Partial pair - should be removed (objB exists in complete pair)
62+
]
63+
64+
const result = removeRedundantPartialPairs(input)
65+
expect(result).toEqual([[objA, objB]])
66+
expect(result).toHaveLength(1)
67+
})
68+
69+
test('should remove partial pair when complete pair exists with same value at position 0 (left)', () => {
70+
const objA = { id: 'A' }
71+
const objB = { id: 'B' }
72+
73+
const input: [object | undefined, object | undefined][] = [
74+
[objA, objB], // Complete pair
75+
[objA, undefined], // Partial pair - should be removed (objA exists in complete pair)
76+
]
77+
78+
const result = removeRedundantPartialPairs(input)
79+
expect(result).toEqual([[objA, objB]])
80+
expect(result).toHaveLength(1)
81+
})
82+
83+
test('should keep partial pair when complete pair exists but with different values', () => {
84+
const objA = { id: 'A' }
85+
const objB = { id: 'B' }
86+
const objC = { id: 'C' }
87+
88+
const input: [object | undefined, object | undefined][] = [
89+
[objA, objB], // Complete pair
90+
[undefined, objC], // Partial pair - should be kept (objC not in any complete pair at position 1)
91+
]
92+
93+
const result = removeRedundantPartialPairs(input)
94+
expect(result).toEqual(input)
95+
expect(result).toHaveLength(2)
96+
})
97+
98+
test('should handle multiple complete pairs with overlapping values', () => {
99+
const objA = { id: 'A' }
100+
const objB = { id: 'B' }
101+
const objC = { id: 'C' }
102+
const objD = { id: 'D' }
103+
104+
const input: [object | undefined, object | undefined][] = [
105+
[objA, objB], // Complete pair 1
106+
[objC, objD], // Complete pair 2
107+
[objA, undefined], // Partial - should be removed (objA at position 0 in complete pair 1)
108+
[undefined, objD], // Partial - should be removed (objD at position 1 in complete pair 2)
109+
]
110+
111+
const result = removeRedundantPartialPairs(input)
112+
expect(result).toEqual([
113+
[objA, objB],
114+
[objC, objD],
115+
])
116+
expect(result).toHaveLength(2)
117+
})
118+
119+
test('should handle complex scenario with mixed complete and partial pairs', () => {
120+
const objA = { id: 'A' }
121+
const objB = { id: 'B' }
122+
const objC = { id: 'C' }
123+
const objD = { id: 'D' }
124+
const objE = { id: 'E' }
125+
const objF = { id: 'F' }
126+
127+
const input: [object | undefined, object | undefined][] = [
128+
[objA, objB], // Complete pair 1
129+
[objC, objD], // Complete pair 2
130+
[objA, undefined], // Partial - should be removed (objA in complete pair 1)
131+
[undefined, objB], // Partial - should be removed (objB in complete pair 1)
132+
[undefined, objE], // Partial - should be kept (objE not in any complete pair)
133+
[objF, undefined], // Partial - should be kept (objF not in any complete pair)
134+
[objC, undefined], // Partial - should be removed (objC in complete pair 2)
135+
]
136+
137+
const result = removeRedundantPartialPairs(input)
138+
expect(result).toEqual([
139+
[objA, objB],
140+
[objC, objD],
141+
[undefined, objE],
142+
[objF, undefined],
143+
])
144+
expect(result).toHaveLength(4)
145+
})
146+
147+
test('should handle single complete pair with multiple redundant partial pairs', () => {
148+
const objA = { id: 'A' }
149+
const objB = { id: 'B' }
150+
151+
const input: [object | undefined, object | undefined][] = [
152+
[objA, objB], // Complete pair
153+
[objA, undefined], // Redundant partial
154+
[undefined, objB], // Redundant partial
155+
[objA, undefined], // Another redundant partial (duplicate)
156+
]
157+
158+
const result = removeRedundantPartialPairs(input)
159+
expect(result).toEqual([[objA, objB]])
160+
expect(result).toHaveLength(1)
161+
})
162+
163+
test('should handle pairs where same object appears at different positions in different pairs', () => {
164+
const objA = { id: 'A' }
165+
const objB = { id: 'B' }
166+
167+
const input: [object | undefined, object | undefined][] = [
168+
[objA, objB], // Complete pair: objA at position 0, objB at position 1
169+
[objB, objA], // Complete pair: objB at position 0, objA at position 1
170+
[objA, undefined], // Partial - should be removed (objA at position 0 in first complete pair)
171+
[undefined, objA], // Partial - should be removed (objA at position 1 in second complete pair)
172+
[objB, undefined], // Partial - should be removed (objB at position 0 in second complete pair)
173+
[undefined, objB], // Partial - should be removed (objB at position 1 in first complete pair)
174+
]
175+
176+
const result = removeRedundantPartialPairs(input)
177+
expect(result).toEqual([
178+
[objA, objB],
179+
[objB, objA],
180+
])
181+
expect(result).toHaveLength(2)
182+
})
183+
})
184+

0 commit comments

Comments
 (0)