diff --git a/packages/rtn-web-browser/jest.config.js b/packages/rtn-web-browser/jest.config.js index 3a0e3340f9f..fd22133a060 100644 --- a/packages/rtn-web-browser/jest.config.js +++ b/packages/rtn-web-browser/jest.config.js @@ -2,10 +2,10 @@ module.exports = { ...require('../../jest.config'), coverageThreshold: { global: { - branches: 100, - functions: 100, - lines: 100, - statements: 100, + branches: 25, + functions: 25, + lines: 35, + statements: 35, }, }, }; diff --git a/packages/rtn-web-browser/package.json b/packages/rtn-web-browser/package.json index 3a9d7917bd2..929a8988a41 100644 --- a/packages/rtn-web-browser/package.json +++ b/packages/rtn-web-browser/package.json @@ -11,7 +11,7 @@ "access": "public" }, "scripts": { - "test": "echo 'no-op'", + "test": "jest -w 1 --coverage --logHeapUsage", "test:android": "./android/gradlew test -p ./android", "build-with-test": "npm run clean && npm test && tsc", "build:esm-cjs": "rollup --forceExit -c rollup.config.mjs", diff --git a/packages/rtn-web-browser/src/apis/__tests__/openAuthSessionAsync.test.ts b/packages/rtn-web-browser/src/apis/__tests__/openAuthSessionAsync.test.ts new file mode 100644 index 00000000000..afbb034ff6e --- /dev/null +++ b/packages/rtn-web-browser/src/apis/__tests__/openAuthSessionAsync.test.ts @@ -0,0 +1,78 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 + +import { Platform } from 'react-native'; + +import { nativeModule } from '../../nativeModule'; +import { isChromebook, openAuthSessionAsync } from '../openAuthSessionAsync'; + +jest.mock('react-native', () => ({ + Platform: { OS: 'ios' }, + AppState: { addEventListener: jest.fn() }, + Linking: { addEventListener: jest.fn() }, + NativeModules: {}, +})); + +jest.mock('../../nativeModule', () => ({ + nativeModule: { openAuthSessionAsync: jest.fn() }, +})); + +const mockPlatform = Platform as jest.Mocked; +const mockNativeModule = nativeModule as jest.Mocked; + +describe('openAuthSessionAsync', () => { + beforeEach(() => { + jest.clearAllMocks(); + mockPlatform.OS = 'ios'; + }); + + describe('isChromebook', () => { + it('returns false by default', async () => { + const result = await isChromebook(); + expect(result).toBe(false); + }); + }); + + describe('openAuthSessionAsync', () => { + it('enforces HTTPS on URLs', async () => { + mockNativeModule.openAuthSessionAsync.mockResolvedValue('result'); + + await openAuthSessionAsync('http://example.com', ['myapp://callback']); + + expect(mockNativeModule.openAuthSessionAsync).toHaveBeenCalledWith( + 'https://example.com', + 'myapp://callback', + false, + ); + }); + + it('calls iOS implementation', async () => { + mockNativeModule.openAuthSessionAsync.mockResolvedValue('result'); + + const result = await openAuthSessionAsync( + 'https://example.com', + ['myapp://callback'], + true, + ); + + expect(result).toBe('result'); + expect(mockNativeModule.openAuthSessionAsync).toHaveBeenCalledWith( + 'https://example.com', + 'myapp://callback', + true, + ); + }); + + it('finds first non-web URL for redirect', async () => { + const redirectUrls = ['https://web.com', 'myapp://deep']; + + await openAuthSessionAsync('https://example.com', redirectUrls); + + expect(mockNativeModule.openAuthSessionAsync).toHaveBeenCalledWith( + 'https://example.com', + 'myapp://deep', + false, + ); + }); + }); +});