@@ -12,37 +12,109 @@ describe('lib/index.ts', () => {
1212 await page . goto ( `file://${ path . join ( __dirname , '../fixtures/page.html' ) } ` )
1313 } )
1414
15- it ( 'should export the utilities ' , async ( ) => {
15+ test ( 'should handle the query* methods ' , async ( ) => {
1616 const document = await getDocument ( page )
17- const element = await queries . getByText ( document , 'Hello h1' )
18- expect ( await queries . getNodeText ( element ) ) . toEqual ( 'Hello h1' )
17+ const element = await queries . queryByText ( document , 'Hello h1' )
18+
19+ expect ( element ) . toBeTruthy ( )
20+ expect ( await element ! . textContent ( ) ) . toEqual ( 'Hello h1' )
21+ } )
22+
23+ test ( 'should use the new v3 methods' , async ( ) => {
24+ const document = await getDocument ( page )
25+ const element = await queries . queryByRole ( document , 'presentation' )
26+
27+ expect ( element ) . toBeTruthy ( )
28+ expect ( await element ! . textContent ( ) ) . toContain ( 'Layout table' )
29+ } )
30+
31+ test ( 'should handle regex matching' , async ( ) => {
32+ const document = await getDocument ( page )
33+ const element = await queries . getByText ( document , / H e L l O h ( 1 | 7 ) / i)
34+
35+ expect ( await element . textContent ( ) ) . toEqual ( 'Hello h1' )
36+ } )
37+
38+ test ( 'handles page navigations' , async ( ) => {
39+ await page . goto ( `file://${ path . join ( __dirname , '../fixtures/page.html' ) } ` )
40+
41+ const element = await queries . getByText ( await getDocument ( page ) , 'Hello h1' )
42+
43+ expect ( await element . textContent ( ) ) . toEqual ( 'Hello h1' )
1944 } )
2045
21- it ( 'should support custom data-testid attribute name' , async ( ) => {
22- configure ( { testIdAttribute : 'data-id' } )
46+ test ( 'should handle the queryAll* methods' , async ( ) => {
2347 const document = await getDocument ( page )
24- const element = await queries . getByTestId ( document , 'second-level-header' )
25- expect ( await queries . getNodeText ( element ) ) . toEqual ( 'Hello h2' )
48+ const elements = await queries . queryAllByText ( document , / H e l l o / )
49+
50+ expect ( elements ) . toHaveLength ( 3 )
51+
52+ const text = await Promise . all ( [
53+ page . evaluate ( el => el . textContent , elements [ 0 ] ) ,
54+ page . evaluate ( el => el . textContent , elements [ 1 ] ) ,
55+ page . evaluate ( el => el . textContent , elements [ 2 ] ) ,
56+ ] )
57+
58+ expect ( text ) . toEqual ( [ 'Hello h1' , 'Hello h2' , 'Hello h3' ] )
2659 } )
2760
28- it ( 'should support subsequent changing the data-testid attribute names' , async ( ) => {
29- configure ( { testIdAttribute : 'data-id' } )
30- configure ( { testIdAttribute : 'data-new-id' } )
61+ test ( 'should handle the queryAll* methods with a selector' , async ( ) => {
3162 const document = await getDocument ( page )
32- const element = await queries . getByTestId ( document , 'first-level-header' )
63+ const elements = await queries . queryAllByText ( document , / H e l l o / , { selector : 'h2' } )
64+
65+ expect ( elements ) . toHaveLength ( 1 )
66+
67+ const text = await page . evaluate ( el => el . textContent , elements [ 0 ] )
68+
69+ expect ( text ) . toEqual ( 'Hello h2' )
70+ } )
71+
72+ test ( 'should handle the getBy* methods with a selector' , async ( ) => {
73+ const document = await getDocument ( page )
74+ const element = await queries . getByText ( document , / H e l l o / , { selector : 'h2' } )
75+
76+ const text = await page . evaluate ( el => el . textContent , element )
77+
78+ expect ( text ) . toEqual ( 'Hello h2' )
79+ } )
80+
81+ it ( 'attaches `getNodeText`' , async ( ) => {
82+ const document = await getDocument ( page )
83+ const element = await queries . getByText ( document , 'Hello h1' )
84+
3385 expect ( await queries . getNodeText ( element ) ) . toEqual ( 'Hello h1' )
3486 } )
3587
36- it . each ( [ { } , undefined , null , { testIdAttribute : '' } ] ) (
37- 'should keep the default data-testid when input passed is invalid' ,
38- async options => {
88+ describe ( 'configuration' , ( ) => {
89+ afterEach ( ( ) => {
90+ configure ( { testIdAttribute : 'data-testid' } ) // cleanup
91+ } )
92+
93+ it ( 'should support custom data-testid attribute name' , async ( ) => {
94+ configure ( { testIdAttribute : 'data-id' } )
95+ const document = await getDocument ( page )
96+ const element = await queries . getByTestId ( document , 'second-level-header' )
97+ expect ( await queries . getNodeText ( element ) ) . toEqual ( 'Hello h2' )
98+ } )
99+
100+ it ( 'should support subsequent changing the data-testid attribute names' , async ( ) => {
101+ configure ( { testIdAttribute : 'data-id' } )
102+ configure ( { testIdAttribute : 'data-new-id' } )
39103 const document = await getDocument ( page )
40- configure ( options as any )
41- const element = await queries . getByTestId ( document , 'testid-label' )
42- expect ( await queries . getNodeText ( element ) ) . toEqual ( 'Label A' )
43- } ,
44- )
104+ const element = await queries . getByTestId ( document , 'first-level-header' )
105+ expect ( await queries . getNodeText ( element ) ) . toEqual ( 'Hello h1' )
106+ } )
45107
108+ it . each ( [ { } , undefined , null , { testIdAttribute : '' } ] ) (
109+ 'should keep the default data-testid when input passed is invalid (%s)' ,
110+ async options => {
111+ const document = await getDocument ( page )
112+ configure ( options as any )
113+ const element = await queries . getByTestId ( document , 'testid-label' )
114+ expect ( await queries . getNodeText ( element ) ) . toEqual ( 'Label A' )
115+ } ,
116+ )
117+ } )
46118 it ( 'should support regex on raw queries object' , async ( ) => {
47119 const scope = await page . $ ( '#scoped' )
48120 if ( ! scope ) throw new Error ( 'Should have scope' )
@@ -75,10 +147,6 @@ describe('lib/index.ts', () => {
75147 } , 9000 )
76148 } )
77149
78- afterEach ( ( ) => {
79- configure ( { testIdAttribute : 'data-testid' } ) // cleanup
80- } )
81-
82150 afterAll ( async ( ) => {
83151 await browser . close ( )
84152 } )
0 commit comments