|
1 | | -import {promises as fs} from 'fs' |
2 | | - |
3 | | -import type {Locator, PlaywrightTestArgs, TestFixture} from '@playwright/test' |
| 1 | +import type {Locator, Page, PlaywrightTestArgs, TestFixture} from '@playwright/test' |
4 | 2 | import {selectors} from '@playwright/test' |
5 | 3 |
|
6 | | -import {queryNames as allQueryNames} from '../common' |
7 | | - |
8 | | -import {replacer, reviver} from './helpers' |
9 | | -import type { |
10 | | - AllQuery, |
11 | | - FindQuery, |
12 | | - LocatorQueries as Queries, |
13 | | - Query, |
14 | | - Selector, |
15 | | - SelectorEngine, |
16 | | - SupportedQuery, |
17 | | -} from './types' |
| 4 | +import {queryNames as allQueryNames} from '../../common' |
| 5 | +import {replacer} from '../helpers' |
| 6 | +import type {Config, LocatorQueries as Queries, SelectorEngine, SupportedQuery} from '../types' |
18 | 7 |
|
19 | | -const isAllQuery = (query: Query): query is AllQuery => query.includes('All') |
20 | | -const isNotFindQuery = (query: Query): query is Exclude<Query, FindQuery> => |
21 | | - !query.startsWith('find') |
| 8 | +import {buildTestingLibraryScript, isAllQuery, isNotFindQuery, queryToSelector} from './helpers' |
22 | 9 |
|
23 | 10 | const queryNames = allQueryNames.filter(isNotFindQuery) |
| 11 | +const defaultConfig: Config = {testIdAttribute: 'data-testid', asyncUtilTimeout: 1000} |
24 | 12 |
|
25 | | -const queryToSelector = (query: SupportedQuery) => |
26 | | - query.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase() as Selector |
| 13 | +const options = Object.fromEntries( |
| 14 | + Object.entries(defaultConfig).map(([key, value]) => [key, [value, {option: true}] as const]), |
| 15 | +) |
27 | 16 |
|
28 | | -const queriesFixture: TestFixture<Queries, PlaywrightTestArgs> = async ({page}, use) => { |
29 | | - const queries = queryNames.reduce( |
| 17 | +const queriesFor = (pageOrLocator: Page | Locator) => |
| 18 | + queryNames.reduce( |
30 | 19 | (rest, query) => ({ |
31 | 20 | ...rest, |
32 | 21 | [query]: (...args: Parameters<Queries[keyof Queries]>) => |
33 | | - page.locator(`${queryToSelector(query)}=${JSON.stringify(args, replacer)}`), |
| 22 | + pageOrLocator.locator(`${queryToSelector(query)}=${JSON.stringify(args, replacer)}`), |
34 | 23 | }), |
35 | 24 | {} as Queries, |
36 | 25 | ) |
37 | 26 |
|
38 | | - await use(queries) |
39 | | -} |
| 27 | +const queriesFixture: TestFixture<Queries, PlaywrightTestArgs> = async ({page}, use) => |
| 28 | + use(queriesFor(page)) |
40 | 29 |
|
41 | | -const within = (locator: Locator): Queries => |
42 | | - queryNames.reduce( |
43 | | - (rest, query) => ({ |
44 | | - ...rest, |
45 | | - [query]: (...args: Parameters<Queries[keyof Queries]>) => |
46 | | - locator.locator(`${queryToSelector(query)}=${JSON.stringify(args, replacer)}`), |
47 | | - }), |
48 | | - {} as Queries, |
49 | | - ) |
| 30 | +const within = (locator: Locator): Queries => queriesFor(locator) |
50 | 31 |
|
51 | 32 | declare const queryName: SupportedQuery |
52 | 33 |
|
@@ -108,25 +89,18 @@ const registerSelectorsFixture: [ |
108 | 89 | ] |
109 | 90 |
|
110 | 91 | const installTestingLibraryFixture: [ |
111 | | - TestFixture<void, PlaywrightTestArgs>, |
| 92 | + TestFixture<void, PlaywrightTestArgs & Config>, |
112 | 93 | {scope: 'test'; auto?: boolean}, |
113 | 94 | ] = [ |
114 | | - async ({context}, use) => { |
115 | | - const testingLibraryDomUmdScript = await fs.readFile( |
116 | | - require.resolve('@testing-library/dom/dist/@testing-library/dom.umd.js'), |
117 | | - 'utf8', |
| 95 | + async ({context, asyncUtilTimeout, testIdAttribute}, use) => { |
| 96 | + await context.addInitScript( |
| 97 | + await buildTestingLibraryScript({config: {asyncUtilTimeout, testIdAttribute}}), |
118 | 98 | ) |
119 | 99 |
|
120 | | - await context.addInitScript(` |
121 | | - ${testingLibraryDomUmdScript} |
122 | | - |
123 | | - window.__testingLibraryReviver = ${reviver.toString()}; |
124 | | - `) |
125 | | - |
126 | 100 | await use() |
127 | 101 | }, |
128 | 102 | {scope: 'test', auto: true}, |
129 | 103 | ] |
130 | 104 |
|
131 | | -export {queriesFixture, registerSelectorsFixture, installTestingLibraryFixture, within} |
| 105 | +export {installTestingLibraryFixture, options, queriesFixture, registerSelectorsFixture, within} |
132 | 106 | export type {Queries} |
0 commit comments