Skip to content

Commit

Permalink
explorer: add tests for +page.svelte routes
Browse files Browse the repository at this point in the history
  • Loading branch information
deuch13 committed May 23, 2024
1 parent c3c86e4 commit 4c15146
Show file tree
Hide file tree
Showing 9 changed files with 4,109 additions and 5 deletions.
1,217 changes: 1,217 additions & 0 deletions explorer/src/routes/blocks/__tests__/__snapshots__/page.spec.js.snap

Large diffs are not rendered by default.

58 changes: 58 additions & 0 deletions explorer/src/routes/blocks/__tests__/page.spec.js
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);
});
});
Loading

0 comments on commit 4c15146

Please sign in to comment.