@mndw/cli 0.1.6 → 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 +80 -24
- package/dist/mndw.js +382 -69
- 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.1.6.js → mndw-ui@0.2.0.js} +171 -156
- 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-B_LYyF1e.css +0 -1
- package/host-dev/assets/index-MuieOssk.js +0 -94
- package/host-dev/assets/sandbox-url-CsfwrS11.js +0 -1
- 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
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mndw/cli",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"homepage": "https://eralde.github.io/mndw/",
|
|
6
6
|
"repository": {
|
|
@@ -12,6 +12,9 @@
|
|
|
12
12
|
"bin": {
|
|
13
13
|
"mndw": "./dist/mndw.js"
|
|
14
14
|
},
|
|
15
|
+
"engines": {
|
|
16
|
+
"node": ">=22"
|
|
17
|
+
},
|
|
15
18
|
"exports": {
|
|
16
19
|
"./package.json": "./package.json"
|
|
17
20
|
},
|
|
@@ -26,12 +29,12 @@
|
|
|
26
29
|
"@types/node": "22.19.15",
|
|
27
30
|
"happy-dom": "20.11.1",
|
|
28
31
|
"vitest": "3.2.7",
|
|
29
|
-
"@mndw/ui": "^0.
|
|
30
|
-
"@mndw/ui
|
|
31
|
-
"@mndw/ui-angular": "^0.
|
|
32
|
-
"@mndw/ui-vue": "^0.
|
|
33
|
-
"@mndw/
|
|
34
|
-
"@mndw/
|
|
32
|
+
"@mndw/ui-svelte": "^0.2.0",
|
|
33
|
+
"@mndw/ui": "^0.2.0",
|
|
34
|
+
"@mndw/ui-angular": "^0.2.0",
|
|
35
|
+
"@mndw/ui-vue": "^0.2.0",
|
|
36
|
+
"@mndw/sdk-internal": "0.0.0",
|
|
37
|
+
"@mndw/angular-compiler": "2.0.0"
|
|
35
38
|
},
|
|
36
39
|
"dependencies": {
|
|
37
40
|
"@sveltejs/vite-plugin-svelte": "^5.0.0",
|
|
@@ -43,6 +46,7 @@
|
|
|
43
46
|
"@formatjs/icu-messageformat-parser": "^3.5.16",
|
|
44
47
|
"intl-messageformat": "^11.2.13",
|
|
45
48
|
"@vitejs/plugin-vue": "^6.0.0",
|
|
49
|
+
"postcss": "^8.5.23",
|
|
46
50
|
"chokidar": "^5.0.0",
|
|
47
51
|
"commander": "^15.0.0",
|
|
48
52
|
"cross-spawn": "^7.0.6",
|
|
@@ -61,15 +65,15 @@
|
|
|
61
65
|
"vite": "^6.4.3",
|
|
62
66
|
"zone.js": "^0.15.1",
|
|
63
67
|
"zod": "^4.4.3",
|
|
64
|
-
"@mndw/palette-core": "^0.
|
|
65
|
-
"@mndw/sdk": "^0.
|
|
68
|
+
"@mndw/palette-core": "^0.2.0",
|
|
69
|
+
"@mndw/sdk": "^0.2.0"
|
|
66
70
|
},
|
|
67
71
|
"optionalDependencies": {
|
|
68
|
-
"@mndw/mndwd-darwin-arm64": "0.
|
|
69
|
-
"@mndw/mndwd-
|
|
70
|
-
"@mndw/mndwd-linux-
|
|
71
|
-
"@mndw/mndwd-
|
|
72
|
-
"@mndw/mndwd-win32-x64": "0.
|
|
72
|
+
"@mndw/mndwd-darwin-arm64": "0.2.0",
|
|
73
|
+
"@mndw/mndwd-darwin-x64": "0.2.0",
|
|
74
|
+
"@mndw/mndwd-linux-arm64": "0.2.0",
|
|
75
|
+
"@mndw/mndwd-linux-x64": "0.2.0",
|
|
76
|
+
"@mndw/mndwd-win32-x64": "0.2.0"
|
|
73
77
|
},
|
|
74
78
|
"description": "Command-line tools to create, validate, develop, and package mndw plugins.",
|
|
75
79
|
"scripts": {
|
|
@@ -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
|
}
|
|
@@ -1,57 +1,57 @@
|
|
|
1
|
-
import {CUSTOM_ELEMENTS_SCHEMA, Component, InjectionToken,
|
|
2
|
-
import {toSignal} from '@angular/core/rxjs-interop';
|
|
1
|
+
import {CUSTOM_ELEMENTS_SCHEMA, Component, InjectionToken, inject, input, signal} from '@angular/core';
|
|
3
2
|
import type {PluginContext} from '@mndw/sdk';
|
|
4
3
|
import {TRANSLATOR} from '@mndw/sdk/angular';
|
|
5
|
-
import {type Column, column} from '@mndw/ui';
|
|
6
|
-
import {MndwCellDirective, MndwTableDirective} from '@mndw/ui-angular';
|
|
7
|
-
|
|
8
|
-
interface TableRow {
|
|
9
|
-
id: string;
|
|
10
|
-
name: string;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
function isTableRow(value: unknown): value is TableRow {
|
|
14
|
-
return typeof value === 'object' && value !== null && 'id' in value && 'name' in value
|
|
15
|
-
&& typeof value.id === 'string' && typeof value.name === 'string';
|
|
16
|
-
}
|
|
17
4
|
|
|
18
5
|
export const PLUGIN_CONTEXT = new InjectionToken<PluginContext>('PLUGIN_CONTEXT');
|
|
19
6
|
|
|
20
7
|
@Component({
|
|
21
8
|
selector: 'app-root',
|
|
22
9
|
standalone: true,
|
|
23
|
-
imports: [MndwTableDirective, MndwCellDirective],
|
|
24
10
|
schemas: [CUSTOM_ELEMENTS_SCHEMA],
|
|
25
11
|
template: `
|
|
26
|
-
<
|
|
27
|
-
|
|
12
|
+
<section class="__MNDW_NAME__" [attr.aria-labelledby]="isPage ? 'plugin-title' : null"
|
|
13
|
+
style="padding:var(--mndw-space-4);font-family:var(--mndw-font-sans);color:var(--mndw-text);background:var(--mndw-surface)">
|
|
14
|
+
@if (isPage) {
|
|
15
|
+
<h2 id="plugin-title">{{ translator.t('l10n.title') }}</h2>
|
|
16
|
+
}
|
|
28
17
|
<p>Device: {{ deviceName() }}</p>
|
|
29
|
-
<button
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
18
|
+
<mndw-button variant="primary" type="button" (click)="load()">
|
|
19
|
+
{{ translator.t('l10n.button') }}
|
|
20
|
+
</mndw-button>
|
|
21
|
+
<p data-testid="l10n-result" role="status" aria-live="polite">
|
|
22
|
+
@if (backendError()) {
|
|
23
|
+
{{ translator.t('l10n.error') }}
|
|
24
|
+
} @else if (backendMessage() !== undefined) {
|
|
25
|
+
{{ translator.t('l10n.result') }}: {{ backendMessage() }}
|
|
26
|
+
} @else {
|
|
27
|
+
{{ translator.t('l10n.result') }}
|
|
28
|
+
}
|
|
29
|
+
</p>
|
|
30
|
+
</section>
|
|
38
31
|
`,
|
|
39
32
|
})
|
|
40
33
|
export class AppComponent {
|
|
41
34
|
private readonly ctx = inject(PLUGIN_CONTEXT);
|
|
35
|
+
protected readonly isPage = this.ctx.kind === 'page';
|
|
42
36
|
protected readonly translator = inject(TRANSLATOR);
|
|
43
|
-
protected readonly theme = toSignal(this.ctx.theme$, {initialValue: this.ctx.theme});
|
|
44
|
-
protected readonly rows = computed<readonly TableRow[]>(() => [
|
|
45
|
-
{id: 'first', name: this.translator.t('table.first')},
|
|
46
|
-
{id: 'second', name: this.translator.t('table.second')},
|
|
47
|
-
]);
|
|
48
|
-
protected readonly columns = computed<Column<unknown>[]>(() => [
|
|
49
|
-
column<unknown>()({
|
|
50
|
-
id: 'name',
|
|
51
|
-
label: this.translator.t('table.column'),
|
|
52
|
-
accessor: (row) => isTableRow(row) ? row.name : '',
|
|
53
|
-
}),
|
|
54
|
-
]);
|
|
55
37
|
public readonly deviceName = input('');
|
|
56
|
-
protected readonly
|
|
38
|
+
protected readonly backendMessage = signal<string | undefined>(undefined);
|
|
39
|
+
protected readonly backendError = signal(false);
|
|
40
|
+
|
|
41
|
+
protected async load(): Promise<void> {
|
|
42
|
+
if (this.ctx.backend === undefined) {
|
|
43
|
+
this.backendError.set(true);
|
|
44
|
+
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
try {
|
|
49
|
+
const response = await this.ctx.backend.call<{message: string}>('hello', {as: 'json'});
|
|
50
|
+
this.backendMessage.set(response.message);
|
|
51
|
+
this.backendError.set(false);
|
|
52
|
+
} catch {
|
|
53
|
+
this.backendMessage.set(undefined);
|
|
54
|
+
this.backendError.set(true);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
57
|
}
|
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
import {reflectComponentType} from '@angular/core';
|
|
2
2
|
import type {PluginContext, PluginInstance} from '@mndw/sdk';
|
|
3
3
|
import {createAngularPluginInstance, provideTranslator} from '@mndw/sdk/angular';
|
|
4
|
+
import {bindLocale} from '@mndw/sdk/bind-locale';
|
|
4
5
|
import 'zone.js';
|
|
5
6
|
import '@angular/compiler';
|
|
6
7
|
|
|
7
8
|
import {AppComponent, PLUGIN_CONTEXT} from './app.component';
|
|
8
9
|
|
|
9
10
|
export function createInstance(el: HTMLElement, ctx: PluginContext): PluginInstance {
|
|
10
|
-
|
|
11
|
+
const instance = createAngularPluginInstance(el, {
|
|
11
12
|
rootComponent: AppComponent,
|
|
12
13
|
providers: [provideTranslator(ctx.i18n), {provide: PLUGIN_CONTEXT, useValue: ctx}],
|
|
13
14
|
configure: ({component}) => {
|
|
@@ -18,4 +19,16 @@ export function createInstance(el: HTMLElement, ctx: PluginContext): PluginInsta
|
|
|
18
19
|
}
|
|
19
20
|
},
|
|
20
21
|
});
|
|
22
|
+
const unsubscribeLocale = bindLocale(ctx.i18n, () => {
|
|
23
|
+
if (ctx.kind === 'widget') {
|
|
24
|
+
ctx.setTileTitle?.(ctx.i18n.t('l10n.title'));
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
return {
|
|
29
|
+
async unmount(): Promise<void> {
|
|
30
|
+
unsubscribeLocale();
|
|
31
|
+
await instance.unmount();
|
|
32
|
+
},
|
|
33
|
+
};
|
|
21
34
|
}
|
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
import 'zone.js';
|
|
4
4
|
import '@angular/compiler';
|
|
5
|
-
import type {PluginContext} from '@mndw/sdk';
|
|
5
|
+
import type {BackendClient, PluginContext} from '@mndw/sdk';
|
|
6
6
|
import {createTestI18n, createTestPluginContext} from '@mndw/sdk/testing';
|
|
7
|
-
import {describe, expect, it} from 'vitest';
|
|
7
|
+
import {describe, expect, it, vi} from 'vitest';
|
|
8
8
|
import {createInstance} from './index.js';
|
|
9
9
|
|
|
10
10
|
async function stabilize(): Promise<void> {
|
|
@@ -13,36 +13,69 @@ async function stabilize(): Promise<void> {
|
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
describe('generated Angular locale reactivity', () => {
|
|
16
|
-
it('updates
|
|
17
|
-
const en = {l10n: {
|
|
18
|
-
const ru = {l10n: {
|
|
16
|
+
it('updates visible control and result without remounting', async () => {
|
|
17
|
+
const en = {l10n: {title: 'Plugin title', button: 'Run English', result: 'English result', error: 'Error'}};
|
|
18
|
+
const ru = {l10n: {title: 'Заголовок', button: 'Запустить', result: 'Русский результат', error: 'Ошибка'}};
|
|
19
19
|
const i18n = createTestI18n(en, 'en');
|
|
20
20
|
const root = document.createElement('div');
|
|
21
|
-
const context = createTestPluginContext({i18n}).context;
|
|
21
|
+
const context = createTestPluginContext({i18n, kind: 'page'}).context;
|
|
22
22
|
const instance = createInstance(root, context);
|
|
23
23
|
await stabilize();
|
|
24
|
-
const
|
|
24
|
+
const title = root.querySelector('h2');
|
|
25
|
+
const button = root.querySelector('mndw-button');
|
|
26
|
+
const result = root.querySelector('[data-testid="l10n-result"]');
|
|
25
27
|
|
|
26
|
-
expect(
|
|
27
|
-
expect(
|
|
28
|
+
expect(title?.textContent).toBe(en.l10n.title);
|
|
29
|
+
expect(root.querySelector('section')?.getAttribute('aria-labelledby')).toBe(title?.id);
|
|
30
|
+
expect(button?.textContent).toContain(en.l10n.button);
|
|
31
|
+
expect(result?.textContent?.trim()).toBe(en.l10n.result);
|
|
28
32
|
i18n.setLocale('ru', ru);
|
|
29
33
|
await stabilize();
|
|
30
34
|
|
|
31
|
-
expect(
|
|
32
|
-
expect(
|
|
35
|
+
expect(button?.textContent).toContain(ru.l10n.button);
|
|
36
|
+
expect(title?.textContent).toBe(ru.l10n.title);
|
|
37
|
+
expect(result?.textContent?.trim()).toBe(ru.l10n.result);
|
|
33
38
|
instance.unmount();
|
|
34
39
|
});
|
|
35
40
|
|
|
36
|
-
it('
|
|
37
|
-
const
|
|
41
|
+
it('sets a translated host title for widgets without drawing an internal heading', async () => {
|
|
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 fixture = createTestPluginContext({i18n});
|
|
47
|
+
const context = Object.assign(Object.create(fixture.context), {setTileTitle}) as PluginContext;
|
|
38
48
|
const root = document.createElement('div');
|
|
39
|
-
const instance = createInstance(root,
|
|
49
|
+
const instance = createInstance(root, context);
|
|
40
50
|
await stabilize();
|
|
41
51
|
|
|
42
|
-
expect(
|
|
43
|
-
|
|
52
|
+
expect(setTileTitle).toHaveBeenLastCalledWith(en.l10n.title);
|
|
53
|
+
expect(root.querySelector('h2')).toBeNull();
|
|
54
|
+
expect(root.querySelector('section')?.hasAttribute('aria-labelledby')).toBe(false);
|
|
55
|
+
i18n.setLocale('ru', ru);
|
|
56
|
+
await stabilize();
|
|
57
|
+
|
|
58
|
+
expect(setTileTitle).toHaveBeenLastCalledWith(ru.l10n.title);
|
|
59
|
+
instance.unmount();
|
|
60
|
+
fixture.unmount();
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it('calls the backend when the shared button is clicked', async () => {
|
|
64
|
+
const call = vi.fn().mockResolvedValue({message: 'hello'});
|
|
65
|
+
const i18n = createTestI18n({
|
|
66
|
+
l10n: {title: 'Plugin title', button: 'Run', result: 'Backend response', error: 'Unavailable'},
|
|
67
|
+
});
|
|
68
|
+
const fixture = createTestPluginContext({i18n, backend: {call} as unknown as BackendClient});
|
|
69
|
+
const root = document.createElement('div');
|
|
70
|
+
const instance = createInstance(root, fixture.context);
|
|
44
71
|
await stabilize();
|
|
45
|
-
|
|
72
|
+
|
|
73
|
+
root.querySelector('mndw-button')?.dispatchEvent(new MouseEvent('click', {bubbles: true}));
|
|
74
|
+
|
|
75
|
+
await vi.waitFor(() => {
|
|
76
|
+
expect(call).toHaveBeenCalledWith('hello', {as: 'json'});
|
|
77
|
+
expect(root.querySelector('[data-testid="l10n-result"]')?.textContent?.trim()).toBe('Backend response: hello');
|
|
78
|
+
});
|
|
46
79
|
|
|
47
80
|
instance.unmount();
|
|
48
81
|
fixture.unmount();
|
|
@@ -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
|
}
|
|
@@ -6,41 +6,27 @@ import {createInstance} from './index.js';
|
|
|
6
6
|
import {makeStubContext} from './test-helpers.js';
|
|
7
7
|
|
|
8
8
|
describe('generated frontend', () => {
|
|
9
|
-
it('
|
|
10
|
-
const
|
|
9
|
+
it('calls hello and renders the typed message after a button click', async () => {
|
|
10
|
+
const call = vi.fn().mockResolvedValue({message: 'hello'});
|
|
11
11
|
const container = document.createElement('div');
|
|
12
|
-
const instance = createInstance(container, makeStubContext({backend: {
|
|
12
|
+
const instance = createInstance(container, makeStubContext({backend: {call}}));
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
expect(fetch).toHaveBeenCalledWith('hello');
|
|
16
|
-
expect(container.querySelector('[data-testid="l10n-result"]')?.textContent).toBe('l10n.result: hello');
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
instance.unmount();
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
it('requests hello again when the button is clicked', async () => {
|
|
23
|
-
const fetch = vi.fn().mockResolvedValue(new Response(JSON.stringify({message: 'hello', plugin: 'p'})));
|
|
24
|
-
const container = document.createElement('div');
|
|
25
|
-
const instance = createInstance(container, makeStubContext({backend: {fetch}}));
|
|
14
|
+
container.querySelector('mndw-button')?.dispatchEvent(new MouseEvent('click', {bubbles: true}));
|
|
26
15
|
|
|
27
16
|
await vi.waitFor(() => {
|
|
28
|
-
expect(
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
(container.querySelector('button') as HTMLButtonElement).click();
|
|
32
|
-
|
|
33
|
-
await vi.waitFor(() => {
|
|
34
|
-
expect(fetch).toHaveBeenCalledTimes(2);
|
|
17
|
+
expect(call).toHaveBeenCalledWith('hello', {as: 'json'});
|
|
18
|
+
expect(container.querySelector('[data-testid="l10n-result"]')?.textContent).toBe('l10n.result: hello');
|
|
35
19
|
});
|
|
36
20
|
|
|
37
21
|
instance.unmount();
|
|
38
22
|
});
|
|
39
23
|
|
|
40
24
|
it('renders a localized error when the backend request fails', async () => {
|
|
41
|
-
const
|
|
25
|
+
const call = vi.fn().mockRejectedValue(new Error('unavailable'));
|
|
42
26
|
const container = document.createElement('div');
|
|
43
|
-
const instance = createInstance(container, makeStubContext({backend: {
|
|
27
|
+
const instance = createInstance(container, makeStubContext({backend: {call}}));
|
|
28
|
+
|
|
29
|
+
container.querySelector('mndw-button')?.dispatchEvent(new MouseEvent('click', {bubbles: true}));
|
|
44
30
|
|
|
45
31
|
await vi.waitFor(() => {
|
|
46
32
|
expect(container.querySelector('[data-testid="l10n-result"]')?.textContent).toBe('l10n.error');
|
|
@@ -54,6 +40,8 @@ describe('generated frontend', () => {
|
|
|
54
40
|
const instance = createInstance(container, makeStubContext({backend: undefined}));
|
|
55
41
|
|
|
56
42
|
expect(container.querySelector('[data-testid="l10n-result"]')?.textContent).toBe('l10n.result');
|
|
43
|
+
container.querySelector('mndw-button')?.dispatchEvent(new MouseEvent('click', {bubbles: true}));
|
|
44
|
+
expect(container.querySelector('[data-testid="l10n-result"]')?.textContent).toBe('l10n.error');
|
|
57
45
|
instance.unmount();
|
|
58
46
|
});
|
|
59
47
|
});
|
|
@@ -4,18 +4,22 @@ 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 device = 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
|
let mounted = true;
|
|
21
25
|
let backendMessage: string | undefined;
|
|
@@ -23,7 +27,11 @@ export function createInstance(container: HTMLElement, ctx: PluginContext): Plug
|
|
|
23
27
|
|
|
24
28
|
const render = (): void => {
|
|
25
29
|
if (title !== null) {
|
|
26
|
-
title.textContent = ctx.i18n.t('l10n.
|
|
30
|
+
title.textContent = ctx.i18n.t('l10n.title');
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (ctx.kind === 'widget') {
|
|
34
|
+
ctx.setTileTitle?.(ctx.i18n.t('l10n.title'));
|
|
27
35
|
}
|
|
28
36
|
|
|
29
37
|
if (device !== null) {
|
|
@@ -49,14 +57,15 @@ export function createInstance(container: HTMLElement, ctx: PluginContext): Plug
|
|
|
49
57
|
|
|
50
58
|
const load = async (): Promise<void> => {
|
|
51
59
|
if (ctx.backend === undefined) {
|
|
60
|
+
backendError = true;
|
|
52
61
|
render();
|
|
62
|
+
|
|
53
63
|
return;
|
|
54
64
|
}
|
|
55
65
|
|
|
56
66
|
try {
|
|
57
|
-
const response = await ctx.backend.
|
|
58
|
-
|
|
59
|
-
backendMessage = data.message;
|
|
67
|
+
const response = await ctx.backend.call<{message: string}>('hello', {as: 'json'});
|
|
68
|
+
backendMessage = response.message;
|
|
60
69
|
backendError = false;
|
|
61
70
|
} catch {
|
|
62
71
|
backendMessage = undefined;
|
|
@@ -74,19 +83,13 @@ export function createInstance(container: HTMLElement, ctx: PluginContext): Plug
|
|
|
74
83
|
|
|
75
84
|
button?.addEventListener('click', onClick);
|
|
76
85
|
const teardown = bindLocale(ctx.i18n, render);
|
|
77
|
-
const themeSubscription = ctx.theme$.subscribe((theme) => {
|
|
78
|
-
container.dataset.theme = theme;
|
|
79
|
-
});
|
|
80
|
-
void load();
|
|
81
86
|
|
|
82
87
|
return {
|
|
83
88
|
unmount() {
|
|
84
89
|
mounted = false;
|
|
85
90
|
teardown();
|
|
86
|
-
themeSubscription.unsubscribe();
|
|
87
91
|
button?.removeEventListener('click', onClick);
|
|
88
92
|
container.innerHTML = '';
|
|
89
|
-
delete container.dataset.theme;
|
|
90
93
|
},
|
|
91
94
|
};
|
|
92
95
|
}
|
|
@@ -9,58 +9,72 @@ import {createInstance} from './index.js';
|
|
|
9
9
|
const contextFor = (
|
|
10
10
|
i18n: ReturnType<typeof createTestI18n>,
|
|
11
11
|
backend?: Partial<BackendClient>,
|
|
12
|
-
|
|
12
|
+
kind: 'widget' | 'page' = 'page',
|
|
13
|
+
setTileTitle = vi.fn(),
|
|
14
|
+
): PluginContext => {
|
|
15
|
+
const fixture = createTestPluginContext({i18n, backend: backend as BackendClient | undefined, kind});
|
|
16
|
+
|
|
17
|
+
return Object.assign(Object.create(fixture.context), {setTileTitle}) as PluginContext;
|
|
18
|
+
};
|
|
13
19
|
|
|
14
20
|
describe('generated Go locale reactivity', () => {
|
|
15
|
-
it('updates
|
|
16
|
-
const en = {l10n: {
|
|
17
|
-
const ru = {l10n: {
|
|
21
|
+
it('updates visible control and result without remounting', () => {
|
|
22
|
+
const en = {l10n: {title: 'Plugin title', button: 'Run English', result: 'English result', error: 'Error'}};
|
|
23
|
+
const ru = {l10n: {title: 'Заголовок', button: 'Запустить', result: 'Русский result', error: 'Ошибка'}};
|
|
18
24
|
const i18n = createTestI18n(en, 'en');
|
|
19
25
|
const root = document.createElement('div');
|
|
20
26
|
const instance = createInstance(root, contextFor(i18n));
|
|
21
|
-
const
|
|
22
|
-
const button = root.querySelector('button');
|
|
27
|
+
const title = root.querySelector('h2');
|
|
28
|
+
const button = root.querySelector('mndw-button');
|
|
23
29
|
const result = root.querySelector('[data-testid="l10n-result"]');
|
|
24
30
|
|
|
25
|
-
expect(
|
|
31
|
+
expect(title?.textContent).toBe(en.l10n.title);
|
|
32
|
+
expect(root.querySelector('section')?.getAttribute('aria-labelledby')).toBe(title?.id);
|
|
33
|
+
expect(button?.textContent).toBe(en.l10n.button);
|
|
34
|
+
expect(result?.textContent?.trim()).toBe(en.l10n.result);
|
|
26
35
|
i18n.setLocale('ru', ru);
|
|
27
36
|
|
|
28
|
-
expect(
|
|
29
|
-
expect(probe?.textContent).toBe(ru.l10n.probe);
|
|
37
|
+
expect(title?.textContent).toBe(ru.l10n.title);
|
|
30
38
|
expect(button?.textContent).toBe(ru.l10n.button);
|
|
31
|
-
expect(result?.textContent).toBe(ru.l10n.result);
|
|
39
|
+
expect(result?.textContent?.trim()).toBe(ru.l10n.result);
|
|
32
40
|
instance.unmount();
|
|
33
41
|
});
|
|
34
42
|
|
|
35
|
-
it('
|
|
36
|
-
const
|
|
43
|
+
it('sets a translated host title for widgets without drawing an internal heading', () => {
|
|
44
|
+
const en = {l10n: {title: 'Plugin title', button: 'Run', result: 'Result', error: 'Error'}};
|
|
45
|
+
const ru = {l10n: {title: 'Заголовок', button: 'Запустить', result: 'Результат', error: 'Ошибка'}};
|
|
46
|
+
const i18n = createTestI18n(en, 'en');
|
|
47
|
+
const setTileTitle = vi.fn();
|
|
37
48
|
const root = document.createElement('div');
|
|
38
|
-
const instance = createInstance(root,
|
|
49
|
+
const instance = createInstance(root, contextFor(i18n, undefined, 'widget', setTileTitle));
|
|
39
50
|
|
|
40
|
-
expect(
|
|
41
|
-
|
|
42
|
-
expect(root.
|
|
51
|
+
expect(setTileTitle).toHaveBeenLastCalledWith(en.l10n.title);
|
|
52
|
+
expect(root.querySelector('h2')).toBeNull();
|
|
53
|
+
expect(root.firstElementChild?.hasAttribute('aria-labelledby')).toBe(false);
|
|
54
|
+
i18n.setLocale('ru', ru);
|
|
43
55
|
|
|
56
|
+
expect(setTileTitle).toHaveBeenLastCalledWith(ru.l10n.title);
|
|
44
57
|
instance.unmount();
|
|
45
|
-
fixture.unmount();
|
|
46
58
|
});
|
|
47
59
|
|
|
48
60
|
it('updates the localized result label after a backend response', async () => {
|
|
49
|
-
const en = {l10n: {
|
|
50
|
-
const ru = {l10n: {
|
|
61
|
+
const en = {l10n: {title: 'Plugin title', button: 'Run English', result: 'English result', error: 'Error'}};
|
|
62
|
+
const ru = {l10n: {title: 'Заголовок', button: 'Запустить', result: 'Русский result', error: 'Ошибка'}};
|
|
51
63
|
const i18n = createTestI18n(en, 'en');
|
|
52
|
-
const
|
|
64
|
+
const call = vi.fn().mockResolvedValue({message: 'hello'});
|
|
53
65
|
const root = document.createElement('div');
|
|
54
|
-
const instance = createInstance(root, contextFor(i18n, {
|
|
66
|
+
const instance = createInstance(root, contextFor(i18n, {call}));
|
|
55
67
|
const result = root.querySelector('[data-testid="l10n-result"]');
|
|
56
68
|
|
|
69
|
+
root.querySelector('mndw-button')?.dispatchEvent(new MouseEvent('click', {bubbles: true}));
|
|
70
|
+
|
|
57
71
|
await vi.waitFor(() => {
|
|
58
|
-
expect(result?.textContent).toBe('English result: hello');
|
|
72
|
+
expect(result?.textContent?.trim()).toBe('English result: hello');
|
|
59
73
|
});
|
|
60
74
|
|
|
61
75
|
i18n.setLocale('ru', ru);
|
|
62
76
|
|
|
63
|
-
expect(result?.textContent).toBe('Русский result: hello');
|
|
77
|
+
expect(result?.textContent?.trim()).toBe('Русский result: hello');
|
|
64
78
|
instance.unmount();
|
|
65
79
|
});
|
|
66
80
|
});
|
|
@@ -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
|
}
|