Skip to content
This repository was archived by the owner on Dec 11, 2025. It is now read-only.
Draft
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
59 changes: 58 additions & 1 deletion packages/unuse/src/useIntervalFn/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @vitest-environment happy-dom

import type { UnComputed } from 'unuse-reactivity';
import { isUnComputed, unSignal } from 'unuse-reactivity';
import { isUnComputed, unEffectScope, unSignal } from 'unuse-reactivity';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import type { Pausable } from '.';
import { useIntervalFn } from '.';
Expand Down Expand Up @@ -39,6 +39,28 @@
expect(callback).toHaveBeenCalledTimes(2);
}

async function execImmediateCallback({ isActive, pause, resume }: Pausable) {
expect(isActive).toSatisfy(isUnComputed);
expect((isActive as unknown as UnComputed<boolean>).get()).toBeTruthy();
expect(callback).toHaveBeenCalledTimes(1);

await vi.advanceTimersByTimeAsync(60);
expect(callback).toHaveBeenCalledTimes(2);

pause();
expect((isActive as unknown as UnComputed<boolean>).get()).toBeFalsy();

await vi.advanceTimersByTimeAsync(60);
expect(callback).toHaveBeenCalledTimes(2);

resume();
expect((isActive as unknown as UnComputed<boolean>).get()).toBeTruthy();
expect(callback).toHaveBeenCalledTimes(3);

await vi.advanceTimersByTimeAsync(60);
expect(callback).toHaveBeenCalledTimes(4);
}

it('basic pause/resume', async () => {
await exec(useIntervalFn(callback, 50));

Expand All @@ -53,6 +75,41 @@
expect(callback).toHaveBeenCalledTimes(1);
});

it('pause/resume with immediateCallback', async () => {
await execImmediateCallback(
useIntervalFn(callback, 50, { immediateCallback: true })
);

callback = vi.fn();

const interval = unSignal(50);
await execImmediateCallback(
useIntervalFn(callback, interval, { immediateCallback: true })
);

callback.mockClear();
interval.set(20);
// mimic nextTick
await vi.waitFor(() => {
expect(callback).toHaveBeenCalledTimes(1);

Check failure on line 94 in packages/unuse/src/useIntervalFn/index.spec.ts

View workflow job for this annotation

GitHub Actions / coverage-pr

packages/unuse/src/useIntervalFn/index.spec.ts > useIntervalFn > pause/resume with immediateCallback

AssertionError: expected "spy" to be called 1 times, but got 51 times ❯ packages/unuse/src/useIntervalFn/index.spec.ts:94:24

Check failure on line 94 in packages/unuse/src/useIntervalFn/index.spec.ts

View workflow job for this annotation

GitHub Actions / Build & Unit Test: node-24, ubuntu-latest

packages/unuse/src/useIntervalFn/index.spec.ts > useIntervalFn > pause/resume with immediateCallback

AssertionError: expected "spy" to be called 1 times, but got 51 times ❯ packages/unuse/src/useIntervalFn/index.spec.ts:94:24
});
});

it('pause/resume in scope', async () => {
let promise!: Promise<void>;

const scope = unEffectScope(() => {
promise = exec(useIntervalFn(callback, 50));
});

await promise;

callback.mockClear();
scope();
await vi.advanceTimersByTimeAsync(60);
expect(callback).toHaveBeenCalledTimes(0);

Check failure on line 110 in packages/unuse/src/useIntervalFn/index.spec.ts

View workflow job for this annotation

GitHub Actions / coverage-pr

packages/unuse/src/useIntervalFn/index.spec.ts > useIntervalFn > pause/resume in scope

AssertionError: expected "spy" to be called +0 times, but got 1 times ❯ packages/unuse/src/useIntervalFn/index.spec.ts:110:22

Check failure on line 110 in packages/unuse/src/useIntervalFn/index.spec.ts

View workflow job for this annotation

GitHub Actions / Build & Unit Test: node-24, ubuntu-latest

packages/unuse/src/useIntervalFn/index.spec.ts > useIntervalFn > pause/resume in scope

AssertionError: expected "spy" to be called +0 times, but got 1 times ❯ packages/unuse/src/useIntervalFn/index.spec.ts:110:22
});

it('pause in callback', async () => {
const pausable = useIntervalFn(
() => {
Expand Down
Loading