Skip to content
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

explorer: add tests for +page.svelte routes #1770

Merged
merged 1 commit into from
May 23, 2024
Merged
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
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
Loading