File size: 1,953 Bytes
689359a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import "@testing-library/jest-dom/vitest";
import type { ComponentType, ReactNode } from "react";
import { afterAll, afterEach, beforeAll, vi } from "vitest";
import { cleanup } from "@testing-library/react";
import { setupServer } from "msw/node";

vi.mock("@testing-library/react", async () => {
  const { readFileSync, readdirSync } = await import("node:fs");
  const { join } = await import("node:path");
  const actual = await vi.importActual<typeof import("@testing-library/react")>("@testing-library/react");
  const { createElement } = await import("react");
  const { NextIntlClientProvider } = await import("next-intl");
  const dir = join(process.cwd(), "messages/en");
  const messages: Record<string, unknown> = {};
  for (const file of readdirSync(dir)) {
    if (!file.endsWith(".json")) continue;
    messages[file.replace(/\.json$/, "")] = JSON.parse(readFileSync(join(dir, file), "utf8"));
  }

  function withIntl(options?: { wrapper?: ComponentType<{ children: ReactNode }> }) {
    const UserWrapper = options?.wrapper;
    return function IntlWrapper({ children }: { children: ReactNode }) {
      const inner = UserWrapper ? createElement(UserWrapper, null, children) : children;
      return createElement(NextIntlClientProvider, { locale: "en", messages, children: inner });
    };
  }

  return {
    ...actual,
    render: ((ui: Parameters<typeof actual.render>[0], options?: Parameters<typeof actual.render>[1]) =>
      actual.render(ui, { ...options, wrapper: withIntl(options) })) as typeof actual.render,
    renderHook: ((callback: never, options?: { wrapper?: ComponentType<{ children: ReactNode }> }) =>
      actual.renderHook(callback, { ...options, wrapper: withIntl(options) })) as typeof actual.renderHook,
  };
});

export const server = setupServer();

beforeAll(() => server.listen({ onUnhandledRequest: "error" }));
afterEach(() => {
  cleanup();
  server.resetHandlers();
});
afterAll(() => server.close());