|
| 1 | +import { render, screen } from "@testing-library/react"; |
| 2 | + |
| 3 | +import SetOfCardsWrapper from "../../components/SetOfCardsWrapper"; |
| 4 | + |
| 5 | +describe("SetOfCardsWrapper", () => { |
| 6 | + const mockProps = { |
| 7 | + component: "set-of-cards" as const, |
| 8 | + id: "test-set-of-cards", |
| 9 | + title: "Test Movies", |
| 10 | + fields: [ |
| 11 | + { |
| 12 | + name: "Title", |
| 13 | + data_path: "movie.title", |
| 14 | + data: ["Toy Story", "Finding Nemo"], |
| 15 | + }, |
| 16 | + { |
| 17 | + name: "Year", |
| 18 | + data_path: "movie.year", |
| 19 | + data: [1995, 2003], |
| 20 | + }, |
| 21 | + { |
| 22 | + name: "Rating", |
| 23 | + data_path: "movie.rating", |
| 24 | + data: [8.3, 8.1], |
| 25 | + }, |
| 26 | + ], |
| 27 | + }; |
| 28 | + |
| 29 | + it("renders the component with correct title", () => { |
| 30 | + render(<SetOfCardsWrapper {...mockProps} />); |
| 31 | + expect(screen.getByText("Test Movies")).toBeInTheDocument(); |
| 32 | + }); |
| 33 | + |
| 34 | + it("renders the correct number of cards", () => { |
| 35 | + render(<SetOfCardsWrapper {...mockProps} />); |
| 36 | + // Should render 2 cards based on the data length |
| 37 | + expect(screen.getByText("Test Movies 1")).toBeInTheDocument(); |
| 38 | + expect(screen.getByText("Test Movies 2")).toBeInTheDocument(); |
| 39 | + }); |
| 40 | + |
| 41 | + it("renders cards with correct field data", () => { |
| 42 | + render(<SetOfCardsWrapper {...mockProps} />); |
| 43 | + |
| 44 | + // Check first card data |
| 45 | + expect(screen.getByText("Toy Story")).toBeInTheDocument(); |
| 46 | + expect(screen.getByText("1995")).toBeInTheDocument(); |
| 47 | + expect(screen.getByText("8.3")).toBeInTheDocument(); |
| 48 | + |
| 49 | + // Check second card data |
| 50 | + expect(screen.getByText("Finding Nemo")).toBeInTheDocument(); |
| 51 | + expect(screen.getByText("2003")).toBeInTheDocument(); |
| 52 | + expect(screen.getByText("8.1")).toBeInTheDocument(); |
| 53 | + }); |
| 54 | + |
| 55 | + it("handles empty fields array", () => { |
| 56 | + const emptyProps = { |
| 57 | + ...mockProps, |
| 58 | + fields: [], |
| 59 | + }; |
| 60 | + render(<SetOfCardsWrapper {...emptyProps} />); |
| 61 | + expect(screen.getByText("Test Movies")).toBeInTheDocument(); |
| 62 | + // Should not render any cards |
| 63 | + expect(screen.queryByText("Test Movies 1")).not.toBeInTheDocument(); |
| 64 | + }); |
| 65 | + |
| 66 | + it("handles fields with different data lengths", () => { |
| 67 | + const unevenProps = { |
| 68 | + ...mockProps, |
| 69 | + fields: [ |
| 70 | + { |
| 71 | + name: "Title", |
| 72 | + data_path: "movie.title", |
| 73 | + data: ["Movie 1", "Movie 2", "Movie 3"], |
| 74 | + }, |
| 75 | + { |
| 76 | + name: "Year", |
| 77 | + data_path: "movie.year", |
| 78 | + data: [1995, 2003], // Shorter array |
| 79 | + }, |
| 80 | + ], |
| 81 | + }; |
| 82 | + render(<SetOfCardsWrapper {...unevenProps} />); |
| 83 | + |
| 84 | + // Should render 3 cards based on the longest data array |
| 85 | + expect(screen.getByText("Test Movies 1")).toBeInTheDocument(); |
| 86 | + expect(screen.getByText("Test Movies 2")).toBeInTheDocument(); |
| 87 | + expect(screen.getByText("Test Movies 3")).toBeInTheDocument(); |
| 88 | + }); |
| 89 | + |
| 90 | + it("handles array data correctly", () => { |
| 91 | + const arrayProps = { |
| 92 | + ...mockProps, |
| 93 | + fields: [ |
| 94 | + { |
| 95 | + name: "Actors", |
| 96 | + data_path: "movie.actors", |
| 97 | + data: [ |
| 98 | + ["Tom Hanks", "Tim Allen"], |
| 99 | + ["Albert Brooks", "Ellen DeGeneres"], |
| 100 | + ], |
| 101 | + }, |
| 102 | + ], |
| 103 | + }; |
| 104 | + render(<SetOfCardsWrapper {...arrayProps} />); |
| 105 | + |
| 106 | + // Arrays should be joined with commas |
| 107 | + expect(screen.getByText("Tom Hanks, Tim Allen")).toBeInTheDocument(); |
| 108 | + expect( |
| 109 | + screen.getByText("Albert Brooks, Ellen DeGeneres") |
| 110 | + ).toBeInTheDocument(); |
| 111 | + }); |
| 112 | + |
| 113 | + it("handles null/undefined values", () => { |
| 114 | + const nullProps = { |
| 115 | + ...mockProps, |
| 116 | + fields: [ |
| 117 | + { |
| 118 | + name: "Title", |
| 119 | + data_path: "movie.title", |
| 120 | + data: ["Movie 1", null, "Movie 3"], |
| 121 | + }, |
| 122 | + ], |
| 123 | + }; |
| 124 | + render(<SetOfCardsWrapper {...nullProps} />); |
| 125 | + |
| 126 | + // Should render 3 cards, with empty content for null value |
| 127 | + expect(screen.getByText("Test Movies 1")).toBeInTheDocument(); |
| 128 | + expect(screen.getByText("Test Movies 2")).toBeInTheDocument(); |
| 129 | + expect(screen.getByText("Test Movies 3")).toBeInTheDocument(); |
| 130 | + }); |
| 131 | + |
| 132 | + it("applies custom className", () => { |
| 133 | + const customProps = { |
| 134 | + ...mockProps, |
| 135 | + className: "custom-class", |
| 136 | + }; |
| 137 | + const { container } = render(<SetOfCardsWrapper {...customProps} />); |
| 138 | + expect(container.firstChild).toHaveClass("custom-class"); |
| 139 | + }); |
| 140 | + |
| 141 | + it("renders with correct container structure", () => { |
| 142 | + const { container } = render(<SetOfCardsWrapper {...mockProps} />); |
| 143 | + |
| 144 | + // Check container structure |
| 145 | + const containerElement = container.querySelector("#test-set-of-cards"); |
| 146 | + expect(containerElement).toBeInTheDocument(); |
| 147 | + expect(containerElement).toHaveClass("set-of-cards-container"); |
| 148 | + |
| 149 | + // Check grid structure |
| 150 | + const gridElement = container.querySelector(".set-of-cards-grid"); |
| 151 | + expect(gridElement).toBeInTheDocument(); |
| 152 | + }); |
| 153 | +}); |
| 154 | + |
0 commit comments