adf0d17497
publish / version_or_publish (push) Waiting to run
storybook-build / changes (push) Waiting to run
storybook-build / :storybook-build (push) Blocked by required conditions
Sync Gradio Skills to Hugging Face / sync-skills (push) Waiting to run
functional / changes (push) Waiting to run
functional / build-frontend (push) Blocked by required conditions
functional / functional-test-SSR=false (push) Blocked by required conditions
functional / functional-reload (push) Blocked by required conditions
functional / functional-test-SSR=true (push) Blocked by required conditions
hygiene / hygiene-test (push) Waiting to run
python / changes (push) Waiting to run
python / build (push) Blocked by required conditions
python / test-ubuntu-latest-flaky (push) Blocked by required conditions
python / test-ubuntu-latest-not-flaky (push) Blocked by required conditions
python / test-windows-latest-flaky (push) Blocked by required conditions
python / test-windows-latest-not-flaky (push) Blocked by required conditions
js / changes (push) Waiting to run
js / js-test (push) Blocked by required conditions
docs-build / changes (push) Waiting to run
docs-build / docs-build (push) Blocked by required conditions
docs-build / website-build (push) Blocked by required conditions
203 lines
4.7 KiB
TypeScript
203 lines
4.7 KiB
TypeScript
import { test, describe, afterEach, expect, vi } from "vitest";
|
|
import { cleanup, render, waitFor } from "@self/tootils/render";
|
|
|
|
import Timer from "./Index.svelte";
|
|
|
|
const default_props = {
|
|
value: 1,
|
|
active: true
|
|
};
|
|
|
|
describe("Timer", () => {
|
|
afterEach(() => cleanup());
|
|
|
|
test("renders without errors when active", async () => {
|
|
const result = await render(Timer, default_props);
|
|
expect(result.container).toBeInTheDocument();
|
|
});
|
|
|
|
test("renders without errors when inactive", async () => {
|
|
const result = await render(Timer, {
|
|
...default_props,
|
|
active: false
|
|
});
|
|
expect(result.container).toBeInTheDocument();
|
|
});
|
|
});
|
|
|
|
describe("Props: active", () => {
|
|
afterEach(() => cleanup());
|
|
|
|
test("active=true dispatches tick events", async () => {
|
|
const { listen } = await render(Timer, {
|
|
...default_props,
|
|
value: 0.1,
|
|
active: true
|
|
});
|
|
|
|
const tick = listen("tick");
|
|
|
|
await waitFor(() => {
|
|
expect(tick).toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
test("active=false does not dispatch tick events", async () => {
|
|
const { listen } = await render(Timer, {
|
|
...default_props,
|
|
value: 0.1,
|
|
active: false
|
|
});
|
|
|
|
const tick = listen("tick");
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, 300));
|
|
expect(tick).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test("setting active to false via set_data stops tick events", async () => {
|
|
const { listen, set_data } = await render(Timer, {
|
|
...default_props,
|
|
value: 0.1,
|
|
active: true
|
|
});
|
|
|
|
const tick = listen("tick");
|
|
|
|
await waitFor(() => {
|
|
expect(tick).toHaveBeenCalled();
|
|
});
|
|
|
|
await set_data({ active: false });
|
|
|
|
const count_after_stop = tick.mock.calls.length;
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, 400));
|
|
expect(tick.mock.calls.length).toBe(count_after_stop);
|
|
});
|
|
|
|
test("setting active to true via set_data starts tick events", async () => {
|
|
const { listen, set_data } = await render(Timer, {
|
|
...default_props,
|
|
value: 0.1,
|
|
active: false
|
|
});
|
|
|
|
const tick = listen("tick");
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, 300));
|
|
expect(tick).not.toHaveBeenCalled();
|
|
|
|
await set_data({ active: true });
|
|
|
|
await waitFor(() => {
|
|
expect(tick).toHaveBeenCalled();
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("Props: value", () => {
|
|
afterEach(() => cleanup());
|
|
|
|
test("value controls the tick interval in seconds", async () => {
|
|
const { listen } = await render(Timer, {
|
|
...default_props,
|
|
value: 0.5,
|
|
active: true
|
|
});
|
|
|
|
const tick = listen("tick");
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, 120));
|
|
expect(tick).not.toHaveBeenCalled();
|
|
|
|
await waitFor(() => {
|
|
expect(tick).toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
test("changing value via set_data resets the interval", async () => {
|
|
const { listen, set_data } = await render(Timer, {
|
|
...default_props,
|
|
value: 0.1,
|
|
active: true
|
|
});
|
|
|
|
const tick = listen("tick");
|
|
|
|
await waitFor(() => {
|
|
expect(tick).toHaveBeenCalled();
|
|
});
|
|
|
|
await set_data({ value: 0.5 });
|
|
|
|
const count_after_change = tick.mock.calls.length;
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, 200));
|
|
expect(tick.mock.calls.length).toBe(count_after_change);
|
|
|
|
await waitFor(() => {
|
|
expect(tick.mock.calls.length).toBeGreaterThan(count_after_change);
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("get_data / set_data", () => {
|
|
afterEach(() => cleanup());
|
|
|
|
test("get_data returns the current value and active state", async () => {
|
|
const { get_data } = await render(Timer, default_props);
|
|
|
|
const data = await get_data();
|
|
expect(data.value).toBe(1);
|
|
expect(data.active).toBe(true);
|
|
});
|
|
|
|
test("set_data updates the value and active state", async () => {
|
|
const { set_data, get_data } = await render(Timer, default_props);
|
|
|
|
await set_data({ value: 5, active: false });
|
|
const data = await get_data();
|
|
expect(data.value).toBe(5);
|
|
expect(data.active).toBe(false);
|
|
});
|
|
|
|
test("set_data then get_data round-trips", async () => {
|
|
const { set_data, get_data } = await render(Timer, default_props);
|
|
|
|
await set_data({ value: 2.5, active: false });
|
|
const data = await get_data();
|
|
expect(data.value).toBe(2.5);
|
|
expect(data.active).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe("Edge cases", () => {
|
|
afterEach(() => cleanup());
|
|
|
|
test("large interval value dispatches ticks infrequently", async () => {
|
|
const { listen } = await render(Timer, {
|
|
...default_props,
|
|
value: 10,
|
|
active: true
|
|
});
|
|
|
|
const tick = listen("tick");
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
expect(tick).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test("no spurious tick event on mount before the first interval", async () => {
|
|
const { listen } = await render(Timer, {
|
|
...default_props,
|
|
value: 10,
|
|
active: true
|
|
});
|
|
|
|
const tick = listen("tick");
|
|
|
|
expect(tick).not.toHaveBeenCalled();
|
|
});
|
|
});
|