@getmicdrop/svelte-components 5.8.1 → 5.8.2
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.
- package/dist/calendar/AboutShow/AboutShow.svelte +172 -172
- package/dist/calendar/Calendar/MiniMonthCalendar.svelte +782 -782
- package/dist/calendar/FAQs/FAQs.svelte +75 -75
- package/dist/calendar/MonthSwitcher/MonthSwitcher.svelte +126 -126
- package/dist/calendar/OrderSummary/OrderSummary.svelte +367 -367
- package/dist/calendar/PublicCard/PublicCard.svelte +134 -134
- package/dist/calendar/ShowCard/ShowCard.svelte +157 -157
- package/dist/calendar/ShowTimeCard/ShowTimeCard.svelte +61 -61
- package/dist/components/Heading.svelte +60 -58
- package/dist/components/Heading.svelte.d.ts +1 -0
- package/dist/components/Heading.svelte.d.ts.map +1 -1
- package/dist/components/Layout/AppShell.svelte +104 -104
- package/dist/components/Layout/ContentSection.svelte +80 -80
- package/dist/components/Layout/Grid.svelte +4 -4
- package/dist/components/Layout/Heading.svelte +81 -81
- package/dist/components/Layout/PageContainer.svelte +69 -69
- package/dist/components/Layout/Responsive.svelte +75 -75
- package/dist/components/Layout/Section.svelte +80 -80
- package/dist/components/Layout/ShowOnDesktop.svelte +37 -37
- package/dist/components/Layout/ShowOnMobile.svelte +37 -37
- package/dist/components/Layout/Sidebar.svelte +108 -108
- package/dist/components/Layout/Stack.spec.js +1 -1
- package/dist/components/Layout/Stack.svelte +6 -6
- package/dist/components/Layout/Text.svelte +87 -87
- package/dist/components/Layout/TwoColumn.svelte +108 -108
- package/dist/components/Text.svelte +53 -53
- package/dist/constants/validation.js +91 -91
- package/dist/constants/validation.spec.js +64 -64
- package/dist/datetime/__tests__/format.test.js +1 -1
- package/dist/datetime/__tests__/parse.test.js +1 -1
- package/dist/datetime/__tests__/timezone.test.js +1 -1
- package/dist/datetime/parse.js +1 -1
- package/dist/forms/createFormStore.svelte.js +1 -0
- package/dist/index.js +50 -50
- package/dist/patterns/data/DataGrid.svelte +45 -45
- package/dist/patterns/data/DataList.svelte +24 -24
- package/dist/patterns/data/DataTable.svelte +36 -36
- package/dist/patterns/forms/FormActions.spec.js +95 -95
- package/dist/patterns/forms/FormActions.stories.svelte +97 -97
- package/dist/patterns/forms/FormActions.svelte +46 -46
- package/dist/patterns/forms/FormGrid.svelte +33 -33
- package/dist/patterns/forms/FormSection.svelte +32 -32
- package/dist/patterns/forms/FormValidationSummary.stories.svelte +83 -83
- package/dist/patterns/forms/FormValidationSummary.svelte +74 -74
- package/dist/patterns/layout/Sidebar.svelte +39 -39
- package/dist/patterns/layout/index.js +29 -29
- package/dist/patterns/navigation/BottomNav.stories.svelte +117 -117
- package/dist/patterns/navigation/BottomNav.svelte +74 -74
- package/dist/patterns/navigation/Header.stories.svelte +77 -77
- package/dist/patterns/navigation/Header.svelte +193 -193
- package/dist/patterns/page/PageHeader.svelte +18 -18
- package/dist/patterns/page/PageLayout.svelte +40 -40
- package/dist/patterns/page/PageLoader.spec.js +57 -57
- package/dist/patterns/page/PageLoader.stories.svelte +137 -137
- package/dist/patterns/page/PageLoader.svelte +24 -24
- package/dist/patterns/page/SectionHeader.svelte +29 -29
- package/dist/presets/badges.js +112 -112
- package/dist/presets/buttons.js +76 -76
- package/dist/presets/index.js +9 -9
- package/dist/primitives/Accordion/Accordion.stories.svelte +75 -75
- package/dist/primitives/Accordion/Accordion.svelte +42 -42
- package/dist/primitives/Accordion/AccordionItem.svelte +95 -95
- package/dist/primitives/Alert/Alert.spec.js +173 -173
- package/dist/primitives/Alert/Alert.stories.svelte +88 -88
- package/dist/primitives/Alert/Alert.svelte +27 -27
- package/dist/primitives/Avatar/Avatar.stories.svelte +94 -94
- package/dist/primitives/Avatar/Avatar.svelte +66 -66
- package/dist/primitives/Badges/Badge.spec.js +144 -144
- package/dist/primitives/Badges/Badge.stories.svelte +86 -86
- package/dist/primitives/Badges/Badge.svelte +79 -79
- package/dist/primitives/BottomSheet/BottomSheet.spec.js +136 -136
- package/dist/primitives/BottomSheet/BottomSheet.stories.svelte +83 -83
- package/dist/primitives/BottomSheet/BottomSheet.svelte +100 -100
- package/dist/primitives/Breadcrumb/Breadcrumb.spec.js +122 -122
- package/dist/primitives/Breadcrumb/Breadcrumb.stories.svelte +23 -23
- package/dist/primitives/Breadcrumb/Breadcrumb.svelte +89 -89
- package/dist/primitives/Button/Button.spec.js +223 -223
- package/dist/primitives/Button/Button.stories.svelte +76 -76
- package/dist/primitives/Button/Button.svelte +270 -270
- package/dist/primitives/Button/ButtonSaveDemo.spec.js +146 -146
- package/dist/primitives/Button/ButtonSaveDemo.svelte +25 -25
- package/dist/primitives/Button/ButtonVariantShowcase.svelte +129 -129
- package/dist/primitives/Card.spec.js +49 -49
- package/dist/primitives/Card.stories.svelte +22 -22
- package/dist/primitives/Card.svelte +28 -28
- package/dist/primitives/Checkbox/Checkbox.stories.svelte +84 -84
- package/dist/primitives/Checkbox/Checkbox.svelte +88 -88
- package/dist/primitives/DarkModeToggle.spec.js +390 -390
- package/dist/primitives/DarkModeToggle.stories.svelte +57 -57
- package/dist/primitives/DarkModeToggle.svelte +136 -136
- package/dist/primitives/Drawer/Drawer.stories.svelte +80 -80
- package/dist/primitives/Drawer/Drawer.svelte +120 -120
- package/dist/primitives/Dropdown/Dropdown.stories.svelte +137 -137
- package/dist/primitives/Dropdown/Dropdown.svelte +14 -14
- package/dist/primitives/Dropdown/DropdownDivider.svelte +9 -9
- package/dist/primitives/Dropdown/DropdownItem.svelte +80 -80
- package/dist/primitives/Helper/Helper.svelte +33 -33
- package/dist/primitives/Icons/ArrowLeft.svelte +8 -8
- package/dist/primitives/Icons/ArrowRight.svelte +8 -8
- package/dist/primitives/Icons/Availability.svelte +14 -14
- package/dist/primitives/Icons/Back.svelte +14 -14
- package/dist/primitives/Icons/CheckCircle.svelte +6 -6
- package/dist/primitives/Icons/CheckCircleOutline.svelte +15 -15
- package/dist/primitives/Icons/ChevronLeft.svelte +4 -4
- package/dist/primitives/Icons/ChevronRight.svelte +4 -4
- package/dist/primitives/Icons/Copy.svelte +15 -15
- package/dist/primitives/Icons/Cross.svelte +5 -5
- package/dist/primitives/Icons/DownArrow.svelte +8 -8
- package/dist/primitives/Icons/ErrorCircle.svelte +6 -6
- package/dist/primitives/Icons/FacebookIcon.svelte +2 -2
- package/dist/primitives/Icons/Home.svelte +15 -15
- package/dist/primitives/Icons/Icon.spec.js +169 -169
- package/dist/primitives/Icons/Icon.stories.svelte +100 -100
- package/dist/primitives/Icons/Icon.svelte +52 -52
- package/dist/primitives/Icons/IconGallery.stories.svelte +235 -235
- package/dist/primitives/Icons/Info.svelte +7 -7
- package/dist/primitives/Icons/InstagramIcon.svelte +4 -4
- package/dist/primitives/Icons/LogoInstagram.svelte +2 -2
- package/dist/primitives/Icons/Message.svelte +15 -15
- package/dist/primitives/Icons/MoonIcon.svelte +5 -5
- package/dist/primitives/Icons/More.svelte +21 -21
- package/dist/primitives/Icons/MoreHori.spec.js +61 -61
- package/dist/primitives/Icons/MoreHori.svelte +22 -22
- package/dist/primitives/Icons/Notification.svelte +14 -14
- package/dist/primitives/Icons/Payment.svelte +14 -14
- package/dist/primitives/Icons/Profile.svelte +21 -21
- package/dist/primitives/Icons/Reload.svelte +29 -29
- package/dist/primitives/Icons/Shows.svelte +21 -21
- package/dist/primitives/Icons/Signout.svelte +21 -21
- package/dist/primitives/Icons/SunIcon.svelte +8 -8
- package/dist/primitives/Icons/TiktokIcon.svelte +2 -2
- package/dist/primitives/Icons/TwitterIcon.svelte +2 -2
- package/dist/primitives/Icons/WarningIcon.spec.js +18 -18
- package/dist/primitives/Icons/WarningIcon.svelte +5 -5
- package/dist/primitives/Input/Input.spec.js +573 -573
- package/dist/primitives/Input/Input.stories.svelte +139 -139
- package/dist/primitives/Input/Select.spec.js +212 -212
- package/dist/primitives/Input/Select.stories.svelte +112 -112
- package/dist/primitives/Input/Select.svelte +128 -128
- package/dist/primitives/Input/Textarea.stories.svelte +137 -137
- package/dist/primitives/Input/Textarea.svelte +35 -35
- package/dist/primitives/Label/Label.svelte +37 -37
- package/dist/primitives/Modal/Modal.spec.js +99 -99
- package/dist/primitives/Modal/Modal.stories.svelte +86 -86
- package/dist/primitives/Pagination/Pagination.stories.svelte +76 -76
- package/dist/primitives/Pagination/Pagination.svelte +261 -261
- package/dist/primitives/Radio/Radio.stories.svelte +80 -80
- package/dist/primitives/Radio/Radio.svelte +67 -67
- package/dist/primitives/Skeleton/CardPlaceholder.svelte +87 -87
- package/dist/primitives/Skeleton/ImagePlaceholder.svelte +59 -59
- package/dist/primitives/Skeleton/ListPlaceholder.svelte +76 -76
- package/dist/primitives/Skeleton/Skeleton.stories.svelte +151 -151
- package/dist/primitives/Skeleton/Skeleton.svelte +26 -26
- package/dist/primitives/Spinner/Spinner.spec.js +71 -71
- package/dist/primitives/Spinner/Spinner.stories.svelte +29 -29
- package/dist/primitives/Spinner/Spinner.svelte +20 -20
- package/dist/primitives/Tabs/TabItem.svelte +49 -49
- package/dist/primitives/Tabs/Tabs.stories.svelte +112 -112
- package/dist/primitives/Tabs/Tabs.svelte +123 -123
- package/dist/primitives/Toggle.spec.js +143 -143
- package/dist/primitives/Toggle.stories.svelte +92 -92
- package/dist/primitives/Tooltip/Tooltip.svelte +83 -83
- package/dist/primitives/Typography/Typography.svelte +53 -53
- package/dist/primitives/ValidationError.spec.js +103 -103
- package/dist/primitives/ValidationError.stories.svelte +69 -69
- package/dist/primitives/ValidationError.svelte +29 -29
- package/dist/primitives/index.js +91 -91
- package/dist/recipes/CropImage/CropImage.spec.js +208 -208
- package/dist/recipes/CropImage/CropImage.stories.svelte +104 -104
- package/dist/recipes/CropImage/CropImage.svelte +238 -238
- package/dist/recipes/ImageUploader/ImageUploader.stories.svelte +125 -125
- package/dist/recipes/ImageUploader/ImageUploader.svelte +804 -804
- package/dist/recipes/Toaster/Toaster.stories.svelte +62 -62
- package/dist/recipes/feedback/EmptyState/EmptyState.svelte +1 -1
- package/dist/recipes/feedback/ErrorDisplay.spec.js +69 -69
- package/dist/recipes/feedback/ErrorDisplay.stories.svelte +101 -101
- package/dist/recipes/feedback/ErrorDisplay.svelte +1 -1
- package/dist/recipes/feedback/StatusIndicator/StatusIndicator.spec.js +133 -133
- package/dist/recipes/feedback/StatusIndicator/StatusIndicator.svelte +157 -157
- package/dist/recipes/fields/CheckboxField.svelte +85 -85
- package/dist/recipes/fields/FormField.svelte +58 -58
- package/dist/recipes/fields/RadioGroup.svelte +95 -95
- package/dist/recipes/fields/SelectField.svelte +80 -80
- package/dist/recipes/fields/TextareaField.svelte +97 -97
- package/dist/recipes/fields/ToggleField.svelte +60 -60
- package/dist/recipes/fields/index.js +7 -7
- package/dist/recipes/inputs/MultiSelect.spec.js +258 -258
- package/dist/recipes/inputs/MultiSelect.stories.svelte +133 -133
- package/dist/recipes/inputs/OTPInput.spec.js +251 -251
- package/dist/recipes/inputs/OTPInput.stories.svelte +162 -162
- package/dist/recipes/inputs/OTPInput.svelte +29 -29
- package/dist/recipes/inputs/PasswordInput.svelte +22 -22
- package/dist/recipes/inputs/PasswordStrengthIndicator/PasswordStrengthIndicator.svelte +117 -117
- package/dist/recipes/inputs/PlaceAutocomplete/PlaceAutocomplete.stories.svelte +123 -123
- package/dist/recipes/inputs/Search.svelte +37 -37
- package/dist/recipes/inputs/SelectDropdown.svelte +57 -57
- package/dist/recipes/modals/AlertModal.svelte +130 -130
- package/dist/recipes/modals/ConfirmationModal.spec.js +206 -206
- package/dist/recipes/modals/ConfirmationModal.stories.svelte +119 -119
- package/dist/recipes/modals/ConfirmationModal.svelte +152 -152
- package/dist/recipes/modals/InputModal.svelte +182 -182
- package/dist/recipes/modals/ModalStateManager.spec.js +100 -100
- package/dist/recipes/modals/ModalStateManager.svelte +77 -77
- package/dist/recipes/modals/ModalTestWrapper.svelte +65 -65
- package/dist/recipes/modals/StatusModal.svelte +206 -206
- package/dist/services/EventService.js +75 -75
- package/dist/services/EventService.spec.js +217 -217
- package/dist/services/ShowService.spec.js +345 -345
- package/dist/stores/toaster.js +13 -13
- package/dist/stories/ButtonAuditReview.stories.svelte +14 -14
- package/dist/stories/ButtonAuditReview.svelte +427 -427
- package/dist/stories/PatternsGallery.stories.svelte +19 -19
- package/dist/stories/PatternsGallery.svelte +206 -206
- package/dist/stories/PrimitivesGallery.stories.svelte +19 -19
- package/dist/stories/PrimitivesGallery.svelte +725 -725
- package/dist/stories/RecipesGallery.stories.svelte +19 -19
- package/dist/stories/RecipesGallery.svelte +271 -271
- package/dist/stories/button-audit-manifest.json +11186 -11186
- package/dist/tailwind/preset.cjs +82 -82
- package/dist/telemetry.js +405 -405
- package/dist/telemetry.spec.js +1169 -1169
- package/dist/tokens/tokens.css +87 -87
- package/dist/tokens/typography-base.css +163 -163
- package/dist/tokens/utilities.css +353 -0
- package/dist/utils/apiConfig.spec.js +219 -219
- package/dist/utils/transitions.js +62 -62
- package/dist/utils/utils.js +354 -354
- package/package.json +296 -295
|
@@ -1,251 +1,251 @@
|
|
|
1
|
-
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
2
|
-
import { render, fireEvent } from '@testing-library/svelte';
|
|
3
|
-
import OTPInput from './OTPInput.svelte';
|
|
4
|
-
|
|
5
|
-
describe('OTPInput Component', () => {
|
|
6
|
-
it('renders 6 input fields by default', () => {
|
|
7
|
-
const { container } = render(OTPInput);
|
|
8
|
-
const inputs = container.querySelectorAll('input');
|
|
9
|
-
expect(inputs.length).toBe(6);
|
|
10
|
-
});
|
|
11
|
-
|
|
12
|
-
it('renders custom number of input fields', () => {
|
|
13
|
-
const { container } = render(OTPInput, { props: { length: 4 } });
|
|
14
|
-
const inputs = container.querySelectorAll('input');
|
|
15
|
-
expect(inputs.length).toBe(4);
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
it('has numeric inputmode', () => {
|
|
19
|
-
const { container } = render(OTPInput);
|
|
20
|
-
const input = container.querySelector('input');
|
|
21
|
-
expect(input.getAttribute('inputmode')).toBe('numeric');
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
it('has maxlength of 1', () => {
|
|
25
|
-
const { container } = render(OTPInput);
|
|
26
|
-
const input = container.querySelector('input');
|
|
27
|
-
expect(input.getAttribute('maxlength')).toBe('1');
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
it('has autocomplete one-time-code', () => {
|
|
31
|
-
const { container } = render(OTPInput);
|
|
32
|
-
const input = container.querySelector('input');
|
|
33
|
-
expect(input.getAttribute('autocomplete')).toBe('one-time-code');
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
it('disables inputs when disabled prop is true', () => {
|
|
37
|
-
const { container } = render(OTPInput, { props: { disabled: true } });
|
|
38
|
-
const inputs = container.querySelectorAll('input');
|
|
39
|
-
inputs.forEach((input) => {
|
|
40
|
-
expect(input.disabled).toBe(true);
|
|
41
|
-
});
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
it('enables inputs when disabled prop is false', () => {
|
|
45
|
-
const { container } = render(OTPInput, { props: { disabled: false } });
|
|
46
|
-
const inputs = container.querySelectorAll('input');
|
|
47
|
-
inputs.forEach((input) => {
|
|
48
|
-
expect(input.disabled).toBe(false);
|
|
49
|
-
});
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
it('accepts digit input', async () => {
|
|
53
|
-
let changeEvent = null;
|
|
54
|
-
const { container } = render(OTPInput, {
|
|
55
|
-
props: {
|
|
56
|
-
onchange: (e) => {
|
|
57
|
-
changeEvent = e;
|
|
58
|
-
},
|
|
59
|
-
},
|
|
60
|
-
});
|
|
61
|
-
const inputs = container.querySelectorAll('input');
|
|
62
|
-
|
|
63
|
-
await fireEvent.input(inputs[0], { target: { value: '5' } });
|
|
64
|
-
|
|
65
|
-
expect(changeEvent).toBeTruthy();
|
|
66
|
-
expect(changeEvent.value[0]).toBe('5');
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
it('rejects non-digit input', async () => {
|
|
70
|
-
let changeEvent = null;
|
|
71
|
-
const { container } = render(OTPInput, {
|
|
72
|
-
props: {
|
|
73
|
-
onchange: (e) => {
|
|
74
|
-
changeEvent = e;
|
|
75
|
-
},
|
|
76
|
-
},
|
|
77
|
-
});
|
|
78
|
-
const inputs = container.querySelectorAll('input');
|
|
79
|
-
|
|
80
|
-
await fireEvent.input(inputs[0], { target: { value: 'a' } });
|
|
81
|
-
|
|
82
|
-
// Change event should not include the non-digit
|
|
83
|
-
expect(changeEvent?.value[0] || '').toBe('');
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
it('auto-advances to next input on valid digit', async () => {
|
|
87
|
-
const { container } = render(OTPInput);
|
|
88
|
-
const inputs = container.querySelectorAll('input');
|
|
89
|
-
|
|
90
|
-
inputs[0].focus();
|
|
91
|
-
await fireEvent.input(inputs[0], { target: { value: '1' } });
|
|
92
|
-
|
|
93
|
-
// Next input should be focused (tested by being active element)
|
|
94
|
-
expect(document.activeElement).toBe(inputs[1]);
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
it('dispatches complete event when all digits entered', async () => {
|
|
98
|
-
let completeEvent = null;
|
|
99
|
-
const { container } = render(OTPInput, {
|
|
100
|
-
props: {
|
|
101
|
-
oncomplete: (e) => {
|
|
102
|
-
completeEvent = e;
|
|
103
|
-
},
|
|
104
|
-
},
|
|
105
|
-
});
|
|
106
|
-
const inputs = container.querySelectorAll('input');
|
|
107
|
-
|
|
108
|
-
// Fill all inputs
|
|
109
|
-
for (let i = 0; i < 6; i++) {
|
|
110
|
-
await fireEvent.input(inputs[i], { target: { value: String(i + 1) } });
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
expect(completeEvent).toBeTruthy();
|
|
114
|
-
expect(completeEvent.code).toBe('123456');
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
it('moves focus back on backspace when current input is empty', async () => {
|
|
118
|
-
const { container } = render(OTPInput);
|
|
119
|
-
const inputs = container.querySelectorAll('input');
|
|
120
|
-
|
|
121
|
-
// Set first input value
|
|
122
|
-
await fireEvent.input(inputs[0], { target: { value: '1' } });
|
|
123
|
-
|
|
124
|
-
// Focus second input (empty)
|
|
125
|
-
inputs[1].focus();
|
|
126
|
-
|
|
127
|
-
// Press backspace
|
|
128
|
-
await fireEvent.keyDown(inputs[1], { key: 'Backspace' });
|
|
129
|
-
|
|
130
|
-
expect(document.activeElement).toBe(inputs[0]);
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
it('does not move focus back on backspace when current input has value', async () => {
|
|
134
|
-
const { container, component } = render(OTPInput, {
|
|
135
|
-
props: { value: ['1', '2', '', '', '', ''] },
|
|
136
|
-
});
|
|
137
|
-
const inputs = container.querySelectorAll('input');
|
|
138
|
-
|
|
139
|
-
inputs[1].focus();
|
|
140
|
-
await fireEvent.keyDown(inputs[1], { key: 'Backspace' });
|
|
141
|
-
|
|
142
|
-
// Should stay on same input since it has a value
|
|
143
|
-
expect(document.activeElement).toBe(inputs[1]);
|
|
144
|
-
});
|
|
145
|
-
|
|
146
|
-
it('handles paste of complete code', async () => {
|
|
147
|
-
vi.useFakeTimers();
|
|
148
|
-
let completeEvent = null;
|
|
149
|
-
const { container } = render(OTPInput, {
|
|
150
|
-
props: {
|
|
151
|
-
oncomplete: (e) => {
|
|
152
|
-
completeEvent = e;
|
|
153
|
-
},
|
|
154
|
-
},
|
|
155
|
-
});
|
|
156
|
-
const inputs = container.querySelectorAll('input');
|
|
157
|
-
|
|
158
|
-
await fireEvent.paste(inputs[0], {
|
|
159
|
-
clipboardData: { getData: () => '123456' },
|
|
160
|
-
});
|
|
161
|
-
|
|
162
|
-
vi.advanceTimersByTime(150);
|
|
163
|
-
|
|
164
|
-
expect(completeEvent).toBeTruthy();
|
|
165
|
-
expect(completeEvent.code).toBe('123456');
|
|
166
|
-
vi.useRealTimers();
|
|
167
|
-
});
|
|
168
|
-
|
|
169
|
-
it('ignores paste if code length does not match', async () => {
|
|
170
|
-
let completeEvent = null;
|
|
171
|
-
const { container } = render(OTPInput, {
|
|
172
|
-
props: {
|
|
173
|
-
oncomplete: (e) => {
|
|
174
|
-
completeEvent = e;
|
|
175
|
-
},
|
|
176
|
-
},
|
|
177
|
-
});
|
|
178
|
-
const inputs = container.querySelectorAll('input');
|
|
179
|
-
|
|
180
|
-
await fireEvent.paste(inputs[0], {
|
|
181
|
-
clipboardData: { getData: () => '12345' }, // Only 5 digits
|
|
182
|
-
});
|
|
183
|
-
|
|
184
|
-
expect(completeEvent).toBeFalsy();
|
|
185
|
-
});
|
|
186
|
-
|
|
187
|
-
it('strips non-digits from pasted content', async () => {
|
|
188
|
-
vi.useFakeTimers();
|
|
189
|
-
let completeEvent = null;
|
|
190
|
-
const { container } = render(OTPInput, {
|
|
191
|
-
props: {
|
|
192
|
-
oncomplete: (e) => {
|
|
193
|
-
completeEvent = e;
|
|
194
|
-
},
|
|
195
|
-
},
|
|
196
|
-
});
|
|
197
|
-
const inputs = container.querySelectorAll('input');
|
|
198
|
-
|
|
199
|
-
await fireEvent.paste(inputs[0], {
|
|
200
|
-
clipboardData: { getData: () => '1-2-3-4-5-6' }, // Code with dashes
|
|
201
|
-
});
|
|
202
|
-
|
|
203
|
-
vi.advanceTimersByTime(150);
|
|
204
|
-
|
|
205
|
-
expect(completeEvent).toBeTruthy();
|
|
206
|
-
expect(completeEvent.code).toBe('123456');
|
|
207
|
-
vi.useRealTimers();
|
|
208
|
-
});
|
|
209
|
-
|
|
210
|
-
it('has aria-label for each input', () => {
|
|
211
|
-
const { container } = render(OTPInput);
|
|
212
|
-
const inputs = container.querySelectorAll('input');
|
|
213
|
-
|
|
214
|
-
inputs.forEach((input, index) => {
|
|
215
|
-
expect(input.getAttribute('aria-label')).toBe(`Digit ${index + 1} of 6`);
|
|
216
|
-
});
|
|
217
|
-
});
|
|
218
|
-
|
|
219
|
-
it('has role="group" on container', () => {
|
|
220
|
-
const { container } = render(OTPInput);
|
|
221
|
-
const group = container.querySelector('[role="group"]');
|
|
222
|
-
expect(group).toBeTruthy();
|
|
223
|
-
});
|
|
224
|
-
|
|
225
|
-
it('has screen reader instructions', () => {
|
|
226
|
-
const { container } = render(OTPInput);
|
|
227
|
-
const instructions = container.querySelector('#otp-instructions');
|
|
228
|
-
expect(instructions).toBeTruthy();
|
|
229
|
-
expect(instructions.classList.contains('sr-only')).toBe(true);
|
|
230
|
-
});
|
|
231
|
-
|
|
232
|
-
it('initializes value array when length changes', async () => {
|
|
233
|
-
const { container, rerender } = render(OTPInput, { props: { length: 4 } });
|
|
234
|
-
let inputs = container.querySelectorAll('input');
|
|
235
|
-
expect(inputs.length).toBe(4);
|
|
236
|
-
|
|
237
|
-
await rerender({ length: 8 });
|
|
238
|
-
inputs = container.querySelectorAll('input');
|
|
239
|
-
expect(inputs.length).toBe(8);
|
|
240
|
-
});
|
|
241
|
-
|
|
242
|
-
it('exposes focus method', () => {
|
|
243
|
-
const { component } = render(OTPInput);
|
|
244
|
-
expect(typeof component.focus).toBe('function');
|
|
245
|
-
});
|
|
246
|
-
|
|
247
|
-
it('exposes clear method', () => {
|
|
248
|
-
const { component } = render(OTPInput);
|
|
249
|
-
expect(typeof component.clear).toBe('function');
|
|
250
|
-
});
|
|
251
|
-
});
|
|
1
|
+
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
2
|
+
import { render, fireEvent } from '@testing-library/svelte';
|
|
3
|
+
import OTPInput from './OTPInput.svelte';
|
|
4
|
+
|
|
5
|
+
describe('OTPInput Component', () => {
|
|
6
|
+
it('renders 6 input fields by default', () => {
|
|
7
|
+
const { container } = render(OTPInput);
|
|
8
|
+
const inputs = container.querySelectorAll('input');
|
|
9
|
+
expect(inputs.length).toBe(6);
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
it('renders custom number of input fields', () => {
|
|
13
|
+
const { container } = render(OTPInput, { props: { length: 4 } });
|
|
14
|
+
const inputs = container.querySelectorAll('input');
|
|
15
|
+
expect(inputs.length).toBe(4);
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it('has numeric inputmode', () => {
|
|
19
|
+
const { container } = render(OTPInput);
|
|
20
|
+
const input = container.querySelector('input');
|
|
21
|
+
expect(input.getAttribute('inputmode')).toBe('numeric');
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it('has maxlength of 1', () => {
|
|
25
|
+
const { container } = render(OTPInput);
|
|
26
|
+
const input = container.querySelector('input');
|
|
27
|
+
expect(input.getAttribute('maxlength')).toBe('1');
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('has autocomplete one-time-code', () => {
|
|
31
|
+
const { container } = render(OTPInput);
|
|
32
|
+
const input = container.querySelector('input');
|
|
33
|
+
expect(input.getAttribute('autocomplete')).toBe('one-time-code');
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('disables inputs when disabled prop is true', () => {
|
|
37
|
+
const { container } = render(OTPInput, { props: { disabled: true } });
|
|
38
|
+
const inputs = container.querySelectorAll('input');
|
|
39
|
+
inputs.forEach((input) => {
|
|
40
|
+
expect(input.disabled).toBe(true);
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('enables inputs when disabled prop is false', () => {
|
|
45
|
+
const { container } = render(OTPInput, { props: { disabled: false } });
|
|
46
|
+
const inputs = container.querySelectorAll('input');
|
|
47
|
+
inputs.forEach((input) => {
|
|
48
|
+
expect(input.disabled).toBe(false);
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('accepts digit input', async () => {
|
|
53
|
+
let changeEvent = null;
|
|
54
|
+
const { container } = render(OTPInput, {
|
|
55
|
+
props: {
|
|
56
|
+
onchange: (e) => {
|
|
57
|
+
changeEvent = e;
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
});
|
|
61
|
+
const inputs = container.querySelectorAll('input');
|
|
62
|
+
|
|
63
|
+
await fireEvent.input(inputs[0], { target: { value: '5' } });
|
|
64
|
+
|
|
65
|
+
expect(changeEvent).toBeTruthy();
|
|
66
|
+
expect(changeEvent.value[0]).toBe('5');
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it('rejects non-digit input', async () => {
|
|
70
|
+
let changeEvent = null;
|
|
71
|
+
const { container } = render(OTPInput, {
|
|
72
|
+
props: {
|
|
73
|
+
onchange: (e) => {
|
|
74
|
+
changeEvent = e;
|
|
75
|
+
},
|
|
76
|
+
},
|
|
77
|
+
});
|
|
78
|
+
const inputs = container.querySelectorAll('input');
|
|
79
|
+
|
|
80
|
+
await fireEvent.input(inputs[0], { target: { value: 'a' } });
|
|
81
|
+
|
|
82
|
+
// Change event should not include the non-digit
|
|
83
|
+
expect(changeEvent?.value[0] || '').toBe('');
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it('auto-advances to next input on valid digit', async () => {
|
|
87
|
+
const { container } = render(OTPInput);
|
|
88
|
+
const inputs = container.querySelectorAll('input');
|
|
89
|
+
|
|
90
|
+
inputs[0].focus();
|
|
91
|
+
await fireEvent.input(inputs[0], { target: { value: '1' } });
|
|
92
|
+
|
|
93
|
+
// Next input should be focused (tested by being active element)
|
|
94
|
+
expect(document.activeElement).toBe(inputs[1]);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it('dispatches complete event when all digits entered', async () => {
|
|
98
|
+
let completeEvent = null;
|
|
99
|
+
const { container } = render(OTPInput, {
|
|
100
|
+
props: {
|
|
101
|
+
oncomplete: (e) => {
|
|
102
|
+
completeEvent = e;
|
|
103
|
+
},
|
|
104
|
+
},
|
|
105
|
+
});
|
|
106
|
+
const inputs = container.querySelectorAll('input');
|
|
107
|
+
|
|
108
|
+
// Fill all inputs
|
|
109
|
+
for (let i = 0; i < 6; i++) {
|
|
110
|
+
await fireEvent.input(inputs[i], { target: { value: String(i + 1) } });
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
expect(completeEvent).toBeTruthy();
|
|
114
|
+
expect(completeEvent.code).toBe('123456');
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it('moves focus back on backspace when current input is empty', async () => {
|
|
118
|
+
const { container } = render(OTPInput);
|
|
119
|
+
const inputs = container.querySelectorAll('input');
|
|
120
|
+
|
|
121
|
+
// Set first input value
|
|
122
|
+
await fireEvent.input(inputs[0], { target: { value: '1' } });
|
|
123
|
+
|
|
124
|
+
// Focus second input (empty)
|
|
125
|
+
inputs[1].focus();
|
|
126
|
+
|
|
127
|
+
// Press backspace
|
|
128
|
+
await fireEvent.keyDown(inputs[1], { key: 'Backspace' });
|
|
129
|
+
|
|
130
|
+
expect(document.activeElement).toBe(inputs[0]);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it('does not move focus back on backspace when current input has value', async () => {
|
|
134
|
+
const { container, component } = render(OTPInput, {
|
|
135
|
+
props: { value: ['1', '2', '', '', '', ''] },
|
|
136
|
+
});
|
|
137
|
+
const inputs = container.querySelectorAll('input');
|
|
138
|
+
|
|
139
|
+
inputs[1].focus();
|
|
140
|
+
await fireEvent.keyDown(inputs[1], { key: 'Backspace' });
|
|
141
|
+
|
|
142
|
+
// Should stay on same input since it has a value
|
|
143
|
+
expect(document.activeElement).toBe(inputs[1]);
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it('handles paste of complete code', async () => {
|
|
147
|
+
vi.useFakeTimers();
|
|
148
|
+
let completeEvent = null;
|
|
149
|
+
const { container } = render(OTPInput, {
|
|
150
|
+
props: {
|
|
151
|
+
oncomplete: (e) => {
|
|
152
|
+
completeEvent = e;
|
|
153
|
+
},
|
|
154
|
+
},
|
|
155
|
+
});
|
|
156
|
+
const inputs = container.querySelectorAll('input');
|
|
157
|
+
|
|
158
|
+
await fireEvent.paste(inputs[0], {
|
|
159
|
+
clipboardData: { getData: () => '123456' },
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
vi.advanceTimersByTime(150);
|
|
163
|
+
|
|
164
|
+
expect(completeEvent).toBeTruthy();
|
|
165
|
+
expect(completeEvent.code).toBe('123456');
|
|
166
|
+
vi.useRealTimers();
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
it('ignores paste if code length does not match', async () => {
|
|
170
|
+
let completeEvent = null;
|
|
171
|
+
const { container } = render(OTPInput, {
|
|
172
|
+
props: {
|
|
173
|
+
oncomplete: (e) => {
|
|
174
|
+
completeEvent = e;
|
|
175
|
+
},
|
|
176
|
+
},
|
|
177
|
+
});
|
|
178
|
+
const inputs = container.querySelectorAll('input');
|
|
179
|
+
|
|
180
|
+
await fireEvent.paste(inputs[0], {
|
|
181
|
+
clipboardData: { getData: () => '12345' }, // Only 5 digits
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
expect(completeEvent).toBeFalsy();
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
it('strips non-digits from pasted content', async () => {
|
|
188
|
+
vi.useFakeTimers();
|
|
189
|
+
let completeEvent = null;
|
|
190
|
+
const { container } = render(OTPInput, {
|
|
191
|
+
props: {
|
|
192
|
+
oncomplete: (e) => {
|
|
193
|
+
completeEvent = e;
|
|
194
|
+
},
|
|
195
|
+
},
|
|
196
|
+
});
|
|
197
|
+
const inputs = container.querySelectorAll('input');
|
|
198
|
+
|
|
199
|
+
await fireEvent.paste(inputs[0], {
|
|
200
|
+
clipboardData: { getData: () => '1-2-3-4-5-6' }, // Code with dashes
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
vi.advanceTimersByTime(150);
|
|
204
|
+
|
|
205
|
+
expect(completeEvent).toBeTruthy();
|
|
206
|
+
expect(completeEvent.code).toBe('123456');
|
|
207
|
+
vi.useRealTimers();
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
it('has aria-label for each input', () => {
|
|
211
|
+
const { container } = render(OTPInput);
|
|
212
|
+
const inputs = container.querySelectorAll('input');
|
|
213
|
+
|
|
214
|
+
inputs.forEach((input, index) => {
|
|
215
|
+
expect(input.getAttribute('aria-label')).toBe(`Digit ${index + 1} of 6`);
|
|
216
|
+
});
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
it('has role="group" on container', () => {
|
|
220
|
+
const { container } = render(OTPInput);
|
|
221
|
+
const group = container.querySelector('[role="group"]');
|
|
222
|
+
expect(group).toBeTruthy();
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
it('has screen reader instructions', () => {
|
|
226
|
+
const { container } = render(OTPInput);
|
|
227
|
+
const instructions = container.querySelector('#otp-instructions');
|
|
228
|
+
expect(instructions).toBeTruthy();
|
|
229
|
+
expect(instructions.classList.contains('sr-only')).toBe(true);
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
it('initializes value array when length changes', async () => {
|
|
233
|
+
const { container, rerender } = render(OTPInput, { props: { length: 4 } });
|
|
234
|
+
let inputs = container.querySelectorAll('input');
|
|
235
|
+
expect(inputs.length).toBe(4);
|
|
236
|
+
|
|
237
|
+
await rerender({ length: 8 });
|
|
238
|
+
inputs = container.querySelectorAll('input');
|
|
239
|
+
expect(inputs.length).toBe(8);
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
it('exposes focus method', () => {
|
|
243
|
+
const { component } = render(OTPInput);
|
|
244
|
+
expect(typeof component.focus).toBe('function');
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
it('exposes clear method', () => {
|
|
248
|
+
const { component } = render(OTPInput);
|
|
249
|
+
expect(typeof component.clear).toBe('function');
|
|
250
|
+
});
|
|
251
|
+
});
|