|
| 1 | +import { Theme } from "@radix-ui/themes"; |
1 | 2 | import { |
2 | 3 | render, |
3 | 4 | screen, |
4 | 5 | waitForElementToBeRemoved, |
5 | 6 | } 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"; |
10 | 7 | import userEvent from "@testing-library/user-event"; |
11 | | -import { db } from "../mocks/db"; |
12 | 8 | import { Category, Product } from "../../src/entities"; |
| 9 | +import BrowseProducts from "../../src/pages/BrowseProductsPage"; |
13 | 10 | import { CartProvider } from "../../src/providers/CartProvider"; |
| 11 | +import { db } from "../mocks/db"; |
14 | 12 | import { simulateDelay, simulateEror } from "../utils"; |
15 | 13 |
|
16 | 14 | describe("BrowseProductsPage", () => { |
17 | 15 | const categories: Category[] = []; |
18 | 16 | const products: Product[] = []; |
19 | 17 |
|
20 | 18 | 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 | + }); |
24 | 25 | }); |
25 | 26 | }); |
26 | 27 |
|
@@ -125,4 +126,59 @@ describe("BrowseProductsPage", () => { |
125 | 126 | expect(screen.getByText(product.name)).toBeInTheDocument(); |
126 | 127 | }); |
127 | 128 | }); |
| 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 | + }); |
128 | 184 | }); |
0 commit comments