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
45import type { Schema } from "@/core/index.js" ;
56import { theme } from "@/basic-theme/index.js" ;
@@ -9,21 +10,25 @@ import { translation } from "@/translations/en.js";
910import { FORM_CONTEXT } from "./context/index.js" ;
1011import { createForm3 } from "./create-form.svelte.js" ;
1112import 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} ) ;
0 commit comments