github.com/tilt-dev/tilt@v0.36.0/web/src/HeaderBar.test.tsx (about) 1 import { render, screen } from "@testing-library/react" 2 import userEvent from "@testing-library/user-event" 3 import React from "react" 4 import { act } from "react-dom/test-utils" 5 import { MemoryRouter } from "react-router-dom" 6 import HeaderBar, { HeaderBarPage } from "./HeaderBar" 7 import { SnapshotActionTestProvider } from "./snapshot" 8 import { nResourceView } from "./testdata" 9 10 describe("HeaderBar", () => { 11 describe("keyboard shortcuts", () => { 12 const openModal = jest.fn() 13 14 beforeEach(() => { 15 openModal.mockReset() 16 17 const snapshotAction = { 18 enabled: true, 19 openModal, 20 } 21 22 render( 23 <MemoryRouter initialEntries={["/"]}> 24 <SnapshotActionTestProvider value={snapshotAction}> 25 <HeaderBar 26 view={nResourceView(2)} 27 currentPage={HeaderBarPage.Detail} 28 isSocketConnected={true} 29 /> 30 </SnapshotActionTestProvider> 31 </MemoryRouter> 32 ) 33 }) 34 35 it("opens the help dialog on '?' keypress", () => { 36 // Expect that the help dialog is NOT visible at start 37 expect(screen.queryByRole("heading", { name: /Help/i })).toBeNull() 38 39 act(() => { 40 userEvent.keyboard("?") 41 }) 42 43 expect(screen.getByRole("heading", { name: /Help/i })).toBeInTheDocument() 44 }) 45 46 it("calls `openModal` snapshot callback on 's' keypress", () => { 47 expect(openModal).not.toBeCalled() 48 49 act(() => { 50 userEvent.keyboard("s") 51 }) 52 53 expect(openModal).toBeCalledTimes(1) 54 }) 55 }) 56 })