Skip to content

Commit f7606b2

Browse files
authored
feat: support for within across page loads/reloads (#32)
* feat: support for within across page loads * lint
1 parent e53f0f2 commit f7606b2

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

src/index.js

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,25 +31,18 @@ Object.keys(queries).forEach(queryName => {
3131
)
3232
})
3333

34-
export const within = async sel => {
35-
const sanitizedSel = sel.replace(/"/g, "'")
36-
await ClientFunction(
37-
new Function(
38-
`
39-
40-
window.TestcafeTestingLibrary = window.TestcafeTestingLibrary || {}
41-
const elem = document.querySelector("${sanitizedSel}");
42-
window.TestcafeTestingLibrary["within_${sanitizedSel}"] = TestingLibraryDom.within(elem);
34+
export const within = selector => {
35+
const sanitizedSelector = selector.replace(/"/g, "'")
4336

44-
`,
45-
),
46-
)()
4737
const container = {}
4838

4939
Object.keys(queries).forEach(queryName => {
5040
container[queryName] = Selector(
5141
new Function(
52-
`return window.TestcafeTestingLibrary["within_${sanitizedSel}"].${queryName}(...arguments)`,
42+
`
43+
window.TestcafeTestingLibrary = window.TestcafeTestingLibrary || {}
44+
window.TestcafeTestingLibrary["within_${sanitizedSelector}"] = window.TestcafeTestingLibrary["within_${sanitizedSelector}"] || TestingLibraryDom.within(document.querySelector("${sanitizedSelector}"))
45+
return window.TestcafeTestingLibrary["within_${sanitizedSelector}"].${queryName}(...arguments)`,
5346
),
5447
)
5548
})

tests/testcafe/within.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,12 @@ test('quotes in selector', async t => {
2626
.click(getByText('Button Text'))
2727
.expect(Selector('button').withExactText('Button Clicked').exists)
2828
.ok()
29+
});
30+
31+
test('still works after browser page reload', async t => {
32+
const nested = await within('#nested');
33+
await t.expect(nested.getByText('Button Text').exists).ok()
34+
35+
await t.eval(() => location.reload(true));
36+
await t.expect(nested.getByText('Button Text').exists).ok()
2937
})

0 commit comments

Comments
 (0)