@dhis2-ui/transfer 10.16.2 → 10.16.3

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 (109) hide show
  1. package/package.json +10 -9
  2. package/src/__e2e__/add_remove-highlighted-options.e2e.stories.js +30 -0
  3. package/src/__e2e__/common/options.js +90 -0
  4. package/src/__e2e__/common/stateful-decorator.js +33 -0
  5. package/src/__e2e__/common.js +0 -0
  6. package/src/__e2e__/disabled-transfer-buttons.e2e.stories.js +49 -0
  7. package/src/__e2e__/disabled-transfer-options.e2e.stories.js +21 -0
  8. package/src/__e2e__/display-order.e2e.stories.js +24 -0
  9. package/src/__e2e__/filter-options-list.e2e.stories.js +87 -0
  10. package/src/__e2e__/highlight-range-of-options.e2e.stories.js +52 -0
  11. package/src/__e2e__/loading_lists.e2e.stories.js +26 -0
  12. package/src/__e2e__/notify_at_end_of_list.e2e.stories.js +116 -0
  13. package/src/__e2e__/reorder-with-buttons.e2e.stories.js +35 -0
  14. package/src/__e2e__/set_unset-highlighted-option.e2e.stories.js +30 -0
  15. package/src/__e2e__/transferring-items.e2e.stories.js +27 -0
  16. package/src/__tests__/common.test.js +131 -0
  17. package/src/__tests__/helper/add-all-selectable-source-options.test.js +46 -0
  18. package/src/__tests__/helper/add-individual-source-options.test.js +80 -0
  19. package/src/__tests__/helper/default-filter-callback.test.js +45 -0
  20. package/src/__tests__/helper/is-reorder-down-disabled.test.js +96 -0
  21. package/src/__tests__/helper/is-reorder-up-disabled.test.js +96 -0
  22. package/src/__tests__/helper/move-highlighted-picked-option-down.test.js +111 -0
  23. package/src/__tests__/helper/move-highlighted-picked-option-to-bottom.test.js +101 -0
  24. package/src/__tests__/helper/move-highlighted-picked-option-to-top.test.js +101 -0
  25. package/src/__tests__/helper/move-highlighted-picked-option-up.test.js +111 -0
  26. package/src/__tests__/helper/remove-all-picked-options.test.js +29 -0
  27. package/src/__tests__/helper/remove-individual-picked-options.test.js +38 -0
  28. package/src/__tests__/helper/use-highlighted-option/create-toggle-highlighted-option.test.js +104 -0
  29. package/src/__tests__/helper/use-highlighted-option/toggle-add.test.js +84 -0
  30. package/src/__tests__/helper/use-highlighted-option/toggle-range.test.js +150 -0
  31. package/src/__tests__/helper/use-highlighted-option/toggle-replace.test.js +39 -0
  32. package/src/__tests__/helper/use-highlighted-option.test.js +41 -0
  33. package/src/__tests__/reordering-actions.test.js +165 -0
  34. package/src/__tests__/transfer.test.js +137 -0
  35. package/src/actions.js +33 -0
  36. package/src/add-all.js +27 -0
  37. package/src/add-individual.js +27 -0
  38. package/src/common/find-option-index.js +9 -0
  39. package/src/common/get-mode-by-modifier-key.js +35 -0
  40. package/src/common/index.js +5 -0
  41. package/src/common/is-option.js +7 -0
  42. package/src/common/modes.js +11 -0
  43. package/src/common/remove-option.js +19 -0
  44. package/src/common/toggle-value.js +18 -0
  45. package/src/container.js +23 -0
  46. package/src/end-intersection-detector.js +37 -0
  47. package/src/features/add_remove-highlighted-options/index.js +92 -0
  48. package/src/features/add_remove-highlighted-options.feature +41 -0
  49. package/src/features/common/index.js +8 -0
  50. package/src/features/disabled-transfer-buttons/index.js +118 -0
  51. package/src/features/disabled-transfer-buttons.feature +46 -0
  52. package/src/features/disabled-transfer-options/index.js +182 -0
  53. package/src/features/disabled-transfer-options.feature +42 -0
  54. package/src/features/display-order/index.js +205 -0
  55. package/src/features/display-order.feature +30 -0
  56. package/src/features/filter-options-list/index.js +133 -0
  57. package/src/features/filter-options-list.feature +40 -0
  58. package/src/features/highlight-range-of-options/index.js +336 -0
  59. package/src/features/highlight-range-of-options.feature +70 -0
  60. package/src/features/loading_lists/index.js +43 -0
  61. package/src/features/loading_lists.feature +19 -0
  62. package/src/features/notify_at_end_of_list/index.js +125 -0
  63. package/src/features/notify_at_end_of_list.feature +64 -0
  64. package/src/features/reorder-with-buttons/index.js +181 -0
  65. package/src/features/reorder-with-buttons.feature +138 -0
  66. package/src/features/set_unset-highlighted-option/index.js +121 -0
  67. package/src/features/set_unset-highlighted-option.feature +42 -0
  68. package/src/features/transferring-items/index.js +375 -0
  69. package/src/features/transferring-items.feature +44 -0
  70. package/src/filter.js +38 -0
  71. package/src/icons.js +194 -0
  72. package/src/index.js +2 -0
  73. package/src/left-footer.js +22 -0
  74. package/src/left-header.js +22 -0
  75. package/src/left-side.js +34 -0
  76. package/src/locales/en/translations.json +7 -0
  77. package/src/locales/index.js +16 -0
  78. package/src/options-container.js +127 -0
  79. package/src/remove-all.js +27 -0
  80. package/src/remove-individual.js +27 -0
  81. package/src/reordering-actions.js +136 -0
  82. package/src/right-footer.js +22 -0
  83. package/src/right-header.js +22 -0
  84. package/src/right-side.js +33 -0
  85. package/src/transfer/add-all-selectable-source-options.js +37 -0
  86. package/src/transfer/add-individual-source-options.js +61 -0
  87. package/src/transfer/create-double-click-handlers.js +36 -0
  88. package/src/transfer/default-filter-callback.js +17 -0
  89. package/src/transfer/get-highlighted-picked-indices.js +26 -0
  90. package/src/transfer/get-option-click-handlers.js +19 -0
  91. package/src/transfer/index.js +17 -0
  92. package/src/transfer/is-reorder-down-disabled.js +34 -0
  93. package/src/transfer/is-reorder-up-disabled.js +30 -0
  94. package/src/transfer/move-highlighted-picked-option-down.js +54 -0
  95. package/src/transfer/move-highlighted-picked-option-to-bottom.js +44 -0
  96. package/src/transfer/move-highlighted-picked-option-to-top.js +38 -0
  97. package/src/transfer/move-highlighted-picked-option-up.js +47 -0
  98. package/src/transfer/remove-all-picked-options.js +13 -0
  99. package/src/transfer/remove-individual-picked-options.js +49 -0
  100. package/src/transfer/use-filter.js +17 -0
  101. package/src/transfer/use-highlighted-options/create-toggle-highlighted-option.js +64 -0
  102. package/src/transfer/use-highlighted-options/toggle-add.js +20 -0
  103. package/src/transfer/use-highlighted-options/toggle-range.js +61 -0
  104. package/src/transfer/use-highlighted-options/toggle-replace.js +26 -0
  105. package/src/transfer/use-highlighted-options.js +34 -0
  106. package/src/transfer/use-options-key-monitor.js +41 -0
  107. package/src/transfer-option.js +91 -0
  108. package/src/transfer.js +539 -0
  109. package/src/transfer.prod.stories.js +621 -0
@@ -0,0 +1,101 @@
1
+ import { moveHighlightedPickedOptionToBottom } from '../../transfer/move-highlighted-picked-option-to-bottom.js'
2
+
3
+ describe('Transfer - moveHighlightedPickedOptionToBottom', () => {
4
+ const onChange = jest.fn()
5
+
6
+ afterEach(() => {
7
+ onChange.mockClear()
8
+ })
9
+
10
+ it('should move a single highlighted option to the bottom', () => {
11
+ moveHighlightedPickedOptionToBottom({
12
+ selected: ['a', 'b', 'c', 'd'],
13
+ highlightedPickedOptions: ['b'],
14
+ onChange,
15
+ })
16
+
17
+ expect(onChange).toHaveBeenCalledWith({
18
+ selected: ['a', 'c', 'd', 'b'],
19
+ })
20
+ })
21
+
22
+ it('should move a contiguous block to the bottom as a group', () => {
23
+ moveHighlightedPickedOptionToBottom({
24
+ selected: ['a', 'b', 'c', 'd', 'e'],
25
+ highlightedPickedOptions: ['b', 'c'],
26
+ onChange,
27
+ })
28
+
29
+ expect(onChange).toHaveBeenCalledWith({
30
+ selected: ['a', 'd', 'e', 'b', 'c'],
31
+ })
32
+ })
33
+
34
+ it('should collapse a non-contiguous selection to the bottom preserving relative order', () => {
35
+ moveHighlightedPickedOptionToBottom({
36
+ selected: ['a', 'b', 'c', 'd', 'e'],
37
+ highlightedPickedOptions: ['b', 'd'],
38
+ onChange,
39
+ })
40
+
41
+ expect(onChange).toHaveBeenCalledWith({
42
+ selected: ['a', 'c', 'e', 'b', 'd'],
43
+ })
44
+ })
45
+
46
+ it('should preserve relative order regardless of highlight input order', () => {
47
+ moveHighlightedPickedOptionToBottom({
48
+ selected: ['a', 'b', 'c', 'd', 'e'],
49
+ highlightedPickedOptions: ['d', 'b'],
50
+ onChange,
51
+ })
52
+
53
+ expect(onChange).toHaveBeenCalledWith({
54
+ selected: ['a', 'c', 'e', 'b', 'd'],
55
+ })
56
+ })
57
+
58
+ it('should do nothing when the highlighted block is already flush to the bottom', () => {
59
+ moveHighlightedPickedOptionToBottom({
60
+ selected: ['a', 'b', 'c', 'd'],
61
+ highlightedPickedOptions: ['c', 'd'],
62
+ onChange,
63
+ })
64
+
65
+ expect(onChange).toHaveBeenCalledTimes(0)
66
+ })
67
+
68
+ it('should still run when the selection includes the bottom item but has a gap', () => {
69
+ moveHighlightedPickedOptionToBottom({
70
+ selected: ['a', 'b', 'c', 'd'],
71
+ highlightedPickedOptions: ['b', 'd'],
72
+ onChange,
73
+ })
74
+
75
+ expect(onChange).toHaveBeenCalledWith({
76
+ selected: ['a', 'c', 'b', 'd'],
77
+ })
78
+ })
79
+
80
+ it('should do nothing when no highlighted options exist in selected', () => {
81
+ moveHighlightedPickedOptionToBottom({
82
+ selected: ['a', 'b', 'c'],
83
+ highlightedPickedOptions: ['ghost'],
84
+ onChange,
85
+ })
86
+
87
+ expect(onChange).toHaveBeenCalledTimes(0)
88
+ })
89
+
90
+ it('should ignore highlighted values that do not exist in selected', () => {
91
+ moveHighlightedPickedOptionToBottom({
92
+ selected: ['a', 'b', 'c'],
93
+ highlightedPickedOptions: ['ghost', 'a'],
94
+ onChange,
95
+ })
96
+
97
+ expect(onChange).toHaveBeenCalledWith({
98
+ selected: ['b', 'c', 'a'],
99
+ })
100
+ })
101
+ })
@@ -0,0 +1,101 @@
1
+ import { moveHighlightedPickedOptionToTop } from '../../transfer/move-highlighted-picked-option-to-top.js'
2
+
3
+ describe('Transfer - moveHighlightedPickedOptionToTop', () => {
4
+ const onChange = jest.fn()
5
+
6
+ afterEach(() => {
7
+ onChange.mockClear()
8
+ })
9
+
10
+ it('should move a single highlighted option to the top', () => {
11
+ moveHighlightedPickedOptionToTop({
12
+ selected: ['a', 'b', 'c', 'd'],
13
+ highlightedPickedOptions: ['c'],
14
+ onChange,
15
+ })
16
+
17
+ expect(onChange).toHaveBeenCalledWith({
18
+ selected: ['c', 'a', 'b', 'd'],
19
+ })
20
+ })
21
+
22
+ it('should move a contiguous block to the top as a group', () => {
23
+ moveHighlightedPickedOptionToTop({
24
+ selected: ['a', 'b', 'c', 'd', 'e'],
25
+ highlightedPickedOptions: ['c', 'd'],
26
+ onChange,
27
+ })
28
+
29
+ expect(onChange).toHaveBeenCalledWith({
30
+ selected: ['c', 'd', 'a', 'b', 'e'],
31
+ })
32
+ })
33
+
34
+ it('should collapse a non-contiguous selection to the top preserving relative order', () => {
35
+ moveHighlightedPickedOptionToTop({
36
+ selected: ['a', 'b', 'c', 'd', 'e'],
37
+ highlightedPickedOptions: ['b', 'd'],
38
+ onChange,
39
+ })
40
+
41
+ expect(onChange).toHaveBeenCalledWith({
42
+ selected: ['b', 'd', 'a', 'c', 'e'],
43
+ })
44
+ })
45
+
46
+ it('should preserve relative order regardless of highlight input order', () => {
47
+ moveHighlightedPickedOptionToTop({
48
+ selected: ['a', 'b', 'c', 'd', 'e'],
49
+ highlightedPickedOptions: ['d', 'b'],
50
+ onChange,
51
+ })
52
+
53
+ expect(onChange).toHaveBeenCalledWith({
54
+ selected: ['b', 'd', 'a', 'c', 'e'],
55
+ })
56
+ })
57
+
58
+ it('should do nothing when the highlighted block is already flush to the top', () => {
59
+ moveHighlightedPickedOptionToTop({
60
+ selected: ['a', 'b', 'c', 'd'],
61
+ highlightedPickedOptions: ['a', 'b'],
62
+ onChange,
63
+ })
64
+
65
+ expect(onChange).toHaveBeenCalledTimes(0)
66
+ })
67
+
68
+ it('should still run when the selection includes the top item but has a gap', () => {
69
+ moveHighlightedPickedOptionToTop({
70
+ selected: ['a', 'b', 'c', 'd'],
71
+ highlightedPickedOptions: ['a', 'c'],
72
+ onChange,
73
+ })
74
+
75
+ expect(onChange).toHaveBeenCalledWith({
76
+ selected: ['a', 'c', 'b', 'd'],
77
+ })
78
+ })
79
+
80
+ it('should do nothing when no highlighted options exist in selected', () => {
81
+ moveHighlightedPickedOptionToTop({
82
+ selected: ['a', 'b', 'c'],
83
+ highlightedPickedOptions: ['ghost'],
84
+ onChange,
85
+ })
86
+
87
+ expect(onChange).toHaveBeenCalledTimes(0)
88
+ })
89
+
90
+ it('should ignore highlighted values that do not exist in selected', () => {
91
+ moveHighlightedPickedOptionToTop({
92
+ selected: ['a', 'b', 'c'],
93
+ highlightedPickedOptions: ['ghost', 'c'],
94
+ onChange,
95
+ })
96
+
97
+ expect(onChange).toHaveBeenCalledWith({
98
+ selected: ['c', 'a', 'b'],
99
+ })
100
+ })
101
+ })
@@ -0,0 +1,111 @@
1
+ import { moveHighlightedPickedOptionUp } from '../../transfer/move-highlighted-picked-option-up.js'
2
+
3
+ describe('Transfer - moveHighlightedPickedOptionUp', () => {
4
+ const onChange = jest.fn()
5
+
6
+ afterEach(() => {
7
+ onChange.mockClear()
8
+ })
9
+
10
+ it('should move a single highlighted option up', () => {
11
+ moveHighlightedPickedOptionUp({
12
+ selected: ['foo', 'bar', 'baz'],
13
+ highlightedPickedOptions: ['bar'],
14
+ onChange,
15
+ })
16
+
17
+ expect(onChange).toHaveBeenCalledWith({
18
+ selected: ['bar', 'foo', 'baz'],
19
+ })
20
+ })
21
+
22
+ it('should do nothing when trying to move up the first option', () => {
23
+ moveHighlightedPickedOptionUp({
24
+ selected: ['foo', 'bar', 'baz'],
25
+ highlightedPickedOptions: ['foo'],
26
+ onChange,
27
+ })
28
+
29
+ expect(onChange).toHaveBeenCalledTimes(0)
30
+ })
31
+
32
+ it('should do nothing when trying to move up a non-existing option', () => {
33
+ moveHighlightedPickedOptionUp({
34
+ selected: ['foo', 'bar', 'baz'],
35
+ highlightedPickedOptions: ['ghost'],
36
+ onChange,
37
+ })
38
+
39
+ expect(onChange).toHaveBeenCalledTimes(0)
40
+ })
41
+
42
+ it('should shift a contiguous block of highlighted options up as a group', () => {
43
+ moveHighlightedPickedOptionUp({
44
+ selected: ['a', 'b', 'c', 'd', 'e'],
45
+ highlightedPickedOptions: ['c', 'd'],
46
+ onChange,
47
+ })
48
+
49
+ expect(onChange).toHaveBeenCalledWith({
50
+ selected: ['a', 'c', 'd', 'b', 'e'],
51
+ })
52
+ })
53
+
54
+ it('should collapse and shift a non-contiguous selection up in one call', () => {
55
+ moveHighlightedPickedOptionUp({
56
+ selected: ['a', 'b', 'c', 'd', 'e'],
57
+ highlightedPickedOptions: ['b', 'd'],
58
+ onChange,
59
+ })
60
+
61
+ expect(onChange).toHaveBeenCalledWith({
62
+ selected: ['b', 'd', 'a', 'c', 'e'],
63
+ })
64
+ })
65
+
66
+ it('should preserve the relative order of highlighted items regardless of input order', () => {
67
+ moveHighlightedPickedOptionUp({
68
+ selected: ['a', 'b', 'c', 'd', 'e'],
69
+ highlightedPickedOptions: ['d', 'b'],
70
+ onChange,
71
+ })
72
+
73
+ expect(onChange).toHaveBeenCalledWith({
74
+ selected: ['b', 'd', 'a', 'c', 'e'],
75
+ })
76
+ })
77
+
78
+ it('should collapse a non-contiguous selection containing the first item without shifting past index 0', () => {
79
+ moveHighlightedPickedOptionUp({
80
+ selected: ['a', 'b', 'c'],
81
+ highlightedPickedOptions: ['a', 'c'],
82
+ onChange,
83
+ })
84
+
85
+ expect(onChange).toHaveBeenCalledWith({
86
+ selected: ['a', 'c', 'b'],
87
+ })
88
+ })
89
+
90
+ it('should do nothing when the highlighted block is already flush to the top', () => {
91
+ moveHighlightedPickedOptionUp({
92
+ selected: ['a', 'b', 'c', 'd'],
93
+ highlightedPickedOptions: ['a', 'b'],
94
+ onChange,
95
+ })
96
+
97
+ expect(onChange).toHaveBeenCalledTimes(0)
98
+ })
99
+
100
+ it('should ignore highlighted values that do not exist in selected', () => {
101
+ moveHighlightedPickedOptionUp({
102
+ selected: ['a', 'b', 'c'],
103
+ highlightedPickedOptions: ['ghost', 'c'],
104
+ onChange,
105
+ })
106
+
107
+ expect(onChange).toHaveBeenCalledWith({
108
+ selected: ['a', 'c', 'b'],
109
+ })
110
+ })
111
+ })
@@ -0,0 +1,29 @@
1
+ import { removeAllPickedOptions } from '../../transfer/remove-all-picked-options.js'
2
+
3
+ describe('Transfer - removeAllPickedOptions', () => {
4
+ const onChange = jest.fn()
5
+ const setHighlightedPickedOptions = jest.fn()
6
+
7
+ afterEach(() => {
8
+ onChange.mockClear()
9
+ setHighlightedPickedOptions.mockClear()
10
+ })
11
+
12
+ it('should reset the selected array', () => {
13
+ removeAllPickedOptions({
14
+ onChange,
15
+ setHighlightedPickedOptions,
16
+ })
17
+
18
+ expect(onChange).toHaveBeenCalledWith({ selected: [] })
19
+ })
20
+
21
+ it('should reset the highlighted picked options', () => {
22
+ removeAllPickedOptions({
23
+ onChange,
24
+ setHighlightedPickedOptions,
25
+ })
26
+
27
+ expect(setHighlightedPickedOptions).toHaveBeenCalledWith([])
28
+ })
29
+ })
@@ -0,0 +1,38 @@
1
+ import { removeIndividualPickedOptions } from '../../transfer/remove-individual-picked-options.js'
2
+
3
+ describe('Transfer - removeIndividualPickedOptions', () => {
4
+ const onChange = jest.fn()
5
+ const setHighlightedPickedOptions = jest.fn()
6
+
7
+ afterEach(() => {
8
+ onChange.mockClear()
9
+ setHighlightedPickedOptions.mockClear()
10
+ })
11
+
12
+ const selected = ['foo', 'bar', 'baz']
13
+ const highlightedPickedOptions = ['bar', 'baz']
14
+
15
+ it('should remove the highlighted picked options', () => {
16
+ removeIndividualPickedOptions({
17
+ highlightedPickedOptions,
18
+ selected,
19
+ onChange,
20
+ setHighlightedPickedOptions,
21
+ })
22
+
23
+ expect(onChange).toHaveBeenCalledWith({
24
+ selected: ['foo'],
25
+ })
26
+ })
27
+
28
+ it('should reset the highlighted picked options', () => {
29
+ removeIndividualPickedOptions({
30
+ highlightedPickedOptions,
31
+ selected,
32
+ onChange,
33
+ setHighlightedPickedOptions,
34
+ })
35
+
36
+ expect(setHighlightedPickedOptions).toHaveBeenCalledWith([])
37
+ })
38
+ })
@@ -0,0 +1,104 @@
1
+ import { ADD_MODE, RANGE_MODE, REPLACE_MODE } from '../../../common/index.js'
2
+ import { createToggleHighlightedOption } from '../../../transfer/use-highlighted-options/create-toggle-highlighted-option.js'
3
+ import { toggleAdd } from '../../../transfer/use-highlighted-options/toggle-add.js'
4
+ import { toggleRange } from '../../../transfer/use-highlighted-options/toggle-range.js'
5
+ import { toggleReplace } from '../../../transfer/use-highlighted-options/toggle-replace.js'
6
+
7
+ jest.mock('../../../transfer/use-highlighted-options/toggle-add.js', () => ({
8
+ toggleAdd: jest.fn(),
9
+ }))
10
+
11
+ jest.mock('../../../transfer/use-highlighted-options/toggle-range.js', () => ({
12
+ toggleRange: jest.fn(),
13
+ }))
14
+
15
+ jest.mock(
16
+ '../../../transfer/use-highlighted-options/toggle-replace.js',
17
+ () => ({ toggleReplace: jest.fn() })
18
+ )
19
+
20
+ describe('Transfer- useHighlightedOptions - createToggleHighlightedOption', () => {
21
+ let toggleHighlightedOption
22
+ const disabled = false
23
+ const lastClicked = null
24
+ const setLastClicked = jest.fn()
25
+ const highlightedOptions = []
26
+ const maxSelections = Infinity
27
+ const setHighlightedOptions = jest.fn()
28
+ const options = []
29
+ const option = { value: 'foo', label: 'Foo' }
30
+ const createToggleHighlightedOptionDefaultPayload = {
31
+ options,
32
+ disabled,
33
+ highlightedOptions,
34
+ lastClicked,
35
+ maxSelections,
36
+ setHighlightedOptions,
37
+ setLastClicked,
38
+ }
39
+
40
+ beforeEach(() => {
41
+ toggleHighlightedOption = createToggleHighlightedOption(
42
+ createToggleHighlightedOptionDefaultPayload
43
+ )
44
+ })
45
+
46
+ afterEach(() => {
47
+ setLastClicked.mockClear()
48
+ toggleAdd.mockClear()
49
+ toggleRange.mockClear()
50
+ toggleReplace.mockClear()
51
+ })
52
+
53
+ it('should set the lastClicked to the clicked option when mode is ADD_MODE', () => {
54
+ toggleHighlightedOption({ option, mode: ADD_MODE })
55
+ expect(setLastClicked).toHaveBeenCalledWith(option.value)
56
+ })
57
+
58
+ it('should set the lastClicked to the clicked option when mode is REPLACE_MODE', () => {
59
+ toggleHighlightedOption({ option, mode: REPLACE_MODE })
60
+ expect(setLastClicked).toHaveBeenCalledWith(option.value)
61
+ })
62
+
63
+ it('should not overwrite the lastClicked to the clicked option when mode is RANGE_MODE', () => {
64
+ toggleHighlightedOption({ option, mode: RANGE_MODE })
65
+ expect(setLastClicked).toHaveBeenCalledTimes(0)
66
+ })
67
+
68
+ it('should use the toggleAdd function when mode is ADD_MODE', () => {
69
+ toggleHighlightedOption({ option, mode: ADD_MODE })
70
+ expect(toggleAdd).toHaveBeenCalledTimes(1)
71
+ expect(toggleRange).toHaveBeenCalledTimes(0)
72
+ expect(toggleReplace).toHaveBeenCalledTimes(0)
73
+ })
74
+
75
+ it('should use the toggleRange function when mode is RANGE_MODE', () => {
76
+ toggleHighlightedOption({ option, mode: RANGE_MODE })
77
+ expect(toggleRange).toHaveBeenCalledTimes(1)
78
+ expect(toggleAdd).toHaveBeenCalledTimes(0)
79
+ expect(toggleReplace).toHaveBeenCalledTimes(0)
80
+ })
81
+
82
+ it('should use the toggleReplace function when mode is REPLACE_MODE', () => {
83
+ toggleHighlightedOption({ option, mode: REPLACE_MODE })
84
+ expect(toggleReplace).toHaveBeenCalledTimes(1)
85
+ expect(toggleAdd).toHaveBeenCalledTimes(0)
86
+ expect(toggleRange).toHaveBeenCalledTimes(0)
87
+ })
88
+
89
+ it('should do nothing when disabled is true', () => {
90
+ toggleHighlightedOption = createToggleHighlightedOption({
91
+ ...createToggleHighlightedOptionDefaultPayload,
92
+ disabled: true,
93
+ })
94
+
95
+ toggleHighlightedOption({ option, mode: ADD_MODE })
96
+ toggleHighlightedOption({ option, mode: RANGE_MODE })
97
+ toggleHighlightedOption({ option, mode: REPLACE_MODE })
98
+
99
+ expect(toggleReplace).toHaveBeenCalledTimes(0)
100
+ expect(toggleAdd).toHaveBeenCalledTimes(0)
101
+ expect(toggleRange).toHaveBeenCalledTimes(0)
102
+ expect(setLastClicked).toHaveBeenCalledTimes(0)
103
+ })
104
+ })
@@ -0,0 +1,84 @@
1
+ import { toggleAdd } from '../../../transfer/use-highlighted-options/toggle-add.js'
2
+
3
+ describe('Transfer - useHighlightedOptions - toggleAdd', () => {
4
+ const setHighlightedOptions = jest.fn()
5
+
6
+ afterEach(() => {
7
+ setHighlightedOptions.mockClear()
8
+ })
9
+
10
+ describe('maxSelections=1', () => {
11
+ const maxSelections = 1
12
+
13
+ it('should replace the current highlighted option', () => {
14
+ const highlightedOptions = ['foo']
15
+ const option = { value: 'bar', label: 'Bar' }
16
+ const expected = [option.value]
17
+
18
+ toggleAdd({
19
+ maxSelections,
20
+ highlightedOptions,
21
+ option,
22
+ setHighlightedOptions,
23
+ })
24
+
25
+ expect(setHighlightedOptions).toHaveBeenCalledWith(expected)
26
+ })
27
+
28
+ it('should empty the highlighted options', () => {
29
+ const highlightedOptions = ['foo']
30
+ const option = { value: 'foo', label: 'Foo' }
31
+ const expected = []
32
+
33
+ toggleAdd({
34
+ maxSelections,
35
+ highlightedOptions,
36
+ option,
37
+ setHighlightedOptions,
38
+ })
39
+
40
+ expect(setHighlightedOptions).toHaveBeenCalledWith(expected)
41
+ })
42
+ })
43
+
44
+ describe('maxSelections=Infinity', () => {
45
+ const maxSelections = Infinity
46
+
47
+ it('should add the option to the highlighted options when maxSelections is Infinity', () => {
48
+ const highlightedOptions = ['foo']
49
+ const option = { value: 'bar', label: 'Bar' }
50
+ const expected = [...highlightedOptions, option.value]
51
+
52
+ toggleAdd({
53
+ maxSelections,
54
+ highlightedOptions,
55
+ option,
56
+ setHighlightedOptions,
57
+ })
58
+
59
+ expect(setHighlightedOptions).toHaveBeenCalledWith(expected)
60
+ })
61
+
62
+ it('should remove the option from the highlighted list', () => {
63
+ const optionOne = { value: 'foo', label: 'Foo' }
64
+ const optionTwo = { value: 'bar', label: 'Bar' }
65
+ const optionThree = { value: 'baz', label: 'Baz' }
66
+ const highlightedOptions = [
67
+ optionOne.value,
68
+ optionTwo.value,
69
+ optionThree.value,
70
+ ]
71
+ const expected = [optionOne, optionThree].map(({ value }) => value)
72
+ const option = optionTwo
73
+
74
+ toggleAdd({
75
+ maxSelections,
76
+ highlightedOptions,
77
+ option,
78
+ setHighlightedOptions,
79
+ })
80
+
81
+ expect(setHighlightedOptions).toHaveBeenCalledWith(expected)
82
+ })
83
+ })
84
+ })