|
| 1 | +import { beforeEach, describe, expect, test, vi } from 'vitest'; |
| 2 | + |
| 3 | +const mocks = vi.hoisted(() => ({ |
| 4 | + available: {} as Record<string, unknown>, |
| 5 | +})); |
| 6 | + |
| 7 | +// Only returns a module when one is registered under the exact name requested. |
| 8 | +vi.mock('expo', () => ({ |
| 9 | + requireNativeModule: (name: string) => { |
| 10 | + const nativeModule = mocks.available[name]; |
| 11 | + if (!nativeModule) { |
| 12 | + throw new Error(`Cannot find native module '${name}'`); |
| 13 | + } |
| 14 | + return nativeModule; |
| 15 | + }, |
| 16 | + requireOptionalNativeModule: (name: string) => mocks.available[name] ?? null, |
| 17 | +})); |
| 18 | + |
| 19 | +// Path is a variable because TypeScript cannot resolve the platform suffix in a |
| 20 | +// dynamic import. |
| 21 | +const importSpec = async (path: string) => (await import(path)).default; |
| 22 | + |
| 23 | +const importSpecs = async () => ({ |
| 24 | + clerkExpo: await importSpec('../NativeClerkModule.android'), |
| 25 | + googleSignIn: await importSpec('../NativeClerkGoogleSignIn.android'), |
| 26 | +}); |
| 27 | + |
| 28 | +describe('android native module specs', () => { |
| 29 | + beforeEach(() => { |
| 30 | + vi.resetModules(); |
| 31 | + mocks.available = {}; |
| 32 | + }); |
| 33 | + |
| 34 | + test('resolve to null instead of throwing when not compiled in (Expo Go)', async () => { |
| 35 | + const { clerkExpo, googleSignIn } = await importSpecs(); |
| 36 | + |
| 37 | + expect(clerkExpo).toBeNull(); |
| 38 | + expect(googleSignIn).toBeNull(); |
| 39 | + }); |
| 40 | + |
| 41 | + test('resolve the module registered under the expected name (development build)', async () => { |
| 42 | + const clerkExpoModule = { configure: vi.fn() }; |
| 43 | + const googleSignInModule = { signIn: vi.fn() }; |
| 44 | + mocks.available = { ClerkExpo: clerkExpoModule, ClerkGoogleSignIn: googleSignInModule }; |
| 45 | + |
| 46 | + const { clerkExpo, googleSignIn } = await importSpecs(); |
| 47 | + |
| 48 | + expect(clerkExpo).toBe(clerkExpoModule); |
| 49 | + expect(googleSignIn).toBe(googleSignInModule); |
| 50 | + }); |
| 51 | +}); |
0 commit comments