11import * as path from 'path'
22import * as playwright from 'playwright'
3- import { getDocument , queries , getQueriesForElement , wait } from '..'
3+ import { getDocument , queries , getQueriesForElement , wait , configure } from '..'
44
55describe ( 'lib/index.ts' , ( ) => {
66 let browser : playwright . Browser
@@ -18,6 +18,30 @@ describe('lib/index.ts', () => {
1818 expect ( await queries . getNodeText ( element ) ) . toEqual ( 'Hello h1' )
1919 } )
2020
21+ it ( 'should support custom data-testid attribute name' , async ( ) => {
22+ configure ( { testIdAttribute : 'data-id' } )
23+ const document = await getDocument ( page )
24+ const element = await queries . getByTestId ( document , 'second-level-header' )
25+ expect ( await queries . getNodeText ( element ) ) . toEqual ( 'Hello h2' )
26+ } )
27+
28+ it ( 'should support subsequent changing the data-testid attribute names' , async ( ) => {
29+ configure ( { testIdAttribute : 'data-id' } )
30+ configure ( { testIdAttribute : 'data-new-id' } )
31+ const document = await getDocument ( page )
32+ const element = await queries . getByTestId ( document , 'first-level-header' )
33+ expect ( await queries . getNodeText ( element ) ) . toEqual ( 'Hello h1' )
34+ } )
35+
36+ it ( 'should keep the default data-testid when input passed is invalid' , async ( ) => {
37+ ; [ { } , undefined , null , { testIdAttribute : '' } ] . forEach ( async options => {
38+ const document = await getDocument ( page )
39+ configure ( options as any )
40+ const element = await queries . getByTestId ( document , 'testid-label' )
41+ expect ( await queries . getNodeText ( element ) ) . toEqual ( 'Label A' )
42+ } )
43+ } )
44+
2145 it ( 'should support regex on raw queries object' , async ( ) => {
2246 const scope = await page . $ ( '#scoped' )
2347 if ( ! scope ) throw new Error ( 'Should have scope' )
@@ -42,6 +66,10 @@ describe('lib/index.ts', () => {
4266 expect ( await getByText ( 'Loaded!' ) ) . toBeTruthy ( )
4367 } , 9000 )
4468
69+ afterEach ( ( ) => {
70+ configure ( { testIdAttribute : 'data-testid' } ) //cleanup
71+ } )
72+
4573 afterAll ( async ( ) => {
4674 await browser . close ( )
4775 } )
0 commit comments