From 529ec929ac6c03501dce86fb8f3235dcaf20ac50 Mon Sep 17 00:00:00 2001 From: Silviu Avram Date: Wed, 5 Aug 2020 14:56:36 +0300 Subject: [PATCH 1/4] feat: export configure function with data-testid override --- lib/index.ts | 19 +++++++++++++++++-- lib/typedefs.ts | 4 ++++ test/fixtures/page.html | 2 +- test/index.test.ts | 9 ++++++++- 4 files changed, 30 insertions(+), 4 deletions(-) diff --git a/lib/index.ts b/lib/index.ts index 09c90bd..4646946 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -3,7 +3,7 @@ import * as path from 'path' import {ElementHandle, EvaluateFn, JSHandle, Page} from 'puppeteer' import waitForExpect from 'wait-for-expect' -import {IQueryUtils, IScopedQueryUtils} from './typedefs' +import {IQueryUtils, IScopedQueryUtils, Config} from './typedefs' const domLibraryAsString = readFileSync( path.join(__dirname, '../dom-testing-library.js'), @@ -17,7 +17,7 @@ function mapArgument(argument: any, index: number): any { : argument } -const delegateFnBodyToExecuteInPage = ` +let delegateFnBodyToExecuteInPage = ` ${domLibraryAsString}; const mappedArgs = args.map(${mapArgument.toString()}); @@ -130,6 +130,21 @@ export function wait( return waitForExpect(callback, timeout, interval) } +export function configure(newConfig: Partial) { + const { testIdAttribute } = newConfig; + + if ( + testIdAttribute && + typeof testIdAttribute === 'string' && + testIdAttribute !== '' + ) { + delegateFnBodyToExecuteInPage = delegateFnBodyToExecuteInPage.replace( + `testIdAttribute: 'data-testid'`, + `testIdAttribute: '${newConfig.testIdAttribute}'` + ) + } +} + export function getQueriesForElement( object: T, contextFn?: ContextFn, diff --git a/lib/typedefs.ts b/lib/typedefs.ts index 57c87e2..113f3a1 100644 --- a/lib/typedefs.ts +++ b/lib/typedefs.ts @@ -58,3 +58,7 @@ export interface IQueryUtils extends IQueryMethods { getQueriesForElement(): IQueryUtils & IScopedQueryUtils getNodeText(el: Element): Promise } + +export interface Config { + testIdAttribute: string; +} diff --git a/test/fixtures/page.html b/test/fixtures/page.html index 227d8a8..884cec2 100644 --- a/test/fixtures/page.html +++ b/test/fixtures/page.html @@ -3,7 +3,7 @@

Hello h1

-

Hello h2

+

Hello h2

Image A diff --git a/test/index.test.ts b/test/index.test.ts index 7e7fc43..412852f 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -1,6 +1,6 @@ import * as path from 'path' import * as puppeteer from 'puppeteer' -import {getDocument, queries, getQueriesForElement, wait} from '../lib' +import {getDocument, queries, getQueriesForElement, wait, configure} from '../lib' describe('lib/index.ts', () => { let browser: puppeteer.Browser @@ -17,6 +17,13 @@ describe('lib/index.ts', () => { const element = await queries.getByText(document, 'Hello h1') expect(await queries.getNodeText(element)).toEqual('Hello h1') }) + + it('should support custom data-testid names', async () => { + configure({testIdAttribute: 'data-id'}) + const document = await getDocument(page) + const element = await queries.getByTestId(document, 'my-header') + expect(await queries.getNodeText(element)).toEqual('Hello h2') + }) it('should support regex on raw queries object', async () => { const scope = await page.$('#scoped') From da84093e7b7b3761b9ab65c6b330fe0539c400dd Mon Sep 17 00:00:00 2001 From: Silviu Avram Date: Fri, 7 Aug 2020 10:30:14 +0300 Subject: [PATCH 2/4] address comments from review --- lib/index.ts | 24 +++++++++++------------- lib/typedefs.ts | 2 +- test/fixtures/page.html | 4 ++-- test/index.test.ts | 12 ++++++++++-- 4 files changed, 24 insertions(+), 18 deletions(-) diff --git a/lib/index.ts b/lib/index.ts index 4646946..a41a763 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -3,7 +3,7 @@ import * as path from 'path' import {ElementHandle, EvaluateFn, JSHandle, Page} from 'puppeteer' import waitForExpect from 'wait-for-expect' -import {IQueryUtils, IScopedQueryUtils, Config} from './typedefs' +import {IQueryUtils, IScopedQueryUtils, IConfigureOptions} from './typedefs' const domLibraryAsString = readFileSync( path.join(__dirname, '../dom-testing-library.js'), @@ -17,7 +17,7 @@ function mapArgument(argument: any, index: number): any { : argument } -let delegateFnBodyToExecuteInPage = ` +const delegateFnBodyToExecuteInPageInitial = ` ${domLibraryAsString}; const mappedArgs = args.map(${mapArgument.toString()}); @@ -27,6 +27,8 @@ let delegateFnBodyToExecuteInPage = ` return moduleWithFns[fnName](container, ...mappedArgs); ` +let delegateFnBodyToExecuteInPage = delegateFnBodyToExecuteInPageInitial + type DOMReturnType = ElementHandle | ElementHandle[] | null type ContextFn = (...args: any[]) => ElementHandle @@ -130,17 +132,13 @@ export function wait( return waitForExpect(callback, timeout, interval) } -export function configure(newConfig: Partial) { - const { testIdAttribute } = newConfig; - - if ( - testIdAttribute && - typeof testIdAttribute === 'string' && - testIdAttribute !== '' - ) { - delegateFnBodyToExecuteInPage = delegateFnBodyToExecuteInPage.replace( - `testIdAttribute: 'data-testid'`, - `testIdAttribute: '${newConfig.testIdAttribute}'` +export function configure(options: Partial) { + const { testIdAttribute } = options; + + if (testIdAttribute && typeof testIdAttribute === 'string') { + delegateFnBodyToExecuteInPage = delegateFnBodyToExecuteInPageInitial.replace( + /testIdAttribute: (['|"])data-testid(['|"])/g, + `testIdAttribute: $1${testIdAttribute}$2` ) } } diff --git a/lib/typedefs.ts b/lib/typedefs.ts index 113f3a1..dde8f38 100644 --- a/lib/typedefs.ts +++ b/lib/typedefs.ts @@ -59,6 +59,6 @@ export interface IQueryUtils extends IQueryMethods { getNodeText(el: Element): Promise } -export interface Config { +export interface IConfigureOptions { testIdAttribute: string; } diff --git a/test/fixtures/page.html b/test/fixtures/page.html index 884cec2..899aca3 100644 --- a/test/fixtures/page.html +++ b/test/fixtures/page.html @@ -2,8 +2,8 @@ -

Hello h1

-

Hello h2

+

Hello h1

+

Hello h2

Image A diff --git a/test/index.test.ts b/test/index.test.ts index 412852f..175f76d 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -18,13 +18,21 @@ describe('lib/index.ts', () => { expect(await queries.getNodeText(element)).toEqual('Hello h1') }) - it('should support custom data-testid names', async () => { + it('should support custom data-testid attribute name', async () => { configure({testIdAttribute: 'data-id'}) const document = await getDocument(page) - const element = await queries.getByTestId(document, 'my-header') + const element = await queries.getByTestId(document, 'second-level-header') expect(await queries.getNodeText(element)).toEqual('Hello h2') }) + it('should support subsequent changing the data-testid attribute names', async () => { + configure({testIdAttribute: 'data-id'}) + configure({testIdAttribute: 'data-new-id'}) + const document = await getDocument(page) + const element = await queries.getByTestId(document, 'first-level-header') + expect(await queries.getNodeText(element)).toEqual('Hello h1') + }) + it('should support regex on raw queries object', async () => { const scope = await page.$('#scoped') if (!scope) throw new Error('Should have scope') From 19cc5af2251f1060388d54b378744804a0b21f2b Mon Sep 17 00:00:00 2001 From: Silviu Avram Date: Fri, 7 Aug 2020 11:28:26 +0300 Subject: [PATCH 3/4] more tests --- lib/index.ts | 4 ++++ test/fixtures/page.html | 2 +- test/index.test.ts | 15 ++++++++++++++- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/index.ts b/lib/index.ts index a41a763..11e8b70 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -133,6 +133,10 @@ export function wait( } export function configure(options: Partial) { + if (!options) { + return + } + const { testIdAttribute } = options; if (testIdAttribute && typeof testIdAttribute === 'string') { diff --git a/test/fixtures/page.html b/test/fixtures/page.html index 899aca3..484a179 100644 --- a/test/fixtures/page.html +++ b/test/fixtures/page.html @@ -6,7 +6,7 @@

Hello h1

Hello h2

Image A - +
diff --git a/test/index.test.ts b/test/index.test.ts index 175f76d..3a1f6dc 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -17,7 +17,7 @@ describe('lib/index.ts', () => { const element = await queries.getByText(document, 'Hello h1') expect(await queries.getNodeText(element)).toEqual('Hello h1') }) - + it('should support custom data-testid attribute name', async () => { configure({testIdAttribute: 'data-id'}) const document = await getDocument(page) @@ -33,6 +33,15 @@ describe('lib/index.ts', () => { expect(await queries.getNodeText(element)).toEqual('Hello h1') }) + it('should keep the default data-testid when input passed is invalid', async () => { + ;[{}, undefined, null, {testIdAttribute: ''}].forEach(async options => { + const document = await getDocument(page) + configure(options as any) + const element = await queries.getByTestId(document, 'testid-label') + expect(await queries.getNodeText(element)).toEqual('Label A') + }) + }) + it('should support regex on raw queries object', async () => { const scope = await page.$('#scoped') if (!scope) throw new Error('Should have scope') @@ -56,6 +65,10 @@ describe('lib/index.ts', () => { expect(await getByText('Loaded!')).toBeTruthy() }, 9000) + afterEach(() => { + configure({testIdAttribute: 'data-testid'}) //cleanup + }) + afterAll(async () => { await browser.close() }) From 419ca94a7b15a18440daf1ba99394aa7e66bbeb3 Mon Sep 17 00:00:00 2001 From: Silviu Avram Date: Fri, 7 Aug 2020 19:04:17 +0300 Subject: [PATCH 4/4] fix lint --- lib/index.ts | 10 +++++----- lib/typedefs.ts | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/index.ts b/lib/index.ts index 11e8b70..acc5662 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -3,7 +3,7 @@ import * as path from 'path' import {ElementHandle, EvaluateFn, JSHandle, Page} from 'puppeteer' import waitForExpect from 'wait-for-expect' -import {IQueryUtils, IScopedQueryUtils, IConfigureOptions} from './typedefs' +import {IConfigureOptions, IQueryUtils, IScopedQueryUtils} from './typedefs' const domLibraryAsString = readFileSync( path.join(__dirname, '../dom-testing-library.js'), @@ -132,17 +132,17 @@ export function wait( return waitForExpect(callback, timeout, interval) } -export function configure(options: Partial) { +export function configure(options: Partial): void { if (!options) { return } - const { testIdAttribute } = options; + const { testIdAttribute } = options - if (testIdAttribute && typeof testIdAttribute === 'string') { + if (testIdAttribute) { delegateFnBodyToExecuteInPage = delegateFnBodyToExecuteInPageInitial.replace( /testIdAttribute: (['|"])data-testid(['|"])/g, - `testIdAttribute: $1${testIdAttribute}$2` + `testIdAttribute: $1${testIdAttribute}$2`, ) } } diff --git a/lib/typedefs.ts b/lib/typedefs.ts index dde8f38..c163154 100644 --- a/lib/typedefs.ts +++ b/lib/typedefs.ts @@ -60,5 +60,5 @@ export interface IQueryUtils extends IQueryMethods { } export interface IConfigureOptions { - testIdAttribute: string; + testIdAttribute: string }