|
| 1 | +import { MockUser, MockUserAgent, WrapperComponent } from "../../../test_helpers" |
| 2 | +import { UserCell, UserCellProps } from "./UserCell" |
| 3 | +import React from "react" |
| 4 | +import { fireEvent, render, screen } from "@testing-library/react" |
| 5 | + |
| 6 | +namespace Helpers { |
| 7 | + export const Props: UserCellProps = { |
| 8 | + Avatar: { |
| 9 | + username: MockUser.username, |
| 10 | + }, |
| 11 | + caption: MockUserAgent.ip_address, |
| 12 | + primaryText: MockUser.username, |
| 13 | + onPrimaryTextSelect: jest.fn(), |
| 14 | + } |
| 15 | + |
| 16 | + export const Component: React.FC<UserCellProps> = (props) => ( |
| 17 | + <WrapperComponent> |
| 18 | + <UserCell {...props} /> |
| 19 | + </WrapperComponent> |
| 20 | + ) |
| 21 | +} |
| 22 | + |
| 23 | +describe("UserCell", () => { |
| 24 | + // callbacks |
| 25 | + it("calls onPrimaryTextSelect when primaryText is clicked", () => { |
| 26 | + // Given |
| 27 | + const onPrimaryTextSelectMock = jest.fn() |
| 28 | + const props: UserCellProps = { |
| 29 | + ...Helpers.Props, |
| 30 | + onPrimaryTextSelect: onPrimaryTextSelectMock, |
| 31 | + } |
| 32 | + |
| 33 | + // When - click the user's email address |
| 34 | + render(<Helpers.Component {...props} />) |
| 35 | + fireEvent.click(screen.getByText(props.primaryText)) |
| 36 | + |
| 37 | + // Then - callback was fired once |
| 38 | + expect(onPrimaryTextSelectMock).toHaveBeenCalledTimes(1) |
| 39 | + }) |
| 40 | + |
| 41 | + // primaryText |
| 42 | + it("renders primaryText as a link when onPrimaryTextSelect is defined", () => { |
| 43 | + // Given |
| 44 | + const props: UserCellProps = Helpers.Props |
| 45 | + |
| 46 | + // When |
| 47 | + render(<Helpers.Component {...props} />) |
| 48 | + const primaryTextNode = screen.getByText(props.primaryText) |
| 49 | + |
| 50 | + // Then |
| 51 | + expect(primaryTextNode.tagName).toBe("A") |
| 52 | + }) |
| 53 | + it("renders primaryText without a link when onPrimaryTextSelect is undefined", () => { |
| 54 | + // Given |
| 55 | + const props: UserCellProps = { |
| 56 | + ...Helpers.Props, |
| 57 | + onPrimaryTextSelect: undefined, |
| 58 | + } |
| 59 | + |
| 60 | + // When |
| 61 | + render(<Helpers.Component {...props} />) |
| 62 | + const primaryTextNode = screen.getByText(props.primaryText) |
| 63 | + |
| 64 | + // Then |
| 65 | + expect(primaryTextNode.tagName).toBe("P") |
| 66 | + }) |
| 67 | + |
| 68 | + // caption |
| 69 | + it("renders caption", () => { |
| 70 | + // Given |
| 71 | + const caption = "definitely a caption" |
| 72 | + const props: UserCellProps = { |
| 73 | + ...Helpers.Props, |
| 74 | + caption, |
| 75 | + } |
| 76 | + |
| 77 | + // When |
| 78 | + render(<Helpers.Component {...props} />) |
| 79 | + |
| 80 | + // Then |
| 81 | + expect(screen.getByText(caption)).toBeDefined() |
| 82 | + }) |
| 83 | +}) |
0 commit comments