forked from ayastreb/money-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEntityMap.test.js
243 lines (221 loc) · 6.27 KB
/
EntityMap.test.js
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
import EntityMap from './EntityMap';
describe('create map from array of entities', () => {
it('creats empty map from empty array', () => {
const map = EntityMap.fromArray([]);
expect(map).toEqual({
byKey: {},
keys: []
});
});
it('creates map from given array', () => {
const map = EntityMap.fromArray([
{ id: 'foo', name: 'Foo' },
{ id: 'bar', name: 'Bar' }
]);
expect(map).toEqual({
byKey: {
foo: { id: 'foo', name: 'Foo' },
bar: { id: 'bar', name: 'Bar' }
},
keys: ['foo', 'bar']
});
});
it('skips duplicates from given array', () => {
const map = EntityMap.fromArray([
{ id: 'foo', name: 'Foo' },
{ id: 'bar', name: 'Bar' },
{ id: 'bar', name: 'BarDup' }
]);
expect(map).toEqual({
byKey: {
foo: { id: 'foo', name: 'Foo' },
bar: { id: 'bar', name: 'BarDup' }
},
keys: ['foo', 'bar']
});
});
});
describe('merge array of entities into existing map', () => {
const map = {
byKey: {
foo: { id: 'foo', name: 'Foo' },
bar: { id: 'bar', name: 'Bar' }
},
keys: ['foo', 'bar']
};
const original = { ...map };
it('merges new entity', () => {
const mapWithBaz = EntityMap.merge(map, [{ id: 'baz', name: 'Baz' }]);
expect(map === mapWithBaz).toBeFalsy();
expect(map).toEqual(original);
expect(mapWithBaz).toEqual({
byKey: {
foo: { id: 'foo', name: 'Foo' },
bar: { id: 'bar', name: 'Bar' },
baz: { id: 'baz', name: 'Baz' }
},
keys: ['baz', 'foo', 'bar']
});
});
it('merges and replaces entities', () => {
const mapWithBax = EntityMap.merge(map, [
{ id: 'bax', name: 'Bax' },
{ id: 'foo', name: 'Foo with bax' }
]);
expect(map === mapWithBax).toBeFalsy();
expect(map).toEqual(original);
expect(mapWithBax).toEqual({
byKey: {
foo: { id: 'foo', name: 'Foo with bax' },
bar: { id: 'bar', name: 'Bar' },
bax: { id: 'bax', name: 'Bax' }
},
keys: ['bax', 'foo', 'bar']
});
});
});
describe('set entity in map', () => {
const map = {
byKey: {
foo: { id: 'foo', name: 'Foo' },
bar: { id: 'bar', name: 'Bar' }
},
keys: ['foo', 'bar']
};
const original = { ...map };
it('sets new entity', () => {
const mapWithBaz = EntityMap.set(map, { id: 'baz', name: 'Baz' });
expect(map === mapWithBaz).toBeFalsy();
expect(map).toEqual(original);
expect(mapWithBaz).toEqual({
byKey: {
foo: { id: 'foo', name: 'Foo' },
bar: { id: 'bar', name: 'Bar' },
baz: { id: 'baz', name: 'Baz' }
},
keys: ['baz', 'foo', 'bar']
});
});
it('updates existing value with given key', () => {
const mapUpdated = EntityMap.set(map, { id: 'bar', name: 'Bar Updated' });
expect(map === mapUpdated).toBeFalsy();
expect(map).toEqual(original);
expect(mapUpdated).toEqual({
byKey: {
foo: { id: 'foo', name: 'Foo' },
bar: { id: 'bar', name: 'Bar Updated' }
},
keys: ['foo', 'bar']
});
});
});
describe('remove value by key', () => {
const map = {
byKey: {
foo: { id: 'foo', name: 'Foo' },
bar: { id: 'bar', name: 'Bar' }
},
keys: ['foo', 'bar']
};
const original = { ...map };
it('removes original map if key not found', () => {
const removedMap = EntityMap.remove(map, 'baz');
expect(map === removedMap).toBeTruthy();
expect(map).toEqual(original);
});
it('removes value by given key', () => {
const mapWithoutBar = EntityMap.remove(map, 'bar');
expect(map === mapWithoutBar).toBeFalsy();
expect(map).toEqual(original);
expect(mapWithoutBar).toEqual({
byKey: {
foo: { id: 'foo', name: 'Foo' }
},
keys: ['foo']
});
});
});
describe('get value from map', () => {
const map = {
byKey: {
foo: { id: 'foo', name: 'Foo' },
bar: { id: 'bar', name: 'Bar' }
},
keys: ['foo', 'bar']
};
it('returns default fallback when key is not found', () => {
expect(EntityMap.get(map, 'baz')).toEqual({});
});
it('returns given fallback when key is not found', () => {
expect(EntityMap.get(map, 'baz', 'no baz')).toEqual('no baz');
});
it('returns entity for given key', () => {
expect(EntityMap.get(map, 'bar')).toEqual({ id: 'bar', name: 'Bar' });
expect(EntityMap.get(map, 'bar') === map.byKey.bar).toBeTruthy();
});
});
describe('map function to values', () => {
const map = {
byKey: {
foo: { id: 'foo', name: 'Foo' },
bar: { id: 'bar', name: 'Bar' }
},
keys: ['foo', 'bar']
};
it('maps values with given function', () => {
const idWithNameArray = EntityMap.map(
map,
(value, key) => `${key}_${value.name}`
);
expect(idWithNameArray).toEqual(['foo_Foo', 'bar_Bar']);
});
});
describe('filter values', () => {
const map = {
byKey: {
foo: { id: 'foo', name: 'Foo' },
bar: { id: 'bar', name: 'Bar' }
},
keys: ['foo', 'bar']
};
const original = { ...map };
it('filters values with given function', () => {
expect(map).toEqual(original);
const onlyFoo = EntityMap.filter(map, entity => entity.name === 'Foo');
expect(onlyFoo).toEqual([{ id: 'foo', name: 'Foo' }]);
const onlyBar = EntityMap.filter(map, entity => entity.name === 'Bar');
expect(onlyBar).toEqual([{ id: 'bar', name: 'Bar' }]);
});
});
describe('appliy function to values', () => {
const ref = Object.create({ iAmDeep: true });
const map = {
byKey: {
foo: { id: 'foo', deep: { prop: ref }, name: 'Foo' },
bar: { id: 'bar', name: 'Bar' }
},
keys: ['foo', 'bar']
};
const original = { ...map };
it('applies given function to map', () => {
const upperCaseNames = EntityMap.apply(map, value => {
value.name = `${value.name} update`;
value.nameToUpper = value.name.toUpperCase();
return value;
});
expect(upperCaseNames === map).toBeFalsy();
expect(map).toEqual(original);
expect(upperCaseNames).toEqual({
byKey: {
foo: {
id: 'foo',
deep: { prop: ref },
name: 'Foo update',
nameToUpper: 'FOO UPDATE'
},
bar: { id: 'bar', name: 'Bar update', nameToUpper: 'BAR UPDATE' }
},
keys: ['foo', 'bar']
});
});
});