|
| 1 | +/* @vitest-environment jsdom */ |
| 2 | +/// <reference types="@testing-library/jest-dom" /> |
| 3 | +import "@testing-library/jest-dom/vitest"; |
| 4 | +import {describe, it, expect, vi, beforeAll, afterAll, beforeEach, afterEach} from "vitest"; |
| 5 | +import {render, waitFor, cleanup} from "@testing-library/react"; |
| 6 | +import userEvent from "@testing-library/user-event"; |
| 7 | +import SearchByBar from "./SearchByBar"; |
| 8 | +import {SearchProvider} from "../../../context/SearchContext"; |
| 9 | +import {DataProvider} from "../../../context/DataContext"; |
| 10 | +import {getFilteredProducts} from "../../../services/Requests"; |
| 11 | + |
| 12 | +const __realGetComputedStyle = window.getComputedStyle; |
| 13 | +const __realMatchMedia = window.matchMedia as any; |
| 14 | + |
| 15 | +vi.mock("../../../services/Requests", () => ({ |
| 16 | + getFilteredProducts: vi.fn(), |
| 17 | + getCategories: vi.fn(), |
| 18 | + getSummary: vi.fn(), |
| 19 | +})); |
| 20 | + |
| 21 | +function renderWithProviders() { |
| 22 | + const u = userEvent.setup(); |
| 23 | + const view = render( |
| 24 | + <SearchProvider> |
| 25 | + <DataProvider> |
| 26 | + <SearchByBar/> |
| 27 | + </DataProvider> |
| 28 | + </SearchProvider> |
| 29 | + ); |
| 30 | + return {user: u, ...view}; |
| 31 | +} |
| 32 | + |
| 33 | +describe("SearchByBar tests", () => { |
| 34 | + beforeAll(() => { |
| 35 | + window.getComputedStyle = (elt: Element, _pseudo?: string | null) => { |
| 36 | + const style = __realGetComputedStyle(elt); |
| 37 | + const gpv = (prop: string) => { |
| 38 | + if (typeof (style as any).getPropertyValue === "function") { |
| 39 | + return (style as any).getPropertyValue(prop); |
| 40 | + } |
| 41 | + if ( |
| 42 | + prop === "animation-duration" || |
| 43 | + prop === "animation-delay" || |
| 44 | + prop === "transition-duration" || |
| 45 | + prop === "transition-delay" |
| 46 | + ) { |
| 47 | + return "0s"; |
| 48 | + } |
| 49 | + return ""; |
| 50 | + }; |
| 51 | + return {...style, getPropertyValue: gpv} as CSSStyleDeclaration; |
| 52 | + }; |
| 53 | + window.matchMedia = (query: string) => ({ |
| 54 | + matches: false, |
| 55 | + media: query, |
| 56 | + onchange: null, |
| 57 | + addListener: vi.fn(), |
| 58 | + removeListener: vi.fn(), |
| 59 | + addEventListener: vi.fn(), |
| 60 | + removeEventListener: vi.fn(), |
| 61 | + dispatchEvent: vi.fn(), |
| 62 | + }); |
| 63 | + }); |
| 64 | + afterAll(() => { |
| 65 | + window.getComputedStyle = __realGetComputedStyle; |
| 66 | + window.matchMedia = __realMatchMedia; |
| 67 | + }); |
| 68 | + beforeEach(() => { |
| 69 | + vi.clearAllMocks(); |
| 70 | + vi.mocked(getFilteredProducts).mockResolvedValueOnce({ |
| 71 | + data: {products: [], totalPages: 3}, |
| 72 | + } as any); |
| 73 | + }); |
| 74 | + afterEach(() => { |
| 75 | + cleanup(); |
| 76 | + vi.resetAllMocks(); |
| 77 | + }); |
| 78 | + |
| 79 | + it("debounce name input and triggers refetch", async () => { |
| 80 | + const {user, findByPlaceholderText, getByPlaceholderText} = renderWithProviders(); |
| 81 | + await findByPlaceholderText(/Watermelon/i); |
| 82 | + const name = getByPlaceholderText(/Watermelon/i); |
| 83 | + await user.click(name); |
| 84 | + await user.type(name, "Watermelon"); |
| 85 | + await waitFor(() => { |
| 86 | + expect(getFilteredProducts).toHaveBeenCalledTimes(2); |
| 87 | + }); |
| 88 | + expect(getFilteredProducts).toHaveBeenLastCalledWith( |
| 89 | + expect.objectContaining({name: "Watermelon", page: 0}) |
| 90 | + ); |
| 91 | + }); |
| 92 | + |
| 93 | + it("debounce category input and triggers refetch", async () => { |
| 94 | + const {user, findByPlaceholderText, getByPlaceholderText} = renderWithProviders(); |
| 95 | + await findByPlaceholderText(/Food/i); |
| 96 | + const category = getByPlaceholderText(/Food/i); |
| 97 | + await user.click(category); |
| 98 | + await user.type(category, "Beverages"); |
| 99 | + await waitFor(() => { |
| 100 | + expect(getFilteredProducts).toHaveBeenCalledTimes(2); |
| 101 | + }); |
| 102 | + expect(getFilteredProducts).toHaveBeenLastCalledWith( |
| 103 | + expect.objectContaining({category: "Beverages", page: 0}) |
| 104 | + ); |
| 105 | + }); |
| 106 | + |
| 107 | + it("choosing In stock in the cascader and triggers refetch", async () => { |
| 108 | + const {user, findByText, getByText, findAllByText} = renderWithProviders(); |
| 109 | + await findByText(/Everything/i); |
| 110 | + const stockCascader = getByText(/Everything/i); |
| 111 | + await user.click(stockCascader); |
| 112 | + const inStock = await findAllByText(/stock/i); |
| 113 | + await user.click(inStock[inStock.length - 1]); |
| 114 | + await waitFor(() => { |
| 115 | + expect(getFilteredProducts).toHaveBeenCalledTimes(2); |
| 116 | + }); |
| 117 | + expect(getFilteredProducts).toHaveBeenLastCalledWith( |
| 118 | + expect.objectContaining({stockQuantity: expect.anything()}) |
| 119 | + ); |
| 120 | + }); |
| 121 | +}); |
0 commit comments