@mgdis/core-ui-helpers 1.2.0 → 1.2.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/CHANGELOG.md +12 -0
- package/package.json +8 -8
- package/rollup.config.mjs +3 -3
- package/src/angular/mg-angular-logger/mg-angular-logger.spec.ts +1 -1
- package/src/angular/mg-angular-logger/mg-angular-logger.ts +2 -1
- package/src/locale/index.spec.ts +10 -1
- package/src/locale/locale.spec.ts +43 -18
- package/src/locale/locale.ts +13 -5
- package/src/playwright/utils/utils.spec.ts +16 -9
- package/src/playwright/utils/utils.ts +14 -6
- package/src/stencil/ide/cem.spec.ts +4 -4
- package/src/stencil/ide/cem.ts +9 -9
- package/src/stencil/ide/ide.ts +26 -26
- package/src/storybook/stencil/stencil.spec.ts +30 -6
- package/src/storybook/stencil/stencil.ts +24 -13
- package/src/tests/index.spec.ts +7 -1
- package/src/tests/unit/unit.conf.ts +9 -2
- package/src/tests/unit/unit.spec.ts +103 -61
- package/src/tests/unit/unit.ts +38 -11
- package/src/utils/attributes/id/id.spec.ts +9 -6
- package/src/utils/attributes/id/id.ts +3 -2
- package/src/utils/components/components.spec.ts +6 -6
- package/src/utils/components/components.ts +19 -5
- package/src/utils/elements/elements.ts +4 -2
- package/src/utils/number/number.spec.ts +2 -2
- package/src/utils/object/object.spec.ts +2 -2
- package/src/utils/object/object.ts +2 -1
- package/src/utils/string/string.spec.ts +12 -5
- package/src/utils/string/string.ts +2 -1
- package/src/utils/windows/windows.spec.ts +6 -2
- package/src/vue/mg-model/mg-model.ts +7 -2
- package/vitest.config.mts +1 -1
- package/.turbo/turbo-build.log +0 -52
- package/dist/angular/index.cjs +0 -27
- package/dist/angular/index.cjs.map +0 -1
- package/dist/angular/index.d.ts +0 -21
- package/dist/angular/index.js +0 -25
- package/dist/angular/index.js.map +0 -1
- package/dist/angular/index.umd.js +0 -33
- package/dist/angular/index.umd.js.map +0 -1
- package/dist/locale/index.cjs +0 -188
- package/dist/locale/index.cjs.map +0 -1
- package/dist/locale/index.d.ts +0 -110
- package/dist/locale/index.js +0 -179
- package/dist/locale/index.js.map +0 -1
- package/dist/locale/index.umd.js +0 -194
- package/dist/locale/index.umd.js.map +0 -1
- package/dist/playwright/index.cjs +0 -130
- package/dist/playwright/index.cjs.map +0 -1
- package/dist/playwright/index.d.ts +0 -39
- package/dist/playwright/index.js +0 -126
- package/dist/playwright/index.js.map +0 -1
- package/dist/playwright/index.umd.js +0 -136
- package/dist/playwright/index.umd.js.map +0 -1
- package/dist/stencil/index.cjs +0 -363
- package/dist/stencil/index.cjs.map +0 -1
- package/dist/stencil/index.d.ts +0 -222
- package/dist/stencil/index.js +0 -357
- package/dist/stencil/index.js.map +0 -1
- package/dist/stencil/index.umd.js +0 -369
- package/dist/stencil/index.umd.js.map +0 -1
- package/dist/storybook/index.cjs +0 -2333
- package/dist/storybook/index.cjs.map +0 -1
- package/dist/storybook/index.d.ts +0 -85
- package/dist/storybook/index.js +0 -2327
- package/dist/storybook/index.js.map +0 -1
- package/dist/storybook/index.umd.js +0 -2339
- package/dist/storybook/index.umd.js.map +0 -1
- package/dist/tests/index.cjs +0 -300
- package/dist/tests/index.cjs.map +0 -1
- package/dist/tests/index.d.ts +0 -100
- package/dist/tests/index.js +0 -294
- package/dist/tests/index.js.map +0 -1
- package/dist/tests/index.umd.js +0 -306
- package/dist/tests/index.umd.js.map +0 -1
- package/dist/utils/index.cjs +0 -433
- package/dist/utils/index.cjs.map +0 -1
- package/dist/utils/index.d.ts +0 -265
- package/dist/utils/index.js +0 -409
- package/dist/utils/index.js.map +0 -1
- package/dist/utils/index.umd.js +0 -439
- package/dist/utils/index.umd.js.map +0 -1
- package/dist/vue/index.cjs +0 -1934
- package/dist/vue/index.cjs.map +0 -1
- package/dist/vue/index.d.ts +0 -35
- package/dist/vue/index.js +0 -1932
- package/dist/vue/index.js.map +0 -1
- package/dist/vue/index.umd.js +0 -1940
- package/dist/vue/index.umd.js.map +0 -1
|
@@ -15,7 +15,12 @@ const renderAttribute = (element: HTMLElement, name: string, value: any): void =
|
|
|
15
15
|
if ([null, undefined, '', false].includes(value) || ['innerHTML', 'style', ''].includes(name)) {
|
|
16
16
|
return;
|
|
17
17
|
}
|
|
18
|
-
element.setAttribute(
|
|
18
|
+
element.setAttribute(
|
|
19
|
+
name,
|
|
20
|
+
!['object', 'function'].includes(typeof value)
|
|
21
|
+
? value
|
|
22
|
+
: `⚠️ Property must be set through a script or a framework-specific syntax.`
|
|
23
|
+
);
|
|
19
24
|
};
|
|
20
25
|
|
|
21
26
|
/**
|
|
@@ -26,15 +31,21 @@ const renderAttribute = (element: HTMLElement, name: string, value: any): void =
|
|
|
26
31
|
* @param children - of the new element
|
|
27
32
|
* @param text - of the new element
|
|
28
33
|
*/
|
|
29
|
-
const renderElement = (
|
|
34
|
+
const renderElement = (
|
|
35
|
+
parentNode: HTMLElement,
|
|
36
|
+
tagName: VNode['$tag$'],
|
|
37
|
+
attributes: VNode['$attrs$'],
|
|
38
|
+
children: VNode[],
|
|
39
|
+
text: VNode['$text$']
|
|
40
|
+
): void => {
|
|
30
41
|
// render HTML
|
|
31
42
|
if (tagName && typeof tagName === 'string') {
|
|
32
43
|
const element = document.createElement(tagName);
|
|
33
|
-
Object.keys(attributes || {}).forEach(attr => {
|
|
44
|
+
Object.keys(attributes || {}).forEach((attr) => {
|
|
34
45
|
renderAttribute(element, attr, attributes[attr]);
|
|
35
46
|
});
|
|
36
47
|
|
|
37
|
-
children?.forEach(child => {
|
|
48
|
+
children?.forEach((child) => {
|
|
38
49
|
renderElement(element, child.$tag$, child.$attrs$, child.$children$, child.$text$);
|
|
39
50
|
});
|
|
40
51
|
|
|
@@ -109,7 +120,7 @@ export const stencilWrapper = (storyFn: (ctx: any) => void, context: ArgsType):
|
|
|
109
120
|
},
|
|
110
121
|
$hostElement$: host,
|
|
111
122
|
},
|
|
112
|
-
storyFn(context)
|
|
123
|
+
storyFn(context)
|
|
113
124
|
);
|
|
114
125
|
return host.children[host.children.length - 1];
|
|
115
126
|
};
|
|
@@ -174,7 +185,7 @@ export class StorybookPreview {
|
|
|
174
185
|
* @returns component data
|
|
175
186
|
*/
|
|
176
187
|
#getComponentData = (tagName: string): JsonDocsComponent | undefined => {
|
|
177
|
-
return this.jsonDoc.components.find(component => component.tag === tagName);
|
|
188
|
+
return this.jsonDoc.components.find((component) => component.tag === tagName);
|
|
178
189
|
};
|
|
179
190
|
|
|
180
191
|
/**
|
|
@@ -188,9 +199,9 @@ export class StorybookPreview {
|
|
|
188
199
|
const types: (string | undefined)[] = prop.type
|
|
189
200
|
.replace(/"([^"]+)"/g, '$1') // Remove quotes
|
|
190
201
|
.replace(/\s/g, '') // Remove all whitespace for simplicity
|
|
191
|
-
.replace(/\(.*?\)/g, match => match.replace(/\|/g, ' OR ')) // Replace '|' inside parentheses
|
|
202
|
+
.replace(/\(.*?\)/g, (match) => match.replace(/\|/g, ' OR ')) // Replace '|' inside parentheses
|
|
192
203
|
.split('|')
|
|
193
|
-
.map(type => type.trim().replace(/ OR /g, '|')); // Revert ' OR ' back to '|'
|
|
204
|
+
.map((type) => type.trim().replace(/ OR /g, '|')); // Revert ' OR ' back to '|'
|
|
194
205
|
|
|
195
206
|
// Return control and options
|
|
196
207
|
if (prop.type === 'string') {
|
|
@@ -205,7 +216,7 @@ export class StorybookPreview {
|
|
|
205
216
|
// Manage case when multiple types are possible
|
|
206
217
|
if (types.includes('string')) {
|
|
207
218
|
return { control: { type: 'text' } };
|
|
208
|
-
} else if (types.every(type => type?.includes('[]'))) {
|
|
219
|
+
} else if (types.every((type) => type?.includes('[]'))) {
|
|
209
220
|
return { control: { type: 'object' } };
|
|
210
221
|
} else {
|
|
211
222
|
// Add the posibility to set undefined
|
|
@@ -258,7 +269,7 @@ export class StorybookPreview {
|
|
|
258
269
|
},
|
|
259
270
|
},
|
|
260
271
|
}),
|
|
261
|
-
{}
|
|
272
|
+
{}
|
|
262
273
|
);
|
|
263
274
|
|
|
264
275
|
// Extracts Methods arg types
|
|
@@ -274,7 +285,7 @@ export class StorybookPreview {
|
|
|
274
285
|
},
|
|
275
286
|
},
|
|
276
287
|
}),
|
|
277
|
-
{}
|
|
288
|
+
{}
|
|
278
289
|
);
|
|
279
290
|
|
|
280
291
|
// Extracts Slots arg types
|
|
@@ -290,7 +301,7 @@ export class StorybookPreview {
|
|
|
290
301
|
},
|
|
291
302
|
},
|
|
292
303
|
}),
|
|
293
|
-
{}
|
|
304
|
+
{}
|
|
294
305
|
);
|
|
295
306
|
|
|
296
307
|
// Extracts CSS Properties arg types
|
|
@@ -306,7 +317,7 @@ export class StorybookPreview {
|
|
|
306
317
|
},
|
|
307
318
|
},
|
|
308
319
|
}),
|
|
309
|
-
{}
|
|
320
|
+
{}
|
|
310
321
|
);
|
|
311
322
|
|
|
312
323
|
// Extract component dependencies
|
package/src/tests/index.spec.ts
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
import { describe, expect, test } from 'vitest';
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
setUpHTMLInputElementValidity,
|
|
4
|
+
setupMutationObserverMock,
|
|
5
|
+
setUpRequestAnimationFrameMock,
|
|
6
|
+
setupResizeObserverMock,
|
|
7
|
+
setupSubmitEventMock,
|
|
8
|
+
} from './';
|
|
3
9
|
|
|
4
10
|
describe('tests', () => {
|
|
5
11
|
describe('unit', () => {
|
|
@@ -1,9 +1,16 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Define MutationObserver setup function signature
|
|
3
3
|
*/
|
|
4
|
-
export type SetupMutationObserverMockParams = {
|
|
4
|
+
export type SetupMutationObserverMockParams = {
|
|
5
|
+
disconnect: MutationObserver['disconnect'];
|
|
6
|
+
observe: MutationObserver['observe'];
|
|
7
|
+
takeRecords: MutationObserver['takeRecords'];
|
|
8
|
+
};
|
|
5
9
|
|
|
6
10
|
/**
|
|
7
11
|
* Define ResizeObserver setup function signature
|
|
8
12
|
*/
|
|
9
|
-
export type setupResizeObserverMockParams = {
|
|
13
|
+
export type setupResizeObserverMockParams = {
|
|
14
|
+
disconnect: ResizeObserver['disconnect'];
|
|
15
|
+
observe: ResizeObserver['observe'];
|
|
16
|
+
};
|
|
@@ -1,16 +1,25 @@
|
|
|
1
1
|
import { beforeEach, describe, expect, test, vi } from 'vitest';
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
setUpHTMLInputElementValidity,
|
|
4
|
+
setupMutationObserverMock,
|
|
5
|
+
setUpRequestAnimationFrameMock,
|
|
6
|
+
setupResizeObserverMock,
|
|
7
|
+
setupSubmitEventMock,
|
|
8
|
+
} from './unit.ts';
|
|
3
9
|
import type { SetupMutationObserverMockParams, setupResizeObserverMockParams } from './unit.conf.ts';
|
|
4
10
|
|
|
5
11
|
describe('unit', () => {
|
|
6
12
|
describe('setupMutationObserverMock', () => {
|
|
7
|
-
test.each([{}, { disconnect: vi.fn(), observe: ({}) => {}, takeRecords: () => [] }])(
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
13
|
+
test.each([{}, { disconnect: vi.fn(), observe: ({}) => {}, takeRecords: () => [] }])(
|
|
14
|
+
'Should render html',
|
|
15
|
+
(args) => {
|
|
16
|
+
const Mock = setupMutationObserverMock(args as SetupMutationObserverMockParams);
|
|
17
|
+
expect(new Mock(args as MutationCallback)).toMatchSnapshot();
|
|
18
|
+
}
|
|
19
|
+
);
|
|
11
20
|
});
|
|
12
21
|
describe('setupResizeObserverMock', () => {
|
|
13
|
-
test.each([{}, { disconnect: vi.fn(), observe: ({}) => {}, unobserve: () => [] }])('Should render html', args => {
|
|
22
|
+
test.each([{}, { disconnect: vi.fn(), observe: ({}) => {}, unobserve: () => [] }])('Should render html', (args) => {
|
|
14
23
|
const Mock = setupResizeObserverMock(args as setupResizeObserverMockParams);
|
|
15
24
|
expect(new Mock(args as ResizeObserverCallback)).toMatchSnapshot();
|
|
16
25
|
});
|
|
@@ -26,7 +35,7 @@ describe('unit', () => {
|
|
|
26
35
|
vi.useFakeTimers();
|
|
27
36
|
});
|
|
28
37
|
|
|
29
|
-
test.each([window, global])('Should render html', element => {
|
|
38
|
+
test.each([window, global])('Should render html', (element) => {
|
|
30
39
|
const timer = vi.fn();
|
|
31
40
|
const callback = vi.fn();
|
|
32
41
|
|
|
@@ -54,11 +63,23 @@ describe('unit', () => {
|
|
|
54
63
|
{ attributes: { type: 'text', max: '10', value: '10' }, validity: { rangeOverflow: false, valid: true } },
|
|
55
64
|
{ attributes: { type: 'text', max: '10', value: '11' }, validity: { rangeOverflow: false, valid: true } },
|
|
56
65
|
// date rangeUnderflow
|
|
57
|
-
{
|
|
58
|
-
|
|
66
|
+
{
|
|
67
|
+
attributes: { type: 'date', min: '2025-06-20', value: '2025-06-19' },
|
|
68
|
+
validity: { rangeUnderflow: true, valid: false },
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
attributes: { type: 'date', min: '2025-06-19', value: '2025-06-19' },
|
|
72
|
+
validity: { rangeUnderflow: false, valid: true },
|
|
73
|
+
},
|
|
59
74
|
// number rangeOverflow
|
|
60
|
-
{
|
|
61
|
-
|
|
75
|
+
{
|
|
76
|
+
attributes: { type: 'date', max: '2025-06-18', value: '2025-06-19' },
|
|
77
|
+
validity: { rangeOverflow: true, valid: false },
|
|
78
|
+
},
|
|
79
|
+
{
|
|
80
|
+
attributes: { type: 'date', max: '2025-06-19', value: '2025-06-19' },
|
|
81
|
+
validity: { rangeOverflow: false, valid: true },
|
|
82
|
+
},
|
|
62
83
|
// number badInput
|
|
63
84
|
{ attributes: { type: 'number', value: '10' }, validity: { badInput: false, valid: true } },
|
|
64
85
|
// date badInput
|
|
@@ -71,17 +92,26 @@ describe('unit', () => {
|
|
|
71
92
|
// password valueMissing
|
|
72
93
|
{ attributes: { type: 'password', value: '', required: true }, validity: { valueMissing: true, valid: false } },
|
|
73
94
|
{ attributes: { type: 'password', required: true }, validity: { valueMissing: true, valid: false } },
|
|
74
|
-
{
|
|
95
|
+
{
|
|
96
|
+
attributes: { type: 'password', value: 'batman', required: true },
|
|
97
|
+
validity: { valueMissing: false, valid: true },
|
|
98
|
+
},
|
|
75
99
|
// textarea valueMissing
|
|
76
100
|
{ attributes: { type: 'textarea', required: true }, validity: { valueMissing: true, valid: false } },
|
|
77
101
|
{ attributes: { type: 'textarea', value: '', required: true }, validity: { valueMissing: true, valid: false } },
|
|
78
|
-
{
|
|
102
|
+
{
|
|
103
|
+
attributes: { type: 'textarea', value: 'batman', required: true },
|
|
104
|
+
validity: { valueMissing: false, valid: true },
|
|
105
|
+
},
|
|
79
106
|
// number valueMissing
|
|
80
107
|
{ attributes: { type: 'number', value: '', required: true }, validity: { valueMissing: true, valid: false } },
|
|
81
108
|
{ attributes: { type: 'number', value: '1', required: true }, validity: { valueMissing: false, valid: true } },
|
|
82
109
|
// date valueMissing
|
|
83
110
|
{ attributes: { type: 'date', value: '', required: true }, validity: { valueMissing: true, valid: false } },
|
|
84
|
-
{
|
|
111
|
+
{
|
|
112
|
+
attributes: { type: 'date', value: '2025-06-19', required: true },
|
|
113
|
+
validity: { valueMissing: false, valid: true },
|
|
114
|
+
},
|
|
85
115
|
// checkbox valueMissing
|
|
86
116
|
{ attributes: { type: 'checkbox', required: true }, validity: { valueMissing: true, valid: false } },
|
|
87
117
|
{ attributes: { type: 'checkbox', checked: '', required: true }, validity: { valueMissing: false, valid: true } },
|
|
@@ -89,11 +119,20 @@ describe('unit', () => {
|
|
|
89
119
|
{ attributes: { type: 'radio', required: true }, validity: { valueMissing: true, valid: false } },
|
|
90
120
|
{ attributes: { type: 'radio', checked: '', required: true }, validity: { valueMissing: false, valid: true } },
|
|
91
121
|
// select valueMissing
|
|
92
|
-
{
|
|
93
|
-
|
|
122
|
+
{
|
|
123
|
+
attributes: { type: 'select', options: ['batman'], required: true },
|
|
124
|
+
validity: { valueMissing: true, valid: false },
|
|
125
|
+
},
|
|
126
|
+
{
|
|
127
|
+
attributes: { type: 'select', options: ['batman'], value: 'batman', required: true },
|
|
128
|
+
validity: { valueMissing: false, valid: true },
|
|
129
|
+
},
|
|
94
130
|
// input patternMismatch
|
|
95
131
|
{ attributes: { type: 'text', value: '1', pattern: '[a-z]' }, validity: { patternMismatch: true, valid: false } },
|
|
96
|
-
{
|
|
132
|
+
{
|
|
133
|
+
attributes: { type: 'text', value: 'batman', pattern: '[a-z]' },
|
|
134
|
+
validity: { patternMismatch: false, valid: true },
|
|
135
|
+
},
|
|
97
136
|
// text stepMismatch
|
|
98
137
|
{ attributes: { type: 'text', value: '2', step: '10' }, validity: { stepMismatch: false, valid: true } },
|
|
99
138
|
// number stepMismatch
|
|
@@ -109,56 +148,59 @@ describe('unit', () => {
|
|
|
109
148
|
// url typeMismatch
|
|
110
149
|
{ attributes: { type: 'url', value: 'batman' }, validity: { typeMismatch: true, valid: false } },
|
|
111
150
|
{ attributes: { type: 'url', value: 'http://batman.com' }, validity: { typeMismatch: false, valid: true } },
|
|
112
|
-
])(
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
optionElement.setAttribute('
|
|
151
|
+
])(
|
|
152
|
+
'Should get input validity state (%s)',
|
|
153
|
+
({ attributes, validity }: { attributes: Record<string, unknown>; validity: Partial<ValidityState> }) => {
|
|
154
|
+
let input: HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement;
|
|
155
|
+
// HTMLSelectElement
|
|
156
|
+
if (attributes.type === 'select') {
|
|
157
|
+
input = document.createElement('select');
|
|
158
|
+
const defaultOptionElement = document.createElement('option');
|
|
159
|
+
defaultOptionElement.setAttribute('value', '');
|
|
160
|
+
input.appendChild(defaultOptionElement);
|
|
161
|
+
for (const option of attributes.options as string[]) {
|
|
162
|
+
const optionElement = document.createElement('option');
|
|
163
|
+
optionElement.setAttribute('value', option);
|
|
164
|
+
if (option === attributes.value) {
|
|
165
|
+
optionElement.setAttribute('selected', '');
|
|
166
|
+
}
|
|
167
|
+
optionElement.innerText = option;
|
|
168
|
+
input.appendChild(optionElement);
|
|
169
|
+
}
|
|
170
|
+
delete attributes.options;
|
|
171
|
+
delete attributes.valilue;
|
|
172
|
+
delete attributes.type;
|
|
173
|
+
} else if (attributes.type === 'textarea') {
|
|
174
|
+
input = document.createElement('textarea');
|
|
175
|
+
if (typeof attributes.value === 'string') {
|
|
176
|
+
input.value = attributes.value;
|
|
125
177
|
}
|
|
126
|
-
|
|
127
|
-
|
|
178
|
+
delete attributes.type;
|
|
179
|
+
delete attributes.value;
|
|
128
180
|
}
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
} else if (attributes.type === 'textarea') {
|
|
133
|
-
input = document.createElement('textarea');
|
|
134
|
-
if (typeof attributes.value === 'string') {
|
|
135
|
-
input.value = attributes.value;
|
|
181
|
+
// HTMLInputElemenr
|
|
182
|
+
else {
|
|
183
|
+
input = document.createElement('input');
|
|
136
184
|
}
|
|
137
|
-
delete attributes.type;
|
|
138
|
-
delete attributes.value;
|
|
139
|
-
}
|
|
140
|
-
// HTMLInputElemenr
|
|
141
|
-
else {
|
|
142
|
-
input = document.createElement('input');
|
|
143
|
-
}
|
|
144
185
|
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
186
|
+
setUpHTMLInputElementValidity(input as HTMLInputElement);
|
|
187
|
+
Object.keys(attributes).forEach((attr) => {
|
|
188
|
+
input.setAttribute(attr, attributes[attr] as string);
|
|
189
|
+
});
|
|
149
190
|
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
191
|
+
// set custom error
|
|
192
|
+
if (attributes.errorMessage) {
|
|
193
|
+
input.setCustomValidity(attributes.errorMessage as string);
|
|
194
|
+
delete attributes.errorMessage;
|
|
195
|
+
}
|
|
196
|
+
expect(input.validity).toEqual(expect.objectContaining(validity));
|
|
156
197
|
|
|
157
|
-
|
|
158
|
-
|
|
198
|
+
const spyInvalidEvent = vi.spyOn(input, 'dispatchEvent');
|
|
199
|
+
input.checkValidity();
|
|
159
200
|
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
201
|
+
const expectedEvent = new CustomEvent('invalid', { detail: validity.valid });
|
|
202
|
+
expect(spyInvalidEvent).toBeCalledWith(expect.objectContaining({ ...expectedEvent }));
|
|
203
|
+
}
|
|
204
|
+
);
|
|
163
205
|
});
|
|
164
206
|
});
|
package/src/tests/unit/unit.ts
CHANGED
|
@@ -20,7 +20,11 @@ import type { SetupMutationObserverMockParams, setupResizeObserverMockParams } f
|
|
|
20
20
|
* fireMo([{ type: 'childList', addedNodes: [AMockElemenet, AnotherMockElemenet], target: yourMockElemenet }]);;
|
|
21
21
|
* ```
|
|
22
22
|
*/
|
|
23
|
-
export const setupMutationObserverMock = ({
|
|
23
|
+
export const setupMutationObserverMock = ({
|
|
24
|
+
disconnect,
|
|
25
|
+
observe,
|
|
26
|
+
takeRecords,
|
|
27
|
+
}: SetupMutationObserverMockParams): typeof MutationObserver => {
|
|
24
28
|
class MockMutationObserver implements MutationObserver {
|
|
25
29
|
/**
|
|
26
30
|
*
|
|
@@ -43,7 +47,7 @@ export const setupMutationObserverMock = ({ disconnect, observe, takeRecords }:
|
|
|
43
47
|
}
|
|
44
48
|
}
|
|
45
49
|
|
|
46
|
-
[window, global].forEach(element => {
|
|
50
|
+
[window, global].forEach((element) => {
|
|
47
51
|
Object.defineProperty(element, 'MutationObserver', {
|
|
48
52
|
writable: true,
|
|
49
53
|
configurable: true,
|
|
@@ -80,7 +84,10 @@ export const setupMutationObserverMock = ({ disconnect, observe, takeRecords }:
|
|
|
80
84
|
* }]);;
|
|
81
85
|
* ```
|
|
82
86
|
*/
|
|
83
|
-
export const setupResizeObserverMock = ({
|
|
87
|
+
export const setupResizeObserverMock = ({
|
|
88
|
+
disconnect,
|
|
89
|
+
observe,
|
|
90
|
+
}: setupResizeObserverMockParams): typeof ResizeObserver => {
|
|
84
91
|
class MockResizeObserver implements ResizeObserver {
|
|
85
92
|
/**
|
|
86
93
|
*
|
|
@@ -103,7 +110,7 @@ export const setupResizeObserverMock = ({ disconnect, observe }: setupResizeObse
|
|
|
103
110
|
}
|
|
104
111
|
}
|
|
105
112
|
|
|
106
|
-
[window, global].forEach(element => {
|
|
113
|
+
[window, global].forEach((element) => {
|
|
107
114
|
Object.defineProperty(element, 'ResizeObserver', {
|
|
108
115
|
writable: true,
|
|
109
116
|
configurable: true,
|
|
@@ -132,7 +139,7 @@ class MockCustomEvent extends Event {
|
|
|
132
139
|
export const setupSubmitEventMock = (): typeof MockCustomEvent => {
|
|
133
140
|
class SubmitEvent extends MockCustomEvent {}
|
|
134
141
|
|
|
135
|
-
[window, global].forEach(element => {
|
|
142
|
+
[window, global].forEach((element) => {
|
|
136
143
|
Object.defineProperty(element, 'SubmitEvent', {
|
|
137
144
|
writable: true,
|
|
138
145
|
configurable: true,
|
|
@@ -159,7 +166,7 @@ export const setUpRequestAnimationFrameMock = (faketimer: () => void): typeof re
|
|
|
159
166
|
return 0;
|
|
160
167
|
};
|
|
161
168
|
|
|
162
|
-
[window, global].forEach(element => {
|
|
169
|
+
[window, global].forEach((element) => {
|
|
163
170
|
Object.defineProperty(element, 'requestAnimationFrame', {
|
|
164
171
|
writable: true,
|
|
165
172
|
configurable: true,
|
|
@@ -222,13 +229,16 @@ const getRangeOverflow = (input: HTMLInputElement): boolean => {
|
|
|
222
229
|
* @param input - HTMLElement to test
|
|
223
230
|
* @returns truthy if element is HTMLSelectElement
|
|
224
231
|
*/
|
|
225
|
-
const isHTMLSelectElement = (input: HTMLElement): input is HTMLSelectElement =>
|
|
232
|
+
const isHTMLSelectElement = (input: HTMLElement): input is HTMLSelectElement =>
|
|
233
|
+
(input as HTMLSelectElement).options !== undefined;
|
|
226
234
|
/**
|
|
227
235
|
* Is value missing from input
|
|
228
236
|
* @param input - input to test
|
|
229
237
|
* @returns truthy if value is required
|
|
230
238
|
*/
|
|
231
|
-
const isValueMissing = (
|
|
239
|
+
const isValueMissing = (
|
|
240
|
+
input: HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement | HTMLFieldSetElement
|
|
241
|
+
): boolean => {
|
|
232
242
|
if (input.hasAttribute('required') && (input as HTMLInputElement).required === true) {
|
|
233
243
|
if (['checkbox', 'radio'].includes(input.type)) {
|
|
234
244
|
return (input as HTMLInputElement).checked === false;
|
|
@@ -251,7 +261,9 @@ const getValidity = (input: HTMLInputElement): ValidityState => {
|
|
|
251
261
|
// required field without a value
|
|
252
262
|
const valueMissing = isValueMissing(input);
|
|
253
263
|
// value of a number field is not a number | value of a date field is not a date
|
|
254
|
-
const badInput =
|
|
264
|
+
const badInput =
|
|
265
|
+
['number', 'date'].includes(input.type) &&
|
|
266
|
+
isNaN(input.type === 'date' ? Date.parse(input.value) : (input.value as unknown as number));
|
|
255
267
|
// value does not conform to the pattern
|
|
256
268
|
const patternMismatch = input.hasAttribute('pattern') && new RegExp(input.pattern).test(input.value) === false;
|
|
257
269
|
// value of a number|date field is higher than the max attribute
|
|
@@ -259,7 +271,11 @@ const getValidity = (input: HTMLInputElement): ValidityState => {
|
|
|
259
271
|
// value of a number|date field is lower than the min attribute
|
|
260
272
|
const rangeUnderflow = getRangeUnderflow(input);
|
|
261
273
|
// value of a number field does not conform to the stepattribute
|
|
262
|
-
const stepMismatch =
|
|
274
|
+
const stepMismatch =
|
|
275
|
+
input.type === 'number' &&
|
|
276
|
+
input.hasAttribute('step') &&
|
|
277
|
+
input.step !== 'any' &&
|
|
278
|
+
Number(input.value) % parseFloat(input.step) !== 0;
|
|
263
279
|
// the user has edited a too-long value in a field with maxlength
|
|
264
280
|
const tooLong = input.hasAttribute('maxLength') && input.value?.length > Number(input.maxLength);
|
|
265
281
|
// the user has edited a too-short value in a field with minlength
|
|
@@ -269,7 +285,18 @@ const getValidity = (input: HTMLInputElement): ValidityState => {
|
|
|
269
285
|
// value of validationMessage is not an empty string
|
|
270
286
|
const customError = false;
|
|
271
287
|
|
|
272
|
-
const valid = ![
|
|
288
|
+
const valid = ![
|
|
289
|
+
valueMissing,
|
|
290
|
+
badInput,
|
|
291
|
+
patternMismatch,
|
|
292
|
+
rangeOverflow,
|
|
293
|
+
rangeUnderflow,
|
|
294
|
+
stepMismatch,
|
|
295
|
+
tooLong,
|
|
296
|
+
tooShort,
|
|
297
|
+
typeMismatch,
|
|
298
|
+
customError,
|
|
299
|
+
].some((invalid) => invalid);
|
|
273
300
|
return {
|
|
274
301
|
badInput,
|
|
275
302
|
customError,
|
|
@@ -3,7 +3,7 @@ import { createID, formatID, isValideID } from './id';
|
|
|
3
3
|
|
|
4
4
|
describe('id', () => {
|
|
5
5
|
describe('createID', () => {
|
|
6
|
-
test.each([undefined, '', 'blu'])('Should generate a prefixed ID if defined : %s', prefix => {
|
|
6
|
+
test.each([undefined, '', 'blu'])('Should generate a prefixed ID if defined : %s', (prefix) => {
|
|
7
7
|
const id = createID(prefix);
|
|
8
8
|
let regexp = /^[a-z0-9]{10}$/;
|
|
9
9
|
if (prefix !== undefined && prefix !== '') {
|
|
@@ -19,12 +19,15 @@ describe('id', () => {
|
|
|
19
19
|
});
|
|
20
20
|
|
|
21
21
|
describe('isValideID', () => {
|
|
22
|
-
test.each([undefined, '', { id: 'hello' }, '!batman', '{batman}', '123', '1-batman'])(
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
22
|
+
test.each([undefined, '', { id: 'hello' }, '!batman', '{batman}', '123', '1-batman'])(
|
|
23
|
+
'Should NOT validate ID : %s',
|
|
24
|
+
(id) => {
|
|
25
|
+
const valide = isValideID(id);
|
|
26
|
+
expect(valide).toEqual(false);
|
|
27
|
+
}
|
|
28
|
+
);
|
|
26
29
|
|
|
27
|
-
test.each(['hello', 'hello-batman', 'hello-batman-in-gotham', 'hello-1'])('Should validate ID : %s', id => {
|
|
30
|
+
test.each(['hello', 'hello-batman', 'hello-batman-in-gotham', 'hello-1'])('Should validate ID : %s', (id) => {
|
|
28
31
|
const valide = isValideID(id);
|
|
29
32
|
expect(valide).toEqual(true);
|
|
30
33
|
});
|
|
@@ -14,7 +14,7 @@ export const createID = (prefix = '', length = 10): string => {
|
|
|
14
14
|
crypto.getRandomValues(randomBytes);
|
|
15
15
|
|
|
16
16
|
const hexString = Array.from(randomBytes)
|
|
17
|
-
.map(byte => byte.toString(16).padStart(2, '0'))
|
|
17
|
+
.map((byte) => byte.toString(16).padStart(2, '0'))
|
|
18
18
|
.join('')
|
|
19
19
|
.slice(0, length);
|
|
20
20
|
|
|
@@ -26,7 +26,8 @@ export const createID = (prefix = '', length = 10): string => {
|
|
|
26
26
|
* @param newValue - id value to validate
|
|
27
27
|
* @returns true if `id` is valid
|
|
28
28
|
*/
|
|
29
|
-
export const isValideID = (newValue: unknown): boolean =>
|
|
29
|
+
export const isValideID = (newValue: unknown): boolean =>
|
|
30
|
+
isValidString(newValue) && /^([a-z][a-z0-9]*)(-[a-z0-9]+)*$/.exec(newValue) !== null;
|
|
30
31
|
|
|
31
32
|
/**
|
|
32
33
|
* Format id from value
|
|
@@ -2,7 +2,7 @@ import { describe, expect, test, vi } from 'vitest';
|
|
|
2
2
|
import { Cursor, nextTick, Page, Paginate } from './components';
|
|
3
3
|
import { CursorType } from './components.conf';
|
|
4
4
|
|
|
5
|
-
const initArray = length => new Array(length).fill('').map((_, key) => (key + 1).toString());
|
|
5
|
+
const initArray = (length) => new Array(length).fill('').map((_, key) => (key + 1).toString());
|
|
6
6
|
|
|
7
7
|
describe('components', () => {
|
|
8
8
|
describe('nextTick', () => {
|
|
@@ -72,7 +72,7 @@ describe('components', () => {
|
|
|
72
72
|
|
|
73
73
|
test('Should filter paginated page from instance', () => {
|
|
74
74
|
const res = new Paginate(['batman', 'robin', 'joker', 'bane', 'harley quinn', 'dante']);
|
|
75
|
-
const page = res.getPage(0, val => val.includes('an'));
|
|
75
|
+
const page = res.getPage(0, (val) => val.includes('an'));
|
|
76
76
|
expect(page).toMatchSnapshot();
|
|
77
77
|
});
|
|
78
78
|
|
|
@@ -109,7 +109,7 @@ describe('components', () => {
|
|
|
109
109
|
items,
|
|
110
110
|
next: '/next/url',
|
|
111
111
|
},
|
|
112
|
-
])('Should create Page instance', init => {
|
|
112
|
+
])('Should create Page instance', (init) => {
|
|
113
113
|
const page = new Page(init);
|
|
114
114
|
expect(page).toMatchSnapshot();
|
|
115
115
|
});
|
|
@@ -124,14 +124,14 @@ describe('components', () => {
|
|
|
124
124
|
});
|
|
125
125
|
|
|
126
126
|
describe('getIndexFromCursor()', () => {
|
|
127
|
-
test.each([undefined, 'hello'])('Should get first index by default, cursor %s', cursor => {
|
|
127
|
+
test.each([undefined, 'hello'])('Should get first index by default, cursor %s', (cursor) => {
|
|
128
128
|
const page = new Page({ items });
|
|
129
129
|
page.items.forEach(() => {
|
|
130
130
|
const index = page.getIndexFromCursor(cursor as CursorType);
|
|
131
131
|
expect(index).toEqual(0);
|
|
132
132
|
});
|
|
133
133
|
});
|
|
134
|
-
describe.each(Object.values(Cursor))('cursor %s', cursor => {
|
|
134
|
+
describe.each(Object.values(Cursor))('cursor %s', (cursor) => {
|
|
135
135
|
let result = 0;
|
|
136
136
|
if (cursor === 'first') result = 0;
|
|
137
137
|
if (cursor === 'last') result = items.length - 1;
|
|
@@ -180,7 +180,7 @@ describe('components', () => {
|
|
|
180
180
|
expect(index).toEqual(Number(oldIndex) === 0 ? page.items.length - page.baseIndex : nextIndex);
|
|
181
181
|
}
|
|
182
182
|
});
|
|
183
|
-
test.each(['previous', 'next'])('Should get first index with unavailable item, cursor %s', cursor => {
|
|
183
|
+
test.each(['previous', 'next'])('Should get first index with unavailable item, cursor %s', (cursor) => {
|
|
184
184
|
const page = new Page({ items });
|
|
185
185
|
page.items.forEach(() => {
|
|
186
186
|
const index = page.getIndexFromCursor(cursor as CursorType, 'hello');
|
|
@@ -73,7 +73,7 @@ export class Page<T> {
|
|
|
73
73
|
let newIndex;
|
|
74
74
|
let oldIndex = startIndex;
|
|
75
75
|
if (['previous', 'next'].includes(cursor) && oldItem) {
|
|
76
|
-
const findedIndex = this.items.findIndex(item => JSON.stringify(item) === JSON.stringify(oldItem));
|
|
76
|
+
const findedIndex = this.items.findIndex((item) => JSON.stringify(item) === JSON.stringify(oldItem));
|
|
77
77
|
if (findedIndex === -1) return startIndex;
|
|
78
78
|
oldIndex = findedIndex;
|
|
79
79
|
}
|
|
@@ -83,9 +83,15 @@ export class Page<T> {
|
|
|
83
83
|
} else if (cursor === 'last') {
|
|
84
84
|
newIndex = lastIndex;
|
|
85
85
|
} else if (cursor === 'previous') {
|
|
86
|
-
newIndex =
|
|
86
|
+
newIndex =
|
|
87
|
+
JSON.stringify(this.items[oldIndex]) === JSON.stringify(this.items[startIndex])
|
|
88
|
+
? lastIndex
|
|
89
|
+
: oldIndex - this.baseIndex;
|
|
87
90
|
} else if (cursor === 'next') {
|
|
88
|
-
newIndex =
|
|
91
|
+
newIndex =
|
|
92
|
+
JSON.stringify(this.items[oldIndex]) === JSON.stringify(this.items[lastIndex])
|
|
93
|
+
? startIndex
|
|
94
|
+
: oldIndex + this.baseIndex;
|
|
89
95
|
} else {
|
|
90
96
|
newIndex = startIndex;
|
|
91
97
|
}
|
|
@@ -109,9 +115,17 @@ export class Paginate<T> {
|
|
|
109
115
|
#next?: Page<T>['next'];
|
|
110
116
|
#total?: Page<T>['total'];
|
|
111
117
|
|
|
112
|
-
constructor(
|
|
118
|
+
constructor(
|
|
119
|
+
items: Page<T>['items'],
|
|
120
|
+
options?: { step?: number; top?: Page<T>['top']; total?: Page<T>['total']; next?: Page<T>['next'] }
|
|
121
|
+
) {
|
|
113
122
|
if (Array.isArray(items)) this.items = items;
|
|
114
|
-
if (
|
|
123
|
+
if (
|
|
124
|
+
options &&
|
|
125
|
+
(['string', 'function'].includes(typeof options.next) ||
|
|
126
|
+
(isObject<URL>(options.next) && URL.canParse(options.next)))
|
|
127
|
+
)
|
|
128
|
+
this.#next = options.next;
|
|
115
129
|
if (typeof options?.top === 'number') this.#top = options.top;
|
|
116
130
|
if (typeof options?.total === 'number') this.#total = options.total;
|
|
117
131
|
}
|