forked from GriddleGriddle/Griddle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataReducerTest.js
More file actions
253 lines (216 loc) · 6.38 KB
/
dataReducerTest.js
File metadata and controls
253 lines (216 loc) · 6.38 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import test from 'ava';
import Immutable from 'immutable';
import * as reducers from '../dataReducer';
import constants from '../../constants';
test('initializes data', test => {
const initializedState = reducers.GRIDDLE_INITIALIZED({
renderProperties: {
one: 'one',
two: 'two'
}
});
test.deepEqual(initializedState.get('renderProperties').toJSON(), {
one: 'one',
two: 'two'
});
});
test('creates column properties if none exist for data', test => {
const state = reducers.GRIDDLE_INITIALIZED({
data: [
{one: 1, two: 2, three: 3},
{one: 11, two: 22, three: 33}
],
renderProperties: {},
});
test.deepEqual(state.getIn(['renderProperties', 'columnProperties']).toJSON(), {
one: { id: 'one', visible: true },
two: { id: 'two', visible: true },
three: { id: 'three', visible: true }
});
});
test('does not adjust column properties if exists already', test => {
const state = reducers.GRIDDLE_INITIALIZED({
data: [
{ one: 1, two: 2, three: 3},
{ one: 11, two: 22, three: 33 }
],
renderProperties: {
columnProperties: {
one: { id: 'one', visible: true }
}
}
});
test.deepEqual(state.getIn(['renderProperties', 'columnProperties']).toJSON(), {
one: { id: 'one', visible: true }
});
});
[undefined, null].map(data =>
test(`does not adjust column properties if data is ${data}`, (assert) => {
const state = reducers.GRIDDLE_INITIALIZED({
data,
renderProperties: {
columnProperties: {
one: { id: 'one', visible: true }
}
}
});
assert.deepEqual(state.getIn(['renderProperties', 'columnProperties']).toJSON(), {
one: { id: 'one', visible: true }
});
})
);
test('sets data', test => {
const reducedState = reducers.GRIDDLE_LOADED_DATA(Immutable.fromJS({ renderProperties: {} }),
{ type: 'GRIDDLE_LOADED_DATA', data: [
{name: "one"},
{name: "two"}
]}
);
test.deepEqual(reducedState.toJSON(), {
data: [
{name: "one", griddleKey: 0},
{name: "two", griddleKey: 1}
],
renderProperties: {},
lookup: { 0: 0, 1: 1 },
loading: false
});
});
test('sets the correct page number', test => {
const state = reducers.GRIDDLE_SET_PAGE(new Immutable.Map(), {
pageNumber: 2
});
test.is(state.getIn(['pageProperties', 'currentPage']), 2);
});
test('sets page size', test => {
const state = reducers.GRIDDLE_SET_PAGE_SIZE( new Immutable.Map(), {
pageSize: 11
});
test.is(state.getIn(['pageProperties', 'pageSize']), 11);
});
test('sets filter', test => {
const state = reducers.GRIDDLE_SET_FILTER(new Immutable.Map(), {
filter: 'onetwothree'
});
test.is(state.get('filter'), 'onetwothree');
});
test('sets sort columns', test => {
const state = reducers.GRIDDLE_SET_SORT(new Immutable.Map(), {
sortProperties: [
{ id: 'one', sortAscending: true },
{ id: 'two', sortAscending: false }
]
});
test.deepEqual(state.get('sortProperties').toJSON(), [
{ id: 'one', sortAscending: true },
{ id: 'two', sortAscending: false }
]);
});
test('sets settings visibility', test => {
const initialState = Immutable.fromJS({
});
// should be true when showSettings isn't in state
const trueState = reducers.GRIDDLE_TOGGLE_SETTINGS(initialState);
test.is(trueState.get('showSettings'), true);
const falseState = reducers.GRIDDLE_TOGGLE_SETTINGS(trueState);
test.is(falseState.get('showSettings'), false);
})
test('toggle column changes column properties visibility', test => {
const initialState = Immutable.fromJS({
renderProperties: {
columnProperties: {
name: { id: 'name', visible: false }
}
}
});
const state = reducers.GRIDDLE_TOGGLE_COLUMN(initialState, { columnId: 'name' });
test.deepEqual(state.getIn(['renderProperties', 'columnProperties', 'name']).toJSON(), { id: 'name', visible: true });
})
test('toggle column sets true when no columnProperty for column but other columnProperties exist', test => {
const initialState = Immutable.fromJS({
renderProperties: {
columnProperties: {
name: { id: 'name', visible: false }
}
}
});
const state = reducers.GRIDDLE_TOGGLE_COLUMN(initialState, { columnId: 'state' });
test.deepEqual(state.getIn(['renderProperties', 'columnProperties', 'state']).toJSON(), { id: 'state', visible: true });
});
test('toggle column works when there is no visible property', (t) => {
const initialState = Immutable.fromJS({
renderProperties: {
columnProperties: {
name: { id: 'name' }
}
}
});
// if column isn't in renderProperties->column properties, we should set visible to true
const state = reducers.GRIDDLE_TOGGLE_COLUMN(initialState, { columnId: 'state' });
t.deepEqual(state.getIn(['renderProperties', 'columnProperties', 'state']).toJSON(), { id: 'state', visible: true });
// if column is in reducerProperties but has no visible property should set to false
const otherState = reducers.GRIDDLE_TOGGLE_COLUMN(initialState, { columnId: 'name' });
t.deepEqual(otherState.getIn(['renderProperties', 'columnProperties', 'name']).toJSON(), { id: 'name', visible: false });
});
test('update state merges non-data', (t) => {
const initialState = Immutable.fromJS({
changed: 1,
unchanged: 2,
nested: {
changed: 3,
unchanged: 4,
},
data: [],
lookup: {},
});
const newState = {
changed: -1,
nested: {
changed: -3,
},
};
const state = reducers.GRIDDLE_UPDATE_STATE(initialState, { newState });
t.deepEqual(state.toJSON(), {
changed: -1,
unchanged: 2,
nested: {
changed: -3,
unchanged: 4,
},
data: [],
lookup: {},
});
});
test('update state transforms data', (t) => {
const initialState = Immutable.fromJS({
unchanged: 2,
nested: {
unchanged: 4,
},
data: [
{name: "one", griddleKey: 0},
{name: "two", griddleKey: 1},
],
lookup: { 0: 0, 1: 1 },
});
const newState = {
data: [
{ name: 'uno' },
{ name: 'dos' },
{ name: 'tre' },
]
};
const state = reducers.GRIDDLE_UPDATE_STATE(initialState, { newState });
t.deepEqual(state.toJSON(), {
unchanged: 2,
nested: {
unchanged: 4,
},
data: [
{name: "uno", griddleKey: 0},
{name: "dos", griddleKey: 1},
{name: "tre", griddleKey: 2},
],
lookup: { 0: 0, 1: 1, 2: 2 },
});
});