Skip to content

Commit

Permalink
menu-button tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Chance Strickland committed Mar 11, 2020
1 parent e08bf00 commit 940a5fd
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 12 deletions.
14 changes: 7 additions & 7 deletions packages/combobox/__tests__/combobox.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useState } from "react";
import { render, act, withMarkup, userEvent, cleanup } from "$test/utils";
import { render, act, withMarkup, userEvent } from "$test/utils";
import { axe } from "jest-axe";
import {
Combobox,
Expand All @@ -13,20 +13,20 @@ import cities from "../examples/cities";

describe("<Combobox />", () => {
it("should not have basic a11y issues", async () => {
const { container } = render(<BasicCombobox />);
let results = await axe(container);
expect(results).toHaveNoViolations();
let { container } = render(<BasicCombobox />);
expect(await axe(container)).toHaveNoViolations();
});

it("should open a list on text entry", () => {
jest.useFakeTimers();
let optionToSelect = "Eagle Pass, Texas";

let { getByTestId, getByText } = render(<BasicCombobox />);
let getByTextWithMarkup = withMarkup(getByText);
let input = getByTestId("input");
userEvent.type(input, "e");
act(() => void jest.advanceTimersByTime(100));
act(() => {
userEvent.type(input, "e");
jest.advanceTimersByTime(100);
});
expect(getByTestId("list")).toBeInTheDocument();
expect(getByTextWithMarkup(optionToSelect)).toBeInTheDocument();
});
Expand Down
6 changes: 3 additions & 3 deletions packages/listbox/__tests__/listbox.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";
import { render, act, fireEvent, userEvent } from "$test/utils";
import { render, act, fireEvent } from "$test/utils";
import { axe } from "jest-axe";
import { Listbox, ListboxOption } from "@reach/listbox";
import VisuallyHidden from "@reach/visually-hidden";
Expand Down Expand Up @@ -28,8 +28,8 @@ describe("<Listbox />", () => {
act(clickButton);
expect(getPopover()).not.toBeVisible();

// Listbox doesn't use an HTML5 button, so we also need to test keyboard
// events
// TODO: Listbox doesn't use an HTML5 button, so we also need to test
// keyboard events
});
});

Expand Down
44 changes: 44 additions & 0 deletions packages/menu-button/__tests__/menu-button.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import React from "react";
import { render, act, withMarkup, fireEvent } from "$test/utils";
import { axe } from "jest-axe";
import { Menu, MenuList, MenuButton, MenuItem } from "@reach/menu-button";

describe("<MenuButton />", () => {
it("should not have basic a11y issues", async () => {
let { container } = render(<BasicMenuButton />);
expect(await axe(container)).toHaveNoViolations();
});

it("should toggle on button click", () => {
let { getByRole, getByText, queryByText } = render(<BasicMenuButton />);
let getByTextWithMarkup = withMarkup(getByText);
let queryByTextWithMarkup = withMarkup(queryByText);

// Menu opens on mousedown, not click!
function clickButton() {
fireEvent.mouseDown(getByRole("button"));
fireEvent.mouseUp(getByRole("button"));
}

expect(queryByTextWithMarkup("Create a Copy")).not.toBeTruthy();
act(clickButton);
expect(getByTextWithMarkup("Create a Copy")).toBeInTheDocument();
});
});

function BasicMenuButton() {
return (
<Menu>
<MenuButton id="example-button">
Actions <span aria-hidden="true"></span>
</MenuButton>
<MenuList>
<input type="text" />
<MenuItem onSelect={() => jest.fn()}>Download</MenuItem>
<MenuItem onSelect={() => jest.fn()}>Create a Copy</MenuItem>
<MenuItem onSelect={() => jest.fn()}>Mark as Draft</MenuItem>
<MenuItem onSelect={() => jest.fn()}>Delete</MenuItem>
</MenuList>
</Menu>
);
}
4 changes: 2 additions & 2 deletions test/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { RenderOptions, RenderResult } from "./types";
* @param query The getter function returned from RTL's render method
*/
export function withMarkup(query: Query) {
return (text: string): HTMLElement =>
return (text: string): HTMLElement | null =>
query((content: string, node: HTMLElement) => {
const hasText = (node: HTMLElement) => node.textContent === text;
const childrenDontHaveText = Array.from(node.children).every(
Expand Down Expand Up @@ -82,6 +82,6 @@ export function render<
return result;
}

type Query = (f: MatcherFunction) => HTMLElement;
type Query = (f: MatcherFunction) => HTMLElement | null;

export { act, userEvent, RenderOptions, RenderResult };

0 comments on commit 940a5fd

Please sign in to comment.