Skip to content

Commit

Permalink
test(events): replace fire event with user event
Browse files Browse the repository at this point in the history
  • Loading branch information
john-james-gh committed Jan 23, 2023
1 parent 87b6abe commit 32163d0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
37 changes: 23 additions & 14 deletions src/Modal.test.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { render, screen, fireEvent } from "@testing-library/react"
import { render, screen } from "@testing-library/react"
import { axe } from "jest-axe"
import userEvent from "@testing-library/user-event"

import { Modal } from "./Modal"
import { Modal } from "./"

// create react-app root div element so we can assert aria-hidden is true
/** create react-app root div element so we can assert aria-hidden is true */
const root = document.createElement("div")

const onClose = jest.fn()
Expand Down Expand Up @@ -33,7 +34,9 @@ it("should have no a11y violations", async () => {
expect(results).toHaveNoViolations()
})

it("should call onClose when escape or close buttons are pressed", () => {
it("should call onClose when escape or close buttons are pressed", async () => {
const user = userEvent.setup()

render(
<Modal onClose={onClose} title="Title" description="Description">
<button type="button" onClick={onClose}>
Expand All @@ -42,13 +45,13 @@ it("should call onClose when escape or close buttons are pressed", () => {
</Modal>
)

fireEvent.keyDown(screen.getByRole("dialog"), { key: "Escape" })
await user.keyboard("{Escape}")
expect(onClose).toHaveBeenCalledTimes(1)

fireEvent.click(screen.getByText("X"))
await user.click(screen.getByText("X"))
expect(onClose).toHaveBeenCalledTimes(2)

fireEvent.click(screen.getByText("Save & Close"))
await user.click(screen.getByText("Save & Close"))
expect(onClose).toHaveBeenCalledTimes(3)
})

Expand Down Expand Up @@ -109,7 +112,9 @@ it("document body should have `overflow: hidden` - not scrollable", () => {
expect(document.body).toHaveStyle("overflow: hidden")
})

it("focus should be trapped in a loop when `Tab` key is continiously pressed", () => {
it("focus should be trapped in a loop when `Tab` key is continiously pressed", async () => {
const user = userEvent.setup()

render(
<Modal onClose={onClose} title="Title" description="Description">
<button onClick={onClose}>Close</button>
Expand All @@ -118,16 +123,18 @@ it("focus should be trapped in a loop when `Tab` key is continiously pressed", (

expect(screen.getByText("X")).toHaveFocus()

fireEvent.keyDown(screen.getByRole("dialog"), { key: "Tab" })
await user.keyboard("{Tab}")

expect(screen.getByText("Close")).toHaveFocus()

fireEvent.keyDown(screen.getByRole("dialog"), { key: "Tab" })
await user.keyboard("{Tab}")

expect(screen.getByText("X")).toHaveFocus()
})

it("should not focus a disabled element and it should be ignored and skipped by `Tab` key", () => {
it("should not focus a disabled element and it should be ignored and skipped by `Tab` key", async () => {
const user = userEvent.setup()

render(
<Modal onClose={onClose} title="Title" description="Description">
<button disabled>Disabled close</button>
Expand All @@ -137,13 +144,15 @@ it("should not focus a disabled element and it should be ignored and skipped by

expect(screen.getByText("X")).toHaveFocus()

fireEvent.keyDown(screen.getByRole("dialog"), { key: "Tab" })
await user.keyboard("{Tab}")

expect(screen.getByText("Disabled close")).not.toHaveFocus()
expect(screen.getByText("Enabled close")).toHaveFocus()
})

it("should not focus a `tabIndex={-1}` element and it should be ignored and skipped by `Tab` key", () => {
it("should not focus a `tabIndex={-1}` element and it should be ignored and skipped by `Tab` key", async () => {
const user = userEvent.setup()

render(
<Modal onClose={onClose} title="Title" description="Description">
<button tabIndex={-1}>Tab Index 0</button>
Expand All @@ -153,7 +162,7 @@ it("should not focus a `tabIndex={-1}` element and it should be ignored and skip

expect(screen.getByText("X")).toHaveFocus()

fireEvent.keyDown(screen.getByRole("dialog"), { key: "Tab" })
await user.keyboard("{Tab}")

expect(screen.getByText("Tab Index 0")).not.toHaveFocus()
expect(screen.getByText("Close")).toHaveFocus()
Expand Down
2 changes: 1 addition & 1 deletion src/Portal.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { render } from "@testing-library/react"
import { axe } from "jest-axe"

import { Portal } from "./Portal"
import { Portal } from "./"

it("should have no a11y violations", async () => {
const { baseElement } = render(
Expand Down

0 comments on commit 32163d0

Please sign in to comment.