@mndw/cli 0.1.5 → 0.2.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/dist/meta.json +83 -27
- package/dist/mndw.js +391 -101
- package/host-dev/assets/index-BqQCCILo.css +1 -0
- package/host-dev/assets/index-DFVRx7na.js +94 -0
- package/host-dev/assets/sandbox-url-D8HO7UBF.js +1 -0
- package/host-dev/index.html +3 -3
- package/host-dev/shared/mndw-ui@0.2.0.js +1805 -0
- package/package.json +18 -14
- package/templates/angular/i18n/en.json +4 -9
- package/templates/angular/i18n/ru.json +4 -9
- package/templates/angular/src/app.component.ts +39 -39
- package/templates/angular/src/index.ts +14 -1
- package/templates/angular/src/l10n.test.ts +50 -17
- package/templates/go/i18n/en.json +1 -1
- package/templates/go/i18n/ru.json +1 -1
- package/templates/go/src/index.css +4 -20
- package/templates/go/src/index.test.ts +12 -24
- package/templates/go/src/index.ts +18 -15
- package/templates/go/src/l10n.test.ts +37 -23
- package/templates/svelte/i18n/en.json +4 -9
- package/templates/svelte/i18n/ru.json +4 -9
- package/templates/svelte/src/App.svelte +46 -72
- package/templates/svelte/src/index.ts +7 -0
- package/templates/svelte/src/l10n.test.ts +64 -17
- package/templates/svelte/svelte.config.js +7 -0
- package/templates/svelte/vitest.config.ts +2 -1
- package/templates/vanilla/i18n/en.json +4 -3
- package/templates/vanilla/i18n/ru.json +4 -3
- package/templates/vanilla/src/index.css +4 -20
- package/templates/vanilla/src/index.ts +47 -17
- package/templates/vanilla/src/l10n.test.ts +50 -17
- package/templates/vue/i18n/en.json +4 -9
- package/templates/vue/i18n/ru.json +4 -9
- package/templates/vue/src/App.vue +36 -54
- package/templates/vue/src/index.ts +7 -0
- package/templates/vue/src/l10n.test.ts +50 -17
- package/host-dev/assets/index-CJ80zEWN.js +0 -94
- package/host-dev/assets/index-DNxACGUr.css +0 -1
- package/host-dev/assets/sandbox-url-Dk7HpxDp.js +0 -1
- package/host-dev/shared/mndw-ui@0.1.5.js +0 -1782
- package/templates/angular/src/table.test.ts +0 -59
- package/templates/svelte/src/TableNameCell.svelte +0 -10
- package/templates/svelte/src/table.test.ts +0 -53
- package/templates/vue/src/TableNameCell.vue +0 -12
- package/templates/vue/src/table.test.ts +0 -53
|
@@ -1,13 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"l10n": {
|
|
3
|
-
"
|
|
4
|
-
"button": "
|
|
5
|
-
"result": "
|
|
6
|
-
|
|
7
|
-
"table": {
|
|
8
|
-
"caption": "Таблица-пример",
|
|
9
|
-
"column": "Название",
|
|
10
|
-
"first": "Первая строка",
|
|
11
|
-
"second": "Вторая строка"
|
|
3
|
+
"title": "Начало работы с плагином",
|
|
4
|
+
"button": "Вызвать сервер",
|
|
5
|
+
"result": "Ответ сервера",
|
|
6
|
+
"error": "Сервер недоступен"
|
|
12
7
|
}
|
|
13
8
|
}
|
|
@@ -1,102 +1,76 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
2
|
import type {PluginContext} from '@mndw/sdk';
|
|
3
|
-
import {type MndwTableProps, column} from '@mndw/ui';
|
|
4
3
|
import {createTranslator} from '@mndw/sdk/svelte';
|
|
5
|
-
import {svelteCell} from '@mndw/ui-svelte';
|
|
6
|
-
import TableNameCell from './TableNameCell.svelte';
|
|
7
|
-
|
|
8
|
-
interface TableRow {
|
|
9
|
-
id: string;
|
|
10
|
-
name: string;
|
|
11
|
-
}
|
|
12
4
|
|
|
13
5
|
let {ctx}: {ctx: PluginContext} = $props();
|
|
14
6
|
const translator = $derived(createTranslator(ctx.i18n));
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
column<TableRow>()({
|
|
22
|
-
id: 'name',
|
|
23
|
-
label: $translator.t('table.column'),
|
|
24
|
-
accessor: (row) => row.name,
|
|
25
|
-
renderer: svelteCell<TableRow, string>()(TableNameCell),
|
|
26
|
-
}),
|
|
27
|
-
]);
|
|
28
|
-
let table: (HTMLElement & MndwTableProps<TableRow>) | undefined = $state();
|
|
7
|
+
let backendMessage = $state<string | undefined>();
|
|
8
|
+
let backendError = $state(false);
|
|
9
|
+
|
|
10
|
+
const load = async (): Promise<void> => {
|
|
11
|
+
if (ctx.backend === undefined) {
|
|
12
|
+
backendError = true;
|
|
29
13
|
|
|
30
|
-
$effect(() => {
|
|
31
|
-
if (table === undefined) {
|
|
32
14
|
return;
|
|
33
15
|
}
|
|
34
16
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
17
|
+
try {
|
|
18
|
+
const response = await ctx.backend.call<{message: string}>('hello', {
|
|
19
|
+
as: 'json',
|
|
20
|
+
});
|
|
21
|
+
backendMessage = response.message;
|
|
22
|
+
backendError = false;
|
|
23
|
+
} catch {
|
|
24
|
+
backendMessage = undefined;
|
|
25
|
+
backendError = true;
|
|
26
|
+
}
|
|
27
|
+
};
|
|
45
28
|
</script>
|
|
46
29
|
|
|
47
|
-
<
|
|
48
|
-
class="
|
|
49
|
-
|
|
30
|
+
<section
|
|
31
|
+
class="__MNDW_NAME__"
|
|
32
|
+
aria-labelledby={ctx.kind === 'page' ? 'plugin-title' : undefined}
|
|
50
33
|
>
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
data-plugin-label="__MNDW_LABEL__"
|
|
55
|
-
>
|
|
56
|
-
{$translator.t('l10n.probe')}
|
|
57
|
-
</h3>
|
|
34
|
+
{#if ctx.kind === 'page'}
|
|
35
|
+
<h2 id="plugin-title">{$translator.t('l10n.title')}</h2>
|
|
36
|
+
{/if}
|
|
58
37
|
<p class="device">Device: {ctx.deviceInfo.deviceName}</p>
|
|
59
|
-
<button
|
|
60
|
-
|
|
38
|
+
<mndw-button
|
|
39
|
+
variant="primary"
|
|
61
40
|
type="button"
|
|
62
|
-
onclick={() =>
|
|
41
|
+
onclick={() => void load()}
|
|
63
42
|
>
|
|
64
|
-
{$translator.t('l10n.button')}
|
|
65
|
-
</button>
|
|
66
|
-
<p
|
|
67
|
-
|
|
68
|
-
|
|
43
|
+
{$translator.t('l10n.button')}
|
|
44
|
+
</mndw-button>
|
|
45
|
+
<p
|
|
46
|
+
class="result"
|
|
47
|
+
role={backendError ? 'alert' : 'status'}
|
|
48
|
+
aria-live="polite"
|
|
49
|
+
>
|
|
50
|
+
{#if backendError}
|
|
51
|
+
{$translator.t('l10n.error')}
|
|
52
|
+
{:else if backendMessage !== undefined}
|
|
53
|
+
{$translator.t('l10n.result')}: {backendMessage}
|
|
54
|
+
{:else}
|
|
55
|
+
{$translator.t('l10n.result')}
|
|
56
|
+
{/if}
|
|
57
|
+
</p>
|
|
58
|
+
</section>
|
|
69
59
|
|
|
70
60
|
<style>
|
|
71
|
-
.
|
|
61
|
+
.__MNDW_NAME__ {
|
|
72
62
|
padding: var(--mndw-space-4);
|
|
73
63
|
font-family: var(--mndw-font-sans);
|
|
74
64
|
color: var(--mndw-text);
|
|
75
65
|
background: var(--mndw-surface);
|
|
76
66
|
}
|
|
77
67
|
|
|
78
|
-
.
|
|
79
|
-
margin: 0 0 var(--mndw-space-2);
|
|
80
|
-
font-size: var(--mndw-text-lg);
|
|
81
|
-
font-weight: var(--mndw-weight-semibold);
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
.device {
|
|
68
|
+
.__MNDW_NAME__ .device {
|
|
85
69
|
margin: 0 0 var(--mndw-space-3);
|
|
86
70
|
color: var(--mndw-text-muted);
|
|
87
71
|
}
|
|
88
72
|
|
|
89
|
-
.
|
|
90
|
-
|
|
91
|
-
font: inherit;
|
|
92
|
-
color: var(--mndw-accent-contrast);
|
|
93
|
-
background: var(--mndw-accent);
|
|
94
|
-
border: none;
|
|
95
|
-
border-radius: var(--mndw-radius-md);
|
|
96
|
-
cursor: pointer;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
.count:hover {
|
|
100
|
-
background: var(--mndw-accent-hover);
|
|
73
|
+
.__MNDW_NAME__ .result {
|
|
74
|
+
margin-block: var(--mndw-space-3) 0;
|
|
101
75
|
}
|
|
102
76
|
</style>
|
|
@@ -1,13 +1,20 @@
|
|
|
1
1
|
import {mount, unmount} from 'svelte';
|
|
2
2
|
import type {PluginContext, PluginInstance} from '@mndw/sdk';
|
|
3
|
+
import {bindLocale} from '@mndw/sdk/bind-locale';
|
|
3
4
|
|
|
4
5
|
import App from './App.svelte';
|
|
5
6
|
|
|
6
7
|
export function createInstance(container: HTMLElement, ctx: PluginContext): PluginInstance {
|
|
7
8
|
const app = mount(App, {target: container, props: {ctx}});
|
|
9
|
+
const unsubscribeLocale = bindLocale(ctx.i18n, () => {
|
|
10
|
+
if (ctx.kind === 'widget') {
|
|
11
|
+
ctx.setTileTitle?.(ctx.i18n.t('l10n.title'));
|
|
12
|
+
}
|
|
13
|
+
});
|
|
8
14
|
|
|
9
15
|
return {
|
|
10
16
|
unmount() {
|
|
17
|
+
unsubscribeLocale();
|
|
11
18
|
void unmount(app);
|
|
12
19
|
},
|
|
13
20
|
};
|
|
@@ -1,45 +1,92 @@
|
|
|
1
1
|
// @vitest-environment happy-dom
|
|
2
2
|
|
|
3
3
|
import {flushSync, mount, unmount} from 'svelte';
|
|
4
|
-
import type {PluginContext} from '@mndw/sdk';
|
|
4
|
+
import type {BackendClient, PluginContext} from '@mndw/sdk';
|
|
5
5
|
import {createTestI18n, createTestPluginContext} from '@mndw/sdk/testing';
|
|
6
|
-
import {describe, expect, it} from 'vitest';
|
|
6
|
+
import {describe, expect, it, vi} from 'vitest';
|
|
7
7
|
import App from './App.svelte';
|
|
8
|
+
import {createInstance} from './index.js';
|
|
8
9
|
|
|
9
|
-
const contextFor = (i18n: ReturnType<typeof createTestI18n>): PluginContext =>
|
|
10
|
+
const contextFor = (i18n: ReturnType<typeof createTestI18n>): PluginContext =>
|
|
11
|
+
createTestPluginContext({i18n, kind: 'page'}).context;
|
|
10
12
|
|
|
11
13
|
describe('generated Svelte locale reactivity', () => {
|
|
12
|
-
it('updates
|
|
13
|
-
const en = {l10n: {
|
|
14
|
-
const ru = {l10n: {
|
|
14
|
+
it('updates visible control and result without remounting', () => {
|
|
15
|
+
const en = {l10n: {title: 'Plugin title', button: 'Run English', result: 'English result', error: 'Error'}};
|
|
16
|
+
const ru = {l10n: {title: 'Заголовок', button: 'Запустить', result: 'Русский результат', error: 'Ошибка'}};
|
|
15
17
|
const i18n = createTestI18n(en, 'en');
|
|
16
18
|
const root = document.createElement('div');
|
|
17
19
|
const component = mount(App, {target: root, props: {ctx: contextFor(i18n)}});
|
|
18
20
|
flushSync();
|
|
19
|
-
const
|
|
21
|
+
const title = root.querySelector('h2');
|
|
22
|
+
const button = root.querySelector('mndw-button');
|
|
23
|
+
const result = root.querySelector('.result');
|
|
20
24
|
|
|
21
|
-
expect(
|
|
22
|
-
expect(
|
|
25
|
+
expect(title?.textContent).toBe(en.l10n.title);
|
|
26
|
+
expect(root.firstElementChild?.getAttribute('aria-labelledby')).toBe(title?.id);
|
|
27
|
+
expect(button?.textContent).toContain(en.l10n.button);
|
|
28
|
+
expect(result?.textContent).toBe(en.l10n.result);
|
|
23
29
|
i18n.setLocale('ru', ru);
|
|
24
30
|
flushSync();
|
|
25
31
|
|
|
26
|
-
expect(
|
|
27
|
-
expect(
|
|
32
|
+
expect(title?.textContent).toBe(ru.l10n.title);
|
|
33
|
+
expect(button?.textContent).toContain(ru.l10n.button);
|
|
34
|
+
expect(result?.textContent).toBe(ru.l10n.result);
|
|
28
35
|
unmount(component);
|
|
29
36
|
});
|
|
30
37
|
|
|
31
|
-
it('
|
|
32
|
-
const
|
|
38
|
+
it('sets a translated host title for widgets without drawing an internal heading', () => {
|
|
39
|
+
const en = {l10n: {title: 'Plugin title', button: 'Run', result: 'Result', error: 'Error'}};
|
|
40
|
+
const ru = {l10n: {title: 'Заголовок', button: 'Запустить', result: 'Результат', error: 'Ошибка'}};
|
|
41
|
+
const i18n = createTestI18n(en, 'en');
|
|
42
|
+
const setTileTitle = vi.fn();
|
|
43
|
+
const fixture = createTestPluginContext({i18n});
|
|
44
|
+
const context = Object.assign(Object.create(fixture.context), {setTileTitle}) as PluginContext;
|
|
33
45
|
const root = document.createElement('div');
|
|
34
|
-
const
|
|
46
|
+
const instance = createInstance(root, context);
|
|
47
|
+
flushSync();
|
|
48
|
+
|
|
49
|
+
expect(setTileTitle).toHaveBeenLastCalledWith(en.l10n.title);
|
|
50
|
+
expect(root.querySelector('h2')).toBeNull();
|
|
51
|
+
expect(root.firstElementChild?.hasAttribute('aria-labelledby')).toBe(false);
|
|
52
|
+
i18n.setLocale('ru', ru);
|
|
35
53
|
flushSync();
|
|
36
54
|
|
|
37
|
-
expect(
|
|
38
|
-
|
|
55
|
+
expect(setTileTitle).toHaveBeenLastCalledWith(ru.l10n.title);
|
|
56
|
+
instance.unmount();
|
|
57
|
+
fixture.unmount();
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('calls the backend when the shared button is clicked', async () => {
|
|
61
|
+
const call = vi.fn().mockResolvedValue({message: 'hello'});
|
|
62
|
+
const i18n = createTestI18n({
|
|
63
|
+
l10n: {title: 'Plugin title', button: 'Run', result: 'Backend response', error: 'Unavailable'},
|
|
64
|
+
});
|
|
65
|
+
const fixture = createTestPluginContext({i18n, backend: {call} as unknown as BackendClient});
|
|
66
|
+
const root = document.createElement('div');
|
|
67
|
+
const component = mount(App, {target: root, props: {ctx: fixture.context}});
|
|
39
68
|
flushSync();
|
|
40
|
-
|
|
69
|
+
|
|
70
|
+
root.querySelector('mndw-button')?.dispatchEvent(new MouseEvent('click', {bubbles: true}));
|
|
71
|
+
|
|
72
|
+
await vi.waitFor(() => {
|
|
73
|
+
expect(call).toHaveBeenCalledWith('hello', {as: 'json'});
|
|
74
|
+
expect(root.querySelector('.result')?.textContent).toBe('Backend response: hello');
|
|
75
|
+
});
|
|
41
76
|
|
|
42
77
|
unmount(component);
|
|
43
78
|
fixture.unmount();
|
|
44
79
|
});
|
|
80
|
+
|
|
81
|
+
it('shows the localized fallback when no backend is configured', () => {
|
|
82
|
+
const i18n = createTestI18n({l10n: {title: 'Plugin', button: 'Run', result: 'Result', error: 'Unavailable'}});
|
|
83
|
+
const root = document.createElement('div');
|
|
84
|
+
const component = mount(App, {target: root, props: {ctx: contextFor(i18n)}});
|
|
85
|
+
flushSync();
|
|
86
|
+
root.querySelector('mndw-button')?.dispatchEvent(new MouseEvent('click', {bubbles: true}));
|
|
87
|
+
flushSync();
|
|
88
|
+
|
|
89
|
+
expect(root.querySelector('.result')?.textContent).toBe('Unavailable');
|
|
90
|
+
unmount(component);
|
|
91
|
+
});
|
|
45
92
|
});
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import {svelte} from '@sveltejs/vite-plugin-svelte';
|
|
2
2
|
import {defineConfig} from 'vitest/config';
|
|
3
|
+
import {mndwSvelteWarningFilter} from '@mndw/sdk/svelte';
|
|
3
4
|
|
|
4
5
|
export default defineConfig({
|
|
5
|
-
plugins: [svelte({hot: false})],
|
|
6
|
+
plugins: [svelte({hot: false, compilerOptions: {warningFilter: mndwSvelteWarningFilter}})],
|
|
6
7
|
resolve: {
|
|
7
8
|
conditions: ['browser'],
|
|
8
9
|
},
|
|
@@ -1,31 +1,15 @@
|
|
|
1
|
-
.
|
|
1
|
+
.__MNDW_NAME__ {
|
|
2
2
|
padding: var(--mndw-space-4);
|
|
3
3
|
background: var(--mndw-surface);
|
|
4
4
|
color: var(--mndw-text);
|
|
5
5
|
font-family: var(--mndw-font-sans);
|
|
6
6
|
}
|
|
7
7
|
|
|
8
|
-
.mndw-
|
|
9
|
-
margin: 0 0 var(--mndw-space-2);
|
|
10
|
-
font-weight: var(--mndw-weight-semibold);
|
|
11
|
-
font-size: var(--mndw-text-lg);
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
.mndw-__MNDW_NAME____device {
|
|
8
|
+
.__MNDW_NAME__ .mndw-__MNDW_NAME____device {
|
|
15
9
|
margin: 0 0 var(--mndw-space-3);
|
|
16
10
|
color: var(--mndw-text-muted);
|
|
17
11
|
}
|
|
18
12
|
|
|
19
|
-
.mndw-
|
|
20
|
-
|
|
21
|
-
border: none;
|
|
22
|
-
border-radius: var(--mndw-radius-md);
|
|
23
|
-
background: var(--mndw-accent);
|
|
24
|
-
color: var(--mndw-accent-contrast);
|
|
25
|
-
font: inherit;
|
|
26
|
-
cursor: pointer;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
.mndw-__MNDW_NAME____count:hover {
|
|
30
|
-
background: var(--mndw-accent-hover);
|
|
13
|
+
.__MNDW_NAME__ mndw-button {
|
|
14
|
+
margin-block: var(--mndw-space-3);
|
|
31
15
|
}
|
|
@@ -4,25 +4,35 @@ import {bindLocale} from '@mndw/sdk/bind-locale';
|
|
|
4
4
|
import './index.css';
|
|
5
5
|
|
|
6
6
|
export function createInstance(container: HTMLElement, ctx: PluginContext): PluginInstance {
|
|
7
|
+
const rootTag = ctx.kind === 'page' ? 'section' : 'div';
|
|
8
|
+
const heading = ctx.kind === 'page' ? '<h2 id="plugin-title" class="mndw-__MNDW_NAME____title"></h2>' : '';
|
|
9
|
+
const labelledBy = ctx.kind === 'page' ? 'aria-labelledby="plugin-title"' : '';
|
|
10
|
+
|
|
7
11
|
container.innerHTML = `
|
|
8
|
-
|
|
9
|
-
|
|
12
|
+
<${rootTag} class="__MNDW_NAME__" ${labelledBy}>
|
|
13
|
+
${heading}
|
|
10
14
|
<p class="mndw-__MNDW_NAME____device">Device: <span class="mndw-__MNDW_NAME____device-name"></span></p>
|
|
11
|
-
<button class="mndw-
|
|
15
|
+
<mndw-button class="mndw-__MNDW_NAME____button" variant="primary" type="button"></mndw-button>
|
|
12
16
|
<p class="mndw-__MNDW_NAME____result" data-testid="l10n-result"></p>
|
|
13
|
-
|
|
17
|
+
</${rootTag}>
|
|
14
18
|
`;
|
|
15
19
|
|
|
16
20
|
const title = container.querySelector('.mndw-__MNDW_NAME____title');
|
|
17
21
|
const deviceEl = container.querySelector('.mndw-__MNDW_NAME____device-name');
|
|
18
|
-
const button = container.querySelector('.mndw-
|
|
22
|
+
const button = container.querySelector('.mndw-__MNDW_NAME____button');
|
|
19
23
|
const result = container.querySelector('.mndw-__MNDW_NAME____result');
|
|
20
24
|
|
|
21
|
-
let
|
|
25
|
+
let backendMessage: string | undefined;
|
|
26
|
+
let backendError = false;
|
|
27
|
+
let mounted = true;
|
|
22
28
|
|
|
23
29
|
const render = (): void => {
|
|
24
30
|
if (title !== null) {
|
|
25
|
-
title.textContent = ctx.i18n.t('l10n.
|
|
31
|
+
title.textContent = ctx.i18n.t('l10n.title');
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (ctx.kind === 'widget') {
|
|
35
|
+
ctx.setTileTitle?.(ctx.i18n.t('l10n.title'));
|
|
26
36
|
}
|
|
27
37
|
|
|
28
38
|
if (deviceEl !== null) {
|
|
@@ -34,32 +44,52 @@ export function createInstance(container: HTMLElement, ctx: PluginContext): Plug
|
|
|
34
44
|
}
|
|
35
45
|
|
|
36
46
|
if (result !== null) {
|
|
37
|
-
|
|
47
|
+
if (backendError) {
|
|
48
|
+
result.textContent = ctx.i18n.t('l10n.error');
|
|
49
|
+
} else if (backendMessage !== undefined) {
|
|
50
|
+
result.textContent = `${ctx.i18n.t('l10n.result')}: ${backendMessage}`;
|
|
51
|
+
} else {
|
|
52
|
+
result.textContent = ctx.i18n.t('l10n.result');
|
|
53
|
+
}
|
|
38
54
|
}
|
|
39
55
|
};
|
|
40
56
|
|
|
41
|
-
const
|
|
42
|
-
|
|
57
|
+
const load = async (): Promise<void> => {
|
|
58
|
+
if (ctx.backend === undefined) {
|
|
59
|
+
backendError = true;
|
|
60
|
+
render();
|
|
43
61
|
|
|
44
|
-
|
|
45
|
-
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
try {
|
|
66
|
+
const response = await ctx.backend.call<{message: string}>('hello', {as: 'json'});
|
|
67
|
+
backendMessage = response.message;
|
|
68
|
+
backendError = false;
|
|
69
|
+
} catch {
|
|
70
|
+
backendMessage = undefined;
|
|
71
|
+
backendError = true;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (mounted) {
|
|
75
|
+
render();
|
|
46
76
|
}
|
|
47
77
|
};
|
|
48
78
|
|
|
79
|
+
const onClick = (): void => {
|
|
80
|
+
void load();
|
|
81
|
+
};
|
|
82
|
+
|
|
49
83
|
button?.addEventListener('click', onClick);
|
|
50
84
|
const teardown = bindLocale(ctx.i18n, render);
|
|
51
|
-
const themeSubscription = ctx.theme$.subscribe((theme) => {
|
|
52
|
-
container.dataset.theme = theme;
|
|
53
|
-
});
|
|
54
85
|
|
|
55
86
|
return {
|
|
56
87
|
unmount() {
|
|
88
|
+
mounted = false;
|
|
57
89
|
teardown();
|
|
58
|
-
themeSubscription.unsubscribe();
|
|
59
90
|
button?.removeEventListener('click', onClick);
|
|
60
91
|
|
|
61
92
|
container.innerHTML = '';
|
|
62
|
-
delete container.dataset.theme;
|
|
63
93
|
},
|
|
64
94
|
};
|
|
65
95
|
}
|
|
@@ -1,42 +1,75 @@
|
|
|
1
1
|
// @vitest-environment happy-dom
|
|
2
2
|
|
|
3
|
-
import type {PluginContext} from '@mndw/sdk';
|
|
3
|
+
import type {BackendClient, PluginContext} from '@mndw/sdk';
|
|
4
4
|
import {createTestI18n, createTestPluginContext} from '@mndw/sdk/testing';
|
|
5
|
-
import {describe, expect, it} from 'vitest';
|
|
5
|
+
import {describe, expect, it, vi} from 'vitest';
|
|
6
6
|
import {createInstance} from './index.js';
|
|
7
7
|
|
|
8
|
-
const contextFor = (
|
|
8
|
+
const contextFor = (
|
|
9
|
+
i18n: ReturnType<typeof createTestI18n>,
|
|
10
|
+
kind: 'widget' | 'page' = 'page',
|
|
11
|
+
setTileTitle = vi.fn(),
|
|
12
|
+
): PluginContext => {
|
|
13
|
+
const fixture = createTestPluginContext({i18n, kind});
|
|
14
|
+
|
|
15
|
+
return Object.assign(Object.create(fixture.context), {setTileTitle}) as PluginContext;
|
|
16
|
+
};
|
|
9
17
|
|
|
10
18
|
describe('generated vanilla locale reactivity', () => {
|
|
11
|
-
it('updates
|
|
12
|
-
const en = {l10n: {
|
|
13
|
-
const ru = {l10n: {
|
|
19
|
+
it('updates visible control and result without remounting', () => {
|
|
20
|
+
const en = {l10n: {title: 'Plugin title', button: 'Run English', result: 'English result', error: 'Error'}};
|
|
21
|
+
const ru = {l10n: {title: 'Заголовок', button: 'Запустить', result: 'Русский результат', error: 'Ошибка'}};
|
|
14
22
|
const i18n = createTestI18n(en, 'en');
|
|
15
23
|
const root = document.createElement('div');
|
|
16
24
|
const instance = createInstance(root, contextFor(i18n));
|
|
17
|
-
const
|
|
18
|
-
const button = root.querySelector('button');
|
|
25
|
+
const title = root.querySelector('h2');
|
|
26
|
+
const button = root.querySelector('mndw-button');
|
|
19
27
|
const result = root.querySelector('[data-testid="l10n-result"]');
|
|
20
28
|
|
|
21
|
-
expect(
|
|
22
|
-
expect(
|
|
29
|
+
expect(title?.textContent).toBe(en.l10n.title);
|
|
30
|
+
expect(root.querySelector('section')?.getAttribute('aria-labelledby')).toBe(title?.id);
|
|
31
|
+
expect(button?.textContent).toBe(en.l10n.button);
|
|
32
|
+
expect(result?.textContent).toBe(en.l10n.result);
|
|
23
33
|
i18n.setLocale('ru', ru);
|
|
24
34
|
|
|
25
|
-
expect(
|
|
26
|
-
expect(probe?.textContent).toBe(ru.l10n.probe);
|
|
35
|
+
expect(title?.textContent).toBe(ru.l10n.title);
|
|
27
36
|
expect(button?.textContent).toBe(ru.l10n.button);
|
|
28
37
|
expect(result?.textContent).toBe(ru.l10n.result);
|
|
29
38
|
instance.unmount();
|
|
30
39
|
});
|
|
31
40
|
|
|
32
|
-
it('
|
|
33
|
-
const
|
|
41
|
+
it('sets a translated host title for widgets without drawing an internal heading', () => {
|
|
42
|
+
const en = {l10n: {title: 'Plugin title', button: 'Run', result: 'Result', error: 'Error'}};
|
|
43
|
+
const ru = {l10n: {title: 'Заголовок', button: 'Запустить', result: 'Результат', error: 'Ошибка'}};
|
|
44
|
+
const i18n = createTestI18n(en, 'en');
|
|
45
|
+
const setTileTitle = vi.fn();
|
|
46
|
+
const root = document.createElement('div');
|
|
47
|
+
const instance = createInstance(root, contextFor(i18n, 'widget', setTileTitle));
|
|
48
|
+
|
|
49
|
+
expect(setTileTitle).toHaveBeenLastCalledWith(en.l10n.title);
|
|
50
|
+
expect(root.querySelector('h2')).toBeNull();
|
|
51
|
+
expect(root.firstElementChild?.hasAttribute('aria-labelledby')).toBe(false);
|
|
52
|
+
i18n.setLocale('ru', ru);
|
|
53
|
+
|
|
54
|
+
expect(setTileTitle).toHaveBeenLastCalledWith(ru.l10n.title);
|
|
55
|
+
instance.unmount();
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('calls the backend when the shared button is clicked', async () => {
|
|
59
|
+
const call = vi.fn().mockResolvedValue({message: 'hello'});
|
|
60
|
+
const i18n = createTestI18n({
|
|
61
|
+
l10n: {title: 'Plugin title', button: 'Run', result: 'Backend response', error: 'Unavailable'},
|
|
62
|
+
});
|
|
63
|
+
const fixture = createTestPluginContext({i18n, backend: {call} as unknown as BackendClient});
|
|
34
64
|
const root = document.createElement('div');
|
|
35
65
|
const instance = createInstance(root, fixture.context);
|
|
36
66
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
67
|
+
root.querySelector('mndw-button')?.dispatchEvent(new MouseEvent('click', {bubbles: true}));
|
|
68
|
+
|
|
69
|
+
await vi.waitFor(() => {
|
|
70
|
+
expect(call).toHaveBeenCalledWith('hello', {as: 'json'});
|
|
71
|
+
expect(root.querySelector('[data-testid="l10n-result"]')?.textContent).toBe('Backend response: hello');
|
|
72
|
+
});
|
|
40
73
|
|
|
41
74
|
instance.unmount();
|
|
42
75
|
fixture.unmount();
|
|
@@ -1,13 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"l10n": {
|
|
3
|
-
"
|
|
4
|
-
"button": "
|
|
5
|
-
"result": "
|
|
6
|
-
|
|
7
|
-
"table": {
|
|
8
|
-
"caption": "Starter table",
|
|
9
|
-
"column": "Name",
|
|
10
|
-
"first": "First row",
|
|
11
|
-
"second": "Second row"
|
|
3
|
+
"title": "Plugin starter",
|
|
4
|
+
"button": "Call backend",
|
|
5
|
+
"result": "Backend response",
|
|
6
|
+
"error": "Backend unavailable"
|
|
12
7
|
}
|
|
13
8
|
}
|
|
@@ -1,13 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"l10n": {
|
|
3
|
-
"
|
|
4
|
-
"button": "
|
|
5
|
-
"result": "
|
|
6
|
-
|
|
7
|
-
"table": {
|
|
8
|
-
"caption": "Таблица-пример",
|
|
9
|
-
"column": "Название",
|
|
10
|
-
"first": "Первая строка",
|
|
11
|
-
"second": "Вторая строка"
|
|
3
|
+
"title": "Начало работы с плагином",
|
|
4
|
+
"button": "Вызвать сервер",
|
|
5
|
+
"result": "Ответ сервера",
|
|
6
|
+
"error": "Сервер недоступен"
|
|
12
7
|
}
|
|
13
8
|
}
|