Skip to content

Commit 5998cfd

Browse files
authored
feat: no more having to re-add DomTestingLibrary after page load! (#20)
* feat: no more having to re-add DomTestingLibrary after page load! :party: * configure works after page load and standalone * update snapshot * set browsers * updated readme
1 parent 26a5dfc commit 5998cfd

File tree

8 files changed

+90
-28
lines changed

8 files changed

+90
-28
lines changed

.testcaferc.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
{
22
"src": "tests/testcafe/**/*.js",
3-
"browsers": ["chromium"],
4-
3+
"browsers": [
4+
"chrome:headless",
5+
"firefox:headless"
6+
],
57
"concurrency": 1
6-
}
8+
}

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88

99
<p>Testcafe selectors and utilities that encourage good testing practices laid down by dom-testing-library.</p>
1010

11-
**_Please go upvote this issue in testcafe to help make this library better_** https://github.com/DevExpress/testcafe/issues/3758
12-
1311
[**Read the docs**](https://testing-library.com/docs/testcafe-testing-library/intro) | [Edit the docs](https://github.com/alexkrolick/testing-library-docs)
1412

1513
</div>

src/index.js

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,58 @@ import path from 'path'
55
import { ClientFunction, Selector } from 'testcafe'
66
import { queries } from 'dom-testing-library'
77

8-
const LIBRARY_UMD_PATH = path.join(
8+
const DOM_TESTING_LIBRARY_UMD_PATH = path.join(
99
'./node_modules',
1010
'dom-testing-library/dist/dom-testing-library.umd.js',
1111
)
12-
const LIBRARY_UMD_CONTENT = fs.readFileSync(LIBRARY_UMD_PATH).toString()
12+
const DOM_TESTING_LIBRARY_UMD = fs.readFileSync(DOM_TESTING_LIBRARY_UMD_PATH).toString()
1313

14-
export const addTestcafeTestingLibrary = async t => {
14+
15+
16+
17+
export async function configure(options, t) {
18+
const configFunction =
19+
`
20+
window.DomTestingLibrary.configure(${JSON.stringify(options)});
21+
`;
22+
await ClientFunction(new Function(configFunction))();
23+
24+
if (t) {
25+
t.testRun.injectable.scripts.push('/testcafe-testing-library-config.js');
26+
t.testRun.session.proxy.GET('/testcafe-testing-library-config.js', { content: configFunction, contentType: 'application/x-javascript' })
27+
}
28+
29+
}
30+
31+
export async function addTestcafeTestingLibrary(t) {
32+
// inject for 1st pageload. Then just use injectables for subsequent page loads.
1533
// eslint-disable-next-line
1634
const inject = ClientFunction(
1735
() => {
1836
// eslint-disable-next-line no-undef
1937
window.eval(script)
20-
window.TestCafeTestingLibrary = {}
2138
},
2239
{
23-
dependencies: { script: LIBRARY_UMD_CONTENT },
40+
dependencies: { script: DOM_TESTING_LIBRARY_UMD },
2441
},
2542
)
2643

27-
await inject.with({ boundTestRun: t })()
44+
await inject.with({ boundTestRun: t })();
45+
46+
//and for subsequent pageloads:
47+
t.testRun.injectable.scripts.push('/dom-testing-library.js');
48+
t.testRun.session.proxy.GET('/dom-testing-library.js', { content: DOM_TESTING_LIBRARY_UMD, contentType: 'application/x-javascript' })
49+
50+
if (addTestcafeTestingLibrary.options) {
51+
await configure(addTestcafeTestingLibrary.options, t);
52+
}
53+
54+
}
55+
56+
// eslint-disable-next-line no-shadow
57+
addTestcafeTestingLibrary.configure = function configure(options) {
58+
addTestcafeTestingLibrary.options = { ...options };
59+
return addTestcafeTestingLibrary;
2860
}
2961

3062
Object.keys(queries).forEach(queryName => {
@@ -42,8 +74,10 @@ export const within = async sel => {
4274
await ClientFunction(
4375
new Function(
4476
`
45-
const elem = document.querySelector("${sanitizedSel}");
46-
window.TestCafeTestingLibrary["within_${sanitizedSel}"] = DomTestingLibrary.within(elem);
77+
78+
window.TestcafeTestingLibrary = window.TestcafeTestingLibrary || {}
79+
const elem = document.querySelector("${sanitizedSel}");
80+
window.TestcafeTestingLibrary["within_${sanitizedSel}"] = DomTestingLibrary.within(elem);
4781
4882
`,
4983
),
@@ -53,19 +87,11 @@ export const within = async sel => {
5387
Object.keys(queries).forEach(queryName => {
5488
container[queryName] = Selector(
5589
new Function(
56-
`return window.TestCafeTestingLibrary["within_${sanitizedSel}"].${queryName}(...arguments)`,
90+
`return window.TestcafeTestingLibrary["within_${sanitizedSel}"].${queryName}(...arguments)`,
5791
),
5892
)
5993
})
6094

6195
return container
6296
}
6397

64-
65-
export const configure = async options => {
66-
await ClientFunction(new Function(
67-
`
68-
window.DomTestingLibrary.configure(${JSON.stringify(options)});
69-
`
70-
))
71-
}

test-app/index.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,20 @@ <h2>configure</h2>
5656
<img data-automation-id="image-with-random-alt-tag" src="data:image/png;base64,"
5757
onclick="this.style.border = '5px solid red'" />
5858
</section>
59+
<section>
60+
<h2>configure standalone</h2>
61+
62+
<img data-other-test-id="other-id" src="data:image/png;base64," onclick="this.style.border = '5px solid red'" />
63+
</section>
5964
<section>
6065
<h2>getAllByText</h2>
6166
<button onclick="this.innerText = 'Jackie Kicked'">Jackie Chan 1</button>
6267
<button onclick="this.innerText = 'Jackie Kicked'">Jackie Chan 2</button>
6368
</section>
69+
<section>
70+
<h2>navigate</h2>
71+
<a href="page2.html">Go to Page 2</a>
72+
</section>
6473
<!-- Prettier unindents the script tag below -->
6574
<script>
6675
document

test-app/page2.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<div>second page</div>
2+
<section>
3+
<section>
4+
<h2>configure</h2>
5+
<img data-automation-id="page2-thing" src="data:image/png;base64,"
6+
onclick="this.style.border = '5px solid red'" />
7+
</section>
8+
</section>

tests/testcafe/configure.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,26 @@
11
/* eslint-disable import/named */
2-
import { configure, getByTestId, addTestcafeTestingLibrary } from '../../src'
2+
import { configure, getByTestId, getByText, addTestcafeTestingLibrary } from '../../src'
33

44

55

66
// eslint-disable-next-line babel/no-unused-expressions
7-
fixture`configure`.beforeEach(addTestcafeTestingLibrary)
7+
fixture`configure`.beforeEach(addTestcafeTestingLibrary.configure({ testIdAttribute: 'data-automation-id' }))
88
.page`http://localhost:13370`
99

1010

11-
test('configure', async t => {
12-
await configure({ testIdAttribute: 'data-automation-id' });
13-
11+
test('supports alternative testIdAttribute', async t => {
1412
await t
1513
.click(getByTestId('image-with-random-alt-tag'))
14+
})
15+
16+
test('still works after browser page load', async t => {
17+
await t
18+
.click(getByText('Go to Page 2'))
19+
.click(getByTestId('page2-thing'))
20+
.expect(getByText('second page').exists).ok()
21+
})
22+
23+
test('can be used standalone', async t => {
24+
await configure({ testIdAttribute: 'data-other-test-id' });
25+
await t.click(getByTestId('other-id'));
1626
})

tests/testcafe/selectors.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,15 @@ test('queryAllByText', async t => {
5555
await t.expect(queryAllByText('Non-existing Button Text').exists).notOk()
5656
})
5757

58+
59+
60+
61+
test('still works after browser page load', async t => {
62+
await t
63+
.click(getByText('Go to Page 2'))
64+
.expect(getByText('second page').exists).ok()
65+
})
66+
5867
test.skip('getByTestId only throws the error message', async t => {
5968
const testId = 'Some random id'
6069
const errorMessage = `Unable to find an element by: [data-testid="${testId}"]`

tests/unit/__snapshots__/selectors.test.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
exports[`exports expected selectors 1`] = `
44
Array [
5+
"configure",
56
"addTestcafeTestingLibrary",
67
"within",
7-
"configure",
88
"queryAllByLabelText",
99
"getAllByLabelText",
1010
"queryByLabelText",

0 commit comments

Comments
 (0)