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