|
| 1 | +import { MockUser, MockUserAgent, WrapperComponent } from "../../test_helpers" |
| 2 | +import { LANGUAGE, 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 | + onSelectEmail: jest.fn(), |
| 9 | + user: MockUser, |
| 10 | + userAgent: MockUserAgent, |
| 11 | + } |
| 12 | + |
| 13 | + export const Component: React.FC<UserCellProps> = (props) => ( |
| 14 | + <WrapperComponent> |
| 15 | + <UserCell {...props} /> |
| 16 | + </WrapperComponent> |
| 17 | + ) |
| 18 | +} |
| 19 | + |
| 20 | +describe("UserCell", () => { |
| 21 | + // callbacks |
| 22 | + it("calls onUserClick when an email address is clicked", () => { |
| 23 | + // Given |
| 24 | + const onSelectEmailMock = jest.fn() |
| 25 | + const props: UserCellProps = { |
| 26 | + ...Helpers.Props, |
| 27 | + onSelectEmail: onSelectEmailMock, |
| 28 | + } |
| 29 | + |
| 30 | + // When - click the user's email address |
| 31 | + render(<Helpers.Component {...props} />) |
| 32 | + fireEvent.click(screen.getByText(props.user.email)) |
| 33 | + |
| 34 | + // Then - callback was fired once |
| 35 | + expect(onSelectEmailMock).toHaveBeenCalledTimes(1) |
| 36 | + }) |
| 37 | + |
| 38 | + // email address cases |
| 39 | + it("renders an existing members' email address", () => { |
| 40 | + // Given |
| 41 | + const props: UserCellProps = Helpers.Props |
| 42 | + |
| 43 | + // When |
| 44 | + render(<Helpers.Component {...props} />) |
| 45 | + |
| 46 | + // Then - email address is visible |
| 47 | + expect(screen.getByText(props.user.email)).toBeDefined() |
| 48 | + }) |
| 49 | + it(`renders '${LANGUAGE.emptyUser}' for non-existing members`, () => { |
| 50 | + // Given |
| 51 | + const props: UserCellProps = { |
| 52 | + ...Helpers.Props, |
| 53 | + user: { |
| 54 | + ...MockUser, |
| 55 | + email: "", |
| 56 | + }, |
| 57 | + } |
| 58 | + |
| 59 | + // When |
| 60 | + render(<Helpers.Component {...props} />) |
| 61 | + |
| 62 | + // Then - 'Deleted user' is visible |
| 63 | + expect(screen.getByText(LANGUAGE.emptyUser)).toBeDefined() |
| 64 | + }) |
| 65 | + |
| 66 | + // ip address |
| 67 | + it("renders user agent IP address", () => { |
| 68 | + // Given |
| 69 | + const props: UserCellProps = Helpers.Props |
| 70 | + |
| 71 | + // When |
| 72 | + render(<Helpers.Component {...props} />) |
| 73 | + |
| 74 | + // Then - ip address is visible |
| 75 | + expect(screen.getByText(props.userAgent.ip_address)).toBeDefined() |
| 76 | + }) |
| 77 | +}) |
0 commit comments