Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions web-common/src/features/components/charts/Chart.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import Chart from "@rilldata/web-common/features/components/charts/Chart.svelte";
import type { ChartDataResult } from "@rilldata/web-common/features/components/charts";
import { render } from "@testing-library/svelte";
import chroma from "chroma-js";
import { readable } from "svelte/store";
import { describe, expect, it } from "vitest";

const measure = { name: "operation_jx_cnt", displayName: "JX count" };

function chartData(partial: Partial<ChartDataResult>): ChartDataResult {
return {
data: [],
isFetching: false,
fields: {},
isDarkMode: false,
hasComparison: false,
theme: {
primary: chroma("#1d4ed8"),
secondary: chroma("#7c3aed"),
},
...partial,
};
}

const chartSpec = {
metrics_view: "ncar_jx_detail_metrics",
x: { field: "cal_date", type: "temporal" as const },
y: { field: "operation_jx_cnt", type: "quantitative" as const },
};

function renderChart(data: ChartDataResult, measures = [measure]) {
return render(Chart, {
props: {
chartType: "line_chart",
chartSpec,
chartData: readable(data),
measures,
isCanvas: false,
view: undefined,
},
});
}

describe("Chart loading and error states", () => {
it("shows the query error instead of the spinner", () => {
const { container, queryByText } = renderChart(
chartData({
isFetching: true,
error: new Error("time range has too many bins"),
}),
);

expect(queryByText("time range has too many bins")).toBeInTheDocument();
expect(container.querySelector(".status")).toBeNull();
});
Comment on lines +44 to +55

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Every case here passes measures = [measure], so the measures.length === 0 half of the loading branch, which is what actually kept the chart spinning, is never exercised. With a non-empty measures the old {:else if error} branch was already reachable: the query client only retries network errors (web-common/src/lib/svelte-query/globalQueryClient.ts:13-14), so a failed aggregation settles with isFetching: false and error set. The regression to pin is chartData({ isFetching: false, error }) with measures: [], which is the state of a chart pointed at a missing or invalid metrics view, since getMeasuresForMetricView returns [] whenever there is no validSpec (web-common/src/features/metrics-views/metrics-view-selectors.ts:100-105). The existing isFetching: true plus error case only covers refetch-after-failure.


it("shows the spinner while fetching without an error", () => {
const { container, queryByText } = renderChart(
chartData({ isFetching: true }),
);

expect(container.querySelector(".status")).not.toBeNull();
expect(queryByText("No Data to Display")).toBeNull();
});

it("shows an empty state when the query resolves with no rows", () => {
const { container, queryByText } = renderChart(
chartData({ isFetching: false, data: [] }),
);

expect(queryByText("No Data to Display")).toBeInTheDocument();
expect(container.querySelector(".status")).toBeNull();
});
});
6 changes: 3 additions & 3 deletions web-common/src/features/components/charts/Chart.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -243,12 +243,12 @@
});
</script>

{#if isFetching || measures.length === 0}
{#if error}
<ComponentError error={error.message} />
{:else if isFetching || measures.length === 0}
<div class="flex items-center justify-center h-full w-full">
<Spinner status={EntityStatus.Running} size="20px" />
</div>
{:else if error}
<ComponentError error={error.message} />
{:else if hasNoData}
<div
class="flex w-full h-full p-2 text-xl text-fg-disabled items-center justify-center"
Expand Down
34 changes: 34 additions & 0 deletions web-common/vitest-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,40 @@ Object.defineProperty(window, "scrollTo", {
value: vi.fn(),
});

// Node 22+ exposes a global `localStorage` that is undefined unless
// --localstorage-file is set. Theme and other stores read the global at
// import time, so pin it to a memory-backed mock for jsdom tests.
if (
typeof globalThis.localStorage === "undefined" ||
typeof globalThis.localStorage?.getItem !== "function"
) {
const memory = new Map<string, string>();
const localStorageMock: Storage = {
get length() {
return memory.size;
},
clear: () => memory.clear(),
getItem: (key: string) => memory.get(key) ?? null,
key: (index: number) => [...memory.keys()][index] ?? null,
removeItem: (key: string) => {
memory.delete(key);
},
setItem: (key: string, value: string) => {
memory.set(key, value);
},
};
Object.defineProperty(globalThis, "localStorage", {
configurable: true,
writable: true,
value: localStorageMock,
});
Object.defineProperty(window, "localStorage", {
configurable: true,
writable: true,
value: localStorageMock,
});
}
Comment on lines +25 to +57

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shim is unrelated to the branch reorder and is inert on the pinned toolchain: .nvmrc is Node 22, where localStorage exists only behind --experimental-webstorage, and Chart.spec.ts passes on Node 20 with this file reverted to main. It is needed only on Node 25+, where localStorage is an unflagged global that is undefined without --localstorage-file and ThemeControl reads it at import time via explicitLocalStorageStore (web-common/src/features/themes/theme-control.ts:22-24), so the comment's claim about Node 22+ is inaccurate as written. If it stays, note that clearMocks: true does not reset the backing Map, so storage written by one test is visible to the next in the same file.


Settings.defaultWeekSettings = {
minimalDays: 4,
firstDay: 1,
Expand Down
Loading