|
| 1 | +import { renderHook } from '@testing-library/react'; |
| 2 | +import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'; |
| 3 | + |
| 4 | +import { useSignInWithApple } from '../useSignInWithApple.ios'; |
| 5 | + |
| 6 | +const mocks = vi.hoisted(() => { |
| 7 | + return { |
| 8 | + useSignIn: vi.fn(), |
| 9 | + useSignUp: vi.fn(), |
| 10 | + signInAsync: vi.fn(), |
| 11 | + isAvailableAsync: vi.fn(), |
| 12 | + randomUUID: vi.fn(), |
| 13 | + }; |
| 14 | +}); |
| 15 | + |
| 16 | +vi.mock('@clerk/clerk-react', () => { |
| 17 | + return { |
| 18 | + useSignIn: mocks.useSignIn, |
| 19 | + useSignUp: mocks.useSignUp, |
| 20 | + }; |
| 21 | +}); |
| 22 | + |
| 23 | +vi.mock('expo-apple-authentication', () => { |
| 24 | + return { |
| 25 | + signInAsync: mocks.signInAsync, |
| 26 | + isAvailableAsync: mocks.isAvailableAsync, |
| 27 | + AppleAuthenticationScope: { |
| 28 | + FULL_NAME: 0, |
| 29 | + EMAIL: 1, |
| 30 | + }, |
| 31 | + }; |
| 32 | +}); |
| 33 | + |
| 34 | +vi.mock('expo-crypto', () => { |
| 35 | + return { |
| 36 | + default: { |
| 37 | + randomUUID: mocks.randomUUID, |
| 38 | + }, |
| 39 | + randomUUID: mocks.randomUUID, |
| 40 | + }; |
| 41 | +}); |
| 42 | + |
| 43 | +vi.mock('react-native', () => { |
| 44 | + return { |
| 45 | + Platform: { |
| 46 | + OS: 'ios', |
| 47 | + }, |
| 48 | + }; |
| 49 | +}); |
| 50 | + |
| 51 | +describe('useSignInWithApple', () => { |
| 52 | + const mockSignIn = { |
| 53 | + create: vi.fn(), |
| 54 | + createdSessionId: 'test-session-id', |
| 55 | + firstFactorVerification: { |
| 56 | + status: 'verified', |
| 57 | + }, |
| 58 | + }; |
| 59 | + |
| 60 | + const mockSignUp = { |
| 61 | + create: vi.fn(), |
| 62 | + createdSessionId: null, |
| 63 | + }; |
| 64 | + |
| 65 | + const mockSetActive = vi.fn(); |
| 66 | + |
| 67 | + beforeEach(() => { |
| 68 | + vi.clearAllMocks(); |
| 69 | + |
| 70 | + mocks.useSignIn.mockReturnValue({ |
| 71 | + signIn: mockSignIn, |
| 72 | + setActive: mockSetActive, |
| 73 | + isLoaded: true, |
| 74 | + }); |
| 75 | + |
| 76 | + mocks.useSignUp.mockReturnValue({ |
| 77 | + signUp: mockSignUp, |
| 78 | + isLoaded: true, |
| 79 | + }); |
| 80 | + |
| 81 | + mocks.isAvailableAsync.mockResolvedValue(true); |
| 82 | + mocks.randomUUID.mockReturnValue('test-nonce-uuid'); |
| 83 | + }); |
| 84 | + |
| 85 | + afterEach(() => { |
| 86 | + vi.restoreAllMocks(); |
| 87 | + }); |
| 88 | + |
| 89 | + describe('startAppleAuthenticationFlow', () => { |
| 90 | + test('should return the hook with startAppleAuthenticationFlow function', () => { |
| 91 | + const { result } = renderHook(() => useSignInWithApple()); |
| 92 | + |
| 93 | + expect(result.current).toHaveProperty('startAppleAuthenticationFlow'); |
| 94 | + expect(typeof result.current.startAppleAuthenticationFlow).toBe('function'); |
| 95 | + }); |
| 96 | + |
| 97 | + test('should successfully sign in existing user', async () => { |
| 98 | + const mockIdentityToken = 'mock-identity-token'; |
| 99 | + mocks.signInAsync.mockResolvedValue({ |
| 100 | + identityToken: mockIdentityToken, |
| 101 | + }); |
| 102 | + |
| 103 | + mockSignIn.create.mockResolvedValue(undefined); |
| 104 | + mockSignIn.firstFactorVerification.status = 'verified'; |
| 105 | + mockSignIn.createdSessionId = 'test-session-id'; |
| 106 | + |
| 107 | + const { result } = renderHook(() => useSignInWithApple()); |
| 108 | + |
| 109 | + const response = await result.current.startAppleAuthenticationFlow(); |
| 110 | + |
| 111 | + expect(mocks.isAvailableAsync).toHaveBeenCalled(); |
| 112 | + expect(mocks.randomUUID).toHaveBeenCalled(); |
| 113 | + expect(mocks.signInAsync).toHaveBeenCalledWith( |
| 114 | + expect.objectContaining({ |
| 115 | + requestedScopes: expect.any(Array), |
| 116 | + nonce: 'test-nonce-uuid', |
| 117 | + }), |
| 118 | + ); |
| 119 | + expect(mockSignIn.create).toHaveBeenCalledWith({ |
| 120 | + strategy: 'oauth_token_apple', |
| 121 | + token: mockIdentityToken, |
| 122 | + }); |
| 123 | + expect(response.createdSessionId).toBe('test-session-id'); |
| 124 | + expect(response.setActive).toBe(mockSetActive); |
| 125 | + }); |
| 126 | + |
| 127 | + test('should handle transfer flow for new user', async () => { |
| 128 | + const mockIdentityToken = 'mock-identity-token'; |
| 129 | + mocks.signInAsync.mockResolvedValue({ |
| 130 | + identityToken: mockIdentityToken, |
| 131 | + }); |
| 132 | + |
| 133 | + mockSignIn.create.mockResolvedValue(undefined); |
| 134 | + mockSignIn.firstFactorVerification.status = 'transferable'; |
| 135 | + |
| 136 | + const mockSignUpWithSession = { ...mockSignUp, createdSessionId: 'new-user-session-id' }; |
| 137 | + mocks.useSignUp.mockReturnValue({ |
| 138 | + signUp: mockSignUpWithSession, |
| 139 | + isLoaded: true, |
| 140 | + }); |
| 141 | + |
| 142 | + const { result } = renderHook(() => useSignInWithApple()); |
| 143 | + |
| 144 | + const response = await result.current.startAppleAuthenticationFlow({ |
| 145 | + unsafeMetadata: { source: 'test' }, |
| 146 | + }); |
| 147 | + |
| 148 | + expect(mockSignIn.create).toHaveBeenCalledWith({ |
| 149 | + strategy: 'oauth_token_apple', |
| 150 | + token: mockIdentityToken, |
| 151 | + }); |
| 152 | + expect(mockSignUp.create).toHaveBeenCalledWith({ |
| 153 | + transfer: true, |
| 154 | + unsafeMetadata: { source: 'test' }, |
| 155 | + }); |
| 156 | + expect(response.createdSessionId).toBe('new-user-session-id'); |
| 157 | + }); |
| 158 | + |
| 159 | + test('should handle user cancellation gracefully', async () => { |
| 160 | + const cancelError = Object.assign(new Error('User canceled'), { code: 'ERR_REQUEST_CANCELED' }); |
| 161 | + mocks.signInAsync.mockRejectedValue(cancelError); |
| 162 | + |
| 163 | + const { result } = renderHook(() => useSignInWithApple()); |
| 164 | + |
| 165 | + const response = await result.current.startAppleAuthenticationFlow(); |
| 166 | + |
| 167 | + expect(response.createdSessionId).toBe(null); |
| 168 | + expect(response.setActive).toBe(mockSetActive); |
| 169 | + }); |
| 170 | + |
| 171 | + test('should throw error when Apple Authentication is not available', async () => { |
| 172 | + mocks.isAvailableAsync.mockResolvedValue(false); |
| 173 | + |
| 174 | + const { result } = renderHook(() => useSignInWithApple()); |
| 175 | + |
| 176 | + await expect(result.current.startAppleAuthenticationFlow()).rejects.toThrow( |
| 177 | + 'Apple Authentication is not available on this device.', |
| 178 | + ); |
| 179 | + }); |
| 180 | + |
| 181 | + test('should throw error when no identity token received', async () => { |
| 182 | + mocks.signInAsync.mockResolvedValue({ |
| 183 | + identityToken: null, |
| 184 | + }); |
| 185 | + |
| 186 | + const { result } = renderHook(() => useSignInWithApple()); |
| 187 | + |
| 188 | + await expect(result.current.startAppleAuthenticationFlow()).rejects.toThrow( |
| 189 | + 'No identity token received from Apple Sign-In.', |
| 190 | + ); |
| 191 | + }); |
| 192 | + |
| 193 | + test('should return early when clerk is not loaded', async () => { |
| 194 | + mocks.useSignIn.mockReturnValue({ |
| 195 | + signIn: mockSignIn, |
| 196 | + setActive: mockSetActive, |
| 197 | + isLoaded: false, |
| 198 | + }); |
| 199 | + |
| 200 | + const { result } = renderHook(() => useSignInWithApple()); |
| 201 | + |
| 202 | + const response = await result.current.startAppleAuthenticationFlow(); |
| 203 | + |
| 204 | + expect(mocks.isAvailableAsync).not.toHaveBeenCalled(); |
| 205 | + expect(mocks.signInAsync).not.toHaveBeenCalled(); |
| 206 | + expect(response.createdSessionId).toBe(null); |
| 207 | + }); |
| 208 | + }); |
| 209 | +}); |
0 commit comments