@@ -7,57 +7,51 @@ const user = userEvent.setup()
77
88describe ( 'Svelte Tests' , ( ) => {
99 it ( 'should have initial values' , ( ) => {
10- const { container } = render ( TestForm )
11- expect ( container . querySelector < HTMLInputElement > ( '#firstName' ) ) . toHaveValue (
12- getSampleData ( ) . firstName ,
13- )
14- expect ( container . querySelector < HTMLInputElement > ( '#lastName' ) ) . toHaveValue (
15- getSampleData ( ) . lastName ,
16- )
10+ const { getByLabelText } = render ( TestForm )
11+ expect ( getByLabelText ( 'First Name' ) ) . toHaveValue ( getSampleData ( ) . firstName )
12+ expect ( getByLabelText ( 'Last Name' ) ) . toHaveValue ( getSampleData ( ) . lastName )
1713 } )
1814
1915 it ( 'should change initial values when defaults update' , async ( ) => {
20- const { container } = render ( TestForm )
21- await user . click ( container . querySelector < HTMLButtonElement > ( '#change' ) ! )
16+ const { getByLabelText , getByRole } = render ( TestForm )
17+ await user . click ( getByRole ( 'button' , { name : 'Change Sample Data' } ) )
2218
23- expect ( container . querySelector < HTMLInputElement > ( '#firstName' ) ) . toHaveValue (
24- getSampleData ( ) . firstName ,
25- )
19+ expect ( getByLabelText ( 'First Name' ) ) . toHaveValue ( getSampleData ( ) . firstName )
2620 expect ( getSampleData ( ) . firstName ) . toBe ( 'Julian' )
2721 } )
2822
2923 it ( 'should mirror user input' , async ( ) => {
30- const { container } = render ( TestForm )
31- const lastName = container . querySelector < HTMLInputElement > ( '#lastName' ) !
24+ const { getByLabelText , getByTestId } = render ( TestForm )
25+ const lastName = getByLabelText ( 'Last Name' )
3226 const lastNameValue = 'Jobs'
3327 await user . type ( lastName , lastNameValue )
3428
35- const form = JSON . parse ( container . querySelector ( 'pre' ) ! . textContent ! )
29+ const form = JSON . parse ( getByTestId ( 'form-state' ) . textContent ! )
3630 expect ( form . values . lastName ) . toBe ( lastNameValue )
3731 } )
3832
3933 it ( 'Reset form to initial value' , async ( ) => {
40- const { container } = render ( TestForm )
41- const firstName = container . querySelector < HTMLInputElement > ( '#firstName' ) !
34+ const { getByLabelText , getByRole } = render ( TestForm )
35+ const firstName = getByLabelText ( 'First Name' )
4236 await user . type ( firstName , '-Joseph' )
4337
4438 expect ( firstName ) . toHaveValue ( getSampleData ( ) . firstName + '-Joseph' )
4539
46- await user . click ( container . querySelector < HTMLButtonElement > ( '#reset' ) ! )
40+ await user . click ( getByRole ( 'button' , { name : 'Reset' } ) )
4741 expect ( firstName ) . toHaveValue ( getSampleData ( ) . firstName )
4842 } )
4943
5044 it ( 'should display validation' , async ( ) => {
51- const { container } = render ( TestForm )
52- const lastName = container . querySelector < HTMLInputElement > ( '#lastName' ) !
45+ const { getByLabelText , getByText , queryByText } = render ( TestForm )
46+ const lastName = getByLabelText ( 'Last Name' )
5347 const lastNameValue = 'Jo'
5448 await user . type ( lastName , lastNameValue )
5549 expect ( lastName ) . toHaveValue ( 'Jo' )
56- expect ( container . querySelector ( 'em' ) ?. textContent ) . toBe ( ' Not long enough')
50+ expect ( getByText ( ' Not long enough') ) . toBeInTheDocument ( )
5751
5852 await user . type ( lastName , lastNameValue )
5953
6054 expect ( lastName . getAttribute ( 'error-text' ) ) . toBeFalsy ( )
61- expect ( container . querySelector ( 'em ') ) . not . toBeInTheDocument ( )
55+ expect ( queryByText ( 'Not long enough ') ) . not . toBeInTheDocument ( )
6256 } )
6357} )
0 commit comments