Skip to content

Commit d03c86f

Browse files
committed
add "throwSuggest" to testing library config and follow best practices
1 parent 844c7bc commit d03c86f

File tree

7 files changed

+20
-23
lines changed

7 files changed

+20
-23
lines changed

packages/client/setupTests.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import '@testing-library/jest-dom';
22
import '@testing-library/react';
3+
import {configure} from "@testing-library/react";
34

4-
beforeAll(() => {
5-
global.IS_REACT_ACT_ENVIRONMENT = false;
6-
});
5+
configure({throwSuggestions: true});

packages/client/src/adapters/harness.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,20 @@ export async function makeApp({
2626

2727
const addProductToCart = async (title: string) => {
2828
const product = await app.findByLabelText(title)
29-
const add = within(product).getByText("Add");
29+
const add = within(product).getByRole('button', { name: /add to cart/i });
3030
await userEvent.click(add);
3131
}
3232

3333
const viewCart = async () => {
34-
await userEvent.click(await app.findByText("View cart"));
34+
await userEvent.click(await app.findByRole('button', { name: /view cart/i }));
3535
}
3636

3737
const checkout = async () => {
38-
await userEvent.click(app.getByText("Checkout"));
38+
await userEvent.click(app.getByRole('button', { name: /checkout/i }));
3939
}
4040

4141
const home = async () => {
42-
await userEvent.click(app.getByText("Home"));
42+
await userEvent.click(app.getByRole('button', { name: /home/i }));
4343
}
4444

4545
const driver = {

packages/client/src/components/Cart.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export const Cart: React.FC<CartProps> = ({id, onCheckout}) => {
2929

3030
return <section>
3131
<ul>
32-
{summary?.items.map(({productId, name}) => <li key={productId}>{name}</li>)}
32+
{summary?.items.map(({productId, name}) => <li key={productId} aria-label={name}>{name}</li>)}
3333
</ul>
3434
<button aria-label="Checkout" role="button" onClick={checkoutAndViewOrder}>Checkout</button>
3535
</section>

packages/client/src/components/OrderSummary.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export const OrderSummary: React.FC<{}> = () => {
1919
<h2>Thank You</h2>
2020
<span> {order?.id}</span>
2121
<ul>
22-
{order?.items.map(({productId, name}) => <li key={productId}>{name}</li>)}
22+
{order?.items.map(({productId, name}) => <li key={productId} aria-label={name}>{name}</li>)}
2323
</ul>
2424
<button aria-label="Home" role="button" onClick={() => navigate("/")}>Home</button>
2525

packages/client/src/components/Shop.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export const Shop: React.FC<ShopProps> = ({ cartId }) => {
1717
{fetched && (<p aria-label={`${itemCount} items in cart`}>{itemCount} items in cart</p>)}
1818
{fetched && !!itemCount && <button aria-label="View cart" role="button" onClick={viewCart}>View cart</button>}
1919
<section>
20-
<input type="text" placeholder="Search products" value={freeTextSearch} onChange={(e) => setFreeTextSearch(e.target.value)}/>
20+
<input type="text" aria-label="free-text-search" placeholder="Search products" value={freeTextSearch} onChange={(e) => setFreeTextSearch(e.target.value)}/>
2121
<button aria-label="Search">Search</button>
2222
</section>
2323
<Products addItem={addItem} products={products} isLoading={productsLoading} error={productsError} />

packages/client/test/purchase.flow.spec.tsx

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@ test("a user can purchase a product, see the confirmation page and see their ord
1010
});
1111
const {driver, orderRepo} = harness;
1212

13-
await driver.findByText("0 items in cart");
13+
await driver.findByRole('paragraph', { name: /0 items in cart/i });
1414

1515
await driver.addProductToCart(moogOne.title);
16-
await driver.findByText("1 items in cart");
16+
await driver.findByRole('paragraph', { name: /1 items in cart/i });
1717

1818
await driver.viewCart();
19-
expect(await driver.findByText(moogOne.title)).toBeTruthy();
19+
expect(await driver.findByRole('listitem', { name: moogOne.title })).toBeTruthy();
2020

2121
await driver.checkout();
22-
expect(await driver.findByText("Thank You")).toBeTruthy();
23-
expect(await driver.findByText(moogOne.title)).toBeTruthy();
22+
expect(await driver.findByRole('heading', { name: /thank you/i })).toBeTruthy();
23+
expect(await driver.findByRole('listitem', {name: moogOne.title})).toBeTruthy();
2424

2525
expect(orderRepo.orders).toContainEqual(expect.objectContaining({
2626
items: expect.arrayContaining([
@@ -31,7 +31,5 @@ test("a user can purchase a product, see the confirmation page and see their ord
3131
}));
3232

3333
await driver.home();
34-
await driver.findByText("0 items in cart");
35-
36-
34+
await driver.findByRole('paragraph', { name: /0 items in cart/i });
3735
})

packages/client/test/search.flow.spec.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ test("Product search is case-insensitive", async () => {
1313
});
1414
const {driver} = harness;
1515

16-
await userEvent.type(driver.getByPlaceholderText('Search products'), 'moog');
17-
await userEvent.click(driver.getByLabelText('Search'));
16+
await userEvent.type(driver.getByRole('textbox', {name: 'free-text-search'}), 'moog');
17+
await userEvent.click(driver.getByRole('button', { name: /search/i }));
1818

19-
expect(driver.queryByText(moogOne.title)).toBeInTheDocument();
20-
expect(driver.queryByText(minimoog.title)).toBeInTheDocument();
21-
expect(driver.queryByText(ob8x.title)).not.toBeInTheDocument();
19+
expect(driver.queryByRole('heading', { name: moogOne.title })).toBeInTheDocument();
20+
expect(driver.queryByRole('heading', { name: minimoog.title })).toBeInTheDocument();
21+
expect(driver.queryByRole('heading', { name: ob8x.title })).not.toBeInTheDocument();
2222

2323
})

0 commit comments

Comments
 (0)