Skip to content

Commit

Permalink
test: remove vitest globals
Browse files Browse the repository at this point in the history
  • Loading branch information
igordanchenko committed Aug 12, 2024
1 parent 0c15860 commit b586d6c
Show file tree
Hide file tree
Showing 12 changed files with 69 additions and 56 deletions.
2 changes: 2 additions & 0 deletions test/client/ColumnsPhotoAlbum.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { describe, expect, it } from "vitest";

import { ColumnsPhotoAlbum } from "../../src";
import { render, renderAndMatchSnapshot } from "../test-utils";
import photos from "../photos";
Expand Down
2 changes: 2 additions & 0 deletions test/client/MasonryPhotoAlbum.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { describe, expect, it } from "vitest";

import { ColumnsPhotoAlbum, MasonryPhotoAlbum } from "../../src";
import { render, renderAndMatchSnapshot } from "../test-utils";
import photos from "../photos";
Expand Down
2 changes: 2 additions & 0 deletions test/client/PhotoAlbum.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { describe, expect, it, vi } from "vitest";

import PhotoAlbum from "../../src";
import { render, renderAndMatchSnapshot } from "../test-utils";
import photos from "../photos";
Expand Down
2 changes: 2 additions & 0 deletions test/client/RowsPhotoAlbum.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { describe, expect, it } from "vitest";

import { RowsPhotoAlbum } from "../../src";
import { render, renderAndMatchSnapshot } from "../test-utils";
import photos from "../photos";
Expand Down
2 changes: 2 additions & 0 deletions test/core/heap.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { describe, expect, it } from "vitest";

import MinHeap, { rankingFunctionComparator } from "../../src/layouts/rows/heap";

const NumericComparator = rankingFunctionComparator<number>((x) => x);
Expand Down
38 changes: 19 additions & 19 deletions test/scroll/InfiniteScroll.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { describe, expect, it, vi } from "vitest";

import { MasonryPhotoAlbum, RowsPhotoAlbum } from "../../src";
import { UnstableInfiniteScroll as InfiniteScroll } from "../../src/scroll";
import { act, fireEvent, render } from "../test-utils";
import photos from "../photos";

async function triggerIntersection() {
await act(() => fireEvent(window, new Event("intersect")));
await act(async () => fireEvent(window, new Event("intersect")));
}

function fetcher(index: number) {
Expand All @@ -22,12 +24,10 @@ describe("InfiniteScroll", () => {
});

it("loads photos asynchronously", async () => {
const { getPhotos, queryByText } = await act(async () =>
render(
<InfiniteScroll fetch={fetcher} finished="Done">
<RowsPhotoAlbum photos={[]} />
</InfiniteScroll>,
),
const { getPhotos, queryByText } = render(
<InfiniteScroll fetch={fetcher} finished="Done">
<RowsPhotoAlbum photos={[]} />
</InfiniteScroll>,
);
expect(getPhotos().length).toBe(0);

Expand All @@ -43,8 +43,8 @@ describe("InfiniteScroll", () => {
it("handles fetch errors", async () => {
vi.useFakeTimers();

const { queryByText } = await act(async () =>
render(
try {
const { queryByText } = render(
<InfiniteScroll
fetch={() => {
throw new Error();
Expand All @@ -54,16 +54,16 @@ describe("InfiniteScroll", () => {
>
<RowsPhotoAlbum photos={[]} />
</InfiniteScroll>,
),
);

await triggerIntersection();
await triggerIntersection();
await triggerIntersection();
await act(vi.runAllTimers);
expect(queryByText("Error")).toBeTruthy();

vi.useRealTimers();
);

await triggerIntersection();
await triggerIntersection();
await triggerIntersection();
await act(async () => vi.runAllTimers());
expect(queryByText("Error")).toBeTruthy();
} finally {
vi.useRealTimers();
}
});

it("unloads offscreen photos", async () => {
Expand Down
2 changes: 2 additions & 0 deletions test/server/ServerPhotoAlbum.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { describe, expect, it } from "vitest";

import { LayoutType } from "../../src";
import { UnstableServerPhotoAlbum as ServerPhotoAlbum } from "../../src/server";
import { render } from "../test-utils";
Expand Down
2 changes: 2 additions & 0 deletions test/ssr/SSR.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { describe, expect, it } from "vitest";

import { RowsPhotoAlbum } from "../../src";
import { UnstableSSR as SSR } from "../../src/ssr";
import { render } from "../test-utils";
Expand Down
40 changes: 7 additions & 33 deletions test/test-utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import type React from "react";
import { act, render, RenderOptions } from "@testing-library/react";
import { expect } from "vitest";
import { render as originalRender, RenderOptions } from "@testing-library/react";

export * from "@testing-library/react";

function getPhotos(container: HTMLElement) {
return container.querySelectorAll(".react-photo-album--photo") as NodeListOf<HTMLElement>;
Expand All @@ -20,8 +23,8 @@ function getContainerWidth(container: HTMLElement) {
return cw ? Number.parseInt(cw, 10) : undefined;
}

function customRender(ui: React.ReactElement, options?: RenderOptions) {
const { container, ...rest } = render(ui, options);
export function render(ui: React.ReactElement, options?: RenderOptions) {
const { container, ...rest } = originalRender(ui, options);

return {
container,
Expand All @@ -34,34 +37,5 @@ function customRender(ui: React.ReactElement, options?: RenderOptions) {
}

export function renderAndMatchSnapshot(ui: React.ReactElement, options?: RenderOptions) {
expect(customRender(ui, options).asFragment()).toMatchSnapshot();
expect(render(ui, options).asFragment()).toMatchSnapshot();
}

export function mockObserver<MockEntry extends {}>(event: string, mockEntry?: () => MockEntry) {
return vi.fn().mockImplementation((callback) => {
const targets: Element[] = [];

const listener = () => {
act(() => {
callback(targets.map((target) => ({ target, ...mockEntry?.() })));
});
};

window.addEventListener(event, listener);

return {
observe: (target: Element) => {
targets.push(target);
},
unobserve: (target: Element) => {
targets.splice(targets.indexOf(target), 1);
},
disconnect: () => {
window.removeEventListener(event, listener);
},
};
});
}

export * from "@testing-library/react";
export { customRender as render };
29 changes: 28 additions & 1 deletion test/vitest.setup.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import "@testing-library/jest-dom/vitest";
import { afterEach, beforeEach, vi } from "vitest";

import { act, cleanup, fireEvent, mockObserver } from "./test-utils";
import { act, cleanup, fireEvent } from "./test-utils";

declare global {
interface Window {
Expand Down Expand Up @@ -43,6 +44,32 @@ window.resizeTo = (width: number, height: number) => {
});
};

function mockObserver<MockEntry extends {}>(event: string, mockEntry?: () => MockEntry) {
return vi.fn().mockImplementation((callback) => {
const targets: Element[] = [];

const listener = () => {
act(() => {
callback(targets.map((target) => ({ target, ...mockEntry?.() })));
});
};

window.addEventListener(event, listener);

return {
observe: (target: Element) => {
targets.push(target);
},
unobserve: (target: Element) => {
targets.splice(targets.indexOf(target), 1);
},
disconnect: () => {
window.removeEventListener(event, listener);
},
};
});
}

global.ResizeObserver = mockObserver("resize");

global.IntersectionObserver = mockObserver("intersect", () => ({ isIntersecting: window.isIntersecting }));
3 changes: 1 addition & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
"stripInternal": true,
"isolatedModules": true,
"allowSyntheticDefaultImports": true,
"forceConsistentCasingInFileNames": true,
"types": ["vitest/globals"]
"forceConsistentCasingInFileNames": true
}
}
1 change: 0 additions & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import cleanup from "rollup-plugin-cleanup";
export default defineConfig({
test: {
dir: "test",
globals: true,
environment: "jsdom",
setupFiles: "./test/vitest.setup.ts",
coverage: {
Expand Down

0 comments on commit b586d6c

Please sign in to comment.