Skip to content

Commit

Permalink
Merge pull request #5 from osakunta/cms-hook-tests
Browse files Browse the repository at this point in the history
add vitest and basic tests
  • Loading branch information
vuolen authored Jul 15, 2024
2 parents d5e74da + 2193dc9 commit c73d60c
Show file tree
Hide file tree
Showing 4 changed files with 2,360 additions and 133 deletions.
36 changes: 36 additions & 0 deletions hooks/useCMS.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { expect, test, vi } from "vitest";
import useCMS from "./useCMS";
import { renderHook } from "@testing-library/react";

test("calls fetch", () => {
const fetchMock = vi.spyOn(global, "fetch");

renderHook(() => useCMS({ collection: "text" }));

expect(fetchMock).toHaveBeenCalled();
});

test("does not call fetch on rerender", () => {
const fetchMock = vi.spyOn(global, "fetch");

const { rerender } = renderHook(() => useCMS({ collection: "text" }));
rerender();

expect(fetchMock).toHaveBeenCalledTimes(1);
});

test.skip("calls fetch once for the same collection", () => {
const fetchMock = vi.spyOn(global, "fetch");

renderHook(() => useCMS({ collection: "text" }));
renderHook(() => useCMS({ collection: "text" }));
expect(fetchMock).toHaveBeenCalledTimes(1);
});

test("calls fetch once per collection", () => {
const fetchMock = vi.spyOn(global, "fetch");

renderHook(() => useCMS({ collection: "text" }));
renderHook(() => useCMS({ collection: "image" }));
expect(fetchMock).toHaveBeenCalledTimes(2);
});
Loading

0 comments on commit c73d60c

Please sign in to comment.