Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions .testcaferc.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"src": "tests/testcafe/**/*.js",
"browsers": ["chromium"],

"browsers": [
"chrome:headless",
"firefox:headless"
],
"concurrency": 1
}
}
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

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

**_Please go upvote this issue in testcafe to help make this library better_** https://github.com/DevExpress/testcafe/issues/3758

[**Read the docs**](https://testing-library.com/docs/testcafe-testing-library/intro) | [Edit the docs](https://github.com/alexkrolick/testing-library-docs)

</div>
Expand Down
60 changes: 43 additions & 17 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,58 @@ import path from 'path'
import { ClientFunction, Selector } from 'testcafe'
import { queries } from 'dom-testing-library'

const LIBRARY_UMD_PATH = path.join(
const DOM_TESTING_LIBRARY_UMD_PATH = path.join(
'./node_modules',
'dom-testing-library/dist/dom-testing-library.umd.js',
)
const LIBRARY_UMD_CONTENT = fs.readFileSync(LIBRARY_UMD_PATH).toString()
const DOM_TESTING_LIBRARY_UMD = fs.readFileSync(DOM_TESTING_LIBRARY_UMD_PATH).toString()

export const addTestcafeTestingLibrary = async t => {



export async function configure(options, t) {
const configFunction =
`
window.DomTestingLibrary.configure(${JSON.stringify(options)});
`;
await ClientFunction(new Function(configFunction))();

if (t) {
t.testRun.injectable.scripts.push('/testcafe-testing-library-config.js');
t.testRun.session.proxy.GET('/testcafe-testing-library-config.js', { content: configFunction, contentType: 'application/x-javascript' })
}

}

export async function addTestcafeTestingLibrary(t) {
// inject for 1st pageload. Then just use injectables for subsequent page loads.
// eslint-disable-next-line
const inject = ClientFunction(
() => {
// eslint-disable-next-line no-undef
window.eval(script)
window.TestCafeTestingLibrary = {}
},
{
dependencies: { script: LIBRARY_UMD_CONTENT },
dependencies: { script: DOM_TESTING_LIBRARY_UMD },
},
)

await inject.with({ boundTestRun: t })()
await inject.with({ boundTestRun: t })();

//and for subsequent pageloads:
t.testRun.injectable.scripts.push('/dom-testing-library.js');
t.testRun.session.proxy.GET('/dom-testing-library.js', { content: DOM_TESTING_LIBRARY_UMD, contentType: 'application/x-javascript' })

if (addTestcafeTestingLibrary.options) {
await configure(addTestcafeTestingLibrary.options, t);
}

}

// eslint-disable-next-line no-shadow
addTestcafeTestingLibrary.configure = function configure(options) {
addTestcafeTestingLibrary.options = { ...options };
return addTestcafeTestingLibrary;
}

Object.keys(queries).forEach(queryName => {
Expand All @@ -42,8 +74,10 @@ export const within = async sel => {
await ClientFunction(
new Function(
`
const elem = document.querySelector("${sanitizedSel}");
window.TestCafeTestingLibrary["within_${sanitizedSel}"] = DomTestingLibrary.within(elem);

window.TestcafeTestingLibrary = window.TestcafeTestingLibrary || {}
const elem = document.querySelector("${sanitizedSel}");
window.TestcafeTestingLibrary["within_${sanitizedSel}"] = DomTestingLibrary.within(elem);

`,
),
Expand All @@ -53,19 +87,11 @@ export const within = async sel => {
Object.keys(queries).forEach(queryName => {
container[queryName] = Selector(
new Function(
`return window.TestCafeTestingLibrary["within_${sanitizedSel}"].${queryName}(...arguments)`,
`return window.TestcafeTestingLibrary["within_${sanitizedSel}"].${queryName}(...arguments)`,
),
)
})

return container
}


export const configure = async options => {
await ClientFunction(new Function(
`
window.DomTestingLibrary.configure(${JSON.stringify(options)});
`
))
}
9 changes: 9 additions & 0 deletions test-app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,20 @@ <h2>configure</h2>
<img data-automation-id="image-with-random-alt-tag" src="data:image/png;base64,"
onclick="this.style.border = '5px solid red'" />
</section>
<section>
<h2>configure standalone</h2>

<img data-other-test-id="other-id" src="data:image/png;base64," onclick="this.style.border = '5px solid red'" />
</section>
<section>
<h2>getAllByText</h2>
<button onclick="this.innerText = 'Jackie Kicked'">Jackie Chan 1</button>
<button onclick="this.innerText = 'Jackie Kicked'">Jackie Chan 2</button>
</section>
<section>
<h2>navigate</h2>
<a href="page2.html">Go to Page 2</a>
</section>
<!-- Prettier unindents the script tag below -->
<script>
document
Expand Down
8 changes: 8 additions & 0 deletions test-app/page2.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<div>second page</div>
<section>
<section>
<h2>configure</h2>
<img data-automation-id="page2-thing" src="data:image/png;base64,"
onclick="this.style.border = '5px solid red'" />
</section>
</section>
20 changes: 15 additions & 5 deletions tests/testcafe/configure.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
/* eslint-disable import/named */
import { configure, getByTestId, addTestcafeTestingLibrary } from '../../src'
import { configure, getByTestId, getByText, addTestcafeTestingLibrary } from '../../src'



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


test('configure', async t => {
await configure({ testIdAttribute: 'data-automation-id' });

test('supports alternative testIdAttribute', async t => {
await t
.click(getByTestId('image-with-random-alt-tag'))
})

test('still works after browser page load', async t => {
await t
.click(getByText('Go to Page 2'))
.click(getByTestId('page2-thing'))
.expect(getByText('second page').exists).ok()
})

test('can be used standalone', async t => {
await configure({ testIdAttribute: 'data-other-test-id' });
await t.click(getByTestId('other-id'));
})
9 changes: 9 additions & 0 deletions tests/testcafe/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ test('queryAllByText', async t => {
await t.expect(queryAllByText('Non-existing Button Text').exists).notOk()
})




test('still works after browser page load', async t => {
await t
.click(getByText('Go to Page 2'))
.expect(getByText('second page').exists).ok()
})

test.skip('getByTestId only throws the error message', async t => {
const testId = 'Some random id'
const errorMessage = `Unable to find an element by: [data-testid="${testId}"]`
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/__snapshots__/selectors.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

exports[`exports expected selectors 1`] = `
Array [
"configure",
"addTestcafeTestingLibrary",
"within",
"configure",
"queryAllByLabelText",
"getAllByLabelText",
"queryByLabelText",
Expand Down