Skip to content

Commit 9fb6b7b

Browse files
authored
feat: support for within
* within first pass. not the best, but it works * within kept in window to avoid requerying the dom every time.
1 parent 4161bf5 commit 9fb6b7b

File tree

4 files changed

+67
-10
lines changed

4 files changed

+67
-10
lines changed

src/index.js

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* eslint-disable no-eval */
2+
/* eslint-disable no-new-func */
13
import fs from 'fs'
24
import path from 'path'
35
import {ClientFunction, Selector} from 'testcafe'
@@ -11,20 +13,60 @@ const LIBRARY_UMD_CONTENT = fs.readFileSync(LIBRARY_UMD_PATH).toString()
1113

1214
export const addTestcafeTestingLibrary = async t => {
1315
// eslint-disable-next-line
14-
const inject = ClientFunction(() => window.eval(script), {
15-
dependencies: {script: LIBRARY_UMD_CONTENT},
16-
})
16+
const inject = ClientFunction(
17+
() => {
18+
// eslint-disable-next-line no-undef
19+
window.eval(script)
20+
window.TestCafeTestingLibrary = {}
21+
},
22+
{
23+
dependencies: {script: LIBRARY_UMD_CONTENT},
24+
},
25+
)
1726

1827
await inject.with({boundTestRun: t})()
1928
}
2029

2130
Object.keys(queries).forEach(queryName => {
2231
module.exports[queryName] = Selector(
23-
// eslint-disable-next-line no-new-func
2432
new Function(
2533
`
2634
return DomTestingLibrary.${queryName}(document.body, ...arguments);
2735
`,
2836
),
2937
)
3038
})
39+
40+
export const within = async sel => {
41+
await ClientFunction(
42+
new Function(
43+
`
44+
const elem = document.querySelector("${sel}");
45+
window.TestCafeTestingLibrary["within_${sel}"] = DomTestingLibrary.within(elem);
46+
47+
`,
48+
),
49+
)()
50+
const container = {}
51+
52+
Object.keys(queries).forEach(queryName => {
53+
container[queryName] = Selector(
54+
new Function(
55+
`return window.TestCafeTestingLibrary["within_${sel}"].${queryName}(...arguments)`,
56+
),
57+
)
58+
})
59+
60+
return container
61+
// const container = {}
62+
63+
// Object.keys(queries).forEach(queryName => {
64+
// container[queryName] = Selector(
65+
// new Function(
66+
// `return DomTestingLibrary.within(document.querySelector("${sel}")).${queryName}(...arguments)`,
67+
// ),
68+
// )
69+
// })
70+
71+
// return container
72+
}

tests/testcafe/selectors.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/* eslint-disable jest/no-disabled-tests */
22
/* eslint-disable import/named */
3-
import {Selector} from 'testcafe'
43
import {
54
getByText,
65
getByPlaceholderText,
@@ -56,11 +55,6 @@ test('queryAllByText', async t => {
5655
await t.expect(queryAllByText('Non-existing Button Text').exists).notOk()
5756
})
5857

59-
test.skip('getByText in container', async t => {
60-
const nested = await Selector('#nested')
61-
await t.click(getByText('Button Text', {container: nested}))
62-
})
63-
6458
test.skip('getByTestId only throws the error message', async t => {
6559
const testId = 'Some random id'
6660
const errorMessage = `Unable to find an element by: [data-testid="${testId}"]`

tests/testcafe/within.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import {Selector} from 'testcafe'
2+
import {within, addTestcafeTestingLibrary} from '../../src'
3+
4+
// eslint-disable-next-line babel/no-unused-expressions
5+
fixture`within`.beforeEach(addTestcafeTestingLibrary)
6+
.page`http://localhost:13370`
7+
8+
test('getByText within container', async t => {
9+
const {getByText} = await within('#nested')
10+
await t
11+
.click(getByText('Button Text'))
12+
.expect(Selector('button').withExactText('Button Clicked').exists)
13+
.ok()
14+
})
15+
16+
test("queryByPlaceholder doesn't find anything", async t => {
17+
const {queryByPlaceholderText} = await within('#nested')
18+
19+
await t.expect(queryByPlaceholderText('Placeholder Text').exists).notOk()
20+
})

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
exports[`exports expected selectors 1`] = `
44
Array [
55
"addTestcafeTestingLibrary",
6+
"within",
67
"queryAllByLabelText",
78
"getAllByLabelText",
89
"queryByLabelText",

0 commit comments

Comments
 (0)