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
19 changes: 6 additions & 13 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,18 @@ Object.keys(queries).forEach(queryName => {
)
})

export const within = async sel => {
const sanitizedSel = sel.replace(/"/g, "'")
await ClientFunction(
new Function(
`

window.TestcafeTestingLibrary = window.TestcafeTestingLibrary || {}
const elem = document.querySelector("${sanitizedSel}");
window.TestcafeTestingLibrary["within_${sanitizedSel}"] = TestingLibraryDom.within(elem);
export const within = selector => {
const sanitizedSelector = selector.replace(/"/g, "'")

`,
),
)()
const container = {}

Object.keys(queries).forEach(queryName => {
container[queryName] = Selector(
new Function(
`return window.TestcafeTestingLibrary["within_${sanitizedSel}"].${queryName}(...arguments)`,
`
window.TestcafeTestingLibrary = window.TestcafeTestingLibrary || {}
window.TestcafeTestingLibrary["within_${sanitizedSelector}"] = window.TestcafeTestingLibrary["within_${sanitizedSelector}"] || TestingLibraryDom.within(document.querySelector("${sanitizedSelector}"))
return window.TestcafeTestingLibrary["within_${sanitizedSelector}"].${queryName}(...arguments)`,
),
)
})
Expand Down
8 changes: 8 additions & 0 deletions tests/testcafe/within.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,12 @@ test('quotes in selector', async t => {
.click(getByText('Button Text'))
.expect(Selector('button').withExactText('Button Clicked').exists)
.ok()
});

test('still works after browser page reload', async t => {
const nested = await within('#nested');
await t.expect(nested.getByText('Button Text').exists).ok()

await t.eval(() => location.reload(true));
await t.expect(nested.getByText('Button Text').exists).ok()
})