Skip to content

Commit fafcea2

Browse files
ahmedhamouda78Ahmed Hamouda
andauthored
chore(rtn-web-browser): add initial unit tests (#14524)
Co-authored-by: Ahmed Hamouda <[email protected]>
1 parent 4663a61 commit fafcea2

File tree

3 files changed

+83
-5
lines changed

3 files changed

+83
-5
lines changed

packages/rtn-web-browser/jest.config.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ module.exports = {
22
...require('../../jest.config'),
33
coverageThreshold: {
44
global: {
5-
branches: 100,
6-
functions: 100,
7-
lines: 100,
8-
statements: 100,
5+
branches: 25,
6+
functions: 25,
7+
lines: 35,
8+
statements: 35,
99
},
1010
},
1111
};

packages/rtn-web-browser/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"access": "public"
1212
},
1313
"scripts": {
14-
"test": "echo 'no-op'",
14+
"test": "jest -w 1 --coverage --logHeapUsage",
1515
"test:android": "./android/gradlew test -p ./android",
1616
"build-with-test": "npm run clean && npm test && tsc",
1717
"build:esm-cjs": "rollup --forceExit -c rollup.config.mjs",
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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

Comments
 (0)