@nuskin/foundation-ui-components 1.3.0 → 1.4.0

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 (37) hide show
  1. package/package.json +4 -4
  2. package/src/common/auth-dropdown/NsAuthDropdown.tsx +6 -6
  3. package/src/common/button/NsButton.stories.tsx +3 -2
  4. package/src/common/button/NsButton.tsx +27 -2
  5. package/src/common/button/types.d.ts +21 -17
  6. package/src/common/cart-icon/NsCartIcon.styled.tsx +14 -0
  7. package/src/common/dropdown/NsDropdown.tsx +6 -6
  8. package/src/common/email-simple/NsEmailSimple.tsx +1 -1
  9. package/src/common/flag/NsFlag.spec.tsx +14 -2
  10. package/src/common/flag/NsFlag.styled.tsx +2 -5
  11. package/src/common/hooks/useOnClickOutside.tsx +11 -14
  12. package/src/common/icon/NsIcon.spec.tsx +11 -2
  13. package/src/common/icon/NsIcon.stories.tsx +2 -1
  14. package/src/common/icon/NsIcon.styled.tsx +13 -10
  15. package/src/common/icon/NsIcon.tsx +9 -6
  16. package/src/common/icon/icons/UserIcon.tsx +8 -1
  17. package/src/common/icon/types.d.ts +3 -2
  18. package/src/common/progress/NsProgress.spec.tsx +2 -2
  19. package/src/common/progress/NsProgress.stories.tsx +10 -5
  20. package/src/common/progress/NsProgress.styled.tsx +30 -12
  21. package/src/common/progress/NsProgress.tsx +18 -16
  22. package/src/common/progress/types.d.ts +2 -2
  23. package/src/common/radiogroup/NsRadioGroup.spec.tsx +2 -2
  24. package/src/common/radiogroup/NsRadioGroup.stories.tsx +2 -2
  25. package/src/common/radiogroup/NsRadioGroup.tsx +5 -3
  26. package/src/common/radiogroup/types.d.ts +11 -12
  27. package/src/common/select/NsSelect.styled.tsx +37 -69
  28. package/src/common/select/NsSelect.tsx +86 -91
  29. package/src/common/select/NsSelectOption.tsx +10 -14
  30. package/src/common/select/specs/NsSelect.spec.tsx +183 -0
  31. package/src/common/select/specs/NsSelectOption.spec.tsx +32 -0
  32. package/src/common/select/stories/NsSelect.stories.tsx +25 -80
  33. package/src/common/select/stories/countries.ts +53 -0
  34. package/src/common/select/types.d.ts +21 -8
  35. package/src/common/table/NsTable.stories.tsx +1 -0
  36. package/src/common/cart-icon/NsCartIcon.styled.ts +0 -14
  37. package/src/common/select/NsSelect.spec.tsx +0 -274
@@ -1,274 +0,0 @@
1
- import React from 'react';
2
- import { render, fireEvent, screen, waitFor } from '@/test-utils/index';
3
- import NsSelect from './NsSelect';
4
- import NsSelectOption from './NsSelectOption';
5
-
6
- function createSelectOptions(options: { value: string; label: string }[]) {
7
- return options.map((option) => <NsSelectOption key={option.value} value={option.value} label={option.label} />);
8
- }
9
-
10
- describe('NsSelectOption', () => {
11
- it('1. renders NsSelectOption component without errors', () => {
12
- const value = 'option1';
13
- const label = 'Option 1';
14
- const { getByTestId, getByText } = render(<NsSelectOption value={value} label={label} />);
15
-
16
- // Check if the option container element exists
17
- const optionContainer = getByTestId(`option-${value}`);
18
- expect(optionContainer).toBeInTheDocument();
19
-
20
- // Check if the option label is rendered correctly
21
- const optionLabel = getByText(label);
22
- expect(optionLabel).toBeInTheDocument();
23
- });
24
- it('2. clicking on NsSelectOption selects the option', () => {
25
- const value = 'option1';
26
- const label = 'Option 1';
27
- const onSelect = jest.fn();
28
- const { getByTestId } = render(<NsSelectOption value={value} label={label} onSelect={onSelect} />);
29
- const optionElement = getByTestId(`option-${value}`);
30
- fireEvent.click(optionElement);
31
- expect(onSelect).toHaveBeenCalledWith(value);
32
- });
33
- });
34
-
35
- describe('NsSelect', () => {
36
- const onSelect = jest.fn();
37
- const selectedValue = '';
38
- const selectedLabel = '';
39
-
40
- const options = [
41
- { value: 'Option1', label: 'Option 1' },
42
- { value: 'Option2', label: 'Option 2' },
43
- { value: 'Option3', label: 'Option 3' },
44
- ];
45
-
46
- it('1. renders the placeholder text when no value is selected', () => {
47
- render(
48
- <NsSelect
49
- placeholder="Select an option"
50
- selectedValue={selectedValue}
51
- selectedLabel={selectedLabel}
52
- onSelect={onSelect}
53
- >
54
- {createSelectOptions(options)}
55
- </NsSelect>
56
- );
57
-
58
- expect(screen.getByText('Select an option')).toBeInTheDocument();
59
- });
60
-
61
- it('2. clicking outside the select container closes the dropdown', () => {
62
- const onSelect = jest.fn();
63
- const selectedLabel = 'Option 1';
64
- const { getByTestId, queryByTestId } = render(
65
- <NsSelect selectedValue="Option1" selectedLabel={selectedLabel} onSelect={onSelect} />
66
- );
67
- const selectButton = getByTestId('select-head');
68
- fireEvent.mouseDown(selectButton);
69
- const outsideElement = document.createElement('div');
70
- fireEvent.mouseDown(outsideElement);
71
- // Add assertions to check if the dropdown is closed (isOpen state is false)
72
- expect(queryByTestId('option-Option1')).toBeNull();
73
- });
74
-
75
- it('3. renders the selected value label', () => {
76
- const selectedLabel = 'Option 1';
77
- render(
78
- <NsSelect selectedValue="Option1" selectedLabel={selectedLabel} onSelect={onSelect}>
79
- {createSelectOptions(options)}
80
- </NsSelect>
81
- );
82
-
83
- expect(screen.getByText(selectedLabel)).toBeInTheDocument();
84
- });
85
-
86
- it('4. clicking on select button opens the dropdown', () => {
87
- const { getByTestId, getByText } = render(
88
- <NsSelect selectedValue="Option1" selectedLabel={selectedLabel} onSelect={onSelect}>
89
- {createSelectOptions(options)}
90
- </NsSelect>
91
- );
92
- const selectButton = getByTestId('select-head');
93
- fireEvent.mouseDown(selectButton);
94
-
95
- // Check if the dropdown is opened (isOpen state is true)
96
- const dropdownList = getByTestId('select-list');
97
- expect(dropdownList).toBeInTheDocument();
98
-
99
- // Check if the options are rendered in the dropdown
100
- const option1 = getByText('Option 1');
101
- const option2 = getByText('Option 2');
102
- expect(option1).toBeInTheDocument();
103
- expect(option2).toBeInTheDocument();
104
- });
105
-
106
- it('5. disabling the select component prevents interaction', () => {
107
- const onSelect = jest.fn();
108
- const { getByTestId, queryByTestId } = render(
109
- <NsSelect selectedValue="Option1" selectedLabel={selectedLabel} onSelect={onSelect} disabled>
110
- <NsSelectOption value="option1" label="Option 1" onSelect={onSelect} />
111
- <NsSelectOption value="option2" label="Option 2" onSelect={onSelect} />
112
- </NsSelect>
113
- );
114
- const selectButton = getByTestId('select-head');
115
- fireEvent.mouseDown(selectButton);
116
-
117
- // Check if the dropdown does not open (select list is not present)
118
- const dropdownList = queryByTestId('select-list');
119
- expect(dropdownList).toBeNull();
120
-
121
- // Check if the onSelect callback is not called
122
- expect(onSelect).not.toHaveBeenCalled();
123
- });
124
-
125
- it('6. select closes on mouse leave when closeOnMouseLeave is set to true', async () => {
126
- render(
127
- <NsSelect closeOnMouseLeave selectedValue={selectedValue} selectedLabel={selectedLabel} onSelect={onSelect}>
128
- {createSelectOptions(options)}
129
- </NsSelect>
130
- );
131
-
132
- const selectHead = screen.getByTestId('select-head');
133
- fireEvent.click(selectHead);
134
-
135
- // const selectList = screen.queryByTestId ('select-list');
136
- const option1 = screen.queryByTestId('option-Option1');
137
- const option2 = screen.queryByTestId('option-Option2');
138
- const option3 = screen.queryByTestId('option-Option3');
139
- fireEvent.click(selectHead);
140
-
141
- await waitFor(() => {
142
- expect(option1).not.toBeInTheDocument();
143
- expect(option2).not.toBeInTheDocument();
144
- expect(option3).not.toBeInTheDocument();
145
- });
146
- });
147
- it('7. select does not open when disabled', () => {
148
- render(
149
- <NsSelect selectedValue={selectedValue} selectedLabel={selectedLabel} onSelect={onSelect} disabled>
150
- {createSelectOptions(options)}
151
- </NsSelect>
152
- );
153
-
154
- const selectHead = screen.getByTestId('select-head');
155
- fireEvent.click(selectHead);
156
-
157
- expect(screen.queryByTestId('select-list')).not.toBeInTheDocument();
158
- });
159
-
160
- it('8. opens the select on "ArrowDown" key press', () => {
161
- render(
162
- <NsSelect selectedValue={selectedValue} selectedLabel={selectedLabel} onSelect={onSelect}>
163
- {createSelectOptions(options)}
164
- </NsSelect>
165
- );
166
-
167
- const selectHead = screen.getByTestId('select-head');
168
- fireEvent.keyDown(selectHead, { key: 'arrowDown' });
169
-
170
- expect(selectHead).toBeInTheDocument();
171
- });
172
-
173
- it('9. closes the select when closeSelect is called', () => {
174
- const { getByTestId } = render(
175
- <NsSelect selectedValue={selectedValue} selectedLabel={selectedLabel} onSelect={onSelect} closeOnMouseLeave>
176
- {createSelectOptions(options)}
177
- </NsSelect>
178
- );
179
-
180
- const selectContainer = getByTestId('select-container');
181
- fireEvent.mouseLeave(selectContainer);
182
-
183
- expect(screen.queryByTestId('select-list')).not.toBeInTheDocument();
184
- });
185
-
186
- it('10. does not open the select when disabled', () => {
187
- render(
188
- <NsSelect selectedValue={selectedValue} selectedLabel={selectedLabel} onSelect={onSelect} disabled>
189
- {createSelectOptions(options)}
190
- </NsSelect>
191
- );
192
-
193
- const selectHead = screen.getByTestId('select-head');
194
- fireEvent.mouseDown(selectHead);
195
-
196
- expect(screen.queryByTestId('select-list')).not.toBeInTheDocument();
197
- });
198
-
199
- it('11. selects the next option on arrow right click', () => {
200
- // Render the NsSelect component with options and onSelect function
201
- const onSelect = jest.fn();
202
- const { getByTestId } = render(
203
- <NsSelect selectedValue={selectedValue} selectedLabel={selectedLabel} onSelect={onSelect} hasArrows>
204
- {createSelectOptions(options)}
205
- </NsSelect>
206
- );
207
-
208
- // Simulate the arrow right click on the NsSelect component
209
- const selectHead = getByTestId('select-head');
210
- fireEvent.mouseDown(selectHead);
211
-
212
- const rightArrow = document.getElementsByClassName('RightArrow')[0];
213
- fireEvent.click(rightArrow);
214
-
215
- // Verify that the next option is selected and the onSelect function is called
216
- expect(onSelect).toHaveBeenCalledWith('Option1');
217
- });
218
-
219
- it('12. selects the previous option on arrow left click', () => {
220
- const { getByTestId } = render(
221
- <NsSelect
222
- selectedValue={selectedValue}
223
- selectedLabel={selectedLabel}
224
- onSelect={onSelect}
225
- hasArrows
226
- reverseArrowControls
227
- >
228
- {createSelectOptions(options)}
229
- </NsSelect>
230
- );
231
-
232
- const selectHead = getByTestId('select-head');
233
- fireEvent.mouseDown(selectHead);
234
-
235
- const leftArrow = document.getElementsByClassName('LeftArrow')[0];
236
- fireEvent.click(leftArrow);
237
-
238
- expect(onSelect).toHaveBeenCalledWith('Option1');
239
- });
240
-
241
- it('13. renders an error message with red styles', () => {
242
- const errorMessage = 'Invalid value';
243
- const { getByText } = render(
244
- <NsSelect
245
- selectedValue={selectedValue}
246
- selectedLabel={selectedLabel}
247
- onSelect={onSelect}
248
- errorMessage={errorMessage}
249
- >
250
- {createSelectOptions(options)}
251
- </NsSelect>
252
- );
253
-
254
- // Check if the error message is rendered correctly with red styles
255
- const errorText = getByText(errorMessage);
256
- expect(errorText).toBeInTheDocument();
257
- });
258
- it('14. selecting an option calls the onSelect callback', () => {
259
- const onSelect = jest.fn();
260
- const { getByTestId } = render(
261
- <NsSelect selectedValue={selectedValue} selectedLabel={selectedLabel} onSelect={onSelect}>
262
- {createSelectOptions(options)}
263
- </NsSelect>
264
- );
265
-
266
- const selectHead = getByTestId('select-head');
267
- fireEvent.mouseDown(selectHead);
268
-
269
- const option1 = getByTestId('option-Option1');
270
- fireEvent.click(option1);
271
-
272
- expect(onSelect).toHaveBeenCalledWith('Option1');
273
- });
274
- });