Files
coder/site/components/Form/FormCloseButton.test.tsx
T
Bryan 35291d358a fix: Forms - Add 'escape' button / key handler in forms (#240)
In v1, we had a quick and easy way to leave creation forms - an escape button (and key handler) in the top right corner.

This was especially helpful in cases where the form was long enough that the 'Cancel' button was off-screen.

This ports over that component into v2 and hooks up into our two existing forms:
<img width="977" alt="Screen Shot 2022-02-09 at 5 48 03 PM" src="https://user-images.githubusercontent.com/88213859/153321503-af957f2b-d674-4ebf-9ac5-97939cb9153f.png">

In addition, this adds test cases + a storybook story for it.
2022-02-15 16:43:27 -08:00

71 lines
1.4 KiB
TypeScript

import { fireEvent, render, screen } from "@testing-library/react"
import React from "react"
import { FormCloseButton } from "./FormCloseButton"
describe("FormCloseButton", () => {
it("renders", async () => {
// When
render(
<FormCloseButton
onClose={() => {
return
}}
/>,
)
// Then
await screen.findByText("ESC")
})
it("calls onClose when clicked", async () => {
// Given
const onClose = jest.fn()
// When
render(<FormCloseButton onClose={onClose} />)
// Then
const element = await screen.findByText("ESC")
// When
fireEvent.click(element)
// Then
expect(onClose).toBeCalledTimes(1)
})
it("calls onClose when escape is pressed", async () => {
// Given
const onClose = jest.fn()
// When
render(<FormCloseButton onClose={onClose} />)
// Then
const element = await screen.findByText("ESC")
// When
fireEvent.keyDown(element, { key: "Escape", code: "Esc", charCode: 27 })
// Then
expect(onClose).toBeCalledTimes(1)
})
it("doesn't call onClose if another key is pressed", async () => {
// Given
const onClose = jest.fn()
// When
render(<FormCloseButton onClose={onClose} />)
// Then
const element = await screen.findByText("ESC")
// When
fireEvent.keyDown(element, { key: "Enter", code: "Enter", charCode: 13 })
// Then
expect(onClose).toBeCalledTimes(0)
})
})