|
| 1 | +import { render, screen } from "@testing-library/react"; |
| 2 | +import { ThemeProvider } from "styled-components"; |
| 3 | +import { DEFAULT_THEME } from "@zendeskgarden/react-theming"; |
| 4 | +import ServiceCatalogListItem from "./ServiceCatalogListItem"; |
| 5 | +import type { ServiceCatalogItem } from "../../data-types/ServiceCatalogItem"; |
| 6 | +import { userEvent } from "@testing-library/user-event"; |
| 7 | + |
| 8 | +jest.mock("@zendeskgarden/svg-icons/src/16/shapes-fill.svg", () => { |
| 9 | + return function MockShapesIcon() { |
| 10 | + return <div data-testid="atl-nacional" />; |
| 11 | + }; |
| 12 | +}); |
| 13 | + |
| 14 | +const theme = { |
| 15 | + ...DEFAULT_THEME, |
| 16 | + colors: { |
| 17 | + ...DEFAULT_THEME.colors, |
| 18 | + foreground: "#ff0000", |
| 19 | + primaryHue: "#0000ff", |
| 20 | + }, |
| 21 | +}; |
| 22 | + |
| 23 | +const renderWithTheme = (component: React.ReactElement) => { |
| 24 | + return render(<ThemeProvider theme={theme}>{component}</ThemeProvider>); |
| 25 | +}; |
| 26 | + |
| 27 | +describe("ServiceCatalogListItem", () => { |
| 28 | + const mockServiceItem: ServiceCatalogItem = { |
| 29 | + id: 1989, |
| 30 | + name: "Atl Nacional keyboard", |
| 31 | + description: "This is a keyboard from Atl Nacional", |
| 32 | + form_id: 456, |
| 33 | + }; |
| 34 | + |
| 35 | + const mockHelpCenterPath = "/hc/en-us"; |
| 36 | + |
| 37 | + beforeEach(() => jest.clearAllMocks()); |
| 38 | + |
| 39 | + describe("Rendering", () => { |
| 40 | + it("should render the service item with correct content", () => { |
| 41 | + renderWithTheme( |
| 42 | + <ServiceCatalogListItem |
| 43 | + serviceItem={mockServiceItem} |
| 44 | + helpCenterPath={mockHelpCenterPath} |
| 45 | + /> |
| 46 | + ); |
| 47 | + |
| 48 | + expect(screen.getByText(mockServiceItem.name)).toBeInTheDocument(); |
| 49 | + expect(screen.getByText(mockServiceItem.description)).toBeInTheDocument(); |
| 50 | + expect(screen.getByTestId("atl-nacional")).toBeInTheDocument(); |
| 51 | + }); |
| 52 | + |
| 53 | + it("should render as a link with correct href", () => { |
| 54 | + renderWithTheme( |
| 55 | + <ServiceCatalogListItem |
| 56 | + serviceItem={mockServiceItem} |
| 57 | + helpCenterPath={mockHelpCenterPath} |
| 58 | + /> |
| 59 | + ); |
| 60 | + |
| 61 | + const linkElement = screen.getByRole("link"); |
| 62 | + |
| 63 | + expect(linkElement).toHaveAttribute( |
| 64 | + "href", |
| 65 | + `${mockHelpCenterPath}/services/${mockServiceItem.id}` |
| 66 | + ); |
| 67 | + }); |
| 68 | + |
| 69 | + it("should use the theme foreground color as text color", () => { |
| 70 | + renderWithTheme( |
| 71 | + <ServiceCatalogListItem |
| 72 | + serviceItem={mockServiceItem} |
| 73 | + helpCenterPath={mockHelpCenterPath} |
| 74 | + /> |
| 75 | + ); |
| 76 | + |
| 77 | + const itemContainer = screen.getByTestId( |
| 78 | + "service-catalog-list-item-container" |
| 79 | + ); |
| 80 | + expect(itemContainer).toHaveStyle(`color: ${theme.colors.foreground}`); |
| 81 | + }); |
| 82 | + |
| 83 | + it("should use primaryHue as card border color on hover", async () => { |
| 84 | + renderWithTheme( |
| 85 | + <ServiceCatalogListItem |
| 86 | + serviceItem={mockServiceItem} |
| 87 | + helpCenterPath={mockHelpCenterPath} |
| 88 | + /> |
| 89 | + ); |
| 90 | + |
| 91 | + const user = userEvent.setup(); |
| 92 | + const itemContainer = screen.getByTestId( |
| 93 | + "service-catalog-list-item-container" |
| 94 | + ); |
| 95 | + const defaultBorderColor = DEFAULT_THEME.palette.grey?.[300]; |
| 96 | + |
| 97 | + expect(defaultBorderColor).toBeTruthy(); |
| 98 | + expect(itemContainer).toHaveStyle(`border-color: ${defaultBorderColor}`); |
| 99 | + |
| 100 | + await user.hover(itemContainer); |
| 101 | + |
| 102 | + expect(itemContainer).toHaveStyle( |
| 103 | + `border-color: ${theme.colors.primaryHue}` |
| 104 | + ); |
| 105 | + }); |
| 106 | + }); |
| 107 | +}); |
0 commit comments