Skip to content

Commit 9736c3d

Browse files
committed
Exercise- Testing Filtering
1 parent 9b84d39 commit 9736c3d

File tree

2 files changed

+70
-12
lines changed

2 files changed

+70
-12
lines changed

tests/mocks/db.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
import { factory, primaryKey } from "@mswjs/data";
1+
import { factory, manyOf, oneOf, primaryKey } from "@mswjs/data";
22
import { faker } from "@faker-js/faker";
33

44
export const db = factory({
55
category: {
66
id: primaryKey(faker.number.int),
7-
name : faker.commerce.department
7+
name: faker.commerce.department,
8+
products: manyOf("product"),
89
},
910
product: {
1011
id: primaryKey(faker.number.int),
1112
name: faker.commerce.productName,
1213
price: () => faker.number.int({ min: 1, max: 100 }),
13-
categoryId: faker.number.int
14+
categoryId: faker.number.int,
15+
category: oneOf("category"),
1416
},
15-
});
17+
});

tests/pages/BrowseProductsPage.test.tsx

Lines changed: 64 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
1+
import { Theme } from "@radix-ui/themes";
12
import {
23
render,
34
screen,
45
waitForElementToBeRemoved,
56
} from "@testing-library/react";
6-
import { server } from "../mocks/server";
7-
import { http, HttpResponse } from "msw";
8-
import BrowseProducts from "../../src/pages/BrowseProductsPage";
9-
import { Theme } from "@radix-ui/themes";
107
import userEvent from "@testing-library/user-event";
11-
import { db } from "../mocks/db";
128
import { Category, Product } from "../../src/entities";
9+
import BrowseProducts from "../../src/pages/BrowseProductsPage";
1310
import { CartProvider } from "../../src/providers/CartProvider";
11+
import { db } from "../mocks/db";
1412
import { simulateDelay, simulateEror } from "../utils";
1513

1614
describe("BrowseProductsPage", () => {
1715
const categories: Category[] = [];
1816
const products: Product[] = [];
1917

2018
beforeAll(() => {
21-
[1, 2].forEach((item) => {
22-
categories.push(db.category.create({ name: "Category" + item }));
23-
products.push(db.product.create());
19+
[1, 2].forEach(() => {
20+
const category = db.category.create();
21+
categories.push(category);
22+
[1, 2].forEach(() => {
23+
products.push(db.product.create({ categoryId: category.id }));
24+
});
2425
});
2526
});
2627

@@ -125,4 +126,59 @@ describe("BrowseProductsPage", () => {
125126
expect(screen.getByText(product.name)).toBeInTheDocument();
126127
});
127128
});
129+
130+
it("should filter prodcuts by category", async () => {
131+
const { getCategorySkeleton, getCategoriesComboBox } = renderComponent();
132+
133+
// Arrage
134+
await waitForElementToBeRemoved(getCategorySkeleton);
135+
const combobox = getCategoriesComboBox();
136+
const user = userEvent.setup();
137+
await user.click(combobox!);
138+
139+
// Act
140+
const selectedCategory = categories[0];
141+
const option = screen.getByRole("option", { name: selectedCategory.name });
142+
await user.click(option);
143+
144+
// Assert
145+
const product = db.product.findMany({
146+
where: {
147+
categoryId: { equals: selectedCategory.id },
148+
},
149+
});
150+
151+
const rows = screen.getAllByRole("row");
152+
const dataRows = rows.slice(1);
153+
expect(dataRows).toHaveLength(product.length);
154+
155+
products.forEach((product) => {
156+
expect(screen.getByText(product.name)).toBeInTheDocument();
157+
});
158+
});
159+
160+
it("should all product if all category is selected", async () => {
161+
const { getCategorySkeleton, getCategoriesComboBox } = renderComponent();
162+
163+
// Arrage
164+
await waitForElementToBeRemoved(getCategorySkeleton);
165+
const combobox = getCategoriesComboBox();
166+
const user = userEvent.setup();
167+
await user.click(combobox!);
168+
169+
// Act
170+
const option = screen.getByRole("option", { name: /all/i });
171+
await user.click(option);
172+
173+
// Assert
174+
const product = db.product.getAll();
175+
176+
const rows = screen.getAllByRole("row");
177+
const dataRows = rows.slice(1);
178+
expect(dataRows).toHaveLength(product.length);
179+
180+
products.forEach((product) => {
181+
expect(screen.getByText(product.name)).toBeInTheDocument();
182+
});
183+
});
128184
});

0 commit comments

Comments
 (0)