Skip to content
Open
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
42 changes: 41 additions & 1 deletion lib/__tests__/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { atom } from "nanostores";
import {atom, onMount, onNotify, onSet, onStart, onStop} from "nanostores";
import { nanoquery } from "../main";
import { noop, delay } from "./setup";

Expand All @@ -11,6 +11,46 @@ afterEach(() => {
});

describe("fetcher tests", () => {
describe("given an unmounted store", () => {
it("works without registered event handlers", async () => {
const fetcher = vi.fn().mockImplementation(async () => true);

const keys = ["/api", "/test1"];

const [makeFetcher] = nanoquery();
const store = makeFetcher(keys, {fetcher});

await advance();

expect(fetcher).not.toHaveBeenCalled();
expect(store.value).toEqual({loading: false});
});

it.each([
{ event: 'mount', method: onMount },
{ event: 'start', method: onStart },
{ event: 'stop', method: onStop },
{ event: 'set', method: onSet },
{ event: 'notify', method: onNotify },
])("works with a registered $event event handler", async ({ method: onTestEvent }) => {
const fetcher = vi.fn().mockImplementation(async () => true);

const keys = ["/api", "/test2"];

const [makeFetcher] = nanoquery();
const store = makeFetcher(keys, {fetcher});

const eventHandler = vi.fn();
onTestEvent(store, eventHandler);

await advance();

expect(fetcher).not.toHaveBeenCalled();
expect(store.value).toEqual({loading: false});
expect(eventHandler).not.toHaveBeenCalled();
});
})

test("fetches once for multiple subscriptions", async () => {
const fetcher = vi.fn().mockImplementation(async () => true);

Expand Down