@mgdis/core-ui-helpers 1.2.2 → 1.3.1

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 (149) hide show
  1. package/dist/angular/index.cjs +27 -0
  2. package/dist/angular/index.cjs.map +1 -0
  3. package/dist/angular/index.d.ts +21 -0
  4. package/dist/angular/index.js +25 -0
  5. package/dist/angular/index.js.map +1 -0
  6. package/dist/angular/index.umd.js +33 -0
  7. package/dist/angular/index.umd.js.map +1 -0
  8. package/dist/locale/index.cjs +190 -0
  9. package/dist/locale/index.cjs.map +1 -0
  10. package/dist/locale/index.d.ts +110 -0
  11. package/dist/locale/index.js +181 -0
  12. package/dist/locale/index.js.map +1 -0
  13. package/dist/locale/index.umd.js +196 -0
  14. package/dist/locale/index.umd.js.map +1 -0
  15. package/dist/playwright/index.cjs +137 -0
  16. package/dist/playwright/index.cjs.map +1 -0
  17. package/dist/playwright/index.d.ts +39 -0
  18. package/dist/playwright/index.js +133 -0
  19. package/dist/playwright/index.js.map +1 -0
  20. package/dist/playwright/index.umd.js +143 -0
  21. package/dist/playwright/index.umd.js.map +1 -0
  22. package/dist/stencil/index.cjs +363 -0
  23. package/dist/stencil/index.cjs.map +1 -0
  24. package/dist/stencil/index.d.ts +222 -0
  25. package/dist/stencil/index.js +357 -0
  26. package/dist/stencil/index.js.map +1 -0
  27. package/dist/stencil/index.umd.js +369 -0
  28. package/dist/stencil/index.umd.js.map +1 -0
  29. package/dist/storybook/index.cjs +2335 -0
  30. package/dist/storybook/index.cjs.map +1 -0
  31. package/dist/storybook/index.d.ts +85 -0
  32. package/dist/storybook/index.js +2329 -0
  33. package/dist/storybook/index.js.map +1 -0
  34. package/dist/storybook/index.umd.js +2341 -0
  35. package/dist/storybook/index.umd.js.map +1 -0
  36. package/dist/tests/index.cjs +315 -0
  37. package/dist/tests/index.cjs.map +1 -0
  38. package/dist/tests/index.d.ts +100 -0
  39. package/dist/tests/index.js +309 -0
  40. package/dist/tests/index.js.map +1 -0
  41. package/dist/tests/index.umd.js +321 -0
  42. package/dist/tests/index.umd.js.map +1 -0
  43. package/dist/utils/index.cjs +441 -0
  44. package/dist/utils/index.cjs.map +1 -0
  45. package/dist/utils/index.d.ts +265 -0
  46. package/dist/utils/index.js +417 -0
  47. package/dist/utils/index.js.map +1 -0
  48. package/dist/utils/index.umd.js +447 -0
  49. package/dist/utils/index.umd.js.map +1 -0
  50. package/dist/vue/index.cjs +1935 -0
  51. package/dist/vue/index.cjs.map +1 -0
  52. package/dist/vue/index.d.ts +35 -0
  53. package/dist/vue/index.js +1933 -0
  54. package/dist/vue/index.js.map +1 -0
  55. package/dist/vue/index.umd.js +1941 -0
  56. package/dist/vue/index.umd.js.map +1 -0
  57. package/package.json +14 -11
  58. package/CHANGELOG.md +0 -101
  59. package/json-doc.test.json +0 -264
  60. package/rollup.config.mjs +0 -61
  61. package/src/angular/index.spec.ts +0 -8
  62. package/src/angular/index.ts +0 -1
  63. package/src/angular/mg-angular-logger/index.ts +0 -1
  64. package/src/angular/mg-angular-logger/mg-angular-logger.conf.ts +0 -3
  65. package/src/angular/mg-angular-logger/mg-angular-logger.spec.ts +0 -40
  66. package/src/angular/mg-angular-logger/mg-angular-logger.ts +0 -24
  67. package/src/locale/index.spec.ts +0 -24
  68. package/src/locale/index.ts +0 -1
  69. package/src/locale/locale.spec.ts +0 -291
  70. package/src/locale/locale.ts +0 -201
  71. package/src/playwright/config/index.ts +0 -43
  72. package/src/playwright/index.spec.ts +0 -16
  73. package/src/playwright/index.ts +0 -4
  74. package/src/playwright/utils/__snapshots__/utils.spec.ts.snap +0 -48
  75. package/src/playwright/utils/index.ts +0 -1
  76. package/src/playwright/utils/utils.spec.ts +0 -50
  77. package/src/playwright/utils/utils.ts +0 -77
  78. package/src/stencil/ide/__snapshots__/cem.spec.ts.snap +0 -248
  79. package/src/stencil/ide/__snapshots__/ide.spec.ts.snap +0 -414
  80. package/src/stencil/ide/cem.spec.ts +0 -35
  81. package/src/stencil/ide/cem.ts +0 -96
  82. package/src/stencil/ide/ide.spec.ts +0 -29
  83. package/src/stencil/ide/ide.ts +0 -221
  84. package/src/stencil/ide/index.ts +0 -2
  85. package/src/stencil/index.spec.ts +0 -16
  86. package/src/stencil/index.ts +0 -2
  87. package/src/stencil/normalize-boolean-attributes/index.ts +0 -1
  88. package/src/stencil/normalize-boolean-attributes/normalize-boolean-attributes.spec.ts +0 -63
  89. package/src/stencil/normalize-boolean-attributes/normalize-boolean-attributes.ts +0 -40
  90. package/src/storybook/index.spec.ts +0 -19
  91. package/src/storybook/index.ts +0 -1
  92. package/src/storybook/stencil/__snapshots__/stencil.spec.ts.snap +0 -329
  93. package/src/storybook/stencil/index.ts +0 -1
  94. package/src/storybook/stencil/stencil-shim.d.ts +0 -1
  95. package/src/storybook/stencil/stencil.conf.ts +0 -4
  96. package/src/storybook/stencil/stencil.spec.ts +0 -170
  97. package/src/storybook/stencil/stencil.ts +0 -377
  98. package/src/tests/index.spec.ts +0 -20
  99. package/src/tests/index.ts +0 -1
  100. package/src/tests/unit/__snapshots__/unit.spec.ts.snap +0 -52
  101. package/src/tests/unit/index.ts +0 -1
  102. package/src/tests/unit/unit.conf.ts +0 -16
  103. package/src/tests/unit/unit.spec.ts +0 -206
  104. package/src/tests/unit/unit.ts +0 -333
  105. package/src/utils/attributes/class-list/class-list.spec.ts +0 -36
  106. package/src/utils/attributes/class-list/class-list.ts +0 -51
  107. package/src/utils/attributes/class-list/index.ts +0 -1
  108. package/src/utils/attributes/id/id.spec.ts +0 -54
  109. package/src/utils/attributes/id/id.ts +0 -47
  110. package/src/utils/attributes/id/index.ts +0 -1
  111. package/src/utils/attributes/index.ts +0 -3
  112. package/src/utils/attributes/to-kebab-case/index.ts +0 -1
  113. package/src/utils/attributes/to-kebab-case/to-kebab-case.spec.ts +0 -29
  114. package/src/utils/attributes/to-kebab-case/to-kebab-case.ts +0 -23
  115. package/src/utils/components/__snapshots__/components.spec.ts.snap +0 -313
  116. package/src/utils/components/components.conf.ts +0 -11
  117. package/src/utils/components/components.spec.ts +0 -192
  118. package/src/utils/components/components.ts +0 -152
  119. package/src/utils/components/index.ts +0 -2
  120. package/src/utils/date/date.spec.ts +0 -12
  121. package/src/utils/date/date.ts +0 -20
  122. package/src/utils/date/index.ts +0 -1
  123. package/src/utils/elements/elements.spec.ts +0 -17
  124. package/src/utils/elements/elements.ts +0 -18
  125. package/src/utils/elements/index.ts +0 -1
  126. package/src/utils/index.spec.ts +0 -84
  127. package/src/utils/index.ts +0 -8
  128. package/src/utils/number/index.ts +0 -1
  129. package/src/utils/number/number.spec.ts +0 -14
  130. package/src/utils/number/number.ts +0 -6
  131. package/src/utils/object/index.ts +0 -2
  132. package/src/utils/object/object.conf.ts +0 -4
  133. package/src/utils/object/object.spec.ts +0 -110
  134. package/src/utils/object/object.ts +0 -27
  135. package/src/utils/string/index.ts +0 -1
  136. package/src/utils/string/string.spec.ts +0 -54
  137. package/src/utils/string/string.ts +0 -39
  138. package/src/utils/windows/index.ts +0 -1
  139. package/src/utils/windows/windows.spec.ts +0 -69
  140. package/src/utils/windows/windows.ts +0 -50
  141. package/src/vue/index.spec.ts +0 -8
  142. package/src/vue/index.ts +0 -1
  143. package/src/vue/mg-model/index.ts +0 -1
  144. package/src/vue/mg-model/mg-model.conf.ts +0 -6
  145. package/src/vue/mg-model/mg-model.spec.ts +0 -50
  146. package/src/vue/mg-model/mg-model.ts +0 -55
  147. package/tsconfig.json +0 -17
  148. package/typedoc.json +0 -14
  149. package/vitest.config.mts +0 -13
@@ -1,377 +0,0 @@
1
- import { renderVdom } from '@stencil/core/internal/client';
2
- import type { VNode } from '@stencil/core';
3
- import { ArgsType } from './stencil.conf';
4
- import { JsonDocs, JsonDocsComponent, JsonDocsProp } from '@stencil/core/internal';
5
- import { prettify } from 'htmlfy';
6
-
7
- /**
8
- * Render attribute on the given element
9
- * @param element - targeted to render attribute
10
- * @param name - of the attribute
11
- * @param value - of the attribute
12
- */
13
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
14
- const renderAttribute = (element: HTMLElement, name: string, value: any): void => {
15
- if ([null, undefined, '', false].includes(value) || ['innerHTML', 'style', ''].includes(name)) {
16
- return;
17
- }
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
- );
24
- };
25
-
26
- /**
27
- * Render new element in parent element
28
- * @param parentNode - HTML element
29
- * @param tagName - of the new element
30
- * @param attributes - of the new element
31
- * @param children - of the new element
32
- * @param text - of the new element
33
- */
34
- const renderElement = (
35
- parentNode: HTMLElement,
36
- tagName: VNode['$tag$'],
37
- attributes: VNode['$attrs$'],
38
- children: VNode[],
39
- text: VNode['$text$']
40
- ): void => {
41
- // render HTML
42
- if (tagName && typeof tagName === 'string') {
43
- const element = document.createElement(tagName);
44
- Object.keys(attributes || {}).forEach((attr) => {
45
- renderAttribute(element, attr, attributes[attr]);
46
- });
47
-
48
- children?.forEach((child) => {
49
- renderElement(element, child.$tag$, child.$attrs$, child.$children$, child.$text$);
50
- });
51
-
52
- if (attributes?.innerHTML) element.innerHTML = attributes.innerHTML;
53
-
54
- parentNode.appendChild(element);
55
- }
56
- // render text
57
- if (text) {
58
- parentNode.innerHTML = text;
59
- }
60
- };
61
-
62
- /**
63
- * Filter default argument on component argument to prevent them to be rendered
64
- * @param args - all possible args with custom values
65
- * @param defaultValues - component default args values
66
- * @param slots - slots
67
- * @returns filtres args
68
- * @example
69
- * ```ts
70
- * import { filterArgs } from '@mgdis/core-ui-helpers/dist/storybook';
71
- * const Template = (args: MgBadgeType): HTMLElement => <mg-badge {...filterArgs(args, { variant: 'info' }, ['actions'])}></mg-badge>;
72
- * ```
73
- */
74
- export const filterArgs = <T>(args: T, defaultValues?: Partial<T>, slots: string[] = []): T => {
75
- const filteredArgs = {} as { [key: string]: unknown };
76
- if (typeof args !== 'object') {
77
- throw new Error("filterArgs - args isn't an object.");
78
- }
79
- for (const k in args) {
80
- if (!slots.includes(k)) {
81
- const arg = args[k];
82
- // Change camelCase k to kebab-case
83
- const key = k.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
84
- if (!defaultValues || !Object.keys(defaultValues).includes(k) || defaultValues[k] !== arg) {
85
- filteredArgs[key] = arg;
86
- }
87
- }
88
- }
89
- return filteredArgs as T;
90
- };
91
-
92
- /**
93
- * Storybook stencil wrapper. Used to target element with `storybook-root` id and render virtual DOM inside.
94
- * @param storyFn - storybook render function
95
- * @param context - storybook context
96
- * @returns rendered element
97
- * @example
98
- * ```ts
99
- * // .storybook/preview.ts
100
- * import { stencilWrapper } from '@mgdis/core-ui-helpers/dist/storybook';
101
- * export const decorators: Preview['decorators'] = [stencilWrapper];
102
- * ```
103
- */
104
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
105
- export const stencilWrapper = (storyFn: (ctx: any) => void, context: ArgsType): Element | undefined => {
106
- const host = document.getElementById('storybook-root');
107
- if (host === null) return;
108
-
109
- // update local switcher based on context variable
110
- document.querySelector('[lang]')?.setAttribute('lang', (context.globals as { locale: string })?.locale || 'en');
111
-
112
- renderVdom(
113
- {
114
- $ancestorComponent$: undefined,
115
- $flags$: 0,
116
- $modeName$: undefined,
117
- $cmpMeta$: {
118
- $flags$: 0,
119
- $tagName$: host.tagName,
120
- },
121
- $hostElement$: host,
122
- },
123
- storyFn(context)
124
- );
125
- return host.children[host.children.length - 1];
126
- };
127
-
128
- /**
129
- * Get story HTML from virtual DOM.
130
- * Mainly used to render, component code exemple in stories.
131
- * @param vitualNode - story virtual DOM
132
- * @returns stringified rendered HTML
133
- * @example
134
- * ```ts
135
- * // .storybook/preview.ts
136
- * import { getStoryHTML } from '@mgdis/core-ui-helpers/dist/storybook';
137
- *
138
- * export const parameters: Preview['parameters'] = {
139
- * docs: {
140
- * source: {
141
- * transform: (_, ctx) => getStoryHTML(ctx.originalStoryFn(ctx.args)),
142
- * }
143
- * },
144
- * };
145
- * ```
146
- */
147
- export const getStoryHTML = ({ $tag$, $attrs$, $children$, $text$ }: VNode): string => {
148
- const host = document.createElement('div');
149
-
150
- renderElement(host, $tag$, $attrs$, $children$, $text$);
151
-
152
- return prettify(host.innerHTML, {
153
- tag_wrap: 40,
154
- content_wrap: 120,
155
- }).replace(/="true"/g, '');
156
- };
157
-
158
- /**
159
- * Retrieve Component Storybook URL from file path
160
- * @param storybookBaseUrl - Storybook Base URL
161
- * @param filePath - Component file path
162
- * @returns Component Storybook URL
163
- */
164
- export const getStorybookUrl = (storybookBaseUrl: string, filePath: string | undefined): string | undefined => {
165
- if (!filePath) {
166
- return;
167
- }
168
- const split = filePath.split('/');
169
- return `${storybookBaseUrl}${split.slice(2, split.length - 1).join('-')}--docs`;
170
- };
171
-
172
- export class StorybookPreview {
173
- /**
174
- * JsonDocs
175
- */
176
- jsonDoc: JsonDocs;
177
-
178
- constructor(jsonDoc: JsonDocs) {
179
- this.jsonDoc = jsonDoc;
180
- }
181
-
182
- /**
183
- * Get component data from the jsonDoc
184
- * @param tagName - tag name we want to get the data from
185
- * @returns component data
186
- */
187
- #getComponentData = (tagName: string): JsonDocsComponent | undefined => {
188
- return this.jsonDoc.components.find((component) => component.tag === tagName);
189
- };
190
-
191
- /**
192
- * Get the control for the given prop
193
- * Based on https://storybook.js.org/docs/api/arg-types#controltype
194
- * @param prop - prop to get control for
195
- * @returns control type and options if applicable
196
- */
197
- #getPropControl = (prop: JsonDocsProp) => {
198
- // Get types
199
- const types: (string | undefined)[] = prop.type
200
- .replace(/"([^"]+)"/g, '$1') // Remove quotes
201
- .replace(/\s/g, '') // Remove all whitespace for simplicity
202
- .replace(/\(.*?\)/g, (match) => match.replace(/\|/g, ' OR ')) // Replace '|' inside parentheses
203
- .split('|')
204
- .map((type) => type.trim().replace(/ OR /g, '|')); // Revert ' OR ' back to '|'
205
-
206
- // Return control and options
207
- if (prop.type === 'string') {
208
- return { control: { type: 'text' } };
209
- } else if (prop.type === 'number') {
210
- return { control: { type: 'number' } };
211
- } else if (prop.type === 'boolean') {
212
- return { control: { type: 'boolean' } };
213
- } else if (prop.type.startsWith('{') && prop.type.endsWith('}')) {
214
- return { control: { type: 'object' } };
215
- } else if (types.length > 1) {
216
- // Manage case when multiple types are possible
217
- if (types.includes('string')) {
218
- return { control: { type: 'text' } };
219
- } else if (types.every((type) => type?.includes('[]'))) {
220
- return { control: { type: 'object' } };
221
- } else {
222
- // Add the posibility to set undefined
223
- types.unshift(undefined);
224
- return { control: { type: 'select' }, options: types };
225
- }
226
- } else return { control: { type: 'object' } };
227
- };
228
-
229
- /**
230
- * Extract component arg types from the component data
231
- * @param tagName - tag name we want to extract the arg types from
232
- * @returns component arg types
233
- */
234
- extractArgTypes = (tagName: string) => {
235
- const componentData = this.#getComponentData(tagName);
236
-
237
- // Extract props arg types
238
- const componentPropsArgTypes = componentData?.props.reduce((acc, prop) => {
239
- // Get Controls
240
- const { control, options } = this.#getPropControl(prop);
241
- // Set Component ArgTypes
242
- return {
243
- ...acc,
244
- [prop.name]: {
245
- name: prop.attr || prop.name,
246
- description: prop.docs,
247
- type: { required: prop.required },
248
- table: {
249
- category: 'props',
250
- type: { summary: prop.type },
251
- defaultValue: { summary: prop.default },
252
- },
253
- control,
254
- options,
255
- },
256
- };
257
- }, {});
258
-
259
- // Extract events arg types
260
- const componentEventsArgTypes = componentData?.events.reduce(
261
- (acc, event) => ({
262
- ...acc,
263
- [event.event]: {
264
- name: event.event,
265
- description: event.docs,
266
- table: {
267
- category: 'events',
268
- type: { summary: event.detail },
269
- },
270
- },
271
- }),
272
- {}
273
- );
274
-
275
- // Extracts Methods arg types
276
- const componentMethodsArgTypes = componentData?.methods.reduce(
277
- (acc, method) => ({
278
- ...acc,
279
- [method.name]: {
280
- name: method.name,
281
- description: method.docs,
282
- table: {
283
- category: 'methods',
284
- type: { summary: method.signature },
285
- },
286
- },
287
- }),
288
- {}
289
- );
290
-
291
- // Extracts Slots arg types
292
- const componentSlotsArgTypes = componentData?.slots.reduce(
293
- (acc, slot) => ({
294
- ...acc,
295
- [slot.name]: {
296
- name: slot.name !== '' ? slot.name : 'default', // default slot are unnamed
297
- description: slot.docs,
298
- table: {
299
- category: 'slots',
300
- type: { summary: undefined },
301
- },
302
- },
303
- }),
304
- {}
305
- );
306
-
307
- // Extracts CSS Properties arg types
308
- const componentCSSPropArgTypes = componentData?.styles.reduce(
309
- (acc, style) => ({
310
- ...acc,
311
- [style.name]: {
312
- name: style.name,
313
- description: style.docs,
314
- table: {
315
- category: 'custom properties',
316
- type: { summary: undefined },
317
- },
318
- },
319
- }),
320
- {}
321
- );
322
-
323
- // Extract component dependencies
324
- const componentDependencies = componentData?.dependencies.reduce((acc, dependency) => {
325
- const dependencyData = this.#getComponentData(dependency);
326
- if (!dependencyData) return acc; // Prevents from adding internal dependency
327
- return {
328
- ...acc,
329
- [dependency]: {
330
- name: dependency,
331
- description: `<a href="./?path=/docs/${getStorybookUrl('', dependencyData?.filePath)}">View Component Documentation</a>`,
332
- table: {
333
- category: 'depends on',
334
- type: { summary: undefined },
335
- },
336
- },
337
- };
338
- }, {});
339
-
340
- // Extract dependents components
341
- const componentDependents = componentData?.dependents.reduce((acc, dependent) => {
342
- const dependentData = this.#getComponentData(dependent);
343
- if (!dependentData) return acc; // Prevents from adding internal dependent
344
- return {
345
- ...acc,
346
- [dependent]: {
347
- name: dependent,
348
- description: `<a href="./?path=/docs/${getStorybookUrl('', dependentData?.filePath)}">View Component Documentation</a>`,
349
- table: {
350
- category: 'used by',
351
- type: { summary: undefined },
352
- },
353
- },
354
- };
355
- }, {});
356
-
357
- return {
358
- ...componentPropsArgTypes,
359
- ...componentEventsArgTypes,
360
- ...componentMethodsArgTypes,
361
- ...componentSlotsArgTypes,
362
- ...componentCSSPropArgTypes,
363
- ...componentDependencies,
364
- ...componentDependents,
365
- };
366
- };
367
-
368
- /**
369
- * Extract component description from the component data
370
- * @param tagName - tag name we want to extract the description from
371
- * @returns component description
372
- */
373
- extractComponentDescription = (tagName: string) => {
374
- const componentData = this.#getComponentData(tagName);
375
- return componentData?.readme || componentData?.docs;
376
- };
377
- }
@@ -1,20 +0,0 @@
1
- import { describe, expect, test } from 'vitest';
2
- import {
3
- setUpHTMLInputElementValidity,
4
- setupMutationObserverMock,
5
- setUpRequestAnimationFrameMock,
6
- setupResizeObserverMock,
7
- setupSubmitEventMock,
8
- } from './';
9
-
10
- describe('tests', () => {
11
- describe('unit', () => {
12
- test('import', () => {
13
- expect(setupMutationObserverMock).toBeDefined();
14
- expect(setupResizeObserverMock).toBeDefined();
15
- expect(setupSubmitEventMock).toBeDefined();
16
- expect(setUpRequestAnimationFrameMock).toBeDefined();
17
- expect(setUpHTMLInputElementValidity).toBeDefined();
18
- });
19
- });
20
- });
@@ -1 +0,0 @@
1
- export * from './unit';
@@ -1,52 +0,0 @@
1
- // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2
-
3
- exports[`unit > setupMutationObserverMock > Should render html 1`] = `
4
- MockMutationObserver {
5
- "cb": {},
6
- "disconnect": undefined,
7
- "observe": undefined,
8
- "takeRecords": undefined,
9
- }
10
- `;
11
-
12
- exports[`unit > setupMutationObserverMock > Should render html 2`] = `
13
- MockMutationObserver {
14
- "cb": {
15
- "disconnect": [MockFunction],
16
- "observe": [Function],
17
- "takeRecords": [Function],
18
- },
19
- "disconnect": [MockFunction],
20
- "observe": [Function],
21
- "takeRecords": [Function],
22
- }
23
- `;
24
-
25
- exports[`unit > setupResizeObserverMock > Should render html 1`] = `
26
- MockResizeObserver {
27
- "cb": {},
28
- "disconnect": undefined,
29
- "observe": undefined,
30
- "unobserve": undefined,
31
- }
32
- `;
33
-
34
- exports[`unit > setupResizeObserverMock > Should render html 2`] = `
35
- MockResizeObserver {
36
- "cb": {
37
- "disconnect": [MockFunction],
38
- "observe": [Function],
39
- "unobserve": [Function],
40
- },
41
- "disconnect": [MockFunction],
42
- "observe": [Function],
43
- "unobserve": undefined,
44
- }
45
- `;
46
-
47
- exports[`unit > setupSubmitEventMock > Should render html 1`] = `
48
- SubmitEvent {
49
- "detail": undefined,
50
- "isTrusted": false,
51
- }
52
- `;
@@ -1 +0,0 @@
1
- export * from './unit';
@@ -1,16 +0,0 @@
1
- /**
2
- * Define MutationObserver setup function signature
3
- */
4
- export type SetupMutationObserverMockParams = {
5
- disconnect: MutationObserver['disconnect'];
6
- observe: MutationObserver['observe'];
7
- takeRecords: MutationObserver['takeRecords'];
8
- };
9
-
10
- /**
11
- * Define ResizeObserver setup function signature
12
- */
13
- export type setupResizeObserverMockParams = {
14
- disconnect: ResizeObserver['disconnect'];
15
- observe: ResizeObserver['observe'];
16
- };
@@ -1,206 +0,0 @@
1
- import { beforeEach, describe, expect, test, vi } from 'vitest';
2
- import {
3
- setUpHTMLInputElementValidity,
4
- setupMutationObserverMock,
5
- setUpRequestAnimationFrameMock,
6
- setupResizeObserverMock,
7
- setupSubmitEventMock,
8
- } from './unit.ts';
9
- import type { SetupMutationObserverMockParams, setupResizeObserverMockParams } from './unit.conf.ts';
10
-
11
- describe('unit', () => {
12
- describe('setupMutationObserverMock', () => {
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
- );
20
- });
21
- describe('setupResizeObserverMock', () => {
22
- test.each([{}, { disconnect: vi.fn(), observe: ({}) => {}, unobserve: () => [] }])('Should render html', (args) => {
23
- const Mock = setupResizeObserverMock(args as setupResizeObserverMockParams);
24
- expect(new Mock(args as ResizeObserverCallback)).toMatchSnapshot();
25
- });
26
- });
27
- describe('setupSubmitEventMock', () => {
28
- test('Should render html', () => {
29
- const Mock = setupSubmitEventMock();
30
- expect(new Mock('test')).toMatchSnapshot();
31
- });
32
- });
33
- describe('setUpRequestAnimationFrameMock', () => {
34
- beforeEach(() => {
35
- vi.useFakeTimers();
36
- });
37
-
38
- test.each([window, global])('Should render html', (element) => {
39
- const timer = vi.fn();
40
- const callback = vi.fn();
41
-
42
- setUpRequestAnimationFrameMock(timer);
43
-
44
- expect(element.requestAnimationFrame).toBeDefined();
45
-
46
- element.requestAnimationFrame(callback);
47
- vi.runOnlyPendingTimers();
48
-
49
- expect(timer).toHaveBeenCalled();
50
- expect(callback).toHaveBeenCalled();
51
- });
52
- });
53
- describe('setUpHTMLInputElementValidity', () => {
54
- test.each([
55
- { attributes: {}, validity: { valid: true } },
56
- // number rangeUnderflow
57
- { attributes: { type: 'number', min: '10', value: '9' }, validity: { rangeUnderflow: true, valid: false } },
58
- { attributes: { type: 'number', min: '10', value: '10' }, validity: { rangeUnderflow: false, valid: true } },
59
- // number rangeOverflow
60
- { attributes: { type: 'number', max: '10', value: '11' }, validity: { rangeOverflow: true, valid: false } },
61
- { attributes: { type: 'number', max: '10', value: '10' }, validity: { rangeOverflow: false, valid: true } },
62
- // text rangeOverflow
63
- { attributes: { type: 'text', max: '10', value: '10' }, validity: { rangeOverflow: false, valid: true } },
64
- { attributes: { type: 'text', max: '10', value: '11' }, validity: { rangeOverflow: false, valid: true } },
65
- // date rangeUnderflow
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
- },
74
- // number rangeOverflow
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
- },
83
- // number badInput
84
- { attributes: { type: 'number', value: '10' }, validity: { badInput: false, valid: true } },
85
- // date badInput
86
- { attributes: { type: 'date', value: '2025-06-19' }, validity: { badInput: false, valid: true } },
87
- { attributes: { type: 'date', value: NaN }, validity: { badInput: true, valid: false } },
88
- // text valueMissing
89
- { attributes: { type: 'text', value: '', required: true }, validity: { valueMissing: true, valid: false } },
90
- { attributes: { type: 'text', required: true }, validity: { valueMissing: true, valid: false } },
91
- { attributes: { type: 'text', value: 'batman', required: true }, validity: { valueMissing: false, valid: true } },
92
- // password valueMissing
93
- { attributes: { type: 'password', value: '', required: true }, validity: { valueMissing: true, valid: false } },
94
- { attributes: { type: 'password', required: true }, validity: { valueMissing: true, valid: false } },
95
- {
96
- attributes: { type: 'password', value: 'batman', required: true },
97
- validity: { valueMissing: false, valid: true },
98
- },
99
- // textarea valueMissing
100
- { attributes: { type: 'textarea', required: true }, validity: { valueMissing: true, valid: false } },
101
- { attributes: { type: 'textarea', value: '', required: true }, validity: { valueMissing: true, valid: false } },
102
- {
103
- attributes: { type: 'textarea', value: 'batman', required: true },
104
- validity: { valueMissing: false, valid: true },
105
- },
106
- // number valueMissing
107
- { attributes: { type: 'number', value: '', required: true }, validity: { valueMissing: true, valid: false } },
108
- { attributes: { type: 'number', value: '1', required: true }, validity: { valueMissing: false, valid: true } },
109
- // date valueMissing
110
- { attributes: { type: 'date', value: '', required: true }, validity: { valueMissing: true, valid: false } },
111
- {
112
- attributes: { type: 'date', value: '2025-06-19', required: true },
113
- validity: { valueMissing: false, valid: true },
114
- },
115
- // checkbox valueMissing
116
- { attributes: { type: 'checkbox', required: true }, validity: { valueMissing: true, valid: false } },
117
- { attributes: { type: 'checkbox', checked: '', required: true }, validity: { valueMissing: false, valid: true } },
118
- // radio valueMissing
119
- { attributes: { type: 'radio', required: true }, validity: { valueMissing: true, valid: false } },
120
- { attributes: { type: 'radio', checked: '', required: true }, validity: { valueMissing: false, valid: true } },
121
- // select valueMissing
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
- },
130
- // input patternMismatch
131
- { attributes: { type: 'text', value: '1', pattern: '[a-z]' }, validity: { patternMismatch: true, valid: false } },
132
- {
133
- attributes: { type: 'text', value: 'batman', pattern: '[a-z]' },
134
- validity: { patternMismatch: false, valid: true },
135
- },
136
- // text stepMismatch
137
- { attributes: { type: 'text', value: '2', step: '10' }, validity: { stepMismatch: false, valid: true } },
138
- // number stepMismatch
139
- { attributes: { type: 'number', value: '', step: 'any' }, validity: { stepMismatch: false, valid: true } },
140
- { attributes: { type: 'number', value: '20', step: '10' }, validity: { stepMismatch: false, valid: true } },
141
- { attributes: { type: 'number', value: '2', step: '10' }, validity: { stepMismatch: true, valid: false } },
142
- // text tooLong
143
- { attributes: { type: 'text', value: 'batman', maxLength: '1' }, validity: { tooLong: true, valid: false } },
144
- { attributes: { type: 'text', value: 'batman', maxLength: '10' }, validity: { tooLong: false, valid: true } },
145
- // text tooShort
146
- { attributes: { type: 'text', value: 'batman', minLength: '10' }, validity: { tooShort: true, valid: false } },
147
- { attributes: { type: 'text', value: 'batman', minLength: '1' }, validity: { tooShort: false, valid: true } },
148
- // url typeMismatch
149
- { attributes: { type: 'url', value: 'batman' }, validity: { typeMismatch: true, valid: false } },
150
- { attributes: { type: 'url', value: 'http://batman.com' }, validity: { typeMismatch: false, valid: true } },
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;
177
- }
178
- delete attributes.type;
179
- delete attributes.value;
180
- }
181
- // HTMLInputElemenr
182
- else {
183
- input = document.createElement('input');
184
- }
185
-
186
- setUpHTMLInputElementValidity(input as HTMLInputElement);
187
- Object.keys(attributes).forEach((attr) => {
188
- input.setAttribute(attr, attributes[attr] as string);
189
- });
190
-
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));
197
-
198
- const spyInvalidEvent = vi.spyOn(input, 'dispatchEvent');
199
- input.checkValidity();
200
-
201
- const expectedEvent = new CustomEvent('invalid', { detail: validity.valid });
202
- expect(spyInvalidEvent).toBeCalledWith(expect.objectContaining({ ...expectedEvent }));
203
- }
204
- );
205
- });
206
- });