Skip to content

Commit

Permalink
feat: Part 3 of converting to Restart modals: Preset modals (#2578)
Browse files Browse the repository at this point in the history
  • Loading branch information
hannahpurcell authored May 7, 2024
1 parent 1daa424 commit ed3ea5b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 23 deletions.
29 changes: 13 additions & 16 deletions assets/src/components/inputModal.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { useContext } from "react"
import { StateDispatchContext } from "../contexts/stateDispatchContext"
import { closeInputModal } from "../state"
import { Modal } from "@restart/ui"

const InputModal = ({
children,
Expand All @@ -9,22 +10,18 @@ const InputModal = ({
}) => {
const [, dispatch] = useContext(StateDispatchContext)
return (
<>
<div role="dialog">
<div
className="c-input-modal"
role="presentation"
onKeyDown={(event) => {
if (event.key === "Escape") {
dispatch(closeInputModal())
}
}}
>
{children}
</div>
</div>
<div className="c-input-modal-backdrop" aria-hidden={true} />
</>
<Modal
className="c-input-modal"
onHide={() => {
dispatch(closeInputModal())
}}
show
renderBackdrop={(props) => (
<div {...props} className="c-input-modal-backdrop" aria-hidden />
)}
>
<>{children}</>
</Modal>
)
}

Expand Down
15 changes: 8 additions & 7 deletions assets/tests/components/inputModal.test.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
import { jest, describe, test, expect } from "@jest/globals"
import React from "react"
import { render, fireEvent } from "@testing-library/react"
import { render, waitFor } from "@testing-library/react"
import InputModal from "../../src/components/inputModal"
import { StateDispatchProvider } from "../../src/contexts/stateDispatchContext"
import { initialState, closeInputModal } from "../../src/state"
import userEvent from "@testing-library/user-event"

describe("InputModal", () => {
test("can be dismissed with escape key", () => {
test("can be dismissed with escape key", async () => {
const mockDispatch = jest.fn()

const result = render(
render(
<StateDispatchProvider state={initialState} dispatch={mockDispatch}>
<InputModal>
<>Hello, world!</>
</InputModal>
</StateDispatchProvider>
)

fireEvent.keyDown(result.getByText("Hello, world!"), { key: "Escape" })

expect(mockDispatch).toHaveBeenCalledWith(closeInputModal())
await userEvent.keyboard("{Escape}")
await waitFor(() =>
expect(mockDispatch).toHaveBeenCalledWith(closeInputModal())
)
})
})

0 comments on commit ed3ea5b

Please sign in to comment.