1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
|
import { fireEvent, render, screen, waitFor } from "@testing-library/react"; import { Product, ProductProps } from "./product";
const productProps: ProductProps = { title: "测试商品", cover: "测试封面链接", interest: "测试利益点", price: 100, categorys: ["新品", "促销"], tags: ["标签1", "标签2"], handleBuy: function (): void {}, };
describe("Product 组件", () => { test("渲染商品卡片的内容是否正确", () => { render(<Product {...productProps} />);
const titleElement = screen.getByText(productProps.title); expect(titleElement).toBeInTheDocument();
productProps.categorys?.forEach((category) => { const categorysElement = screen.getByText(category); expect(categorysElement).toBeInTheDocument(); });
if (productProps.interest) { const interestElement = screen.getByText(productProps.interest); expect(interestElement).toBeInTheDocument(); }
const priceElement = screen.getByText(`¥${productProps.price}`); expect(priceElement).toBeInTheDocument();
productProps.tags?.forEach((tag) => { const tagElement = screen.getByText(tag); expect(tagElement).toBeInTheDocument(); });
const buyButtonElement = screen.getByText("购买"); expect(buyButtonElement).toBeInTheDocument(); });
test("当标题为空时,不渲染标题", () => { const propsWithoutInterest: ProductProps = { ...productProps, title: "", };
render(<Product {...propsWithoutInterest} />); const interestElement = screen.queryByTestId("interest"); expect(interestElement).toBeNull(); });
test("当类目数组为空时,不渲染任何类目", () => { const propsWithoutTags: ProductProps = { ...productProps, categorys: [], };
render(<Product {...propsWithoutTags} />); const tagElements = screen.queryAllByTestId("tag"); expect(tagElements).toHaveLength(0); });
test("当利益点为空时,不渲染利益点", () => { const propsWithoutInterest: ProductProps = { ...productProps, interest: undefined, };
render(<Product {...propsWithoutInterest} />); const interestElement = screen.queryByTestId("interest"); expect(interestElement).toBeNull(); });
test("当价格为零时,渲染为免费", () => { const freeProductProps: ProductProps = { ...productProps, price: 0, };
render(<Product {...freeProductProps} />); const priceElement = screen.getByText("免费"); expect(priceElement).toBeInTheDocument(); });
test("当价格为负数时,渲染为免费", () => { const negativePriceProps: ProductProps = { ...productProps, price: -100, };
render(<Product {...negativePriceProps} />); const priceElement = screen.getByText("免费"); expect(priceElement).toBeInTheDocument(); });
test("当标签数组为空时,不渲染任何标签", () => { const propsWithoutTags: ProductProps = { ...productProps, tags: [], };
render(<Product {...propsWithoutTags} />); const tagElements = screen.queryAllByTestId("tag"); expect(tagElements).toHaveLength(0); });
test("渲染商品卡片的布局是否正确", () => { const verticalProps: ProductProps = { ...productProps, layout: "vertical", };
const { container } = render(<Product {...verticalProps} />); const productElement = container.querySelector(".com-product"); expect(productElement).toHaveClass("layout_vertical"); });
test("商品标签应该是唯一的", () => { if (productProps.tags !== undefined) { const uniqueTags = [...new Set(productProps.tags)]; expect(uniqueTags.length).toBe(productProps.tags.length); } });
test("当封面链接为空时,渲染默认封面图", () => { const propsWithoutCover: ProductProps = { ...productProps, cover: "", };
const { container } = render(<Product {...propsWithoutCover} />); const coverElement = container .querySelector(".com-product-cover") ?.querySelector("img"); expect(coverElement?.src).toEqual( "https://pic.imgdb.cn/item/65f9367d9f345e8d036c28cf.png" ); });
test("点击购买按钮触发购买操作", () => { const mockHandleBuy = jest.fn();
const propsWithoutCover: ProductProps = { ...productProps, handleBuy: mockHandleBuy, };
const { container } = render(<Product {...propsWithoutCover} />); const buyButtonElement = container.querySelector(".com-product-buy-button"); if (buyButtonElement) { fireEvent.click(buyButtonElement); } expect(mockHandleBuy).toHaveBeenCalled(); });
test("点击购买按钮触发异步购买操作", async () => { const mockHandleBuy = jest.fn(async () => { await setTimeout(() => { Promise.resolve(); }, 2000); });
const propsWithoutCover: ProductProps = { ...productProps, handleBuy: mockHandleBuy, };
const { container } = render(<Product {...propsWithoutCover} />); const buyButtonElement = container.querySelector(".com-product-buy-button"); if (buyButtonElement) { fireEvent.click(buyButtonElement); } await waitFor(() => { expect(mockHandleBuy).toHaveBeenCalled(); }); });
test("点击组件后,组件新增了一个特定的class", () => { const { container } = render(<Product {...productProps} />); const productElement = container.querySelector(".com-product"); if (productElement) { fireEvent.click(productElement); expect(productElement).toHaveClass("active"); } }); });
|