|
| 1 | +import { render, screen } from "@testing-library/react"; |
| 2 | +import { Input } from ".."; |
| 3 | + |
| 4 | +const props = { label: "Test", message: "Test message", id: "test" }; |
| 5 | + |
| 6 | +describe("Input", () => { |
| 7 | + it("should render correctly", () => { |
| 8 | + const { container, getByLabelText } = render(<Input {...props} />); |
| 9 | + const inputContainer = container.firstChild; |
| 10 | + const input = getByLabelText(props.label); |
| 11 | + expect(inputContainer).toHaveClass("deriv-input__container"); |
| 12 | + expect(inputContainer).toHaveTextContent(props.label); |
| 13 | + expect(inputContainer).toHaveTextContent(props.message); |
| 14 | + expect(input).toBeInTheDocument(); |
| 15 | + }); |
| 16 | + |
| 17 | + it("should show label", () => { |
| 18 | + render(<Input {...props} />); |
| 19 | + const inputLabel = screen.getByText(props.label); |
| 20 | + expect(inputLabel).toBeInTheDocument(); |
| 21 | + }); |
| 22 | + |
| 23 | + it("container should be full width", () => { |
| 24 | + const { container } = render(<Input {...props} isFullWidth />); |
| 25 | + const inputContainer = container.firstChild; |
| 26 | + expect(inputContainer).toHaveClass("deriv-input__container--full"); |
| 27 | + }); |
| 28 | + |
| 29 | + it("should show message", () => { |
| 30 | + render(<Input {...props} />); |
| 31 | + const helperMessage = screen.getByText(props.message); |
| 32 | + expect(helperMessage).toHaveClass("deriv-helper-message--general"); |
| 33 | + expect(helperMessage).toBeInTheDocument(); |
| 34 | + }); |
| 35 | + |
| 36 | + it("should hide message", () => { |
| 37 | + const { container } = render(<Input {...props} hideMessage />); |
| 38 | + const inputContainer = container.firstChild; |
| 39 | + const input = inputContainer?.lastChild; |
| 40 | + expect(input).not.toHaveClass("deriv-helper-message--general"); |
| 41 | + }); |
| 42 | + |
| 43 | + it("should show different variants: General", () => { |
| 44 | + const { container } = render(<Input {...props} />); |
| 45 | + const inputContainer = container.firstChild; |
| 46 | + const input = inputContainer?.firstChild; |
| 47 | + const inputLabel = screen.getByText(props.label); |
| 48 | + const helperMessage = screen.getByText(props.message); |
| 49 | + expect(input).toHaveClass("deriv-input--general"); |
| 50 | + expect(input).not.toHaveClass("deriv-input--error"); |
| 51 | + expect(inputLabel).toHaveClass("deriv-input__label--general"); |
| 52 | + expect(helperMessage).toHaveClass("deriv-helper-message--general"); |
| 53 | + }); |
| 54 | + |
| 55 | + it("should show different variants: Success", () => { |
| 56 | + const { container } = render(<Input variant="success" {...props} />); |
| 57 | + const inputContainer = container.firstChild; |
| 58 | + const input = inputContainer?.firstChild; |
| 59 | + const inputLabel = screen.getByText(props.label); |
| 60 | + const helperMessage = screen.getByText(props.message); |
| 61 | + expect(input).toHaveClass("deriv-input--success"); |
| 62 | + expect(input).not.toHaveClass("deriv-input--error"); |
| 63 | + expect(inputLabel).toHaveClass("deriv-input__label--success"); |
| 64 | + expect(helperMessage).toHaveClass("deriv-helper-message--success"); |
| 65 | + }); |
| 66 | + |
| 67 | + it("should show different variants: Error", () => { |
| 68 | + const { container } = render(<Input variant="error" {...props} />); |
| 69 | + const inputContainer = container.firstChild; |
| 70 | + const input = inputContainer?.firstChild; |
| 71 | + const inputLabel = screen.getByText(props.label); |
| 72 | + const helperMessage = screen.getByText(props.message); |
| 73 | + expect(input).toHaveClass("deriv-input--error"); |
| 74 | + expect(input).not.toHaveClass("deriv-input--success"); |
| 75 | + expect(inputLabel).toHaveClass("deriv-input__label--error"); |
| 76 | + expect(helperMessage).toHaveClass("deriv-helper-message--error"); |
| 77 | + }); |
| 78 | + |
| 79 | + it("should show different variants: Warning", () => { |
| 80 | + const { container } = render(<Input variant="warning" {...props} />); |
| 81 | + const inputContainer = container.firstChild; |
| 82 | + const input = inputContainer?.firstChild; |
| 83 | + const inputLabel = screen.getByText(props.label); |
| 84 | + const helperMessage = screen.getByText(props.message); |
| 85 | + expect(input).toHaveClass("deriv-input--general"); |
| 86 | + expect(input).not.toHaveClass("deriv-input--error"); |
| 87 | + expect(inputLabel).toHaveClass("deriv-input__label--general"); |
| 88 | + expect(helperMessage).toHaveClass("deriv-helper-message--warning"); |
| 89 | + }); |
| 90 | + |
| 91 | + it("should show different variants: Disabled", () => { |
| 92 | + const { container } = render(<Input variant="disabled" {...props} />); |
| 93 | + const inputContainer = container.firstChild; |
| 94 | + const input = inputContainer?.firstChild; |
| 95 | + const inputLabel = screen.getByText(props.label); |
| 96 | + expect(input).toHaveClass("deriv-input--disabled"); |
| 97 | + expect(input).not.toHaveClass("deriv-input--error"); |
| 98 | + expect(inputLabel).toHaveClass("deriv-input__label--disabled"); |
| 99 | + }); |
| 100 | + |
| 101 | + it("should show different variants: Disabled", () => { |
| 102 | + const { container } = render(<Input variant="disabled" {...props} />); |
| 103 | + const inputContainer = container.firstChild; |
| 104 | + const input = inputContainer?.firstChild; |
| 105 | + const inputLabel = screen.getByText(props.label); |
| 106 | + expect(input).toHaveClass("deriv-input--disabled"); |
| 107 | + expect(input).not.toHaveClass("deriv-input--error"); |
| 108 | + expect(inputLabel).toHaveClass("deriv-input__label--disabled"); |
| 109 | + }); |
| 110 | +}); |
0 commit comments