|
| 1 | +import React from "react"; |
| 2 | +import { describe, expect, it, vitest } from "vitest"; |
| 3 | +import BohnenRow from "@/components/bohnen/BohnenRow"; |
| 4 | +import { getMockBohne } from "@/test/mockData"; |
| 5 | +import { screen } from "@testing-library/react"; |
| 6 | +import { render } from "@/test/userEvent"; |
| 7 | + |
| 8 | +const dispatchMock = vitest.hoisted(() => vitest.fn()); |
| 9 | + |
| 10 | +// Hier mocken wir das react Modul, damit useContext unseren dispatchMock zurückgibt |
| 11 | +// der Rest wird mit importOriginal() aus dem Originalmodul geladen |
| 12 | +vitest.mock("react", async (importOriginal) => ({ |
| 13 | + ...(await importOriginal()), |
| 14 | + useContext: () => dispatchMock, |
| 15 | +})); |
| 16 | + |
| 17 | +describe("BohnenRow", () => { |
| 18 | + it("should render an input field to change the Bohnenart", () => { |
| 19 | + const mockBohne = getMockBohne(); |
| 20 | + render( |
| 21 | + <table> |
| 22 | + <tbody> |
| 23 | + <BohnenRow bohne={mockBohne} /> |
| 24 | + </tbody> |
| 25 | + </table>, |
| 26 | + ); |
| 27 | + const input = screen.getByLabelText("Bohnenart"); |
| 28 | + expect(input).toBeVisible(); |
| 29 | + }); |
| 30 | + |
| 31 | + it("should dispatch an UPDATE event if the user clears the input for Bohnenart", async () => { |
| 32 | + const mockBohne = getMockBohne(); |
| 33 | + const { user } = render( |
| 34 | + <table> |
| 35 | + <tbody> |
| 36 | + <BohnenRow bohne={mockBohne} /> |
| 37 | + </tbody> |
| 38 | + </table>, |
| 39 | + ); |
| 40 | + const input = screen.getByLabelText("Bohnenart"); |
| 41 | + |
| 42 | + await user.clear(input); |
| 43 | + await user.type(input, "test"); |
| 44 | + |
| 45 | + expect(dispatchMock).toHaveBeenCalledWith({ |
| 46 | + type: "UPDATE", |
| 47 | + payload: { ...mockBohne, art: "" }, |
| 48 | + }); |
| 49 | + }); |
| 50 | +}); |
0 commit comments