|
| 1 | +import React from "react"; |
| 2 | +import { render, fireEvent, within } from "@testing-library/react"; |
| 3 | +import ModalTopActions from "../ModalTopActions"; |
| 4 | +import IconButton from "../../../IconButton/IconButton"; |
| 5 | +import { Feedback as FeedbackIcon } from "../../../Icon/Icons"; |
| 6 | +import { ButtonColor } from "../../../Button/ButtonConstants"; |
| 7 | +import { camelCase } from "lodash-es"; |
| 8 | + |
| 9 | +describe("ModalTopActions", () => { |
| 10 | + const closeButtonAriaLabel = "Close modal"; |
| 11 | + |
| 12 | + it("renders the close button with the correct aria-label", () => { |
| 13 | + const { getByLabelText } = render(<ModalTopActions closeButtonAriaLabel={closeButtonAriaLabel} />); |
| 14 | + |
| 15 | + expect(getByLabelText(closeButtonAriaLabel)).toBeInTheDocument(); |
| 16 | + }); |
| 17 | + |
| 18 | + it("calls onClose when the close button is clicked", () => { |
| 19 | + const mockOnClose = jest.fn(); |
| 20 | + |
| 21 | + const { getByLabelText } = render( |
| 22 | + <ModalTopActions onClose={mockOnClose} closeButtonAriaLabel={closeButtonAriaLabel} /> |
| 23 | + ); |
| 24 | + |
| 25 | + fireEvent.click(getByLabelText(closeButtonAriaLabel)); |
| 26 | + expect(mockOnClose).toHaveBeenCalled(); |
| 27 | + }); |
| 28 | + |
| 29 | + it("does not fail when onClose is not provided", () => { |
| 30 | + const { getByLabelText } = render(<ModalTopActions closeButtonAriaLabel={closeButtonAriaLabel} />); |
| 31 | + fireEvent.click(getByLabelText(closeButtonAriaLabel)); |
| 32 | + expect(() => getByLabelText(closeButtonAriaLabel)).not.toThrow(); |
| 33 | + }); |
| 34 | + |
| 35 | + it("renders the action button using the renderAction prop as a function", () => { |
| 36 | + const renderAction = jest.fn(color => <IconButton data-testid="extra-action" icon={FeedbackIcon} color={color} />); |
| 37 | + const { getByTestId } = render(<ModalTopActions renderAction={renderAction} />); |
| 38 | + |
| 39 | + expect(within(getByTestId("extra-action")).getByTestId("icon")).toBeInTheDocument(); |
| 40 | + }); |
| 41 | + |
| 42 | + it("calls renderAction with correct color argument", () => { |
| 43 | + const renderAction = jest.fn(color => <IconButton data-testid="extra-action" icon={FeedbackIcon} color={color} />); |
| 44 | + render(<ModalTopActions color="dark" renderAction={renderAction} />); |
| 45 | + |
| 46 | + expect(renderAction).toHaveBeenCalledWith(ButtonColor.ON_INVERTED_BACKGROUND); |
| 47 | + }); |
| 48 | + |
| 49 | + it("renders the action button using the renderAction prop directly", () => { |
| 50 | + const renderAction = ( |
| 51 | + <IconButton data-testid="extra-action" icon={FeedbackIcon} color={IconButton.colors.ON_PRIMARY_COLOR} /> |
| 52 | + ); |
| 53 | + const { getByTestId } = render(<ModalTopActions renderAction={renderAction} />); |
| 54 | + |
| 55 | + expect(within(getByTestId("extra-action")).getByTestId("icon")).toBeInTheDocument(); |
| 56 | + }); |
| 57 | + |
| 58 | + it("applies the correct color when 'dark' is passed", () => { |
| 59 | + const { getByLabelText } = render(<ModalTopActions color="dark" closeButtonAriaLabel={closeButtonAriaLabel} />); |
| 60 | + expect(getByLabelText(closeButtonAriaLabel)).toHaveClass(camelCase("color-" + ButtonColor.ON_INVERTED_BACKGROUND)); |
| 61 | + }); |
| 62 | + |
| 63 | + it("applies the correct color when 'light' is passed", () => { |
| 64 | + const { getByLabelText } = render(<ModalTopActions color="light" closeButtonAriaLabel={closeButtonAriaLabel} />); |
| 65 | + expect(getByLabelText(closeButtonAriaLabel)).toHaveClass(camelCase("color-" + ButtonColor.ON_PRIMARY_COLOR)); |
| 66 | + }); |
| 67 | + |
| 68 | + it("applies the default color when no color is passed", () => { |
| 69 | + const { getByLabelText } = render(<ModalTopActions closeButtonAriaLabel={closeButtonAriaLabel} />); |
| 70 | + expect(getByLabelText(closeButtonAriaLabel)).toHaveClass(camelCase("color-" + ButtonColor.PRIMARY)); |
| 71 | + }); |
| 72 | +}); |
0 commit comments