-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
explorer: Add missing
onDestroy
hooks to stop polling
Resolves #1749
- Loading branch information
1 parent
e9d0498
commit 2509c3b
Showing
6 changed files
with
3,521 additions
and
1 deletion.
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
explorer/src/lib/containers/__tests__/StatisticsPanel.spec.js
This file contains 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,75 @@ | ||
import { afterAll, afterEach, describe, expect, it, vi } from "vitest"; | ||
import { cleanup, render } from "@testing-library/svelte"; | ||
import { get } from "svelte/store"; | ||
|
||
import { duskAPI } from "$lib/services"; | ||
import { appStore } from "$lib/stores"; | ||
import { apiMarketData, apiNodeLocations, apiStats } from "$lib/mock-data"; | ||
|
||
import { StatisticsPanel } from ".."; | ||
|
||
describe("StatisticsPanel", () => { | ||
vi.useFakeTimers(); | ||
|
||
const STATS_FETCH_INTERVAL = 5000; | ||
const { network } = get(appStore); | ||
const getMarketDataSpy = vi | ||
.spyOn(duskAPI, "getMarketData") | ||
.mockResolvedValue({ | ||
currentPrice: apiMarketData.market_data.current_price, | ||
marketCap: apiMarketData.market_data.market_cap, | ||
}); | ||
const getNodeLocationsSpy = vi | ||
.spyOn(duskAPI, "getNodeLocations") | ||
.mockResolvedValue(apiNodeLocations.data); | ||
const getStatsSpy = vi.spyOn(duskAPI, "getStats").mockResolvedValue(apiStats); | ||
|
||
afterEach(() => { | ||
cleanup(); | ||
getMarketDataSpy.mockClear(); | ||
getNodeLocationsSpy.mockClear(); | ||
getStatsSpy.mockClear(); | ||
}); | ||
|
||
afterAll(() => { | ||
vi.useRealTimers(); | ||
getMarketDataSpy.mockRestore(); | ||
getNodeLocationsSpy.mockRestore(); | ||
getStatsSpy.mockRestore(); | ||
}); | ||
|
||
it("should render the StatisticsPanel, query for the necessary info, start polling for stats and stop the polling when unmounted", async () => { | ||
const { container, unmount } = render(StatisticsPanel); | ||
|
||
expect(container.firstChild).toMatchSnapshot(); | ||
expect(getMarketDataSpy).toHaveBeenCalledTimes(1); | ||
expect(getMarketDataSpy).toHaveBeenNthCalledWith(1, network); | ||
expect(getNodeLocationsSpy).toHaveBeenCalledTimes(1); | ||
expect(getNodeLocationsSpy).toHaveBeenNthCalledWith(1, network); | ||
expect(getStatsSpy).toHaveBeenCalledTimes(1); | ||
expect(getStatsSpy).toHaveBeenNthCalledWith(1, network); | ||
|
||
await vi.advanceTimersByTimeAsync(1); | ||
|
||
// snapshot with received data from APIs | ||
expect(container.firstChild).toMatchSnapshot(); | ||
|
||
await vi.advanceTimersByTimeAsync(STATS_FETCH_INTERVAL - 1); | ||
|
||
expect(getStatsSpy).toHaveBeenCalledTimes(2); | ||
expect(getStatsSpy).toHaveBeenNthCalledWith(2, network); | ||
|
||
await vi.advanceTimersByTimeAsync(STATS_FETCH_INTERVAL); | ||
|
||
expect(getStatsSpy).toHaveBeenCalledTimes(3); | ||
expect(getStatsSpy).toHaveBeenNthCalledWith(3, network); | ||
|
||
unmount(); | ||
|
||
await vi.advanceTimersByTimeAsync(STATS_FETCH_INTERVAL * 10); | ||
|
||
expect(getStatsSpy).toHaveBeenCalledTimes(3); | ||
expect(getNodeLocationsSpy).toHaveBeenCalledTimes(1); | ||
expect(getMarketDataSpy).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
Oops, something went wrong.