|
1 | 1 | import React from "react"; |
| 2 | +import { Select } from "./select"; |
| 3 | +import faker from "faker"; |
| 4 | +import { render, fireEvent } from "@testing-library/react"; |
2 | 5 |
|
3 | 6 | describe("Select", () => { |
4 | | - test.skip("TODO - https://github.com/AndcultureCode/AndcultureCode.JavaScript.React.Components/issues/11", () => {}); |
| 7 | + test("when default props, renders with options", () => { |
| 8 | + // Arrange |
| 9 | + const expected = faker.random.word(); |
| 10 | + const testLabel = faker.random.word(); |
| 11 | + const testValue = faker.random.word(); |
| 12 | + |
| 13 | + // Act |
| 14 | + const { getByText } = render( |
| 15 | + <Select |
| 16 | + id={expected} |
| 17 | + options={[{ label: testLabel, value: testValue }]} |
| 18 | + onChange={() => {}} |
| 19 | + /> |
| 20 | + ); |
| 21 | + |
| 22 | + // Assert |
| 23 | + expect(getByText(testLabel)).not.toBeNil(); |
| 24 | + }); |
| 25 | + |
| 26 | + test("when onChange set, calls handler upon change", () => { |
| 27 | + // Arrange |
| 28 | + let isChecked = false; |
| 29 | + const expected = faker.random.word(); |
| 30 | + const handleChange = () => (isChecked = true); |
| 31 | + const testLabel = faker.random.word(); |
| 32 | + const testValue = faker.random.word(); |
| 33 | + |
| 34 | + // Act |
| 35 | + const { getByDisplayValue } = render( |
| 36 | + <Select |
| 37 | + id={expected} |
| 38 | + options={[{ label: testLabel, value: testValue }]} |
| 39 | + onChange={handleChange} |
| 40 | + /> |
| 41 | + ); |
| 42 | + |
| 43 | + fireEvent.change(getByDisplayValue(testLabel), { |
| 44 | + target: { value: faker.random.word() }, |
| 45 | + }); |
| 46 | + |
| 47 | + // Assert |
| 48 | + expect(isChecked).toBeTrue(); |
| 49 | + }); |
| 50 | + |
| 51 | + test("when given cssClassName prop, renders with class name", () => { |
| 52 | + // Arrange |
| 53 | + const expected = faker.random.word(); |
| 54 | + const testLabel = faker.random.word(); |
| 55 | + const testValue = faker.random.word(); |
| 56 | + const testClassName = "testClassName"; |
| 57 | + |
| 58 | + // Act |
| 59 | + const { container } = render( |
| 60 | + <Select |
| 61 | + id={expected} |
| 62 | + options={[{ label: testLabel, value: testValue }]} |
| 63 | + onChange={() => {}} |
| 64 | + cssClassName={testClassName} |
| 65 | + /> |
| 66 | + ); |
| 67 | + const result = container.getElementsByClassName(testClassName); |
| 68 | + |
| 69 | + // Assert |
| 70 | + expect(result).not.toBeNil(); |
| 71 | + }); |
5 | 72 | }); |
0 commit comments