@tak-ps/vue-tabler 5.5.0 → 5.6.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/.github/workflows/test.yml +9 -1
- package/CHANGELOG.md +7 -0
- package/README.md +3 -0
- package/components/Schema.vue +147 -3
- package/package.json +3 -1
- package/test/Schema.spec.ts +354 -0
- package/vitest.config.mjs +5 -0
|
@@ -36,4 +36,12 @@ jobs:
|
|
|
36
36
|
run: npm run lint
|
|
37
37
|
|
|
38
38
|
- name: Test
|
|
39
|
-
run: npm test
|
|
39
|
+
run: npm run test:coverage
|
|
40
|
+
|
|
41
|
+
- name: Upload coverage
|
|
42
|
+
if: matrix.node-version == 24
|
|
43
|
+
uses: codecov/codecov-action@v5
|
|
44
|
+
with:
|
|
45
|
+
token: ${{ secrets.CODECOV_TOKEN }}
|
|
46
|
+
files: coverage/lcov.info
|
|
47
|
+
fail_ci_if_error: false
|
package/CHANGELOG.md
CHANGED
|
@@ -12,6 +12,13 @@
|
|
|
12
12
|
|
|
13
13
|
### Pending
|
|
14
14
|
|
|
15
|
+
### v5.6.0 - 2026-09-15
|
|
16
|
+
|
|
17
|
+
- :white_check_mark: Add `TablerSchema` tests for editing every primitive input type and for default, const and nested default seeding across model and schema changes
|
|
18
|
+
- :white_check_mark: Collect v8 test coverage with `npm run test:coverage` and upload it to Codecov from the Test workflow
|
|
19
|
+
- :tada: Render a `type: object` property in `TablerSchema` as a nested form instead of a raw textarea
|
|
20
|
+
- :tada: Render an `anyOf`/`oneOf` of object shapes in `TablerSchema` with a variant picker, seeding `const` discriminators and resetting the value when the variant changes
|
|
21
|
+
|
|
15
22
|
### v5.5.0
|
|
16
23
|
|
|
17
24
|
- :tada: Render an array of objects in `TablerSchema` as a table with a row editor modal, CSV import and clear, matching the CloudTAK Layer Environment form
|
package/README.md
CHANGED
|
@@ -10,6 +10,9 @@
|
|
|
10
10
|
<a href="https://github.com/tak-ps/vue-tabler/blob/main/LICENSE">
|
|
11
11
|
<img src="https://img.shields.io/npm/l/@tak-ps/vue-tabler.svg" alt="License">
|
|
12
12
|
</a>
|
|
13
|
+
<a href="https://codecov.io/gh/dfpc-coe/vue-tabler">
|
|
14
|
+
<img src="https://codecov.io/gh/dfpc-coe/vue-tabler/graph/badge.svg" alt="Coverage">
|
|
15
|
+
</a>
|
|
13
16
|
</p>
|
|
14
17
|
|
|
15
18
|
<p align="center">
|
package/components/Schema.vue
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
<TablerLoading v-if='loading' />
|
|
4
4
|
<template v-else>
|
|
5
5
|
<div
|
|
6
|
-
v-for='key in
|
|
6
|
+
v-for='key in visibleKeys'
|
|
7
7
|
:key='key'
|
|
8
8
|
class='py-2 floating-input'
|
|
9
9
|
>
|
|
@@ -138,6 +138,46 @@
|
|
|
138
138
|
@create='openRow(key)'
|
|
139
139
|
/>
|
|
140
140
|
</template>
|
|
141
|
+
<template v-else-if='isUnion(s.properties[key])'>
|
|
142
|
+
<TablerEnum
|
|
143
|
+
:model-value='variantLabels(key)[variant[key] || 0]'
|
|
144
|
+
:label='key'
|
|
145
|
+
:disabled='disabled'
|
|
146
|
+
:required='s.properties[key].required || false'
|
|
147
|
+
:description='s.properties[key].description || ""'
|
|
148
|
+
:options='variantLabels(key)'
|
|
149
|
+
@update:model-value='selectVariant(key, $event)'
|
|
150
|
+
/>
|
|
151
|
+
|
|
152
|
+
<div class='border rounded my-2 py-2 mx-2 px-2'>
|
|
153
|
+
<TablerSchema
|
|
154
|
+
:key='variant[key] || 0'
|
|
155
|
+
v-model='data[key]'
|
|
156
|
+
:schema='unionVariants(s.properties[key])[variant[key] || 0]'
|
|
157
|
+
:disabled='disabled'
|
|
158
|
+
/>
|
|
159
|
+
</div>
|
|
160
|
+
</template>
|
|
161
|
+
<template v-else-if='isObject(s.properties[key])'>
|
|
162
|
+
<div class='d-flex align-items-center'>
|
|
163
|
+
<label
|
|
164
|
+
class='form-label mb-0'
|
|
165
|
+
v-text='key'
|
|
166
|
+
/>
|
|
167
|
+
<span
|
|
168
|
+
v-if='s.properties[key].required'
|
|
169
|
+
class='text-red mx-1'
|
|
170
|
+
>*</span>
|
|
171
|
+
</div>
|
|
172
|
+
|
|
173
|
+
<div class='border rounded my-2 py-2 mx-2 px-2'>
|
|
174
|
+
<TablerSchema
|
|
175
|
+
v-model='data[key]'
|
|
176
|
+
:schema='s.properties[key]'
|
|
177
|
+
:disabled='disabled'
|
|
178
|
+
/>
|
|
179
|
+
</div>
|
|
180
|
+
</template>
|
|
141
181
|
<template v-else-if='s.properties[key].type === "array"'>
|
|
142
182
|
<div class='d-flex'>
|
|
143
183
|
<label
|
|
@@ -261,7 +301,7 @@
|
|
|
261
301
|
</template>
|
|
262
302
|
|
|
263
303
|
<script setup lang="ts">
|
|
264
|
-
import { ref, reactive, watch, onMounted } from 'vue'
|
|
304
|
+
import { ref, reactive, computed, watch, onMounted } from 'vue'
|
|
265
305
|
import TablerInput from './input/Input.vue';
|
|
266
306
|
import TablerToggle from './input/Toggle.vue';
|
|
267
307
|
import TablerEnum from './input/Enum.vue';
|
|
@@ -321,6 +361,103 @@ const csv = reactive({
|
|
|
321
361
|
text: ''
|
|
322
362
|
});
|
|
323
363
|
|
|
364
|
+
/** Selected union variant index per property key */
|
|
365
|
+
const variant = reactive<Record<string, number>>({});
|
|
366
|
+
|
|
367
|
+
/** Properties with a fixed const value are seeded but never rendered */
|
|
368
|
+
const visibleKeys = computed<string[]>(() => {
|
|
369
|
+
if (!s.value.properties) return [];
|
|
370
|
+
return Object.keys(s.value.properties).filter((key) => s.value.properties[key].const === undefined);
|
|
371
|
+
});
|
|
372
|
+
|
|
373
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
374
|
+
const isObject = (prop: any): boolean => {
|
|
375
|
+
return prop.type === 'object' && !!prop.properties;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
/** The object members of an anyOf/oneOf, ignoring a null member used to mark a nullable value */
|
|
379
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
380
|
+
const unionVariants = (prop: any): any[] => {
|
|
381
|
+
const members = prop.oneOf || prop.anyOf;
|
|
382
|
+
if (!Array.isArray(members)) return [];
|
|
383
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
384
|
+
return members.filter((m: any) => m && m.type !== 'null');
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/** A choice between two or more object shapes, rendered with a variant picker */
|
|
388
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
389
|
+
const isUnion = (prop: any): boolean => {
|
|
390
|
+
const variants = unionVariants(prop);
|
|
391
|
+
return variants.length >= 2 && variants.every((v) => isObject(v));
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
const variantLabels = (key: string): string[] => {
|
|
395
|
+
const labels: string[] = [];
|
|
396
|
+
|
|
397
|
+
unionVariants(s.value.properties[key]).forEach((v, i) => {
|
|
398
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
399
|
+
const discriminator = Object.values(v.properties as Record<string, any>).find((p) => p.const !== undefined);
|
|
400
|
+
let label = v.title || (discriminator ? String(discriminator.const) : `Option ${i + 1}`);
|
|
401
|
+
if (labels.includes(label)) label = `${label} (${i + 1})`;
|
|
402
|
+
labels.push(label);
|
|
403
|
+
});
|
|
404
|
+
|
|
405
|
+
return labels;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
/** Pick the variant a stored value belongs to by discriminator, then by required keys */
|
|
409
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
410
|
+
const detectVariant = (prop: any, value: any): number => {
|
|
411
|
+
const variants = unionVariants(prop);
|
|
412
|
+
if (!value || typeof value !== 'object') return 0;
|
|
413
|
+
|
|
414
|
+
for (let i = 0; i < variants.length; i++) {
|
|
415
|
+
for (const [k, p] of Object.entries(variants[i].properties)) {
|
|
416
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
417
|
+
if ((p as any).const !== undefined && value[k] === (p as any).const) return i;
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
for (let i = 0; i < variants.length; i++) {
|
|
422
|
+
const required: string[] = variants[i].required || [];
|
|
423
|
+
if (required.length && required.every((k) => value[k] !== undefined)) return i;
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
return 0;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/** A fresh value for a variant holding only its const properties, the nested form seeds the rest */
|
|
430
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
431
|
+
const seedVariant = (prop: any, index: number): Record<string, unknown> => {
|
|
432
|
+
const seed: Record<string, unknown> = {};
|
|
433
|
+
const v = unionVariants(prop)[index];
|
|
434
|
+
if (!v) return seed;
|
|
435
|
+
|
|
436
|
+
for (const [k, p] of Object.entries(v.properties)) {
|
|
437
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
438
|
+
if ((p as any).const !== undefined) seed[k] = (p as any).const;
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
return seed;
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
const selectVariant = (key: string, label: string) => {
|
|
445
|
+
const index = variantLabels(key).indexOf(label);
|
|
446
|
+
if (index === -1 || index === variant[key]) return;
|
|
447
|
+
|
|
448
|
+
variant[key] = index;
|
|
449
|
+
data.value[key] = seedVariant(s.value.properties[key], index);
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
const resolveVariants = () => {
|
|
453
|
+
if (!s.value.properties) return;
|
|
454
|
+
|
|
455
|
+
for (const key in s.value.properties) {
|
|
456
|
+
const prop = s.value.properties[key];
|
|
457
|
+
if (isUnion(prop)) variant[key] = detectVariant(prop, data.value[key]);
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
|
|
324
461
|
/** An array of objects with known properties is rendered as a table with a row editor */
|
|
325
462
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
326
463
|
const isObjectTable = (prop: any): boolean => {
|
|
@@ -424,8 +561,12 @@ const updateDataDefaults = () => {
|
|
|
424
561
|
|
|
425
562
|
const prop = s.value.properties[key];
|
|
426
563
|
|
|
427
|
-
if (prop.
|
|
564
|
+
if (prop.const !== undefined) {
|
|
565
|
+
data.value[key] = JSON.parse(JSON.stringify(prop.const));
|
|
566
|
+
} else if (prop.default !== undefined) {
|
|
428
567
|
data.value[key] = JSON.parse(JSON.stringify(prop.default));
|
|
568
|
+
} else if (isUnion(prop)) {
|
|
569
|
+
data.value[key] = seedVariant(prop, 0);
|
|
429
570
|
} else if (prop.type === 'array') {
|
|
430
571
|
data.value[key] = [];
|
|
431
572
|
} else if (prop.type === 'boolean') {
|
|
@@ -444,12 +585,14 @@ const updateDataDefaults = () => {
|
|
|
444
585
|
watch(() => props.schema, () => {
|
|
445
586
|
updateSchema();
|
|
446
587
|
updateDataDefaults();
|
|
588
|
+
resolveVariants();
|
|
447
589
|
}, { deep: true });
|
|
448
590
|
|
|
449
591
|
watch(() => props.modelValue, () => {
|
|
450
592
|
if (JSON.stringify(props.modelValue) !== JSON.stringify(data.value)) {
|
|
451
593
|
data.value = JSON.parse(JSON.stringify(props.modelValue));
|
|
452
594
|
updateDataDefaults();
|
|
595
|
+
resolveVariants();
|
|
453
596
|
}
|
|
454
597
|
}, { deep: true });
|
|
455
598
|
|
|
@@ -459,6 +602,7 @@ onMounted(async () => {
|
|
|
459
602
|
data.value = JSON.parse(JSON.stringify(props.modelValue));
|
|
460
603
|
updateSchema();
|
|
461
604
|
updateDataDefaults();
|
|
605
|
+
resolveVariants();
|
|
462
606
|
|
|
463
607
|
loading.value = false;
|
|
464
608
|
})
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tak-ps/vue-tabler",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "5.
|
|
4
|
+
"version": "5.6.0",
|
|
5
5
|
"lib": "lib.ts",
|
|
6
6
|
"main": "lib.ts",
|
|
7
7
|
"module": "lib.ts",
|
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
"scripts": {
|
|
13
13
|
"test": "vitest run",
|
|
14
14
|
"test:watch": "vitest",
|
|
15
|
+
"test:coverage": "vitest run --coverage",
|
|
15
16
|
"check": "vue-tsc --noEmit",
|
|
16
17
|
"lint": "eslint lib.ts components/"
|
|
17
18
|
},
|
|
@@ -38,6 +39,7 @@
|
|
|
38
39
|
"@eslint/js": "^10.0.0",
|
|
39
40
|
"@typescript-eslint/parser": "^8.8.1",
|
|
40
41
|
"@vitejs/plugin-vue": "^6.0.5",
|
|
42
|
+
"@vitest/coverage-v8": "^5.0.1",
|
|
41
43
|
"@vue/test-utils": "^2.4.6",
|
|
42
44
|
"eslint": "^10.0.0",
|
|
43
45
|
"eslint-plugin-vue": "^10.0.0",
|
package/test/Schema.spec.ts
CHANGED
|
@@ -162,3 +162,357 @@ describe('TablerSchema object tables', () => {
|
|
|
162
162
|
})
|
|
163
163
|
})
|
|
164
164
|
})
|
|
165
|
+
|
|
166
|
+
const UNION_SCHEMA = {
|
|
167
|
+
type: 'object',
|
|
168
|
+
required: ['Source'],
|
|
169
|
+
properties: {
|
|
170
|
+
Source: {
|
|
171
|
+
anyOf: [
|
|
172
|
+
{
|
|
173
|
+
title: 'Single Map',
|
|
174
|
+
type: 'object',
|
|
175
|
+
required: ['Mode', 'MapId'],
|
|
176
|
+
properties: {
|
|
177
|
+
Mode: { const: 'Map', type: 'string' },
|
|
178
|
+
MapId: { type: 'string' },
|
|
179
|
+
},
|
|
180
|
+
},
|
|
181
|
+
{
|
|
182
|
+
title: 'Team Account',
|
|
183
|
+
type: 'object',
|
|
184
|
+
required: ['Mode', 'TeamId'],
|
|
185
|
+
properties: {
|
|
186
|
+
Mode: { const: 'Team', type: 'string' },
|
|
187
|
+
TeamId: { type: 'string' },
|
|
188
|
+
Verbose: { type: 'boolean', default: true },
|
|
189
|
+
},
|
|
190
|
+
},
|
|
191
|
+
],
|
|
192
|
+
},
|
|
193
|
+
},
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
describe('TablerSchema nested objects', () => {
|
|
197
|
+
it('renders an object property as a nested form and emits the nested value', async () => {
|
|
198
|
+
const wrapper = mount(Schema, {
|
|
199
|
+
props: {
|
|
200
|
+
modelValue: {},
|
|
201
|
+
schema: {
|
|
202
|
+
type: 'object',
|
|
203
|
+
properties: {
|
|
204
|
+
Auth: {
|
|
205
|
+
type: 'object',
|
|
206
|
+
properties: {
|
|
207
|
+
Username: { type: 'string' },
|
|
208
|
+
Remember: { type: 'boolean' },
|
|
209
|
+
},
|
|
210
|
+
},
|
|
211
|
+
},
|
|
212
|
+
},
|
|
213
|
+
},
|
|
214
|
+
})
|
|
215
|
+
|
|
216
|
+
await flushPromises()
|
|
217
|
+
|
|
218
|
+
const input = wrapper.find('input[type="text"]')
|
|
219
|
+
await input.setValue('fred')
|
|
220
|
+
await flushPromises()
|
|
221
|
+
|
|
222
|
+
expect(lastEmitted(wrapper)).toEqual({ Auth: { Username: 'fred', Remember: false } })
|
|
223
|
+
expect(wrapper.find('textarea').exists()).toBe(false)
|
|
224
|
+
})
|
|
225
|
+
})
|
|
226
|
+
|
|
227
|
+
describe('TablerSchema unions', () => {
|
|
228
|
+
it('seeds the first variant and hides the const discriminator', async () => {
|
|
229
|
+
const wrapper = mount(Schema, {
|
|
230
|
+
props: { modelValue: {}, schema: UNION_SCHEMA },
|
|
231
|
+
})
|
|
232
|
+
|
|
233
|
+
await flushPromises()
|
|
234
|
+
|
|
235
|
+
const select = wrapper.find('select')
|
|
236
|
+
expect(select.findAll('option').map((o) => o.text())).toEqual(['Single Map', 'Team Account'])
|
|
237
|
+
expect((select.element as HTMLSelectElement).value).toBe('Single Map')
|
|
238
|
+
expect(wrapper.findAll('input[type="text"]').length).toBe(1)
|
|
239
|
+
expect(lastEmitted(wrapper)).toEqual({ Source: { Mode: 'Map', MapId: '' } })
|
|
240
|
+
})
|
|
241
|
+
|
|
242
|
+
it('selects the variant matching a stored discriminator', async () => {
|
|
243
|
+
const wrapper = mount(Schema, {
|
|
244
|
+
props: {
|
|
245
|
+
modelValue: { Source: { Mode: 'Team', TeamId: 'abc', Verbose: false } },
|
|
246
|
+
schema: UNION_SCHEMA,
|
|
247
|
+
},
|
|
248
|
+
})
|
|
249
|
+
|
|
250
|
+
await flushPromises()
|
|
251
|
+
|
|
252
|
+
expect((wrapper.find('select').element as HTMLSelectElement).value).toBe('Team Account')
|
|
253
|
+
expect((wrapper.find('input[type="text"]').element as HTMLInputElement).value).toBe('abc')
|
|
254
|
+
})
|
|
255
|
+
|
|
256
|
+
it('resets the value to the new variant when switched', async () => {
|
|
257
|
+
const wrapper = mount(Schema, {
|
|
258
|
+
props: {
|
|
259
|
+
modelValue: { Source: { Mode: 'Map', MapId: 'xyz' } },
|
|
260
|
+
schema: UNION_SCHEMA,
|
|
261
|
+
},
|
|
262
|
+
})
|
|
263
|
+
|
|
264
|
+
await flushPromises()
|
|
265
|
+
await wrapper.find('select').setValue('Team Account')
|
|
266
|
+
await flushPromises()
|
|
267
|
+
|
|
268
|
+
expect(lastEmitted(wrapper)).toEqual({ Source: { Mode: 'Team', TeamId: '', Verbose: true } })
|
|
269
|
+
expect(wrapper.find('input[type="checkbox"]').exists()).toBe(true)
|
|
270
|
+
})
|
|
271
|
+
|
|
272
|
+
it('does not treat a nullable anyOf as a union', async () => {
|
|
273
|
+
const wrapper = mount(Schema, {
|
|
274
|
+
props: {
|
|
275
|
+
modelValue: {},
|
|
276
|
+
schema: {
|
|
277
|
+
type: 'object',
|
|
278
|
+
properties: {
|
|
279
|
+
Note: { anyOf: [{ type: 'string' }, { type: 'null' }] },
|
|
280
|
+
},
|
|
281
|
+
},
|
|
282
|
+
},
|
|
283
|
+
})
|
|
284
|
+
|
|
285
|
+
await flushPromises()
|
|
286
|
+
|
|
287
|
+
expect(wrapper.find('select').exists()).toBe(false)
|
|
288
|
+
})
|
|
289
|
+
})
|
|
290
|
+
|
|
291
|
+
const PRIMITIVE_SCHEMA = {
|
|
292
|
+
type: 'object',
|
|
293
|
+
properties: {
|
|
294
|
+
Name: { type: 'string' },
|
|
295
|
+
Count: { type: 'integer' },
|
|
296
|
+
Ratio: { type: 'number' },
|
|
297
|
+
Flag: { type: 'boolean' },
|
|
298
|
+
Level: { type: 'string', enum: ['low', 'high'] },
|
|
299
|
+
Roles: { type: 'array', uniqueItems: true, items: { type: 'string', enum: ['admin', 'user'] } },
|
|
300
|
+
},
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
describe('TablerSchema editing primitives', () => {
|
|
304
|
+
it('emits typed values for string, integer, number and boolean inputs', async () => {
|
|
305
|
+
const wrapper = mount(Schema, {
|
|
306
|
+
props: { modelValue: {}, schema: PRIMITIVE_SCHEMA },
|
|
307
|
+
})
|
|
308
|
+
|
|
309
|
+
await flushPromises()
|
|
310
|
+
|
|
311
|
+
await wrapper.find('input[type="text"]').setValue('Fred')
|
|
312
|
+
const numbers = wrapper.findAll('input[type="number"]')
|
|
313
|
+
await numbers[0].setValue('3')
|
|
314
|
+
await numbers[1].setValue('1.5')
|
|
315
|
+
await wrapper.find('.form-switch input').setValue(true)
|
|
316
|
+
await flushPromises()
|
|
317
|
+
|
|
318
|
+
const data = lastEmitted(wrapper)
|
|
319
|
+
expect(data.Name).toBe('Fred')
|
|
320
|
+
expect(data.Count).toBe(3)
|
|
321
|
+
expect(data.Ratio).toBe(1.5)
|
|
322
|
+
expect(data.Flag).toBe(true)
|
|
323
|
+
})
|
|
324
|
+
|
|
325
|
+
it('emits the selected enum option', async () => {
|
|
326
|
+
const wrapper = mount(Schema, {
|
|
327
|
+
props: { modelValue: {}, schema: PRIMITIVE_SCHEMA },
|
|
328
|
+
})
|
|
329
|
+
|
|
330
|
+
await flushPromises()
|
|
331
|
+
await wrapper.find('select').setValue('high')
|
|
332
|
+
await flushPromises()
|
|
333
|
+
|
|
334
|
+
expect(lastEmitted(wrapper).Level).toBe('high')
|
|
335
|
+
})
|
|
336
|
+
|
|
337
|
+
it('toggles multi enum options in and out of the array', async () => {
|
|
338
|
+
const wrapper = mount(Schema, {
|
|
339
|
+
props: { modelValue: {}, schema: PRIMITIVE_SCHEMA },
|
|
340
|
+
})
|
|
341
|
+
|
|
342
|
+
await flushPromises()
|
|
343
|
+
|
|
344
|
+
const boxes = wrapper.findAll('.form-check input[type="checkbox"]')
|
|
345
|
+
expect(boxes.length).toBe(2)
|
|
346
|
+
|
|
347
|
+
await boxes[0].trigger('change')
|
|
348
|
+
await boxes[1].trigger('change')
|
|
349
|
+
await flushPromises()
|
|
350
|
+
expect(lastEmitted(wrapper).Roles).toEqual(['admin', 'user'])
|
|
351
|
+
|
|
352
|
+
await boxes[0].trigger('change')
|
|
353
|
+
await flushPromises()
|
|
354
|
+
expect(lastEmitted(wrapper).Roles).toEqual(['user'])
|
|
355
|
+
})
|
|
356
|
+
|
|
357
|
+
it('renders a multi line string as a textarea and keeps newlines', async () => {
|
|
358
|
+
const wrapper = mount(Schema, {
|
|
359
|
+
props: {
|
|
360
|
+
modelValue: { Name: 'one\ntwo' },
|
|
361
|
+
schema: { type: 'object', properties: { Name: { type: 'string' } } },
|
|
362
|
+
},
|
|
363
|
+
})
|
|
364
|
+
|
|
365
|
+
await flushPromises()
|
|
366
|
+
|
|
367
|
+
const textarea = wrapper.find('textarea')
|
|
368
|
+
expect(textarea.exists()).toBe(true)
|
|
369
|
+
await textarea.setValue('one\ntwo\nthree')
|
|
370
|
+
await flushPromises()
|
|
371
|
+
|
|
372
|
+
expect(lastEmitted(wrapper).Name).toBe('one\ntwo\nthree')
|
|
373
|
+
})
|
|
374
|
+
|
|
375
|
+
it('disables every primitive input when disabled', async () => {
|
|
376
|
+
const wrapper = mount(Schema, {
|
|
377
|
+
props: { modelValue: {}, schema: PRIMITIVE_SCHEMA, disabled: true },
|
|
378
|
+
})
|
|
379
|
+
|
|
380
|
+
await flushPromises()
|
|
381
|
+
|
|
382
|
+
const controls = wrapper.findAll('input, select')
|
|
383
|
+
expect(controls.length).toBeGreaterThan(0)
|
|
384
|
+
expect(controls.every((c) => (c.element as HTMLInputElement).disabled)).toBe(true)
|
|
385
|
+
})
|
|
386
|
+
})
|
|
387
|
+
|
|
388
|
+
const DEFAULT_SCHEMA = {
|
|
389
|
+
type: 'object',
|
|
390
|
+
properties: {
|
|
391
|
+
Name: { type: 'string', default: 'Fred' },
|
|
392
|
+
Count: { type: 'integer', default: 5 },
|
|
393
|
+
Level: { type: 'string', enum: ['low', 'high'], default: 'high' },
|
|
394
|
+
Tags: { type: 'array', items: { type: 'string' }, default: ['a'] },
|
|
395
|
+
Kind: { type: 'string', const: 'thing' },
|
|
396
|
+
Auth: {
|
|
397
|
+
type: 'object',
|
|
398
|
+
properties: {
|
|
399
|
+
Remember: { type: 'boolean', default: true },
|
|
400
|
+
Username: { type: 'string' },
|
|
401
|
+
},
|
|
402
|
+
},
|
|
403
|
+
},
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
describe('TablerSchema applying default values', () => {
|
|
407
|
+
it('keeps existing values and only fills missing keys', async () => {
|
|
408
|
+
const wrapper = mount(Schema, {
|
|
409
|
+
props: {
|
|
410
|
+
modelValue: { Name: 'Bob', Tags: [] },
|
|
411
|
+
schema: DEFAULT_SCHEMA,
|
|
412
|
+
},
|
|
413
|
+
})
|
|
414
|
+
|
|
415
|
+
await flushPromises()
|
|
416
|
+
|
|
417
|
+
const data = lastEmitted(wrapper)
|
|
418
|
+
expect(data.Name).toBe('Bob')
|
|
419
|
+
expect(data.Tags).toEqual([])
|
|
420
|
+
expect(data.Count).toBe(5)
|
|
421
|
+
expect(data.Level).toBe('high')
|
|
422
|
+
})
|
|
423
|
+
|
|
424
|
+
it('seeds a root level const and hides it from the form', async () => {
|
|
425
|
+
const wrapper = mount(Schema, {
|
|
426
|
+
props: { modelValue: {}, schema: DEFAULT_SCHEMA },
|
|
427
|
+
})
|
|
428
|
+
|
|
429
|
+
await flushPromises()
|
|
430
|
+
|
|
431
|
+
expect(lastEmitted(wrapper).Kind).toBe('thing')
|
|
432
|
+
expect(wrapper.findAll('label').map((l) => l.text())).not.toContain('Kind')
|
|
433
|
+
})
|
|
434
|
+
|
|
435
|
+
it('seeds nested object defaults through the nested form', async () => {
|
|
436
|
+
const wrapper = mount(Schema, {
|
|
437
|
+
props: { modelValue: {}, schema: DEFAULT_SCHEMA },
|
|
438
|
+
})
|
|
439
|
+
|
|
440
|
+
await flushPromises()
|
|
441
|
+
|
|
442
|
+
expect(lastEmitted(wrapper).Auth).toEqual({ Remember: true, Username: '' })
|
|
443
|
+
})
|
|
444
|
+
|
|
445
|
+
it('clones array defaults so edits do not leak back into the schema', async () => {
|
|
446
|
+
const schema = JSON.parse(JSON.stringify(DEFAULT_SCHEMA))
|
|
447
|
+
const wrapper = mount(Schema, {
|
|
448
|
+
props: { modelValue: {}, schema },
|
|
449
|
+
})
|
|
450
|
+
|
|
451
|
+
await flushPromises()
|
|
452
|
+
|
|
453
|
+
const tags = lastEmitted(wrapper).Tags as string[]
|
|
454
|
+
expect(tags).toEqual(['a'])
|
|
455
|
+
tags.push('b')
|
|
456
|
+
expect(schema.properties.Tags.default).toEqual(['a'])
|
|
457
|
+
})
|
|
458
|
+
|
|
459
|
+
it('applies defaults again when the parent replaces the model value', async () => {
|
|
460
|
+
const wrapper = mount(Schema, {
|
|
461
|
+
props: { modelValue: { Name: 'Bob' }, schema: DEFAULT_SCHEMA },
|
|
462
|
+
})
|
|
463
|
+
|
|
464
|
+
await flushPromises()
|
|
465
|
+
await wrapper.setProps({ modelValue: { Name: 'Zed' } })
|
|
466
|
+
await flushPromises()
|
|
467
|
+
|
|
468
|
+
const data = lastEmitted(wrapper)
|
|
469
|
+
expect(data.Name).toBe('Zed')
|
|
470
|
+
expect(data.Count).toBe(5)
|
|
471
|
+
expect(data.Kind).toBe('thing')
|
|
472
|
+
})
|
|
473
|
+
|
|
474
|
+
it('applies defaults for properties added when the schema changes', async () => {
|
|
475
|
+
const wrapper = mount(Schema, {
|
|
476
|
+
props: {
|
|
477
|
+
modelValue: {},
|
|
478
|
+
schema: { type: 'object', properties: { Name: { type: 'string' } } },
|
|
479
|
+
},
|
|
480
|
+
})
|
|
481
|
+
|
|
482
|
+
await flushPromises()
|
|
483
|
+
expect(lastEmitted(wrapper)).toEqual({ Name: '' })
|
|
484
|
+
|
|
485
|
+
await wrapper.setProps({
|
|
486
|
+
schema: {
|
|
487
|
+
type: 'object',
|
|
488
|
+
required: ['Name'],
|
|
489
|
+
properties: {
|
|
490
|
+
Name: { type: 'string' },
|
|
491
|
+
Count: { type: 'integer', default: 7 },
|
|
492
|
+
},
|
|
493
|
+
},
|
|
494
|
+
})
|
|
495
|
+
await flushPromises()
|
|
496
|
+
|
|
497
|
+
expect(lastEmitted(wrapper)).toEqual({ Name: '', Count: 7 })
|
|
498
|
+
expect(wrapper.find('.text-red').exists()).toBe(true)
|
|
499
|
+
})
|
|
500
|
+
|
|
501
|
+
it('re-detects the union variant when the parent replaces the model value', async () => {
|
|
502
|
+
const wrapper = mount(Schema, {
|
|
503
|
+
props: {
|
|
504
|
+
modelValue: { Source: { Mode: 'Map', MapId: 'x' } },
|
|
505
|
+
schema: UNION_SCHEMA,
|
|
506
|
+
},
|
|
507
|
+
})
|
|
508
|
+
|
|
509
|
+
await flushPromises()
|
|
510
|
+
expect((wrapper.find('select').element as HTMLSelectElement).value).toBe('Single Map')
|
|
511
|
+
|
|
512
|
+
await wrapper.setProps({ modelValue: { Source: { Mode: 'Team', TeamId: 't' } } })
|
|
513
|
+
await flushPromises()
|
|
514
|
+
|
|
515
|
+
expect((wrapper.find('select').element as HTMLSelectElement).value).toBe('Team Account')
|
|
516
|
+
expect(lastEmitted(wrapper)).toEqual({ Source: { Mode: 'Team', TeamId: 't', Verbose: true } })
|
|
517
|
+
})
|
|
518
|
+
})
|
package/vitest.config.mjs
CHANGED