Skip to content

fix(within): alias findBy queries for within #261

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 21, 2020
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,5 @@ dist
ignorethis.txt

# OSX
.DS_Store
.DS_Store
yarn.lock
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ const withinSelectors = queryNames.reduce((acc, withinQueryName) => {
}
const el = els[0];
const args = Array.from(arguments).slice(1);
return window.TestingLibraryDom.within(el).${withinQueryName}.apply(null, args);
return window.TestingLibraryDom.within(el).${withinQueryName.replace(
"find",
"query"
)}.apply(null, args);
`),
};
}, {} as Record<QueryName, (node: Element, ...methodParams: any[]) => any>);
Expand Down
12 changes: 11 additions & 1 deletion test-app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,17 @@ <h2>getAllByText</h2>
<h2>navigate</h2>
<a href="page2.html">Go to Page 2</a>
</section>

<section>
<div className="App">
<div role="group" aria-labelledby="group-title" id="group-1">
<div id="group-title">My Group</div>
<button onclick="count.innerText = Number(count.innerText)+1">
Increase B
</button>
<div id="count">0</div>
</div>
</div>
</section>
<!-- Prettier
unindents the script tag below -->
<script>
Expand Down
2 changes: 1 addition & 1 deletion tests/testcafe/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ test("queryAllByText", async (t) => {

test("findByText async", async (t) => {
await t.click(getByText("delayed"));
await t.expect(findByText("updated button async")).ok();
await t.expect(findByText("updated button async").exists).ok();
});

test("still works after browser page load", async (t) => {
Expand Down
27 changes: 19 additions & 8 deletions tests/testcafe/within.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Selector } from "testcafe";
import { within, getAllByTestId, getByTestId } from "../../src";
import { within, screen } from "../../src";

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

Expand Down Expand Up @@ -30,18 +30,20 @@ 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.eval(() => location.reload());
await t.expect(nested.getByText("Button Text").exists).ok();
});

test("works with nested selectors", async (t) => {
await t
.expect(within(getByTestId("nested")).getByText("Button Text").exists)
.expect(
within(screen.getByTestId("nested")).getByText("Button Text").exists
)
.ok();
});

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

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

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

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

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

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

await t.expect(nestedDivs.count).eql(2);
let didThrow = false;
try {
await t.expect(within(nestedDivs).getByText("blah"));
await t.expect(within(nestedDivs).getByText("blah").exists);
} catch (e) {
didThrow = true;
}
await t.expect(didThrow).ok();
});

test("works with findBy queries", async (t) => {
const group = screen.findByRole("group", { name: "My Group" });

await t
.click(within(group).findByRole("button", { name: "Increase B" }))
.expect(within(group).findByText("1").exists)
.ok();
});