-
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 tests for
+page.svelte
routes
- Loading branch information
Showing
9 changed files
with
4,111 additions
and
5 deletions.
There are no files selected for viewing
1,217 changes: 1,217 additions & 0 deletions
1,217
explorer/src/routes/blocks/__tests__/__snapshots__/page.spec.js.snap
Large diffs are not rendered by default.
Oops, something went wrong.
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,58 @@ | ||
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 { transformBlock } from "$lib/chain-info"; | ||
import { appStore } from "$lib/stores"; | ||
import { apiBlocks } from "$lib/mock-data"; | ||
|
||
import Blocks from "../+page.svelte"; | ||
|
||
describe("Blocks page", () => { | ||
vi.useFakeTimers(); | ||
|
||
const { fetchInterval, network } = get(appStore); | ||
const getBlocksSpy = vi | ||
.spyOn(duskAPI, "getBlocks") | ||
.mockResolvedValue(apiBlocks.data.blocks.map(transformBlock)); | ||
|
||
afterEach(() => { | ||
cleanup(); | ||
getBlocksSpy.mockClear(); | ||
}); | ||
|
||
afterAll(() => { | ||
vi.useRealTimers(); | ||
getBlocksSpy.mockRestore(); | ||
}); | ||
|
||
it("should render the Blocks page, start polling for blocks and stop the polling when unmounted", async () => { | ||
const { container, unmount } = render(Blocks); | ||
|
||
expect(container.firstChild).toMatchSnapshot(); | ||
expect(getBlocksSpy).toHaveBeenCalledTimes(1); | ||
expect(getBlocksSpy).toHaveBeenNthCalledWith(1, network); | ||
|
||
await vi.advanceTimersByTimeAsync(1); | ||
|
||
// snapshot with received data from APIs | ||
expect(container.firstChild).toMatchSnapshot(); | ||
|
||
await vi.advanceTimersByTimeAsync(fetchInterval - 1); | ||
|
||
expect(getBlocksSpy).toHaveBeenCalledTimes(2); | ||
expect(getBlocksSpy).toHaveBeenNthCalledWith(2, network); | ||
|
||
await vi.advanceTimersByTimeAsync(fetchInterval); | ||
|
||
expect(getBlocksSpy).toHaveBeenCalledTimes(3); | ||
expect(getBlocksSpy).toHaveBeenNthCalledWith(3, network); | ||
|
||
unmount(); | ||
|
||
await vi.advanceTimersByTimeAsync(fetchInterval * 10); | ||
|
||
expect(getBlocksSpy).toHaveBeenCalledTimes(3); | ||
}); | ||
}); |
Oops, something went wrong.