|
1 | | -import React from "react"; |
| 1 | +import React, { Component } from "react"; |
2 | 2 | import faker from "faker"; |
3 | | -import { render, fireEvent } from "@testing-library/react"; |
| 3 | +import { render, fireEvent, screen } from "@testing-library/react"; |
4 | 4 | import { AccessibleList } from "./accessible-list"; |
5 | 5 | import { KeyboardKeys } from "../../constants/keyboard-keys"; |
6 | 6 |
|
@@ -40,22 +40,48 @@ describe("AccessibleList", () => { |
40 | 40 | expect(isChecked).toBeTrue(); |
41 | 41 | }); |
42 | 42 |
|
43 | | - test("when down arrow pressed, calls setCurrent method", () => { |
| 43 | + test("when invalid react element, does not render in list", () => { |
44 | 44 | // Arrange |
45 | 45 | const buttonText1 = faker.random.word(); |
46 | 46 | const buttonText2 = faker.random.word(); |
47 | 47 |
|
48 | 48 | // Act |
49 | | - const { getByText } = render( |
| 49 | + const { container } = render( |
50 | 50 | <AccessibleList focusFirstItem={true}> |
51 | 51 | <button>{buttonText1}</button> |
| 52 | + {null} |
| 53 | + {undefined} |
52 | 54 | <button>{buttonText2}</button> |
53 | 55 | </AccessibleList> |
54 | 56 | ); |
| 57 | + const buttons = container.getElementsByTagName("button"); |
| 58 | + |
| 59 | + // Assert |
| 60 | + expect(buttons).toHaveLength(2); |
| 61 | + }); |
| 62 | + |
| 63 | + it("when onClick set, calls handler upon click", async () => { |
| 64 | + // Arrange |
| 65 | + let isChecked = false; |
| 66 | + const handleClick = () => { |
| 67 | + isChecked = true; |
| 68 | + }; |
| 69 | + const buttonText1 = faker.random.word(); |
| 70 | + const buttonText2 = faker.random.words(); |
| 71 | + |
| 72 | + // Act |
| 73 | + const { getByText } = render( |
| 74 | + <AccessibleList focusFirstItem={true}> |
| 75 | + <button onClick={handleClick}>{buttonText1}</button> |
| 76 | + <button onClick={handleClick}>{buttonText2}</button> |
| 77 | + </AccessibleList> |
| 78 | + ); |
| 79 | + |
55 | 80 | fireEvent.keyDown(getByText(buttonText1), { |
56 | 81 | key: KeyboardKeys.DownArrow, |
57 | 82 | }); |
58 | 83 |
|
59 | 84 | // Assert |
| 85 | + expect(isChecked).toBeTrue(); |
60 | 86 | }); |
61 | 87 | }); |
0 commit comments