github.com/tilt-dev/tilt@v0.36.0/web/src/OverviewActionBarKeyboardShortcuts.test.tsx (about)

     1  import { render, RenderResult } from "@testing-library/react"
     2  import userEvent from "@testing-library/user-event"
     3  import React from "react"
     4  import { logLinesToString } from "./logs"
     5  import LogStore from "./LogStore"
     6  import OverviewActionBarKeyboardShortcuts from "./OverviewActionBarKeyboardShortcuts"
     7  import { appendLinesForManifestAndSpan } from "./testlogs"
     8  
     9  const TEST_URL_A = { url: "https://tilt.dev:4000" }
    10  const TEST_URL_B = { url: "https://tilt.dev:4001" }
    11  const TEST_RESOURCE_NAME = "fake-resource"
    12  
    13  describe("Detail View keyboard shortcuts", () => {
    14    let rerender: RenderResult["rerender"]
    15    let openEndpointMock: jest.Mock
    16    let logStore: LogStore
    17  
    18    beforeEach(() => {
    19      logStore = new LogStore()
    20      openEndpointMock = jest.fn()
    21      rerender = render(
    22        <OverviewActionBarKeyboardShortcuts
    23          logStore={logStore}
    24          resourceName={TEST_RESOURCE_NAME}
    25          openEndpointUrl={openEndpointMock}
    26          endpoints={[TEST_URL_A, TEST_URL_B]}
    27        />
    28      ).rerender
    29    })
    30  
    31    afterEach(() => {})
    32  
    33    describe("open endpoints", () => {
    34      it("does NOT open any endpoints if there aren't any", () => {
    35        rerender(
    36          <OverviewActionBarKeyboardShortcuts
    37            logStore={logStore}
    38            resourceName={TEST_RESOURCE_NAME}
    39            openEndpointUrl={openEndpointMock}
    40          />
    41        )
    42  
    43        userEvent.keyboard("{Shift>}1")
    44  
    45        expect(openEndpointMock).not.toHaveBeenCalled()
    46      })
    47  
    48      it("opens the first endpoint when SHIFT + 1 are pressed", () => {
    49        userEvent.keyboard("{Shift>}1")
    50  
    51        expect(openEndpointMock).toHaveBeenCalledWith(TEST_URL_A.url)
    52      })
    53  
    54      it("opens the corresponding endpoint when SHIFT + a number are pressed", () => {
    55        userEvent.keyboard("{Shift>}2")
    56  
    57        expect(openEndpointMock).toHaveBeenCalledWith(TEST_URL_B.url)
    58      })
    59    })
    60  
    61    describe("clear logs", () => {
    62      // Add a log to the store
    63      beforeEach(() =>
    64        appendLinesForManifestAndSpan(logStore, TEST_RESOURCE_NAME, "span:1", [
    65          "line 1\n",
    66        ])
    67      )
    68  
    69      it("clears logs when the META + BACKSPACE keys are pressed", () => {
    70        userEvent.keyboard("{Meta>}{Backspace}")
    71  
    72        expect(logLinesToString(logStore.allLog(), false)).toEqual("")
    73      })
    74  
    75      it("clears logs when the CTRL + BACKSPACE keys are pressed", () => {
    76        userEvent.keyboard("{Control>}{Backspace}")
    77  
    78        expect(logLinesToString(logStore.allLog(), false)).toEqual("")
    79      })
    80    })
    81  })