Skip to content

Commit 3121563

Browse files
authored
fix(within): alias findBy queries for within (#261)
1 parent 18d0485 commit 3121563

File tree

5 files changed

+37
-12
lines changed

5 files changed

+37
-12
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,5 @@ dist
8383
ignorethis.txt
8484

8585
# OSX
86-
.DS_Store
86+
.DS_Store
87+
yarn.lock

src/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ const withinSelectors = queryNames.reduce((acc, withinQueryName) => {
2121
}
2222
const el = els[0];
2323
const args = Array.from(arguments).slice(1);
24-
return window.TestingLibraryDom.within(el).${withinQueryName}.apply(null, args);
24+
return window.TestingLibraryDom.within(el).${withinQueryName.replace(
25+
"find",
26+
"query"
27+
)}.apply(null, args);
2528
`),
2629
};
2730
}, {} as Record<QueryName, (node: Element, ...methodParams: any[]) => any>);

test-app/index.html

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,17 @@ <h2>getAllByText</h2>
100100
<h2>navigate</h2>
101101
<a href="page2.html">Go to Page 2</a>
102102
</section>
103-
103+
<section>
104+
<div className="App">
105+
<div role="group" aria-labelledby="group-title" id="group-1">
106+
<div id="group-title">My Group</div>
107+
<button onclick="count.innerText = Number(count.innerText)+1">
108+
Increase B
109+
</button>
110+
<div id="count">0</div>
111+
</div>
112+
</div>
113+
</section>
104114
<!-- Prettier
105115
unindents the script tag below -->
106116
<script>

tests/testcafe/selectors.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ test("queryAllByText", async (t) => {
5959

6060
test("findByText async", async (t) => {
6161
await t.click(getByText("delayed"));
62-
await t.expect(findByText("updated button async")).ok();
62+
await t.expect(findByText("updated button async").exists).ok();
6363
});
6464

6565
test("still works after browser page load", async (t) => {

tests/testcafe/within.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Selector } from "testcafe";
2-
import { within, getAllByTestId, getByTestId } from "../../src";
2+
import { within, screen } from "../../src";
33

44
fixture`within`.page`../../test-app/index.html`;
55

@@ -30,18 +30,20 @@ test("still works after browser page reload", async (t) => {
3030
const nested = await within("#nested");
3131
await t.expect(nested.getByText("Button Text").exists).ok();
3232

33-
await t.eval(() => location.reload(true));
33+
await t.eval(() => location.reload());
3434
await t.expect(nested.getByText("Button Text").exists).ok();
3535
});
3636

3737
test("works with nested selectors", async (t) => {
3838
await t
39-
.expect(within(getByTestId("nested")).getByText("Button Text").exists)
39+
.expect(
40+
within(screen.getByTestId("nested")).getByText("Button Text").exists
41+
)
4042
.ok();
4143
});
4244

4345
test('works with nested selector from "All" query with index - regex', async (t) => {
44-
const nestedDivs = getAllByTestId(/nested/);
46+
const nestedDivs = screen.getAllByTestId(/nested/);
4547
await t.expect(nestedDivs.count).eql(2);
4648

4749
const nested = within(nestedDivs.nth(1));
@@ -54,15 +56,15 @@ test('works with nested selector from "All" query with index - regex', async (t)
5456
});
5557

5658
test('works with nested selector from "All" query with index - exact:false', async (t) => {
57-
const nestedDivs = getAllByTestId("nested", { exact: false });
59+
const nestedDivs = screen.getAllByTestId("nested", { exact: false });
5860
await t.expect(nestedDivs.count).eql(2);
5961
const nested = await within(nestedDivs.nth(0));
6062

6163
await t.expect(nested.getByText("Button Text").exists).ok();
6264
});
6365

6466
test('works with nested selector from "All" query with index - function', async (t) => {
65-
const nestedDivs = getAllByTestId((_content, element) =>
67+
const nestedDivs = screen.getAllByTestId((_content, element) =>
6668
element.getAttribute("data-testid")!.startsWith("nested")
6769
);
6870
await t.expect(nestedDivs.count).eql(2);
@@ -89,14 +91,23 @@ test("should throw if invalid param", async (t) => {
8991
});
9092

9193
test("should throw error if count > 1", async (t) => {
92-
const nestedDivs = getAllByTestId(/nested/);
94+
const nestedDivs = screen.getAllByTestId(/nested/);
9395

9496
await t.expect(nestedDivs.count).eql(2);
9597
let didThrow = false;
9698
try {
97-
await t.expect(within(nestedDivs).getByText("blah"));
99+
await t.expect(within(nestedDivs).getByText("blah").exists);
98100
} catch (e) {
99101
didThrow = true;
100102
}
101103
await t.expect(didThrow).ok();
102104
});
105+
106+
test("works with findBy queries", async (t) => {
107+
const group = screen.findByRole("group", { name: "My Group" });
108+
109+
await t
110+
.click(within(group).findByRole("button", { name: "Increase B" }))
111+
.expect(within(group).findByText("1").exists)
112+
.ok();
113+
});

0 commit comments

Comments
 (0)