1 import { EVENT_ACTIONS } from '../../lib/constants';
2 import updateCollection, { sortCollection } from '../../lib/helpers/updateCollection';
4 describe('update collection', () => {
5 it('should remove items', () => {
15 Action: EVENT_ACTIONS.DELETE,
18 const newLabels = updateCollection({
23 expect(newLabels).toEqual([]);
26 it('should add items', () => {
36 Action: EVENT_ACTIONS.CREATE,
43 const newLabels = updateCollection({ model: labels, events, itemKey: 'Label' });
44 expect(newLabels).toEqual([
45 { ID: '123', foo: 'bar' },
46 { ID: '124', foo: 'bar2' },
50 it('should update items', () => {
61 Action: EVENT_ACTIONS.UPDATE,
68 const newLabels = updateCollection({ model: labels, events, itemKey: 'Label' });
69 expect(newLabels).toEqual([{ ID: '123', foo: 'bar2', kept: true }]);
72 it('should delete, create and update items', () => {
82 Action: EVENT_ACTIONS.UPDATE,
90 Action: EVENT_ACTIONS.DELETE,
94 Action: EVENT_ACTIONS.UPDATE,
102 Action: EVENT_ACTIONS.CREATE,
109 const newLabels = updateCollection({ model: labels, events, itemKey: 'Label', item: ({ Label }) => Label });
110 expect(newLabels).toEqual([{ ID: '124', foo: 'bar3' }]);
113 describe('Sort collection', () => {
114 it('should delete, create and update items and sort them', () => {
125 Action: EVENT_ACTIONS.UPDATE,
133 Action: EVENT_ACTIONS.CREATE,
142 Action: EVENT_ACTIONS.CREATE,
150 const newLabels = updateCollection({ model: labels, events, itemKey: 'Label', item: ({ Label }) => Label });
152 const sortedLabels = sortCollection(key, newLabels);
153 expect(sortedLabels).toEqual([
154 { ID: '123', foo: 'bar2', Order: 1 },
155 { ID: '12345', foo: 'monique', Order: 2 },
156 { ID: '124', foo: 'bar', Order: 3 },
162 Action: EVENT_ACTIONS.DELETE,
166 Action: EVENT_ACTIONS.UPDATE,
175 const newLabels2 = updateCollection({
179 item: ({ Label }) => Label,
181 const sortedLabels2 = sortCollection(key, newLabels2);
182 expect(sortedLabels2).toEqual([
183 { ID: '124', foo: 'bar3', Order: 1 },
184 { ID: '12345', foo: 'monique', Order: 2 },
188 it('should delete, create and update items and not sort them', () => {
198 Action: EVENT_ACTIONS.UPDATE,
206 Action: EVENT_ACTIONS.CREATE,
214 Action: EVENT_ACTIONS.CREATE,
221 const newLabels = updateCollection({ model: labels, events, itemKey: 'Label' });
222 expect(newLabels).toEqual([
223 { ID: '123', foo: 'bar2' },
224 { ID: '12345', foo: 'monique' },
225 { ID: '124', foo: 'bar' },
231 Action: EVENT_ACTIONS.DELETE,
235 Action: EVENT_ACTIONS.UPDATE,
243 const newLabels2 = updateCollection({
247 item: ({ Label }) => Label,
249 expect(newLabels2).toEqual([
250 { ID: '12345', foo: 'monique' },
251 { ID: '124', foo: 'bar3' },
255 it('should delete, create and update items and not sort them by a custom key', () => {
271 Action: EVENT_ACTIONS.UPDATE,
280 Action: EVENT_ACTIONS.CREATE,
289 Action: EVENT_ACTIONS.CREATE,
297 const newLabels = sortCollection('counter', updateCollection({ model: labels, events, itemKey: 'Label' }));
298 expect(newLabels).toEqual([
299 { ID: '12345', foo: 'monique', counter: 2 },
300 { ID: '122', foo: 'bar', counter: 3 },
301 { ID: '124', foo: 'bar', counter: 3 },
302 { ID: '123', foo: 'bar2', counter: 5 },