1515 */
1616
1717import { FazFormElement } from "../src/form" ;
18- import { describe , expect , test } from "vitest" ;
19- import { allComments } from "../src/test" ;
18+ import { allComments } from "../src/test" ;
19+ import { JSX } from "solid-js/jsx-runtime" ;
20+ import { render } from "solid-js/web" ;
21+ import { afterEach , beforeEach , describe , expect , test , vitest } from "vitest" ;
2022
2123class TestForm extends FazFormElement {
2224
25+ public testForm : JSX . Element ;
26+
2327 constructor ( ) {
2428 super ( ) ;
2529 }
30+
31+ get form ( ) : HTMLFormElement | undefined {
32+ return this . testForm as HTMLFormElement ;
33+ }
34+
35+ show ( ) {
36+ this . testForm = < form > </ form > ;
37+ render ( ( ) => this . testForm , this ) ;
38+ }
2639}
2740
2841customElements . define ( "test-form" , TestForm ) ;
2942
43+ beforeEach ( ( ) => {
44+ vitest . useFakeTimers ( ) ;
45+ } ) ;
46+ afterEach ( ( ) => {
47+ vitest . useRealTimers ( ) ;
48+ } ) ;
3049
3150describe ( "Test Forms" , ( ) => {
32- test ( "Form errors" , ( ) => {
51+ test ( "Form errors" , async ( ) => {
3352 document . body . innerHTML = `
3453 <test-form>
54+ <input name="input1" value="1"/>
55+ <input name="input2" value="2"/>
3556 </test-form>
3657 ` ;
3758 const [ formComment ] = allComments ( document . body ) ;
3859 const form = formComment . fazElement ( ) as FazFormElement ;
60+ await vitest . runAllTimersAsync ( ) ;
3961 expect ( form . hasErrors ( ) ) . toBeFalsy ( ) ;
4062 form . pushError ( "field1" , "error 1" ) ;
4163 form . pushError ( "field1" , "error 1" ) ;
@@ -45,10 +67,11 @@ describe("Test Forms", () => {
4567 expect ( form . hasErrorsFor ( "field1" ) ) . toBeTruthy ( ) ;
4668 expect ( form . hasErrorsFor ( "field2" ) ) . toBeTruthy ( ) ;
4769 expect ( form . hasErrorsFor ( "field3" ) ) . toBeFalsy ( ) ;
48- expect ( form . getErrorsFor ( "field1" ) . length == 2 ) . toBeTruthy ( ) ;
49- expect ( form . getErrorsFor ( "field2" ) . length == 1 ) . toBeTruthy ( ) ;
70+ expect ( form . getErrorsFor ( "field1" ) . length ) . toBe ( 2 ) ;
71+ expect ( form . getErrorsFor ( "field2" ) . length ) . toBe ( 1 ) ;
5072 form . clearErrors ( ) ;
5173 expect ( form . hasErrors ( ) ) . toBeFalsy ( ) ;
52-
74+ expect ( form . form ) . toStrictEqual ( ( form as TestForm ) . testForm ) ;
75+ expect ( form . values ) . toStrictEqual ( { input1 : '1' , input2 : '2' } ) ;
5376 } ) ;
5477} ) ;
0 commit comments