-
Notifications
You must be signed in to change notification settings - Fork 22
Migrate to uplot #3295
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
fakemonster
wants to merge
15
commits into
main
Choose a base branch
from
migrate-to-uplot
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Migrate to uplot #3295
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
3301582
Add a couple tooltip tests
fakemonster 3c57526
Get bash from env
fakemonster 633e5e0
Migrate from recharts to uplot
fakemonster 27f4db9
Use theme variables for colors and fonts
fakemonster a56a8d3
Add guard against accidental recreations from formatter prop
fakemonster 9e153db
Fix uPlot initialization in production builds
david-crespo c8e2b78
Tweak hover point colour
benjaminleonard b54ad40
Revert "Fix uPlot initialization in production builds"
fakemonster b2fc380
Avoid recalculating paths
fakemonster 1c8a4bc
Pick a better color name
fakemonster b26d17d
Add a test prohibiting "bad" redraw calls
fakemonster 785e721
patch-package uplot canvas to display-p3 color space
david-crespo ad56887
TimeSeriesChart: drop unused className prop and data memo, hoist CHAR…
david-crespo 823740e
TimeSeriesChart: fix width ratchet, defer chart mount until container…
david-crespo 968418e
TimeSeriesChart: put data memoization in a useful place
fakemonster File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| /* | ||
| * This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, you can obtain one at https://mozilla.org/MPL/2.0/. | ||
| * | ||
| * Copyright Oxide Computer Company | ||
| */ | ||
| import { render } from '@testing-library/react' | ||
| import { useEffect, type ComponentProps } from 'react' | ||
| import type UplotReactComponent from 'uplot-react' | ||
| import { describe, expect, test, vi } from 'vitest' | ||
|
|
||
| import { TimeSeriesChart } from './TimeSeriesChart' | ||
|
|
||
| const redraw = vi.fn() | ||
|
|
||
| const dataPropsPassed: unknown[] = [] | ||
|
|
||
| // the chart only mounts once the container is measured, and jsdom's | ||
| // ResizeObserver stub never fires | ||
| vi.mock('~/hooks/use-element-size', () => ({ | ||
| useElementSize: () => [{ width: 600, height: 300 }, () => {}], | ||
| })) | ||
|
|
||
| vi.mock('uplot-react', () => { | ||
| const MeplotReactComponent = (props: ComponentProps<typeof UplotReactComponent>) => { | ||
| dataPropsPassed.push(props.data) | ||
| useEffect(() => { | ||
| props.onCreate?.({ redraw } as never) | ||
| // eslint-disable-next-line react-hooks/exhaustive-deps | ||
| }, []) | ||
| return null | ||
| } | ||
| return { default: MeplotReactComponent } | ||
| }) | ||
|
|
||
| describe('safe redrawing', () => { | ||
| /* | ||
| * TimeSeriesChart uses uPlot's `redraw` method to repaint when `yAxisTickFormatter` changes. This | ||
| * is perfectly fine as long as it's called "the right way". Calling redraw "the wrong way" can | ||
| * cause uPlot to get stuck with bad settings; in this case, that would be an x range of `null` to | ||
| * `null`. That leaves the series basically unplottable, and the visible effect is a blank chart. | ||
| * | ||
| * This is only visible in production builds because StrictMode incidentally forces a re-create | ||
| * AFTER the issue, hiding it, but these tests are fine either way, because they simply prohibit | ||
| * "wrong" calls to redraw. | ||
| */ | ||
| const props = (formatter: (v: number) => string) => ({ | ||
| data: [{ timestamp: 0, value: 10 }], | ||
| title: 'CPU', | ||
| startTime: new Date(0), | ||
| endTime: new Date(3_600_000), | ||
| yAxisTickFormatter: formatter, | ||
| loading: false, | ||
| }) | ||
|
|
||
| const expectAllRedrawsSafe = () => { | ||
| for (const [rebuildPaths, recalcAxes] of redraw.mock.calls) { | ||
| expect(rebuildPaths).toBe(false) // the important part | ||
| expect(recalcAxes).toBe(true) | ||
| } | ||
| } | ||
|
|
||
| test('mounting never triggers an unsafe redraw', () => { | ||
| render(<TimeSeriesChart {...props((v) => `${v}%`)} />) | ||
| expectAllRedrawsSafe() | ||
| }) | ||
|
|
||
| test('a new formatter triggers a safe redraw', () => { | ||
| const { rerender } = render(<TimeSeriesChart {...props((v) => `${v}%`)} />) | ||
| redraw.mockClear() | ||
| rerender(<TimeSeriesChart {...props((v) => `${v} pct`)} />) | ||
| expect(redraw).toHaveBeenCalled() | ||
| expectAllRedrawsSafe() | ||
| }) | ||
|
|
||
| // uplot-react will do a deep comparison if the data reference changes to avoid rebuilding the | ||
| // chart, but it would be even better to skip that comparison by maintaining a reference | ||
| test('an unchanged data prop sends a stable reference down to uplot-react', () => { | ||
| const data = [ | ||
| { timestamp: 0, value: 10 }, | ||
| { timestamp: 1000, value: 20 }, | ||
| ] | ||
|
|
||
| dataPropsPassed.length = 0 | ||
| const { rerender } = render(<TimeSeriesChart {...props((v) => `${v}%`)} data={data} />) | ||
| rerender(<TimeSeriesChart {...props((v) => `${v} pct`)} data={data} />) | ||
|
|
||
| expect(dataPropsPassed.length).toBeGreaterThan(1) // it re-rendered | ||
| expect(new Set(dataPropsPassed).size).toBe(1) // but every render passed the identical reference | ||
|
|
||
| rerender(<TimeSeriesChart {...props((v) => `${v}%`)} data={[...data]} />) | ||
| expect(new Set(dataPropsPassed).size).toBe(2) // unless the reference changes | ||
| }) | ||
| }) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting kind of test. Basically it guards against removing the
useMemo? I’m ambivalent but there’s not much downside. To test it in a way the feels less like reimplementation, you’d have to assert about whether some action causes re-renders or not, which I don’t think I’ve seen done. Kind of an interesting idea.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i do think in my oncoming work this'll be more important to assert. a deep comparison could be quite a large amount of unnecessary checking in a chart with, say, 40 lines and a few thousand time points