@axinom/mosaic-ui 0.35.0-rc.1 → 0.35.0-rc.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. package/dist/components/DynamicDataList/DynamicDataList.d.ts +5 -14
  2. package/dist/components/DynamicDataList/DynamicDataList.d.ts.map +1 -1
  3. package/dist/components/DynamicDataList/DynamicListDataEntry/DynamicListDataEntry.d.ts +3 -1
  4. package/dist/components/DynamicDataList/DynamicListDataEntry/DynamicListDataEntry.d.ts.map +1 -1
  5. package/dist/components/DynamicDataList/DynamicListHeader/DynamicListHeader.d.ts +5 -1
  6. package/dist/components/DynamicDataList/DynamicListHeader/DynamicListHeader.d.ts.map +1 -1
  7. package/dist/components/DynamicDataList/DynamicListRow/DynamicListRow.d.ts +9 -10
  8. package/dist/components/DynamicDataList/DynamicListRow/DynamicListRow.d.ts.map +1 -1
  9. package/dist/components/DynamicDataList/helpers/DynamicListReducer/DynamicListReducer.actions.d.ts +5 -0
  10. package/dist/components/DynamicDataList/helpers/DynamicListReducer/DynamicListReducer.actions.d.ts.map +1 -0
  11. package/dist/components/DynamicDataList/helpers/DynamicListReducer/DynamicListReducer.d.ts +4 -0
  12. package/dist/components/DynamicDataList/helpers/DynamicListReducer/DynamicListReducer.d.ts.map +1 -0
  13. package/dist/components/DynamicDataList/helpers/DynamicListReducer/DynamicListReducer.init.d.ts +4 -0
  14. package/dist/components/DynamicDataList/helpers/DynamicListReducer/DynamicListReducer.init.d.ts.map +1 -0
  15. package/dist/components/DynamicDataList/helpers/DynamicListReducer/DynamicListReducer.types.d.ts +38 -0
  16. package/dist/components/DynamicDataList/helpers/DynamicListReducer/DynamicListReducer.types.d.ts.map +1 -0
  17. package/dist/components/DynamicDataList/helpers/DynamicListReducer/index.d.ts +4 -0
  18. package/dist/components/DynamicDataList/helpers/DynamicListReducer/index.d.ts.map +1 -0
  19. package/dist/components/DynamicDataList/helpers/generateId.d.ts +6 -0
  20. package/dist/components/DynamicDataList/helpers/generateId.d.ts.map +1 -0
  21. package/dist/components/DynamicDataList/helpers/useColumnDefs.d.ts +14 -0
  22. package/dist/components/DynamicDataList/helpers/useColumnDefs.d.ts.map +1 -0
  23. package/dist/components/DynamicDataList/helpers/useDataHandler.d.ts +9 -0
  24. package/dist/components/DynamicDataList/helpers/useDataHandler.d.ts.map +1 -0
  25. package/dist/components/DynamicDataList/helpers/useRowAnimation.d.ts +12 -0
  26. package/dist/components/DynamicDataList/helpers/useRowAnimation.d.ts.map +1 -0
  27. package/dist/components/DynamicDataList/index.d.ts +1 -1
  28. package/dist/components/DynamicDataList/index.d.ts.map +1 -1
  29. package/dist/components/List/ListRow/ListRow.d.ts.map +1 -1
  30. package/dist/components/List/useColumnsSize.d.ts +1 -0
  31. package/dist/components/List/useColumnsSize.d.ts.map +1 -1
  32. package/dist/index.es.js +11 -3
  33. package/dist/index.es.js.map +1 -1
  34. package/dist/index.js +11 -3
  35. package/dist/index.js.map +1 -1
  36. package/package.json +5 -3
  37. package/src/components/DynamicDataList/DynamicDataList.scss +0 -61
  38. package/src/components/DynamicDataList/DynamicDataList.spec.tsx +126 -393
  39. package/src/components/DynamicDataList/DynamicDataList.stories.tsx +0 -5
  40. package/src/components/DynamicDataList/DynamicDataList.tsx +133 -600
  41. package/src/components/DynamicDataList/DynamicListDataEntry/DynamicListDataEntry.scss +4 -2
  42. package/src/components/DynamicDataList/DynamicListDataEntry/DynamicListDataEntry.spec.tsx +17 -44
  43. package/src/components/DynamicDataList/DynamicListDataEntry/DynamicListDataEntry.tsx +15 -22
  44. package/src/components/DynamicDataList/DynamicListHeader/DynamicListHeader.scss +15 -10
  45. package/src/components/DynamicDataList/DynamicListHeader/DynamicListHeader.spec.tsx +4 -1
  46. package/src/components/DynamicDataList/DynamicListHeader/DynamicListHeader.tsx +16 -14
  47. package/src/components/DynamicDataList/DynamicListRow/DynamicListRow.scss +16 -24
  48. package/src/components/DynamicDataList/DynamicListRow/DynamicListRow.spec.tsx +26 -253
  49. package/src/components/DynamicDataList/DynamicListRow/DynamicListRow.tsx +45 -139
  50. package/src/components/DynamicDataList/helpers/DynamicListReducer/DynamicListReducer.actions.spec.ts +276 -0
  51. package/src/components/DynamicDataList/helpers/DynamicListReducer/DynamicListReducer.actions.ts +86 -0
  52. package/src/components/DynamicDataList/helpers/DynamicListReducer/DynamicListReducer.init.spec.ts +118 -0
  53. package/src/components/DynamicDataList/helpers/DynamicListReducer/DynamicListReducer.init.ts +40 -0
  54. package/src/components/DynamicDataList/helpers/DynamicListReducer/DynamicListReducer.spec.ts +89 -0
  55. package/src/components/DynamicDataList/helpers/DynamicListReducer/DynamicListReducer.ts +42 -0
  56. package/src/components/DynamicDataList/helpers/DynamicListReducer/DynamicListReducer.types.ts +46 -0
  57. package/src/components/DynamicDataList/helpers/DynamicListReducer/index.ts +3 -0
  58. package/src/components/DynamicDataList/helpers/generateId.ts +10 -0
  59. package/src/components/DynamicDataList/helpers/useColumnDefs.ts +56 -0
  60. package/src/components/DynamicDataList/helpers/useDataHandler.ts +77 -0
  61. package/src/components/DynamicDataList/helpers/useRowAnimation.tsx +38 -0
  62. package/src/components/DynamicDataList/index.ts +2 -2
  63. package/src/components/FormElements/BooleanView/BooleanViewField.scss +2 -2
  64. package/src/components/FormStation/FormStation.scss +4 -0
  65. package/src/components/FormStation/FormStation.tsx +2 -2
  66. package/src/components/List/List.tsx +1 -1
  67. package/src/components/List/ListRow/ListRow.tsx +56 -50
  68. package/src/components/List/ListRow/Renderers/BooleanDotRenderer/BooleanDotRenderer.scss +6 -9
  69. package/src/components/List/ListRow/Renderers/BooleanDotRenderer/BooleanDotRenderer.spec.tsx +2 -2
  70. package/src/components/List/ListRow/Renderers/BooleanDotRenderer/BooleanDotRenderer.tsx +1 -1
  71. package/src/components/List/useColumnsSize.ts +20 -3
  72. package/src/styles/variables.scss +0 -5
  73. package/src/components/DynamicDataList/DynamicDataList.reposition.spec.tsx +0 -816
@@ -1,13 +1,53 @@
1
1
  import { mount, shallow } from 'enzyme';
2
2
  import React from 'react';
3
3
  import { act } from 'react-dom/test-utils';
4
- import { config } from 'react-transition-group';
5
- import { Button } from '../Buttons';
6
4
  import { DynamicDataList, DynamicDataListProps } from './DynamicDataList';
7
5
  import { DynamicListColumn } from './DynamicDataList.model';
8
6
  import { DynamicListDataEntry } from './DynamicListDataEntry/DynamicListDataEntry';
9
7
  import { DynamicListHeader } from './DynamicListHeader/DynamicListHeader';
10
8
  import { DynamicListRow } from './DynamicListRow/DynamicListRow';
9
+ import { useDataHandler } from './helpers/useDataHandler';
10
+
11
+ jest.mock('./helpers/useDataHandler');
12
+ jest.mock('./helpers/generateId', () => ({
13
+ uuid: jest.fn().mockReturnValue('test-uuid'),
14
+ }));
15
+
16
+ const mockUseDataHandler = useDataHandler as jest.MockedFunction<
17
+ typeof useDataHandler
18
+ >;
19
+
20
+ jest.mock('react-beautiful-dnd', () => ({
21
+ Droppable: ({
22
+ children,
23
+ }: {
24
+ children: (provided: any, snapshot: any) => React.ReactNode;
25
+ }) =>
26
+ children(
27
+ {
28
+ draggableProps: {
29
+ style: {},
30
+ },
31
+ innerRef: jest.fn(),
32
+ },
33
+ {},
34
+ ),
35
+ Draggable: ({
36
+ children,
37
+ }: {
38
+ children: (provided: any, snapshot: any) => React.ReactNode;
39
+ }) =>
40
+ children(
41
+ {
42
+ draggableProps: {
43
+ style: {},
44
+ },
45
+ innerRef: jest.fn(),
46
+ },
47
+ {},
48
+ ),
49
+ DragDropContext: ({ children }: { children: React.ReactNode }) => children,
50
+ }));
11
51
 
12
52
  interface DynamicListTestData {
13
53
  id: number;
@@ -37,11 +77,17 @@ const defaultColumns: DynamicListColumn<DynamicListTestData>[] = [
37
77
  const defaultProps: DynamicDataListProps<DynamicListTestData> = {
38
78
  value: defaultData,
39
79
  columns: defaultColumns,
80
+ positionPropertyName: 'position',
40
81
  };
41
82
 
42
83
  describe('DynamicDataList', () => {
43
- // Disable Animations for testing
44
- config.disabled = true;
84
+ mockUseDataHandler.mockReturnValue({
85
+ removeItem: jest.fn(),
86
+ addItem: jest.fn(),
87
+ canAddItems: false,
88
+ items: defaultData,
89
+ nextPosition: 2,
90
+ });
45
91
 
46
92
  it('renders the component without crashing', () => {
47
93
  const wrapper = shallow(<DynamicDataList {...defaultProps} />);
@@ -69,10 +115,10 @@ describe('DynamicDataList', () => {
69
115
  expect(header.prop('positionLabel')).toBe(mockLabel);
70
116
  });
71
117
 
72
- it(`creates a position column, columns for each property, and an action column by default`, () => {
73
- const mockColumns = '100px 1fr 1fr 50px';
118
+ it(`creates a position column and columns for each property`, () => {
119
+ const mockColumns = '100px 1fr 1fr';
74
120
 
75
- const wrapper = shallow(<DynamicDataList {...defaultProps} />);
121
+ const wrapper = mount(<DynamicDataList {...defaultProps} />);
76
122
 
77
123
  const header = wrapper.find(DynamicListHeader);
78
124
  const row = wrapper.find(DynamicListRow);
@@ -82,7 +128,7 @@ describe('DynamicDataList', () => {
82
128
  });
83
129
 
84
130
  it(`does not create a column for position if prop 'allowReordering' is false`, () => {
85
- const mockColumns = '1fr 1fr 50px';
131
+ const mockColumns = '1fr 1fr';
86
132
 
87
133
  const wrapper = shallow(
88
134
  <DynamicDataList {...defaultProps} allowReordering={false} />,
@@ -96,9 +142,9 @@ describe('DynamicDataList', () => {
96
142
  });
97
143
 
98
144
  it(`create a smaller position column if 'allowDragging' is false`, () => {
99
- const mockColumns = '60px 1fr 1fr 50px';
145
+ const mockColumns = '50px 1fr 1fr';
100
146
 
101
- const wrapper = shallow(
147
+ const wrapper = mount(
102
148
  <DynamicDataList
103
149
  {...defaultProps}
104
150
  allowReordering={true}
@@ -113,20 +159,6 @@ describe('DynamicDataList', () => {
113
159
  expect(row.prop('columnSizes')).toBe(mockColumns);
114
160
  });
115
161
 
116
- it(`does not create a column for the action button if prop 'allowAddAndRemove' is false`, () => {
117
- const mockColumns = '100px 1fr 1fr';
118
-
119
- const wrapper = shallow(
120
- <DynamicDataList {...defaultProps} allowAddAndRemove={false} />,
121
- );
122
-
123
- const header = wrapper.find(DynamicListHeader);
124
- const row = wrapper.find(DynamicListRow);
125
-
126
- expect(header.prop('columnSizes')).toBe(mockColumns);
127
- expect(row.prop('columnSizes')).toBe(mockColumns);
128
- });
129
-
130
162
  it(`each button element should have type of 'button'`, () => {
131
163
  const wrapper = mount(
132
164
  <DynamicDataList {...defaultProps} allowNewData={true} />,
@@ -170,7 +202,7 @@ describe('DynamicDataList', () => {
170
202
  position: 3,
171
203
  };
172
204
 
173
- const wrapper = shallow(
205
+ const wrapper = mount(
174
206
  <DynamicDataList
175
207
  {...defaultProps}
176
208
  columns={defaultColumns}
@@ -184,26 +216,6 @@ describe('DynamicDataList', () => {
184
216
  expect(row.prop('positionKey')).toBe(mockKey);
185
217
  });
186
218
 
187
- it(`sets 'allowReordering' prop for DynamicListHeader to false if 'positionKey' prop is undefined`, () => {
188
- const dataWithPosition: DynamicListTestData = {
189
- id: 0,
190
- title: 'fses',
191
- position: 3,
192
- };
193
-
194
- const wrapper = shallow(
195
- <DynamicDataList
196
- {...defaultProps}
197
- columns={defaultColumns}
198
- value={[dataWithPosition]}
199
- />,
200
- );
201
-
202
- const header = wrapper.find(DynamicListHeader);
203
-
204
- expect(header.prop('allowReordering')).toBe(false);
205
- });
206
-
207
219
  it(`sets 'allowReordering' prop for DynamicListHeader to true if 'positionPropertyName' prop is set and 'allowReordering' is true`, () => {
208
220
  const mockKey = 'position';
209
221
 
@@ -249,407 +261,128 @@ describe('DynamicDataList', () => {
249
261
  expect(header.prop('allowReordering')).toBe(false);
250
262
  });
251
263
 
252
- it(`sets 'dragging' to true if a row is being dragged and back to false when dragging is done`, () => {
253
- const dataWithPosition: DynamicListTestData = {
254
- id: 0,
255
- title: 'fses',
256
- position: 3,
257
- };
258
-
259
- const wrapper = shallow(
260
- <DynamicDataList
261
- {...defaultProps}
262
- columns={defaultColumns}
263
- value={[dataWithPosition]}
264
- allowReordering={true}
265
- />,
266
- );
267
-
268
- let row = wrapper.find(DynamicListRow);
269
-
270
- expect(row.prop('dragging')).toBe(false);
271
-
272
- // @ts-expect-error not full event args object
273
- row.prop('onDragStart')?.({}, dataWithPosition.position);
274
-
275
- wrapper.update();
276
-
277
- row = wrapper.find(DynamicListRow);
278
-
279
- expect(row.prop('dragging')).toBe(true);
280
-
281
- // @ts-expect-error not full event args object
282
- row.prop('onDragEnd')?.({}, dataWithPosition);
283
-
284
- wrapper.update();
285
-
286
- row = wrapper.find(DynamicListRow);
287
-
288
- expect(row.prop('dragging')).toBe(false);
289
- });
290
-
291
- describe('Data entry', () => {
292
- const mockPos1 = 1;
293
- const mockPos2 = 2;
294
- const mockPos3 = 3;
295
-
296
- const dataWithPositions: DynamicListTestData[] = [
297
- { id: 0, title: 'title-0', position: mockPos1 },
298
- { id: 1, title: 'title-1', position: mockPos2 },
299
- { id: 2, title: 'title-2', position: mockPos3 },
300
- ];
301
-
302
- it('disables new data entry by default', () => {
303
- const wrapper = shallow(<DynamicDataList {...defaultProps} />);
304
-
305
- const dataEntryRow = wrapper.find(DynamicListDataEntry);
306
-
307
- expect(dataEntryRow.exists()).toBe(false);
308
- });
309
-
310
- it(`allows new data entry if 'allowNewData' is true, 'allowAddAndRemove' prop is set to 'true'(default), and maxItemLimit is not set(default)`, () => {
311
- const wrapper = shallow(
312
- <DynamicDataList {...defaultProps} allowNewData={true} />,
313
- );
314
-
315
- const dataEntryRow = wrapper.find(DynamicListDataEntry);
316
-
317
- expect(dataEntryRow.exists()).toBe(true);
318
- });
264
+ describe('useDataHandler', () => {
265
+ it(`calls the 'useDataHandler' hook with the correct arguments`, () => {
266
+ const changeSpy = jest.fn();
267
+ const addTransformSpy = jest.fn();
268
+ const mockPositionKey = 'position';
319
269
 
320
- it(`disables new data entry if 'allowAddAndRemove' prop is set to 'false'`, () => {
321
- const wrapper = shallow(
270
+ mount(
322
271
  <DynamicDataList
323
272
  {...defaultProps}
273
+ positionPropertyName={mockPositionKey}
274
+ maxItemLimit={10}
324
275
  allowNewData={true}
325
- allowAddAndRemove={false}
276
+ onChange={changeSpy}
277
+ onAddTransformData={addTransformSpy}
326
278
  />,
327
279
  );
328
280
 
329
- const dataEntryRow = wrapper.find(DynamicListDataEntry);
330
-
331
- expect(dataEntryRow.exists()).toBe(false);
332
- });
333
-
334
- it(`disables new data entry if current item count is greater then the 'maxItemLimit' prop`, () => {
335
- const currentItemSize = defaultProps.value.length;
336
- const wrapper = shallow(
337
- <DynamicDataList
338
- {...defaultProps}
339
- allowNewData={true}
340
- maxItemLimit={0}
341
- />,
281
+ expect(useDataHandler).toHaveBeenCalledWith(
282
+ defaultData,
283
+ mockPositionKey,
284
+ 10,
285
+ true,
286
+ changeSpy,
287
+ addTransformSpy,
342
288
  );
343
-
344
- const dataEntryRow = wrapper.find(DynamicListDataEntry);
345
-
346
- expect(currentItemSize).toBe(1);
347
- expect(dataEntryRow.exists()).toBe(false);
348
289
  });
349
290
 
350
- it(`disables new data entry if current item count has reached 'maxItemLimit' prop`, () => {
351
- const currentItemSize = defaultProps.value.length;
352
- const wrapper = shallow(
353
- <DynamicDataList
354
- {...defaultProps}
355
- allowNewData={true}
356
- maxItemLimit={1}
357
- />,
291
+ it(`doesn't show new data entry row if 'canAddItems' is false`, () => {
292
+ const wrapper = mount(
293
+ <DynamicDataList {...defaultProps} allowNewData={true} />,
358
294
  );
359
295
 
360
296
  const dataEntryRow = wrapper.find(DynamicListDataEntry);
361
297
 
362
- expect(currentItemSize).toBe(1);
363
298
  expect(dataEntryRow.exists()).toBe(false);
364
299
  });
365
300
 
366
- it(`allows new data entry if current item count has not reached the 'maxItemLimit' prop`, () => {
367
- const currentItemSize = defaultProps.value.length;
368
- const wrapper = shallow(
369
- <DynamicDataList
370
- {...defaultProps}
371
- allowNewData={true}
372
- maxItemLimit={2}
373
- />,
301
+ it(`shows new data entry row if 'canAddItems' is true`, () => {
302
+ mockUseDataHandler.mockReturnValue({
303
+ removeItem: jest.fn(),
304
+ addItem: jest.fn(),
305
+ canAddItems: true,
306
+ items: defaultData,
307
+ nextPosition: 2,
308
+ });
309
+
310
+ const wrapper = mount(
311
+ <DynamicDataList {...defaultProps} allowNewData={true} />,
374
312
  );
375
313
 
376
314
  const dataEntryRow = wrapper.find(DynamicListDataEntry);
377
315
 
378
- expect(currentItemSize).toBe(1);
379
316
  expect(dataEntryRow.exists()).toBe(true);
380
317
  });
381
318
 
382
- it(`new data row position should be set after the last item in the list`, () => {
383
- const lastDataRow: DynamicListTestData = dataWithPositions.slice(-1)[0];
319
+ it(`sets 'newDataPosition' prop for DynamicListDataEntry`, () => {
320
+ const mockLimit = 10;
384
321
 
385
- const wrapper = shallow(
386
- <DynamicDataList
387
- {...defaultProps}
388
- allowNewData={true}
389
- columns={defaultColumns}
390
- value={dataWithPositions}
391
- allowReordering={true}
392
- positionPropertyName={'position'}
393
- />,
394
- );
395
-
396
- const dataEntryRow = wrapper.find(DynamicListDataEntry);
397
-
398
- expect(dataEntryRow.prop('newDataPosition')).toBe(
399
- (lastDataRow.position ?? 0) + 1,
400
- );
401
- });
322
+ mockUseDataHandler.mockReturnValue({
323
+ removeItem: jest.fn(),
324
+ addItem: jest.fn(),
325
+ canAddItems: true,
326
+ items: defaultData,
327
+ nextPosition: mockLimit,
328
+ });
402
329
 
403
- it('new data row position should default to 1 if the list is empty', () => {
404
- const wrapper = shallow(
405
- <DynamicDataList
406
- {...defaultProps}
407
- allowNewData={true}
408
- columns={defaultColumns}
409
- value={[]}
410
- allowReordering={true}
411
- />,
412
- );
330
+ const wrapper = mount(<DynamicDataList {...defaultProps} />);
413
331
 
414
332
  const dataEntryRow = wrapper.find(DynamicListDataEntry);
415
333
 
416
- expect(dataEntryRow.prop('newDataPosition')).toBe(1);
334
+ expect(dataEntryRow.prop('newDataPosition')).toBe(mockLimit);
417
335
  });
418
336
 
419
- it('appends new item to the end of the list array when the add button is clicked', () => {
420
- const addSpy = jest.fn();
421
- const mockNewItem: DynamicListTestData = {
422
- id: 3,
423
- title: 'title-3',
424
- };
425
- const wrapper = shallow(
426
- <DynamicDataList
427
- {...defaultProps}
428
- allowNewData={true}
429
- columns={defaultColumns}
430
- value={dataWithPositions}
431
- allowReordering={true}
432
- onChange={addSpy}
433
- />,
434
- );
435
-
436
- let rows = wrapper.find(DynamicListRow);
337
+ it('calls the addItem function when the add button is clicked', () => {
338
+ const addItemSpy = jest.fn();
437
339
 
438
- expect(rows).toHaveLength(3);
439
-
440
- const entryRow = wrapper.find(DynamicListDataEntry);
441
-
442
- act(() => {
443
- entryRow.prop('onActionClicked')?.(mockNewItem);
340
+ mockUseDataHandler.mockReturnValue({
341
+ removeItem: jest.fn(),
342
+ addItem: addItemSpy,
343
+ canAddItems: true,
344
+ items: defaultData,
345
+ nextPosition: 2,
444
346
  });
445
- wrapper.update();
446
347
 
447
- rows = wrapper.find(DynamicListRow);
448
-
449
- expect(rows).toHaveLength(4);
450
- expect(addSpy).toHaveBeenCalledTimes(1);
451
- expect(addSpy).toHaveBeenCalledWith([...dataWithPositions, mockNewItem]);
452
- });
453
-
454
- it(`transform new data if 'onAddTransformData' is set`, () => {
455
- const mockNewItem: DynamicListTestData = {
456
- id: 3,
457
- title: 'title-3',
348
+ const mockItem = {
349
+ id: 2,
350
+ title: 'test',
458
351
  };
459
- const mockTransformedItem: DynamicListTestData = {
460
- id: 3,
461
- title: 'transformed-title-3',
462
- };
463
- const addSpy = jest.fn();
464
- const transformSpy = jest.fn().mockReturnValueOnce(mockTransformedItem);
465
-
466
- const wrapper = shallow(
467
- <DynamicDataList
468
- {...defaultProps}
469
- allowNewData={true}
470
- columns={defaultColumns}
471
- value={dataWithPositions}
472
- allowReordering={true}
473
- onChange={addSpy}
474
- onAddTransformData={transformSpy}
475
- />,
476
- );
477
-
478
- let rows = wrapper.find(DynamicListRow);
479
-
480
- expect(rows).toHaveLength(3);
481
-
482
- const entryRow = wrapper.find(DynamicListDataEntry);
483
-
484
- act(() => {
485
- entryRow.prop('onActionClicked')?.(mockNewItem);
486
- });
487
- wrapper.update();
488
-
489
- rows = wrapper.find(DynamicListRow);
490
-
491
- expect(rows).toHaveLength(4);
492
- expect(transformSpy).toHaveBeenCalledTimes(1);
493
- expect(transformSpy).toHaveBeenCalledWith(mockNewItem);
494
- expect(addSpy).toHaveBeenCalledWith([
495
- ...dataWithPositions,
496
- mockTransformedItem,
497
- ]);
498
- });
499
-
500
- it('sorts the data using the position parameter', () => {
501
- const dataWithPositions: DynamicListTestData[] = [
502
- { id: 0, title: 'title-0', position: 2 },
503
- { id: 1, title: 'title-1', position: 3 },
504
- { id: 2, title: 'title-2', position: 1 },
505
- ];
506
352
 
507
353
  const wrapper = mount(
508
- <DynamicDataList
509
- {...defaultProps}
510
- allowNewData={true}
511
- columns={defaultColumns}
512
- value={dataWithPositions}
513
- allowRowDragging={false}
514
- allowReordering={false}
515
- positionPropertyName="position"
516
- />,
517
- );
518
-
519
- const rows = wrapper.find(DynamicListRow);
520
-
521
- expect(rows).toHaveLength(3);
522
-
523
- const data = rows.map((row) => row.prop('data'));
524
-
525
- expect(data).toMatchObject([
526
- { id: 2, title: 'title-2', position: 1 },
527
- { id: 0, title: 'title-0', position: 2 },
528
- { id: 1, title: 'title-1', position: 3 },
529
- ]);
530
- });
531
- });
532
-
533
- describe('deleting items', () => {
534
- const mockPos1 = 1;
535
- const mockPos2 = 2;
536
- const mockPos3 = 3;
537
-
538
- const dataWithPositions: DynamicListTestData[] = [
539
- { id: 0, title: 'title-0', position: mockPos1 },
540
- { id: 1, title: 'title-1', position: mockPos2 },
541
- { id: 2, title: 'title-2', position: mockPos3 },
542
- ];
543
-
544
- it('deletes a row when the delete action button is clicked and repositioning is not allowed', () => {
545
- const deleteSpy = jest.fn();
546
- const mockEvent = {
547
- e: 'event',
548
- };
549
- const [, mockRow2, mockRow3] = dataWithPositions;
550
- const wrapper = mount(
551
- <DynamicDataList
552
- {...defaultProps}
553
- allowNewData={true}
554
- columns={defaultColumns}
555
- value={dataWithPositions}
556
- allowRowDragging={false}
557
- allowReordering={false}
558
- onChange={deleteSpy}
559
- />,
354
+ <DynamicDataList {...defaultProps} allowNewData={true} />,
560
355
  );
561
356
 
562
- let rows = wrapper.find(DynamicListRow);
563
-
564
- expect(rows).toHaveLength(3);
565
-
566
- const deleteActions = wrapper.find(Button);
357
+ const dataEntryRow = wrapper.find(DynamicListDataEntry);
567
358
 
568
359
  act(() => {
569
- // @ts-expect-error not full event args object
570
- deleteActions.at(1).prop('onButtonClicked')?.(mockEvent);
360
+ dataEntryRow.prop('onActionClicked')?.(mockItem);
571
361
  });
572
- wrapper.update();
573
-
574
- rows = wrapper.find(DynamicListRow);
575
362
 
576
- expect(rows).toHaveLength(2);
577
- expect(deleteSpy).toHaveBeenCalledTimes(1);
578
- expect(deleteSpy).toHaveBeenCalledWith([mockRow2, mockRow3]);
363
+ expect(addItemSpy).toHaveBeenCalledWith(mockItem);
579
364
  });
580
365
 
581
- it('deletes a row and updates subsequent row positions when row repositioning is allowed', () => {
582
- const mockEvent = {
583
- e: 'event',
584
- };
585
- const deleteSpy = jest.fn();
586
- const [, mockRow2, mockRow3] = dataWithPositions;
587
- const wrapper = mount(
588
- <DynamicDataList
589
- {...defaultProps}
590
- allowNewData={true}
591
- columns={defaultColumns}
592
- value={dataWithPositions}
593
- positionPropertyName={'position'}
594
- allowRowDragging={true}
595
- allowReordering={true}
596
- onChange={deleteSpy}
597
- />,
598
- );
366
+ it('calls the removeItem function when the delete button is clicked', () => {
367
+ const removeItemSpy = jest.fn();
599
368
 
600
- let rows = wrapper.find(DynamicListRow);
601
-
602
- expect(rows).toHaveLength(3);
603
-
604
- const deleteActions = wrapper.find(Button);
605
-
606
- act(() => {
607
- // @ts-expect-error not full event args object
608
- deleteActions.at(1).prop('onButtonClicked')?.(mockEvent);
369
+ mockUseDataHandler.mockReturnValue({
370
+ removeItem: removeItemSpy,
371
+ addItem: jest.fn(),
372
+ canAddItems: true,
373
+ items: defaultData,
374
+ nextPosition: 2,
609
375
  });
610
- wrapper.update();
611
376
 
612
- rows = wrapper.find(DynamicListRow);
377
+ const wrapper = mount(<DynamicDataList {...defaultProps} />);
613
378
 
614
- expect(rows).toHaveLength(2);
615
- expect(deleteSpy).toHaveBeenCalledTimes(1);
616
- expect(deleteSpy).toHaveBeenCalledWith([
617
- { ...mockRow2, position: mockPos1 },
618
- { ...mockRow3, position: mockPos2 },
619
- ]);
620
- });
621
-
622
- it(`data entry row's position should always be after the new last item's position after deleting the previous last item`, () => {
623
- const mockEvent = {
624
- e: 'event',
625
- };
626
-
627
- const wrapper = mount(
628
- <DynamicDataList
629
- {...defaultProps}
630
- allowNewData={true}
631
- columns={defaultColumns}
632
- value={dataWithPositions}
633
- positionPropertyName={'position'}
634
- allowReordering={true}
635
- />,
636
- );
637
-
638
- const deleteActions = wrapper.find(Button);
639
-
640
- let dataEntryRow = wrapper.find(DynamicListDataEntry);
641
-
642
- expect(dataEntryRow.prop('newDataPosition')).toBe(mockPos3 + 1);
379
+ const row = wrapper.find(DynamicListRow);
643
380
 
644
381
  act(() => {
645
- // @ts-expect-error not full event args object
646
- deleteActions.at(2).prop('onButtonClicked')?.(mockEvent);
382
+ row.prop('onActionClicked')?.(defaultData[0]);
647
383
  });
648
- wrapper.update();
649
-
650
- dataEntryRow = wrapper.find(DynamicListDataEntry);
651
384
 
652
- expect(dataEntryRow.prop('newDataPosition')).toBe(mockPos2 + 1);
385
+ expect(removeItemSpy).toHaveBeenCalledWith(defaultData[0]);
653
386
  });
654
387
  });
655
388
  });
@@ -103,11 +103,6 @@ const meta: Meta<StoryDDLType> = {
103
103
  component: DynamicDataList,
104
104
  argTypes: {
105
105
  ...groups,
106
- error: {
107
- table: {
108
- disable: true,
109
- },
110
- },
111
106
  customDataEntry: {
112
107
  ...groups.customDataEntry,
113
108
  control: false,