|
| 1 | +import { fireEvent, render, screen } from "@testing-library/vue"; |
| 2 | +import GuessAutocompleteList from "../GuessAutocompleteList.vue"; |
| 3 | + |
| 4 | +describe("GuessAutoompleteList", () => { |
| 5 | + beforeEach(() => { |
| 6 | + // not available in JSDom |
| 7 | + Element.prototype.scrollIntoView = () => {}; |
| 8 | + }); |
| 9 | + |
| 10 | + it("only displays if there are options", async () => { |
| 11 | + const { rerender } = render(GuessAutocompleteList, { |
| 12 | + props: { |
| 13 | + options: [], |
| 14 | + keyboardFocusIndex: -1, |
| 15 | + }, |
| 16 | + }); |
| 17 | + |
| 18 | + expect(screen.queryAllByRole("button")).toHaveLength(0); |
| 19 | + |
| 20 | + await rerender({ |
| 21 | + options: ["one", "two", "three"], |
| 22 | + }); |
| 23 | + const buttons = screen.queryAllByRole("button"); |
| 24 | + expect(buttons).toHaveLength(3); |
| 25 | + expect(buttons[0]).toHaveTextContent("one"); |
| 26 | + expect(buttons[1]).toHaveTextContent("two"); |
| 27 | + expect(buttons[2]).toHaveTextContent("three"); |
| 28 | + }); |
| 29 | + |
| 30 | + it("marks container as active when keyboardFocusIndex is 0 or greater", async () => { |
| 31 | + const { rerender } = render(GuessAutocompleteList, { |
| 32 | + props: { |
| 33 | + options: ["option"], |
| 34 | + keyboardFocusIndex: -1, |
| 35 | + }, |
| 36 | + }); |
| 37 | + |
| 38 | + expect(screen.getByTestId("options-wrapper")).not.toHaveClass("active"); |
| 39 | + |
| 40 | + await rerender({ |
| 41 | + keyboardFocusIndex: 0, |
| 42 | + }); |
| 43 | + expect(screen.getByTestId("options-wrapper")).toHaveClass("active"); |
| 44 | + |
| 45 | + await rerender({ |
| 46 | + keyboardFocusIndex: 1, |
| 47 | + }); |
| 48 | + expect(screen.getByTestId("options-wrapper")).toHaveClass("active"); |
| 49 | + }); |
| 50 | + |
| 51 | + it("marks particular option as active when the keyboardFocusIndex matches", async () => { |
| 52 | + const { rerender } = render(GuessAutocompleteList, { |
| 53 | + props: { |
| 54 | + options: ["one", "two", "three"], |
| 55 | + keyboardFocusIndex: -1, |
| 56 | + }, |
| 57 | + }); |
| 58 | + |
| 59 | + const options = screen.getAllByRole("button"); |
| 60 | + expect(options[0]).not.toHaveClass("active"); |
| 61 | + expect(options[1]).not.toHaveClass("active"); |
| 62 | + expect(options[2]).not.toHaveClass("active"); |
| 63 | + |
| 64 | + await rerender({ |
| 65 | + keyboardFocusIndex: 0, |
| 66 | + }); |
| 67 | + expect(options[0]).toHaveClass("active"); |
| 68 | + expect(options[1]).not.toHaveClass("active"); |
| 69 | + expect(options[2]).not.toHaveClass("active"); |
| 70 | + |
| 71 | + await rerender({ |
| 72 | + keyboardFocusIndex: 1, |
| 73 | + }); |
| 74 | + expect(options[0]).not.toHaveClass("active"); |
| 75 | + expect(options[1]).toHaveClass("active"); |
| 76 | + expect(options[2]).not.toHaveClass("active"); |
| 77 | + |
| 78 | + await rerender({ |
| 79 | + keyboardFocusIndex: 2, |
| 80 | + }); |
| 81 | + expect(options[0]).not.toHaveClass("active"); |
| 82 | + expect(options[1]).not.toHaveClass("active"); |
| 83 | + expect(options[2]).toHaveClass("active"); |
| 84 | + }); |
| 85 | + |
| 86 | + it("emits option when clicked", async () => { |
| 87 | + const { emitted } = render(GuessAutocompleteList, { |
| 88 | + props: { |
| 89 | + options: ["one", "two", "three"], |
| 90 | + keyboardFocusIndex: -1, |
| 91 | + }, |
| 92 | + }); |
| 93 | + |
| 94 | + await fireEvent.click(screen.getByText("two")); |
| 95 | + |
| 96 | + expect(emitted().pick.length).toBe(1); |
| 97 | + expect(emitted().pick[0]).toEqual(["two"]); |
| 98 | + }); |
| 99 | + |
| 100 | + it("scrolls to element when navigated to", async () => { |
| 101 | + const { rerender } = render(GuessAutocompleteList, { |
| 102 | + props: { |
| 103 | + options: ["one", "two", "three"], |
| 104 | + keyboardFocusIndex: -1, |
| 105 | + }, |
| 106 | + }); |
| 107 | + const options = screen.getAllByRole("button"); |
| 108 | + vi.spyOn(options[0], "scrollIntoView"); |
| 109 | + vi.spyOn(options[1], "scrollIntoView"); |
| 110 | + vi.spyOn(options[2], "scrollIntoView"); |
| 111 | + |
| 112 | + await rerender({ |
| 113 | + keyboardFocusIndex: 1, |
| 114 | + }); |
| 115 | + expect(options[0].scrollIntoView).not.toBeCalled(); |
| 116 | + expect(options[1].scrollIntoView).toBeCalledTimes(1); |
| 117 | + expect(options[2].scrollIntoView).not.toBeCalled(); |
| 118 | + }); |
| 119 | +}); |
0 commit comments