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

     1  import { render, screen } from "@testing-library/react"
     2  import React from "react"
     3  import { MemoryRouter } from "react-router"
     4  import {
     5    getDocumentTitle,
     6    ResourceGroupStatus,
     7    StatusCounts,
     8  } from "./ResourceStatusSummary"
     9  
    10  function expectStatusCounts(expected: { label: string; counts: number[] }[]) {
    11    const actual = expected.map(({ label, counts }) => {
    12      const actualCounts: { label: string; counts: number[] } = {
    13        label,
    14        counts: [],
    15      }
    16  
    17      const actualCount =
    18        screen.queryByLabelText(`${label} count`)?.textContent ?? "0"
    19      actualCounts.counts.push(parseInt(actualCount))
    20  
    21      // Indicates an "out of count" for this label is expected
    22      if (counts[1]) {
    23        const actualCountOf =
    24          screen.queryByLabelText("Out of total resource count")?.textContent ??
    25          "0"
    26        actualCounts.counts.push(parseInt(actualCountOf))
    27      }
    28  
    29      return actualCounts
    30    })
    31  
    32    expect(actual).toEqual(expected)
    33  }
    34  
    35  const testCounts: StatusCounts = {
    36    totalEnabled: 11,
    37    healthy: 0,
    38    warning: 2,
    39    unhealthy: 4,
    40    pending: 0,
    41    disabled: 2,
    42  }
    43  
    44  const testCountsUnhealthy: StatusCounts = {
    45    totalEnabled: 5,
    46    healthy: 3,
    47    unhealthy: 2,
    48    disabled: 0,
    49    pending: 0,
    50    warning: 0,
    51  }
    52  
    53  const testCountsHealthy: StatusCounts = {
    54    totalEnabled: 5,
    55    healthy: 5,
    56    disabled: 3,
    57    unhealthy: 0,
    58    pending: 0,
    59    warning: 0,
    60  }
    61  
    62  const testCountsPending: StatusCounts = {
    63    totalEnabled: 5,
    64    healthy: 3,
    65    pending: 2,
    66    disabled: 2,
    67    unhealthy: 0,
    68    warning: 0,
    69  }
    70  
    71  const testCountsAllDisabled: StatusCounts = {
    72    totalEnabled: 0,
    73    disabled: 5,
    74    healthy: 0,
    75    pending: 0,
    76    unhealthy: 0,
    77    warning: 0,
    78  }
    79  
    80  it("shows the counts it's given", () => {
    81    render(
    82      <MemoryRouter>
    83        <ResourceGroupStatus
    84          counts={testCounts}
    85          healthyLabel="healthy"
    86          displayText="resources"
    87          labelText="Testing resource status summary"
    88          unhealthyLabel="unhealthy"
    89          warningLabel="warning"
    90          linkToLogFilters={true}
    91        />
    92      </MemoryRouter>
    93    )
    94  
    95    // "healthy" gets the denominator (totalEnabled)
    96    // 0 counts are not rendered, except for "healthy"
    97    expectStatusCounts([
    98      { label: "unhealthy", counts: [4] },
    99      { label: "warning", counts: [2] },
   100      { label: "healthy", counts: [0, 11] },
   101      { label: "disabled", counts: [2] },
   102    ])
   103  })
   104  
   105  it("links to warning and unhealthy resources when `linkToLogFilters` is true", () => {
   106    render(
   107      <MemoryRouter>
   108        <ResourceGroupStatus
   109          counts={testCounts}
   110          healthyLabel="healthy"
   111          displayText="resources"
   112          labelText="Testing resource status summary"
   113          unhealthyLabel="unhealthy"
   114          warningLabel="warning"
   115          linkToLogFilters={true}
   116        />
   117      </MemoryRouter>
   118    )
   119  
   120    expect(
   121      screen.getByRole("link", { name: "unhealthy count" })
   122    ).toBeInTheDocument()
   123    expect(
   124      screen.getByRole("link", { name: "warning count" })
   125    ).toBeInTheDocument()
   126  })
   127  
   128  it("does NOT link to warning and unhealthy resources when `linkToLogFilters` is false", () => {
   129    render(
   130      <MemoryRouter>
   131        <ResourceGroupStatus
   132          counts={testCounts}
   133          healthyLabel="healthy"
   134          displayText="resources"
   135          labelText="Testing resource status summary"
   136          unhealthyLabel="unhealthy"
   137          warningLabel="warning"
   138          linkToLogFilters={false}
   139        />
   140      </MemoryRouter>
   141    )
   142  
   143    expect(screen.queryByRole("link")).toBeNull()
   144  })
   145  
   146  describe("document metadata based on resource statuses", () => {
   147    test.each<
   148      [
   149        caseName: string,
   150        args: {
   151          counts: StatusCounts
   152          isSnapshot: boolean
   153          isSocketConnected: boolean
   154        },
   155        title: string,
   156        faviconColor: string
   157      ]
   158    >([
   159      [
   160        "no socket connection",
   161        {
   162          counts: testCountsUnhealthy,
   163          isSnapshot: false,
   164          isSocketConnected: false,
   165        },
   166        "Disconnected ┊ Tilt",
   167        "gray",
   168      ],
   169      [
   170        "viewing a snapshot",
   171        {
   172          counts: testCountsUnhealthy,
   173          isSnapshot: true,
   174          isSocketConnected: false,
   175        },
   176        "Snapshot: ✖︎ 2 ┊ Tilt",
   177        "red",
   178      ],
   179      [
   180        "some resources are unhealthy",
   181        {
   182          counts: testCountsUnhealthy,
   183          isSnapshot: false,
   184          isSocketConnected: true,
   185        },
   186        "✖︎ 2 ┊ Tilt",
   187        "red",
   188      ],
   189      [
   190        "all enabled resources are healthy",
   191        { counts: testCountsHealthy, isSnapshot: false, isSocketConnected: true },
   192        "✔︎ 5/5 ┊ Tilt",
   193        "green",
   194      ],
   195      [
   196        "some resources are pending",
   197        { counts: testCountsPending, isSnapshot: false, isSocketConnected: true },
   198        "… 3/5 ┊ Tilt",
   199        "gray",
   200      ],
   201      [
   202        "all resources are disabled",
   203        {
   204          counts: testCountsAllDisabled,
   205          isSnapshot: false,
   206          isSocketConnected: true,
   207        },
   208        "✔︎ 0/0 ┊ Tilt",
   209        "gray",
   210      ],
   211    ])(
   212      "has correct title and icon when %p",
   213      (_testTitle, args, expectedTitle, expectedIconColor) => {
   214        const { counts, isSnapshot, isSocketConnected } = args
   215        const { title, faviconHref } = getDocumentTitle(
   216          counts,
   217          isSnapshot,
   218          isSocketConnected
   219        )
   220        expect(title).toBe(expectedTitle)
   221        expect(faviconHref).toStrictEqual(
   222          expect.stringContaining(expectedIconColor)
   223        )
   224      }
   225    )
   226  })