-
-
Notifications
You must be signed in to change notification settings - Fork 286
Expand file tree
/
Copy pathPivotTableUI-test.js
More file actions
190 lines (159 loc) · 6.3 KB
/
PivotTableUI-test.js
File metadata and controls
190 lines (159 loc) · 6.3 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import React from 'react';
import PivotTableUI from '../PivotTableUI';
describe('PivotTableUI', () => {
describe('handleDuplicates', () => {
// Create a minimal instance of PivotTableUI for testing
const getInstance = () => {
const pivotTableUI = new PivotTableUI({
onChange: () => {},
renderers: {},
aggregators: {},
rows: [],
cols: [],
rendererName: '',
aggregatorName: '',
vals: [],
valueFilter: {},
rowOrder: 'key_a_to_z',
colOrder: 'key_a_to_z',
derivedAttributes: {},
data: []
});
return pivotTableUI;
};
it('returns existingAttributes when newAttributes is null or undefined', () => {
const instance = getInstance();
expect(instance.handleDuplicates(null, ['a', 'b'])).toEqual(['a', 'b']);
expect(instance.handleDuplicates(undefined, ['a', 'b'])).toEqual(['a', 'b']);
});
it('returns empty array when both inputs are null or undefined', () => {
const instance = getInstance();
expect(instance.handleDuplicates(null, null)).toEqual([]);
expect(instance.handleDuplicates(undefined, undefined)).toEqual([]);
});
it('returns existingAttributes when there are no duplicates', () => {
const instance = getInstance();
const newAttributes = ['a', 'b', 'c'];
const existingAttributes = ['d', 'e', 'f'];
expect(instance.handleDuplicates(newAttributes, existingAttributes))
.toEqual(existingAttributes);
});
it('removes duplicates from existingAttributes', () => {
const instance = getInstance();
const newAttributes = ['a', 'b', 'c'];
const existingAttributes = ['b', 'c', 'd'];
// 'b' and 'c' are duplicates and should be removed
expect(instance.handleDuplicates(newAttributes, existingAttributes))
.toEqual(['d']);
});
it('handles empty newAttributes', () => {
const instance = getInstance();
const newAttributes = [];
const existingAttributes = ['a', 'b', 'c'];
expect(instance.handleDuplicates(newAttributes, existingAttributes))
.toEqual(existingAttributes);
});
it('handles empty existingAttributes', () => {
const instance = getInstance();
const newAttributes = ['a', 'b', 'c'];
const existingAttributes = [];
expect(instance.handleDuplicates(newAttributes, existingAttributes))
.toEqual([]);
});
it('handles case with all attributes being duplicates', () => {
const instance = getInstance();
const newAttributes = ['a', 'b', 'c'];
const existingAttributes = ['a', 'b', 'c'];
expect(instance.handleDuplicates(newAttributes, existingAttributes))
.toEqual([]);
});
});
describe('propUpdater', () => {
// We'll use a mock to check if sendPropUpdate is called with the right arguments
let mockSendPropUpdate;
let instance;
beforeEach(() => {
instance = new PivotTableUI({
onChange: () => {},
renderers: {},
aggregators: {},
rows: ['gender', 'age'],
cols: ['country', 'year'],
rendererName: '',
aggregatorName: '',
vals: [],
valueFilter: {},
rowOrder: 'key_a_to_z',
colOrder: 'key_a_to_z',
derivedAttributes: {},
data: []
});
// Mock the sendPropUpdate method
mockSendPropUpdate = jest.spyOn(instance, 'sendPropUpdate').mockImplementation(() => {});
// Mock the handleDuplicates method to control its return value
jest.spyOn(instance, 'handleDuplicates');
});
afterEach(() => {
mockSendPropUpdate.mockRestore();
instance.handleDuplicates.mockRestore();
});
it('calls handleDuplicates when key is "rows"', () => {
const newRows = ['gender', 'name'];
const updater = instance.propUpdater('rows');
// Set up the mock to return the same cols (no duplicates found)
instance.handleDuplicates.mockReturnValueOnce(instance.props.cols);
updater(newRows);
expect(instance.handleDuplicates).toHaveBeenCalledWith(newRows, instance.props.cols);
expect(mockSendPropUpdate).toHaveBeenCalledWith({
rows: { $set: newRows }
});
});
it('calls handleDuplicates when key is "cols"', () => {
const newCols = ['country', 'city'];
const updater = instance.propUpdater('cols');
// Set up the mock to return the same rows (no duplicates found)
instance.handleDuplicates.mockReturnValueOnce(instance.props.rows);
updater(newCols);
expect(instance.handleDuplicates).toHaveBeenCalledWith(newCols, instance.props.rows);
expect(mockSendPropUpdate).toHaveBeenCalledWith({
cols: { $set: newCols }
});
});
it('updates cols when duplicate is found in rows update', () => {
const newRows = ['gender', 'country']; // 'country' is duplicate
const updater = instance.propUpdater('rows');
// 'country' is removed from cols
const updatedCols = ['year'];
instance.handleDuplicates.mockReturnValueOnce(updatedCols);
updater(newRows);
expect(mockSendPropUpdate).toHaveBeenCalledWith({
rows: { $set: newRows },
cols: { $set: updatedCols }
});
});
it('updates rows when duplicate is found in cols update', () => {
const newCols = ['country', 'gender']; // 'gender' is duplicate
const updater = instance.propUpdater('cols');
// 'gender' is removed from rows
const updatedRows = ['age'];
instance.handleDuplicates.mockReturnValueOnce(updatedRows);
updater(newCols);
expect(mockSendPropUpdate).toHaveBeenCalledWith({
cols: { $set: newCols },
rows: { $set: updatedRows }
});
});
it('does not update the other attribute if no duplicates found', () => {
const newRows = ['gender', 'name'];
const updater = instance.propUpdater('rows');
// No change to cols (same array reference)
instance.handleDuplicates.mockReturnValueOnce(instance.props.cols);
updater(newRows);
expect(mockSendPropUpdate).toHaveBeenCalledWith({
rows: { $set: newRows }
});
// We shouldn't have cols in the update
expect(mockSendPropUpdate.mock.calls[0][0].cols).toBeUndefined();
});
});
});