|
1 | 1 | import React from "react"; |
| 2 | +import faker from "faker"; |
| 3 | +import { PasswordInput } from "./password-input"; |
| 4 | +import { render, fireEvent } from "@testing-library/react"; |
| 5 | +import uuid from "uuid"; |
| 6 | +import { InputTypes } from "../constants/input-types"; |
2 | 7 |
|
3 | 8 | describe("PasswordInput", () => { |
4 | | - test.skip("TODO - https://github.com/AndcultureCode/AndcultureCode.JavaScript.React.Components/issues/7", () => {}); |
| 9 | + test("when given default props, renders input", () => { |
| 10 | + // Arrange |
| 11 | + const testDataId = "testDataId"; |
| 12 | + |
| 13 | + // Act |
| 14 | + const { getByTestId } = render( |
| 15 | + <PasswordInput |
| 16 | + onChange={() => {}} |
| 17 | + isVisible={true} |
| 18 | + id={uuid()} |
| 19 | + testId={testDataId} |
| 20 | + /> |
| 21 | + ); |
| 22 | + |
| 23 | + // Assert |
| 24 | + expect(getByTestId(testDataId)).not.toBeNil(); |
| 25 | + }); |
| 26 | + |
| 27 | + test("when onChange prop set, calls handler upon change", () => { |
| 28 | + // Arrange |
| 29 | + let isChecked = false; |
| 30 | + const testDataId = "testDataId"; |
| 31 | + const handleChange = () => (isChecked = true); |
| 32 | + |
| 33 | + // Act |
| 34 | + const { getByTestId } = render( |
| 35 | + <PasswordInput |
| 36 | + onChange={handleChange} |
| 37 | + isVisible={true} |
| 38 | + id={uuid()} |
| 39 | + testId={testDataId} |
| 40 | + /> |
| 41 | + ); |
| 42 | + fireEvent.change(getByTestId(testDataId), { |
| 43 | + target: { value: faker.random.word() }, |
| 44 | + }); |
| 45 | + |
| 46 | + // Assert |
| 47 | + expect(isChecked).toBeTrue(); |
| 48 | + }); |
| 49 | + |
| 50 | + test("when isVisible prop set to false, renders with type password", () => { |
| 51 | + // Arrange |
| 52 | + const testDataId = "testDataId"; |
| 53 | + |
| 54 | + // Act |
| 55 | + const { container } = render( |
| 56 | + <PasswordInput |
| 57 | + onChange={() => {}} |
| 58 | + isVisible={false} |
| 59 | + id={uuid()} |
| 60 | + testId={testDataId} |
| 61 | + /> |
| 62 | + ); |
| 63 | + const htmlInputElement = container.getElementsByTagName("input"); |
| 64 | + |
| 65 | + // Assert |
| 66 | + expect(htmlInputElement[0].type).toBe(InputTypes.Password); |
| 67 | + }); |
5 | 68 | }); |
0 commit comments