Skip to content

Commit 1f8ac11

Browse files
author
Ben Monro
committed
feat: within support
1 parent dafbb16 commit 1f8ac11

File tree

5 files changed

+104
-8
lines changed

5 files changed

+104
-8
lines changed

src/index.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function injectNWTL(browser) {
2929
}
3030

3131

32-
module.exports.getQueriesFrom = (browser) => {
32+
const getQueriesFrom = (browser, container) => {
3333
const queries = {};
3434
Object.keys(baseQueries).forEach(queryName => {
3535
queries[queryName] = (...args) => new Promise((resolve, reject) => {
@@ -42,7 +42,7 @@ module.exports.getQueriesFrom = (browser) => {
4242

4343
injectNWTL(browser)
4444
// eslint-disable-next-line no-shadow
45-
browser.execute(function (queryName, ...args) {
45+
browser.execute(function (queryName, container, ...args) {
4646
try {
4747
args = args.map(arg => {
4848
if (arg.RegExp) {
@@ -51,14 +51,15 @@ module.exports.getQueriesFrom = (browser) => {
5151
}
5252
return arg;
5353
});
54+
const root = container ? document.querySelector(container) : document.body;
5455
if (/AllBy/.test(queryName)) {
55-
const elms = window.TestingLibraryDom[queryName](document.body, ...args);
56+
const elms = window.TestingLibraryDom[queryName](root, ...args);
5657

5758
const selector = elms.map(elm => window.Simmer(elm)).join(', ');
5859

5960
return selector;
6061
} else {
61-
const elm = window.TestingLibraryDom[queryName](document.body, ...args);
62+
const elm = window.TestingLibraryDom[queryName](root, ...args);
6263

6364
const selector = window.Simmer(elm);
6465

@@ -67,7 +68,7 @@ module.exports.getQueriesFrom = (browser) => {
6768
} catch (e) {
6869
return { error: { message: e.message, stack: e.stack } };
6970
}
70-
}, [queryName, ...args], (result) => {
71+
}, [queryName, container, ...args], (result) => {
7172

7273

7374
if (result.value.error) {
@@ -83,7 +84,8 @@ module.exports.getQueriesFrom = (browser) => {
8384
}
8485
resolve({
8586
selector,
86-
nth(index) { return (({ selector, index })) }
87+
nth(index) { return (({ selector: selector.split(/, /)[index], browser: () => browser })) },
88+
browser: () => browser
8789
})
8890

8991

@@ -93,7 +95,12 @@ module.exports.getQueriesFrom = (browser) => {
9395

9496
return queries;
9597
}
96-
97-
module.exports.configure = (config) => {
98+
const within = (selector) => {
99+
return getQueriesFrom(selector.browser(), selector.selector);
100+
}
101+
const configure = (config) => {
98102
_config = config;
103+
}
104+
module.exports = {
105+
configure, getQueriesFrom, within
99106
}

tests/globals/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
3+
4+
}

tests/nightwatch/configure.test.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const { configure, getQueriesFrom } = require('../../src');
2+
3+
configure({ testIdAttribute: 'data-automation-id' });
4+
5+
module.exports = {
6+
before() {
7+
8+
configure({ testIdAttribute: 'data-automation-id' });
9+
10+
},
11+
after() {
12+
configure(null);
13+
},
14+
beforeEach(browser, done) {
15+
browser
16+
.url('http://localhost:13370');
17+
done()
18+
},
19+
async 'supports alternative testIdAttribute'(browser) {
20+
const { getByTestId } = getQueriesFrom(browser);
21+
22+
const image = await getByTestId('image-with-random-alt-tag');
23+
browser.click(image);
24+
browser.expect.element(image).to.have.css('border').which.equals('5px solid rgb(255, 0, 0)')
25+
26+
},
27+
28+
async 'still works after navigation'(browser) {
29+
const { getByTestId, getByText } = getQueriesFrom(browser);
30+
31+
browser.click(await getByText('Go to Page 2'));
32+
33+
browser.click(await getByTestId('page2-thing'));
34+
35+
browser.expect.element(await getByText('second page')).to.be.present;
36+
37+
}
38+
}

tests/nightwatch/index.test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ const { getQueriesFrom } = require('../../src');
22

33
module.exports = {
44

5+
beforeEach(browser, done) {
6+
browser
7+
.url('http://localhost:13370');
8+
done()
9+
},
510

611
async 'Button click works'(browser) {
712
const { getByText } = getQueriesFrom(browser);

tests/nightwatch/within.test.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const { getQueriesFrom, within } = require('../../src');
2+
3+
4+
module.exports = {
5+
beforeEach(browser, done) {
6+
browser
7+
.url('http://localhost:13370');
8+
done()
9+
},
10+
async 'getByText within container'(browser) {
11+
const { getByTestId } = getQueriesFrom(browser);
12+
13+
const nested = await getByTestId('nested');
14+
const button = await within(nested).getByText('Button Text');
15+
16+
browser.click(button);
17+
browser.expect.element(button).text.to.equal('Button Clicked');
18+
19+
},
20+
21+
22+
async 'works with nested selector from "All" query with index - regex'(browser) {
23+
24+
const { getAllByTestId } = getQueriesFrom(browser);
25+
26+
const nestedDivs = await getAllByTestId(/nested/);
27+
28+
browser.expect.elements(nestedDivs).count.to.equal(2)
29+
30+
const nested = within(nestedDivs.nth(1));
31+
32+
const button = await nested.getByText('Button Text');
33+
const text = await nested.getByText('text only in 2nd nested');
34+
35+
browser.expect.element(button).to.be.present;
36+
browser.expect.element(text).to.be.present;
37+
// await t
38+
// .expect(nested.getByText('Button Text').exists).ok()
39+
// .expect(nested.getByText('text only in 2nd nested').exists).ok()
40+
41+
},
42+
}

0 commit comments

Comments
 (0)