Open History to see the last commit
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;
13}
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 payload bridge when the native review diff view is installed", async () => {
37 setExpoViewConfigAvailable();
38 expoMocks.requireNativeView.mockReturnValue(nativeView);
39 const { resolveNativeReviewDiffView } = await import("./nativeReviewDiffSurface");
40 const resolvedView = resolveNativeReviewDiffView();
41 expect(resolvedView).not.toBeNull();
42 expect(resolvedView).not.toBe(nativeView);
43 expect(resolveNativeReviewDiffView()).toBe(resolvedView);
44 expect(expoMocks.requireNativeView).toHaveBeenCalledWith("T3ReviewDiffSurface");
45 });
46
47 it("does not fall back to stale legacy native review diff view names", async () => {
48 globalThis.expo = {
49 getViewConfig: vi.fn().mockImplementation((moduleName: string) => {
50 if (moduleName === "T3ReviewDiffView") {
51 return { validAttributes: {}, directEventTypes: {} };
52 }
53 return null;
54 }),
55 } as unknown as typeof globalThis.expo;
56 expoMocks.requireNativeView.mockReturnValue(nativeView);
57 const { resolveNativeReviewDiffView } = await import("./nativeReviewDiffSurface");
58 expect(resolveNativeReviewDiffView()).toBeNull();
59 expect(expoMocks.requireNativeView).not.toHaveBeenCalled();
60 });
61
62 it("returns null when the view manager cannot be required", async () => {
63 setExpoViewConfigAvailable();
64 const cause = new Error("boom");
65 expoMocks.requireNativeView.mockImplementation(() => {
66 throw cause;
67 });
68 const consoleError = vi.spyOn(console, "error").mockImplementation(() => undefined);
69 const { resolveNativeReviewDiffView } = await import("./nativeReviewDiffSurface");
70
71 expect(resolveNativeReviewDiffView()).toBeNull();
72 expect(resolveNativeReviewDiffView()).toBeNull();
73 expect(expoMocks.requireNativeView).toHaveBeenCalledTimes(1);
74 expect(consoleError).toHaveBeenCalledWith(
75 expect.objectContaining({
76 _tag: "NativeViewResolutionError",
77 nativeModuleName: "T3ReviewDiffSurface",
78 cause,
79 }),
80 );
81 expect(consoleError).toHaveBeenCalledTimes(1);
82 });
83});
84
85describe("isPendingNativeViewRegistration", () => {
86 it("recognizes registration races for the installed native view name", async () => {
87 const { isPendingNativeViewRegistration } = await import("./nativeReviewDiffSurface");
88
89 expect(
90 isPendingNativeViewRegistration(
91 new Error("Unable to find the 'T3ReviewDiffSurface' view for this native tag"),
92 ),
93 ).toBe(true);
94 expect(
95 isPendingNativeViewRegistration(
96 new Error("Unable to find the 'T3ReviewDiffView' view for this native tag"),
97 ),
98 ).toBe(false);
99 expect(
100 isPendingNativeViewRegistration(
101 new Error(
102 "Unable to find the class expo.modules.t3reviewdiff.T3ReviewDiffView view with tag 1150",
103 ),
104 ),
105 ).toBe(true);
106 });
107});
108
109describe("isNativeReviewDiffDrawEvent", () => {
110 it("accepts only native events emitted after diff rows draw", async () => {
111 const { isNativeReviewDiffDrawEvent } = await import("./nativeReviewDiffSurface");
112
113 expect(isNativeReviewDiffDrawEvent({ message: "draw-metrics" })).toBe(true);
114 expect(isNativeReviewDiffDrawEvent({ message: "visible-range" })).toBe(true);
115 expect(isNativeReviewDiffDrawEvent({ message: "rows-decoded" })).toBe(false);
116 });
117});