|
| 1 | +/* |
| 2 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 | + * file, you can obtain one at https://mozilla.org/MPL/2.0/. |
| 5 | + * |
| 6 | + * Copyright Oxide Computer Company |
| 7 | + */ |
| 8 | +import type uPlot from 'uplot' |
| 9 | +import { describe, expect, test, vi, type MockInstance } from 'vitest' |
| 10 | +import { render } from 'vitest-browser-react' |
| 11 | + |
| 12 | +import { TimeSeriesChart } from './TimeSeriesChart' |
| 13 | + |
| 14 | +const defaultData = [ |
| 15 | + { timestamp: 0, value: 10 }, |
| 16 | + { timestamp: 1000, value: 20 }, |
| 17 | +] |
| 18 | + |
| 19 | +const props = (yAxisTickFormatter: (v: number) => string, data = defaultData) => ({ |
| 20 | + data, |
| 21 | + title: 'CPU', |
| 22 | + startTime: new Date(0), |
| 23 | + endTime: new Date(3_600_000), |
| 24 | + yAxisTickFormatter, |
| 25 | + loading: false, |
| 26 | +}) |
| 27 | + |
| 28 | +type Spies = { |
| 29 | + redraw: MockInstance<uPlot['redraw']> |
| 30 | + setData: MockInstance<uPlot['setData']> |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * Render the chart and wait for the uPlot instance to be created. The chart |
| 35 | + * mounts a beat after render: it waits for a real container size measurement |
| 36 | + * to arrive through ResizeObserver. |
| 37 | + */ |
| 38 | +async function renderChart(formatter: (v: number) => string) { |
| 39 | + let spies: Spies | undefined |
| 40 | + const onCreate = (u: uPlot) => { |
| 41 | + spies = { redraw: vi.spyOn(u, 'redraw'), setData: vi.spyOn(u, 'setData') } |
| 42 | + } |
| 43 | + const { rerender } = await render( |
| 44 | + <TimeSeriesChart {...props(formatter)} onCreate={onCreate} /> |
| 45 | + ) |
| 46 | + await vi.waitFor(() => expect(spies).toBeDefined()) |
| 47 | + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| 48 | + return { rerender, onCreate, ...spies! } // waitFor above guarantees spies is set |
| 49 | +} |
| 50 | + |
| 51 | +describe('safe redrawing', () => { |
| 52 | + /* |
| 53 | + * TimeSeriesChart uses uPlot's `redraw` method to repaint when `yAxisTickFormatter` changes. This |
| 54 | + * is perfectly fine as long as it's called "the right way". Calling redraw "the wrong way" can |
| 55 | + * cause uPlot to get stuck with bad settings; in this case, that would be an x range of `null` to |
| 56 | + * `null`. That leaves the series basically unplottable, and the visible effect is a blank chart. |
| 57 | + * |
| 58 | + * This is only visible in production builds because StrictMode incidentally forces a re-create |
| 59 | + * AFTER the issue, hiding it, but these tests are fine either way, because they simply prohibit |
| 60 | + * "wrong" calls to redraw. |
| 61 | + */ |
| 62 | + const expectAllRedrawsSafe = (redraw: Spies['redraw']) => { |
| 63 | + for (const [rebuildPaths, recalcAxes] of redraw.mock.calls) { |
| 64 | + expect(rebuildPaths).toBe(false) // the important part |
| 65 | + expect(recalcAxes).toBe(true) |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + test('mounting never triggers an unsafe redraw', async () => { |
| 70 | + const { redraw } = await renderChart((v) => `${v}%`) |
| 71 | + expectAllRedrawsSafe(redraw) |
| 72 | + }) |
| 73 | + |
| 74 | + test('a new formatter triggers a safe redraw', async () => { |
| 75 | + const { rerender, onCreate, redraw } = await renderChart((v) => `${v}%`) |
| 76 | + redraw.mockClear() |
| 77 | + await rerender(<TimeSeriesChart {...props((v) => `${v} pct`)} onCreate={onCreate} />) |
| 78 | + expect(redraw).toHaveBeenCalled() |
| 79 | + expectAllRedrawsSafe(redraw) |
| 80 | + }) |
| 81 | +}) |
| 82 | + |
| 83 | +test('rerenders only call setData when the data actually changes', async () => { |
| 84 | + const { rerender, onCreate, setData } = await renderChart((v) => `${v}%`) |
| 85 | + setData.mockClear() |
| 86 | + |
| 87 | + // same data reference, new formatter: nothing for uPlot to update |
| 88 | + await rerender(<TimeSeriesChart {...props((v) => `${v} pct`)} onCreate={onCreate} />) |
| 89 | + // new reference with equal contents: uplot-react's deep compare skips the update |
| 90 | + await rerender( |
| 91 | + <TimeSeriesChart {...props((v) => `${v}%`, [...defaultData])} onCreate={onCreate} /> |
| 92 | + ) |
| 93 | + expect(setData).not.toHaveBeenCalled() |
| 94 | + |
| 95 | + const newData = [...defaultData, { timestamp: 2000, value: 30 }] |
| 96 | + await rerender( |
| 97 | + <TimeSeriesChart {...props((v) => `${v}%`, newData)} onCreate={onCreate} /> |
| 98 | + ) |
| 99 | + expect(setData).toHaveBeenCalledTimes(1) |
| 100 | +}) |
0 commit comments