|
| 1 | +import { fireEvent, render } from "@testing-library/vue"; |
| 2 | + |
| 3 | +import SingleProduct from "../../../resources/js/components/Products/SingleProduct.vue"; |
| 4 | +import { useCart } from "../../../resources/js/store/useCart"; |
| 5 | +import formatPrice from "../../../resources/js/utils/functions"; |
| 6 | + |
| 7 | +import "@testing-library/jest-dom"; |
| 8 | + |
| 9 | +jest.mock("swrv", () => ({ |
| 10 | + __esModule: true, |
| 11 | + default: jest.fn(), |
| 12 | +})); |
| 13 | + |
| 14 | +// Mock the useCart function |
| 15 | +jest.mock("@/store/useCart", () => { |
| 16 | + const addToCartMock = jest.fn(); |
| 17 | + return { |
| 18 | + useCart: jest.fn(() => ({ addToCart: addToCartMock })), |
| 19 | + __addToCartMock: addToCartMock, |
| 20 | + }; |
| 21 | +}); |
| 22 | + |
| 23 | +// Mock the useRoute function |
| 24 | +jest.mock("vue-router", () => ({ |
| 25 | + useRoute: jest.fn(() => ({ |
| 26 | + params: { |
| 27 | + slug: "product-1", |
| 28 | + }, |
| 29 | + })), |
| 30 | +})); |
| 31 | + |
| 32 | +const useRouterLink = { |
| 33 | + template: "<a><slot></slot></a>", |
| 34 | +}; |
| 35 | + |
| 36 | +/** |
| 37 | + * Test case to verify the 'Add to Cart' functionality. |
| 38 | + * |
| 39 | + * @name Verify 'Add to Cart' functionality |
| 40 | + * @function |
| 41 | + * @async |
| 42 | + * @memberof module:tests |
| 43 | + * |
| 44 | + * @description |
| 45 | + * The test case does the following: |
| 46 | + * 1. Mocks the `useSWRV` hook to return the product data. |
| 47 | + * 2. Mocks the `useCart` store to use a Jest function as the `addToCart` method. |
| 48 | + * 3. Renders the `SingleProduct` component with the necessary global components. |
| 49 | + * 4. Verifies the product information is displayed. |
| 50 | + * 5. Clicks the 'Add To Cart' button. |
| 51 | + * 6. Verifies that the `addToCart` method in the store is called with the correct product. |
| 52 | + * |
| 53 | + * @returns {void} |
| 54 | + */ |
| 55 | +test("Verify 'Add to Cart' functionality", async () => { |
| 56 | + const useSWRV = require("swrv").default; |
| 57 | + const product = { |
| 58 | + id: 1, |
| 59 | + name: "Product 1", |
| 60 | + price: 100, |
| 61 | + slug: "product-1", |
| 62 | + imageUrl: "image1.jpg", |
| 63 | + description: "Sample description", |
| 64 | + }; |
| 65 | + useSWRV.mockReturnValue({ data: product, error: null }); |
| 66 | + |
| 67 | + const addToCartMock = jest.fn(); |
| 68 | + useCart.mockReturnValue({ |
| 69 | + addToCart: addToCartMock, |
| 70 | + }); |
| 71 | + |
| 72 | + const { getByText } = render(SingleProduct, { |
| 73 | + global: { |
| 74 | + components: { |
| 75 | + "router-link": useRouterLink, |
| 76 | + }, |
| 77 | + }, |
| 78 | + }); |
| 79 | + |
| 80 | + // Verify product information is displayed |
| 81 | + expect(getByText(product.name)).toBeInTheDocument(); |
| 82 | + expect(getByText(formatPrice(product.price))).toBeInTheDocument(); |
| 83 | + expect(getByText(product.description)).toBeInTheDocument(); |
| 84 | + |
| 85 | + // Click the 'Add To Cart' button |
| 86 | + const addToCartButton = getByText("Add To Cart"); |
| 87 | + await fireEvent.click(addToCartButton); |
| 88 | + |
| 89 | + // Verify that the `addToCart` method in the store is called with the correct product |
| 90 | + expect(addToCartMock).toHaveBeenCalledTimes(1); |
| 91 | + expect(addToCartMock).toHaveBeenCalledWith({ item: product }); |
| 92 | +}); |
0 commit comments