Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

nativeReviewDiffSurface.test.ts

80 lines2.9 KB

1import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
2​
3const expoMocks = vi.hoisted(() => ({
4 requireNativeView: vi.fn(),
5}));
6const nativeView = () => null;
7const originalExpo = globalThis.expo;
8​
9function setExpoViewConfigAvailable() {
10 globalThis.expo = {
11 getViewConfig: vi.fn().mockReturnValue({ validAttributes: {}, directEventTypes: {} }),
12 } as unknown as typeof globalThis.expo;
14​
15vi.mock("expo", () => ({
16 requireNativeView: expoMocks.requireNativeView,
17}));
18​
19describe("resolveNativeReviewDiffView", () => {
20 beforeEach(() => {
21 vi.clearAllMocks();
22 vi.resetModules();
23 globalThis.expo = undefined as unknown as typeof globalThis.expo;
24 });
25​
26 afterEach(() => {
27 globalThis.expo = originalExpo;
28 });
29​
30 it("returns null when the native review diff view config is unavailable", async () => {
31 const { resolveNativeReviewDiffView } = await import("./nativeReviewDiffSurface");
32 expect(resolveNativeReviewDiffView()).toBeNull();
33 expect(expoMocks.requireNativeView).not.toHaveBeenCalled();
34 });
35​
36 it("returns the native review diff view when the view config is installed", async () => {
37 setExpoViewConfigAvailable();
38 expoMocks.requireNativeView.mockReturnValue(nativeView);
39 const { resolveNativeReviewDiffView } = await import("./nativeReviewDiffSurface");
40 expect(resolveNativeReviewDiffView()).toBe(nativeView);
41 expect(expoMocks.requireNativeView).toHaveBeenCalledWith("T3ReviewDiffSurface");
42 });
43​
44 it("does not fall back to stale legacy native review diff view names", async () => {
45 globalThis.expo = {
46 getViewConfig: vi.fn().mockImplementation((moduleName: string) => {
47 if (moduleName === "T3ReviewDiffView") {
48 return { validAttributes: {}, directEventTypes: {} };
49 }
50 return null;
51 }),
52 } as unknown as typeof globalThis.expo;
53 expoMocks.requireNativeView.mockReturnValue(nativeView);
54 const { resolveNativeReviewDiffView } = await import("./nativeReviewDiffSurface");
55 expect(resolveNativeReviewDiffView()).toBeNull();
56 expect(expoMocks.requireNativeView).not.toHaveBeenCalled();
57 });
58​
59 it("returns null when the view manager cannot be required", async () => {
60 setExpoViewConfigAvailable();
61 const cause = new Error("boom");
62 expoMocks.requireNativeView.mockImplementation(() => {
63 throw cause;
64 });
65 const consoleError = vi.spyOn(console, "error").mockImplementation(() => undefined);
66 const { resolveNativeReviewDiffView } = await import("./nativeReviewDiffSurface");
67​
68 expect(resolveNativeReviewDiffView()).toBeNull();
69 expect(resolveNativeReviewDiffView()).toBeNull();
70 expect(expoMocks.requireNativeView).toHaveBeenCalledTimes(1);
71 expect(consoleError).toHaveBeenCalledWith(
72 expect.objectContaining({
73 _tag: "NativeViewResolutionError",
74 nativeModuleName: "T3ReviewDiffSurface",
75 cause,
76 }),
77 );
78 expect(consoleError).toHaveBeenCalledTimes(1);
79 });
80});