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