|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +import { Platform } from 'react-native'; |
| 5 | + |
| 6 | +import { nativeModule } from '../../nativeModule'; |
| 7 | +import { isChromebook, openAuthSessionAsync } from '../openAuthSessionAsync'; |
| 8 | + |
| 9 | +jest.mock('react-native', () => ({ |
| 10 | + Platform: { OS: 'ios' }, |
| 11 | + AppState: { addEventListener: jest.fn() }, |
| 12 | + Linking: { addEventListener: jest.fn() }, |
| 13 | + NativeModules: {}, |
| 14 | +})); |
| 15 | + |
| 16 | +jest.mock('../../nativeModule', () => ({ |
| 17 | + nativeModule: { openAuthSessionAsync: jest.fn() }, |
| 18 | +})); |
| 19 | + |
| 20 | +const mockPlatform = Platform as jest.Mocked<typeof Platform>; |
| 21 | +const mockNativeModule = nativeModule as jest.Mocked<typeof nativeModule>; |
| 22 | + |
| 23 | +describe('openAuthSessionAsync', () => { |
| 24 | + beforeEach(() => { |
| 25 | + jest.clearAllMocks(); |
| 26 | + mockPlatform.OS = 'ios'; |
| 27 | + }); |
| 28 | + |
| 29 | + describe('isChromebook', () => { |
| 30 | + it('returns false by default', async () => { |
| 31 | + const result = await isChromebook(); |
| 32 | + expect(result).toBe(false); |
| 33 | + }); |
| 34 | + }); |
| 35 | + |
| 36 | + describe('openAuthSessionAsync', () => { |
| 37 | + it('enforces HTTPS on URLs', async () => { |
| 38 | + mockNativeModule.openAuthSessionAsync.mockResolvedValue('result'); |
| 39 | + |
| 40 | + await openAuthSessionAsync('http://example.com', ['myapp://callback']); |
| 41 | + |
| 42 | + expect(mockNativeModule.openAuthSessionAsync).toHaveBeenCalledWith( |
| 43 | + 'https://example.com', |
| 44 | + 'myapp://callback', |
| 45 | + false, |
| 46 | + ); |
| 47 | + }); |
| 48 | + |
| 49 | + it('calls iOS implementation', async () => { |
| 50 | + mockNativeModule.openAuthSessionAsync.mockResolvedValue('result'); |
| 51 | + |
| 52 | + const result = await openAuthSessionAsync( |
| 53 | + 'https://example.com', |
| 54 | + ['myapp://callback'], |
| 55 | + true, |
| 56 | + ); |
| 57 | + |
| 58 | + expect(result).toBe('result'); |
| 59 | + expect(mockNativeModule.openAuthSessionAsync).toHaveBeenCalledWith( |
| 60 | + 'https://example.com', |
| 61 | + 'myapp://callback', |
| 62 | + true, |
| 63 | + ); |
| 64 | + }); |
| 65 | + |
| 66 | + it('finds first non-web URL for redirect', async () => { |
| 67 | + const redirectUrls = ['https://web.com', 'myapp://deep']; |
| 68 | + |
| 69 | + await openAuthSessionAsync('https://example.com', redirectUrls); |
| 70 | + |
| 71 | + expect(mockNativeModule.openAuthSessionAsync).toHaveBeenCalledWith( |
| 72 | + 'https://example.com', |
| 73 | + 'myapp://deep', |
| 74 | + false, |
| 75 | + ); |
| 76 | + }); |
| 77 | + }); |
| 78 | +}); |
0 commit comments