Skip to content

Commit 9d0c91c

Browse files
committed
[form] Add content component test
1 parent 8234e3f commit 9d0c91c

File tree

4 files changed

+45
-4
lines changed

4 files changed

+45
-4
lines changed

packages/form/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"@sveltejs/vite-plugin-svelte": "catalog:",
4848
"@testing-library/jest-dom": "^6.6.3",
4949
"@testing-library/svelte": "^5.2.6",
50+
"@testing-library/user-event": "^14.6.1",
5051
"@tsconfig/svelte": "catalog:",
5152
"@types/json-schema": "^7.0.15",
5253
"@types/json-schema-merge-allof": "^0.6.5",

packages/form/src/form/content.test.ts

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { it } from "vitest";
2-
import { render, screen } from "@testing-library/svelte";
1+
import { expect, it } from "vitest";
2+
import { render, screen, getByText } from "@testing-library/svelte";
3+
import { userEvent } from '@testing-library/user-event';
34

45
import type { Schema } from "@/core/index.js";
56
import { theme } from "@/basic-theme/index.js";
@@ -9,21 +10,25 @@ import { translation } from "@/translations/en.js";
910
import { FORM_CONTEXT } from "./context/index.js";
1011
import { createForm3 } from "./create-form.svelte.js";
1112
import Content from "./content.svelte";
13+
import { computePseudoId, DEFAULT_ID_PREFIX, DEFAULT_ID_SEPARATOR, DEFAULT_PSEUDO_ID_SEPARATOR, pathToId } from './id-schema.js';
1214

13-
it("should preserve multi select field state in array", () => {
15+
it("should preserve state of multi select field in array", async () => {
16+
const user = userEvent.setup()
1417
const schema: Schema = {
1518
type: "array",
1619
title: "Array",
1720
items: {
1821
anyOf: [
1922
{
23+
title: "Foo option",
2024
properties: {
2125
foo: {
2226
type: "string",
2327
},
2428
},
2529
},
2630
{
31+
title: "Bar option",
2732
properties: {
2833
bar: {
2934
type: "string",
@@ -48,4 +53,25 @@ it("should preserve multi select field state in array", () => {
4853
context: new Map([[FORM_CONTEXT, form.context]]),
4954
props: { form }
5055
})
56+
57+
const id = pathToId(DEFAULT_ID_PREFIX, DEFAULT_ID_SEPARATOR, [1])
58+
const pseudoId = computePseudoId(DEFAULT_PSEUDO_ID_SEPARATOR, id, 'anyof')
59+
const el = document.getElementById(pseudoId)
60+
if (el === null) {
61+
throw new Error(`cannot find ${pseudoId} select`)
62+
}
63+
await user.selectOptions(el, 'Bar option')
64+
65+
const input = screen.getByLabelText('bar')
66+
await user.type(input, 'bar state')
67+
68+
const firstItem = document.querySelector('[data-layout="array-item"]')
69+
if (!(firstItem instanceof HTMLElement)) {
70+
throw new Error('cannot find first item')
71+
}
72+
const delBtn = getByText(firstItem, 'Del')
73+
user.click(delBtn)
74+
75+
const input2 = screen.getByLabelText('bar')
76+
expect(input2).toHaveValue('bar state')
5177
});

packages/form/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"include": [
2424
"src/**/*.ts",
2525
"src/**/*.js",
26-
"src/**/*.svelte"
26+
"src/**/*.svelte",
27+
"./vitest-setup.ts"
2728
],
2829
}

pnpm-lock.yaml

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)