Skip to content

Commit

Permalink
add "throwSuggest" to testing library config and follow best practices
Browse files Browse the repository at this point in the history
  • Loading branch information
electricmonk committed May 31, 2024
1 parent 844c7bc commit d03c86f
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 23 deletions.
5 changes: 2 additions & 3 deletions packages/client/setupTests.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import '@testing-library/jest-dom';
import '@testing-library/react';
import {configure} from "@testing-library/react";

beforeAll(() => {
global.IS_REACT_ACT_ENVIRONMENT = false;
});
configure({throwSuggestions: true});
8 changes: 4 additions & 4 deletions packages/client/src/adapters/harness.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,20 @@ export async function makeApp({

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

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

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

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

const driver = {
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/components/Cart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const Cart: React.FC<CartProps> = ({id, onCheckout}) => {

return <section>
<ul>
{summary?.items.map(({productId, name}) => <li key={productId}>{name}</li>)}
{summary?.items.map(({productId, name}) => <li key={productId} aria-label={name}>{name}</li>)}
</ul>
<button aria-label="Checkout" role="button" onClick={checkoutAndViewOrder}>Checkout</button>
</section>
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/components/OrderSummary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const OrderSummary: React.FC<{}> = () => {
<h2>Thank You</h2>
<span> {order?.id}</span>
<ul>
{order?.items.map(({productId, name}) => <li key={productId}>{name}</li>)}
{order?.items.map(({productId, name}) => <li key={productId} aria-label={name}>{name}</li>)}
</ul>
<button aria-label="Home" role="button" onClick={() => navigate("/")}>Home</button>

Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/components/Shop.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const Shop: React.FC<ShopProps> = ({ cartId }) => {
{fetched && (<p aria-label={`${itemCount} items in cart`}>{itemCount} items in cart</p>)}
{fetched && !!itemCount && <button aria-label="View cart" role="button" onClick={viewCart}>View cart</button>}
<section>
<input type="text" placeholder="Search products" value={freeTextSearch} onChange={(e) => setFreeTextSearch(e.target.value)}/>
<input type="text" aria-label="free-text-search" placeholder="Search products" value={freeTextSearch} onChange={(e) => setFreeTextSearch(e.target.value)}/>
<button aria-label="Search">Search</button>
</section>
<Products addItem={addItem} products={products} isLoading={productsLoading} error={productsError} />
Expand Down
14 changes: 6 additions & 8 deletions packages/client/test/purchase.flow.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ test("a user can purchase a product, see the confirmation page and see their ord
});
const {driver, orderRepo} = harness;

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

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

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

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

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

await driver.home();
await driver.findByText("0 items in cart");


await driver.findByRole('paragraph', { name: /0 items in cart/i });
})
10 changes: 5 additions & 5 deletions packages/client/test/search.flow.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ test("Product search is case-insensitive", async () => {
});
const {driver} = harness;

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

expect(driver.queryByText(moogOne.title)).toBeInTheDocument();
expect(driver.queryByText(minimoog.title)).toBeInTheDocument();
expect(driver.queryByText(ob8x.title)).not.toBeInTheDocument();
expect(driver.queryByRole('heading', { name: moogOne.title })).toBeInTheDocument();
expect(driver.queryByRole('heading', { name: minimoog.title })).toBeInTheDocument();
expect(driver.queryByRole('heading', { name: ob8x.title })).not.toBeInTheDocument();

})

0 comments on commit d03c86f

Please sign in to comment.