11import  {  fireEvent ,  screen  }  from  '@mui/internal-test-utils' ; 
22import  {  expect  }  from  'chai' ; 
33import  {  Field  }  from  '@base-ui-components/react/field' ; 
4+ import  {  Form  }  from  '@base-ui-components/react/form' ; 
45import  {  createRenderer ,  describeConformance  }  from  '#test-utils' ; 
56
67describe ( '<Field.Error />' ,  ( )  =>  { 
@@ -29,10 +30,13 @@ describe('<Field.Error />', () => {
2930
3031  it ( 'should show error messages by default' ,  async  ( )  =>  { 
3132    await  render ( 
32-       < Field . Root > 
33-         < Field . Control  required  /> 
34-         < Field . Error > Message</ Field . Error > 
35-       </ Field . Root > , 
33+       < Form > 
34+         < Field . Root > 
35+           < Field . Control  required  /> 
36+           < Field . Error > Message</ Field . Error > 
37+         </ Field . Root > 
38+         < button  type = "submit" > submit</ button > 
39+       </ Form > , 
3640    ) ; 
3741
3842    expect ( screen . queryByText ( 'Message' ) ) . to . equal ( null ) ; 
@@ -43,46 +47,58 @@ describe('<Field.Error />', () => {
4347    fireEvent . change ( input ,  {  target : {  value : 'a'  }  } ) ; 
4448    fireEvent . change ( input ,  {  target : {  value : ''  }  } ) ; 
4549    fireEvent . blur ( input ) ; 
50+     expect ( screen . queryByText ( 'Message' ) ) . to . equal ( null ) ; 
4651
52+     fireEvent . click ( screen . getByText ( 'submit' ) ) ; 
4753    expect ( screen . queryByText ( 'Message' ) ) . not . to . equal ( null ) ; 
4854  } ) ; 
4955
5056  describe ( 'prop: match' ,  ( )  =>  { 
5157    it ( 'should only render when `match` matches constraint validation' ,  async  ( )  =>  { 
5258      await  render ( 
53-         < Field . Root > 
54-           < Field . Control  required  /> 
55-           < Field . Error  match = "valueMissing" > Message</ Field . Error > 
56-         </ Field . Root > , 
59+         < Form > 
60+           < Field . Root > 
61+             < Field . Control  required  minLength = { 2 }  /> 
62+             < Field . Error  match = "valueMissing" > Message</ Field . Error > 
63+           </ Field . Root > 
64+           < button  type = "submit" > submit</ button > 
65+         </ Form > , 
5766      ) ; 
5867
5968      expect ( screen . queryByText ( 'Message' ) ) . to . equal ( null ) ; 
6069
70+       fireEvent . click ( screen . getByText ( 'submit' ) ) ; 
71+       expect ( screen . queryByText ( 'Message' ) ) . not . to . equal ( null ) ; 
72+ 
6173      const  input  =  screen . getByRole < HTMLInputElement > ( 'textbox' ) ; 
6274
6375      fireEvent . focus ( input ) ; 
6476      fireEvent . change ( input ,  {  target : {  value : 'a'  }  } ) ; 
65-       fireEvent . change ( input ,  {  target : {  value : ''  }  } ) ; 
66-       fireEvent . blur ( input ) ; 
77+       expect ( screen . queryByText ( 'Message' ) ) . to . equal ( null ) ; 
6778
79+       fireEvent . change ( input ,  {  target : {  value : ''  }  } ) ; 
6880      expect ( screen . queryByText ( 'Message' ) ) . not . to . equal ( null ) ; 
6981    } ) ; 
7082
7183    it ( 'should show custom errors' ,  async  ( )  =>  { 
7284      await  render ( 
73-         < Field . Root  validate = { ( )  =>  'error' } > 
74-           < Field . Control  /> 
75-           < Field . Error  match = "customError" > Message</ Field . Error > 
76-         </ Field . Root > , 
85+         < Form > 
86+           < Field . Root  validate = { ( )  =>  'error' } > 
87+             < Field . Control  /> 
88+             < Field . Error  match = "customError" > Message</ Field . Error > 
89+           </ Field . Root > 
90+           < button  type = "submit" > submit</ button > 
91+         </ Form > , 
7792      ) ; 
7893
7994      const  input  =  screen . getByRole < HTMLInputElement > ( 'textbox' ) ; 
8095
8196      fireEvent . focus ( input ) ; 
8297      fireEvent . change ( input ,  {  target : {  value : 'a'  }  } ) ; 
83-       fireEvent . change ( input ,  {  target : {  value : ''  }  } ) ; 
8498      fireEvent . blur ( input ) ; 
99+       expect ( screen . queryByText ( 'Message' ) ) . to . equal ( null ) ; 
85100
101+       fireEvent . click ( screen . getByText ( 'submit' ) ) ; 
86102      expect ( screen . queryByText ( 'Message' ) ) . not . to . equal ( null ) ; 
87103    } ) ; 
88104
0 commit comments