github.com/tilt-dev/tilt@v0.33.15-0.20240515162809-0a22ed45d8a0/web/src/SidebarItemView.test.tsx (about)

     1  import { render, RenderOptions, screen } from "@testing-library/react"
     2  import React from "react"
     3  import Features, { FeaturesTestProvider } from "./feature"
     4  import { LogAlertIndex } from "./LogStore"
     5  import PathBuilder from "./PathBuilder"
     6  import SidebarItem from "./SidebarItem"
     7  import SidebarItemView from "./SidebarItemView"
     8  import { oneResource, TestResourceOptions } from "./testdata"
     9  import { ResourceView } from "./types"
    10  
    11  const PATH_BUILDER = PathBuilder.forTesting("localhost", "/")
    12  const LOG_ALERT_INDEX: LogAlertIndex = { alertsForSpanId: () => [] }
    13  
    14  function customRender(sidebarItem: SidebarItem, options?: RenderOptions) {
    15    const features = new Features(null)
    16    return render(
    17      <SidebarItemView
    18        item={sidebarItem}
    19        selected={false}
    20        resourceView={ResourceView.Log}
    21        pathBuilder={PATH_BUILDER}
    22        groupView={false}
    23      />,
    24      {
    25        wrapper: ({ children }) => (
    26          <FeaturesTestProvider value={features}>{children}</FeaturesTestProvider>
    27        ),
    28        ...options,
    29      }
    30    )
    31  }
    32  
    33  const oneSidebarItem = (options: TestResourceOptions) => {
    34    return new SidebarItem(oneResource(options), LOG_ALERT_INDEX)
    35  }
    36  
    37  describe("SidebarItemView", () => {
    38    it("does display a disabled resource with disabled view", () => {
    39      const item = oneSidebarItem({ disabled: true })
    40      customRender(item)
    41  
    42      expect(screen.getByText(item.name)).toBeInTheDocument()
    43      expect(screen.getByRole("link", { name: item.name })).toBeInTheDocument()
    44      expect(screen.getByRole("button", { name: /star/i })).toBeInTheDocument()
    45      expect(screen.queryByLabelText("Trigger update")).toBeNull()
    46    })
    47  
    48    it("does render an enabled resource with enabled view", () => {
    49      const item = oneSidebarItem({ disabled: false })
    50      customRender(item)
    51  
    52      expect(screen.getByText(item.name)).toBeInTheDocument()
    53      expect(
    54        screen.getAllByRole("button", { name: /star/i })[0]
    55      ).toBeInTheDocument()
    56      expect(screen.getByLabelText("Trigger update")).toBeInTheDocument()
    57    })
    58  })