1
1
import * as path from 'path'
2
2
import * as puppeteer from 'puppeteer'
3
- import { getDocument , queries , getQueriesForElement , wait } from '../lib'
3
+ import { getDocument , queries , getQueriesForElement , wait , configure } from '../lib'
4
4
5
5
describe ( 'lib/index.ts' , ( ) => {
6
6
let browser : puppeteer . Browser
@@ -18,6 +18,30 @@ describe('lib/index.ts', () => {
18
18
expect ( await queries . getNodeText ( element ) ) . toEqual ( 'Hello h1' )
19
19
} )
20
20
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
+
21
45
it ( 'should support regex on raw queries object' , async ( ) => {
22
46
const scope = await page . $ ( '#scoped' )
23
47
if ( ! scope ) throw new Error ( 'Should have scope' )
@@ -41,6 +65,10 @@ describe('lib/index.ts', () => {
41
65
expect ( await getByText ( 'Loaded!' ) ) . toBeTruthy ( )
42
66
} , 9000 )
43
67
68
+ afterEach ( ( ) => {
69
+ configure ( { testIdAttribute : 'data-testid' } ) //cleanup
70
+ } )
71
+
44
72
afterAll ( async ( ) => {
45
73
await browser . close ( )
46
74
} )
0 commit comments