|
| 1 | +import { expect, it } from "vitest"; |
| 2 | +import { render, screen, getByText } from "@testing-library/svelte"; |
| 3 | +import { userEvent } from '@testing-library/user-event'; |
| 4 | + |
| 5 | +import type { Schema } from "@/core/index.js"; |
| 6 | +import { theme } from "@/basic-theme/index.js"; |
| 7 | +import { createValidator } from "@/fake-validator.js"; |
| 8 | +import { translation } from "@/translations/en.js"; |
| 9 | + |
| 10 | +import { FORM_CONTEXT } from "./context/index.js"; |
| 11 | +import { createForm3 } from "./create-form.svelte.js"; |
| 12 | +import Content from "./content.svelte"; |
| 13 | +import { computePseudoId, DEFAULT_ID_PREFIX, DEFAULT_ID_SEPARATOR, DEFAULT_PSEUDO_ID_SEPARATOR, pathToId } from './id-schema.js'; |
| 14 | + |
| 15 | +it("should preserve state of multi select field in array", async () => { |
| 16 | + const user = userEvent.setup() |
| 17 | + const schema: Schema = { |
| 18 | + type: "array", |
| 19 | + title: "Array", |
| 20 | + items: { |
| 21 | + anyOf: [ |
| 22 | + { |
| 23 | + title: "Foo option", |
| 24 | + properties: { |
| 25 | + foo: { |
| 26 | + type: "string", |
| 27 | + }, |
| 28 | + }, |
| 29 | + }, |
| 30 | + { |
| 31 | + title: "Bar option", |
| 32 | + properties: { |
| 33 | + bar: { |
| 34 | + type: "string", |
| 35 | + }, |
| 36 | + }, |
| 37 | + }, |
| 38 | + ], |
| 39 | + }, |
| 40 | + }; |
| 41 | + |
| 42 | + const validator = createValidator(); |
| 43 | + |
| 44 | + const form = createForm3({ |
| 45 | + ...theme, |
| 46 | + validator, |
| 47 | + translation, |
| 48 | + schema, |
| 49 | + initialValue: [{}, {}], |
| 50 | + }); |
| 51 | + |
| 52 | + render(Content, { |
| 53 | + context: new Map([[FORM_CONTEXT, form.context]]), |
| 54 | + props: { form } |
| 55 | + }) |
| 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') |
| 77 | +}); |
0 commit comments