| 1 | import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test"; |
| 2 | |
| 3 | const expoMocks = vi.hoisted(() => ({ |
| 4 | requireNativeView: vi.fn(), |
| 5 | })); |
| 6 | const nativeView = () => null; |
| 7 | const originalExpo = globalThis.expo; |
| 8 | |
| 9 | function setExpoViewConfigAvailable() { |
| 10 | globalThis.expo = { |
| 11 | getViewConfig: vi.fn().mockReturnValue({ validAttributes: {}, directEventTypes: {} }), |
| 12 | } as unknown as typeof globalThis.expo; |
| 13 | } |
| 14 | |
| 15 | vi.mock("expo", () => ({ |
| 16 | requireNativeView: expoMocks.requireNativeView, |
| 17 | })); |
| 18 | |
| 19 | describe("resolveNativeTerminalSurfaceView", () => { |
| 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 terminal view config is unavailable", async () => { |
| 31 | const { resolveNativeTerminalSurfaceView } = await import("./nativeTerminalModule"); |
| 32 | expect(resolveNativeTerminalSurfaceView()).toBeNull(); |
| 33 | expect(expoMocks.requireNativeView).not.toHaveBeenCalled(); |
| 34 | }); |
| 35 | |
| 36 | it("returns the native terminal view when the view config is installed", async () => { |
| 37 | setExpoViewConfigAvailable(); |
| 38 | expoMocks.requireNativeView.mockReturnValue(nativeView); |
| 39 | const { resolveNativeTerminalSurfaceView } = await import("./nativeTerminalModule"); |
| 40 | expect(resolveNativeTerminalSurfaceView()).toBe(nativeView); |
| 41 | expect(expoMocks.requireNativeView).toHaveBeenCalledWith("T3TerminalSurface"); |
| 42 | }); |
| 43 | |
| 44 | it("returns null when the view manager cannot be required", async () => { |
| 45 | setExpoViewConfigAvailable(); |
| 46 | const cause = new Error("boom"); |
| 47 | expoMocks.requireNativeView.mockImplementation(() => { |
| 48 | throw cause; |
| 49 | }); |
| 50 | const consoleError = vi.spyOn(console, "error").mockImplementation(() => undefined); |
| 51 | const { resolveNativeTerminalSurfaceView } = await import("./nativeTerminalModule"); |
| 52 | |
| 53 | expect(resolveNativeTerminalSurfaceView()).toBeNull(); |
| 54 | expect(resolveNativeTerminalSurfaceView()).toBeNull(); |
| 55 | expect(expoMocks.requireNativeView).toHaveBeenCalledTimes(1); |
| 56 | expect(consoleError).toHaveBeenCalledWith( |
| 57 | expect.objectContaining({ |
| 58 | _tag: "NativeViewResolutionError", |
| 59 | nativeModuleName: "T3TerminalSurface", |
| 60 | cause, |
| 61 | }), |
| 62 | ); |
| 63 | expect(consoleError).toHaveBeenCalledTimes(1); |
| 64 | }); |
| 65 | }); |