|
1 | 1 | import CredentialsManager from '../index'; |
2 | 2 | import CredentialsManagerError from '../credentialsManagerError'; |
3 | | -import { Platform } from 'react-native'; |
| 3 | +import A0Auth0 from '../../specs/NativeA0Auth0'; |
| 4 | + |
| 5 | +// Mock the native module |
| 6 | +jest.mock('../../specs/NativeA0Auth0'); |
4 | 7 |
|
5 | 8 | describe('credentials manager tests', () => { |
6 | 9 | const credentialsManager = new CredentialsManager( |
7 | 10 | 'https://auth0.com', |
8 | 11 | 'abc123' |
9 | 12 | ); |
10 | 13 |
|
11 | | - credentialsManager.Auth0Module.hasValidAuth0InstanceWithConfiguration = () => |
12 | | - Promise.resolve(true); |
13 | | - credentialsManager.Auth0Module.saveCredentials = () => {}; |
14 | | - credentialsManager.Auth0Module.getCredentials = () => {}; |
15 | | - credentialsManager.Auth0Module.hasValidCredentials = () => {}; |
16 | | - credentialsManager.Auth0Module.clearCredentials = () => {}; |
17 | | - credentialsManager.Auth0Module.enableLocalAuthentication = () => {}; |
18 | | - |
19 | 14 | const validToken = { |
20 | 15 | idToken: '1234', |
21 | 16 | accessToken: '1234', |
22 | 17 | tokenType: 'Bearer', |
23 | 18 | expiresAt: 1691603391, |
24 | 19 | }; |
25 | 20 |
|
| 21 | + beforeEach(() => { |
| 22 | + // Reset all mocks before each test |
| 23 | + jest.clearAllMocks(); |
| 24 | + |
| 25 | + // Set default implementation for native module functions |
| 26 | + A0Auth0.hasValidAuth0InstanceWithConfiguration = jest.fn(() => |
| 27 | + Promise.resolve(true) |
| 28 | + ); |
| 29 | + A0Auth0.saveCredentials = jest.fn(() => Promise.resolve(true)); |
| 30 | + A0Auth0.getCredentials = jest.fn(() => Promise.resolve()); |
| 31 | + A0Auth0.hasValidCredentials = jest.fn(() => Promise.resolve(false)); |
| 32 | + A0Auth0.clearCredentials = jest.fn(() => Promise.resolve(true)); |
| 33 | + }); |
| 34 | + |
26 | 35 | describe('test saving credentials', () => { |
27 | 36 | it('throws when access token is empty', async () => { |
28 | 37 | const testToken = Object.assign({}, validToken); |
@@ -65,108 +74,75 @@ describe('credentials manager tests', () => { |
65 | 74 | }); |
66 | 75 |
|
67 | 76 | it('proper error is thrown for exception', async () => { |
68 | | - const newNativeModule = jest |
69 | | - .spyOn( |
70 | | - credentialsManager.Auth0Module, |
71 | | - 'hasValidAuth0InstanceWithConfiguration' |
72 | | - ) |
73 | | - .mockImplementation(() => { |
74 | | - throw Error('123123'); |
75 | | - }); |
| 77 | + A0Auth0.hasValidAuth0InstanceWithConfiguration = jest.fn(() => { |
| 78 | + throw Error('123123'); |
| 79 | + }); |
76 | 80 | await expect( |
77 | 81 | credentialsManager.saveCredentials(validToken) |
78 | 82 | ).rejects.toThrow(); |
79 | | - newNativeModule.mockRestore(); |
80 | 83 | }); |
81 | 84 |
|
82 | 85 | it('succeeds for proper token', async () => { |
83 | | - const newNativeModule = jest |
84 | | - .spyOn(credentialsManager.Auth0Module, 'saveCredentials') |
85 | | - .mockImplementation(() => Promise.resolve(true)); |
| 86 | + A0Auth0.saveCredentials = jest.fn(() => Promise.resolve(true)); |
86 | 87 | await expect( |
87 | 88 | credentialsManager.saveCredentials(validToken) |
88 | 89 | ).resolves.toEqual(true); |
89 | | - newNativeModule.mockRestore(); |
90 | 90 | }); |
91 | 91 | }); |
92 | 92 |
|
93 | 93 | describe('test getting credentials', () => { |
94 | 94 | it('proper error is thrown for exception', async () => { |
95 | | - const newNativeModule = jest |
96 | | - .spyOn(credentialsManager.Auth0Module, 'getCredentials') |
97 | | - .mockImplementation(() => { |
98 | | - throw Error('123123'); |
99 | | - }); |
| 95 | + A0Auth0.getCredentials = jest.fn(() => { |
| 96 | + throw Error('123123'); |
| 97 | + }); |
100 | 98 | await expect(credentialsManager.getCredentials()).rejects.toThrow(); |
101 | | - newNativeModule.mockRestore(); |
102 | 99 | }); |
103 | 100 |
|
104 | 101 | it('succeedsfully returns credentials', async () => { |
105 | | - const newNativeModule = jest |
106 | | - .spyOn(credentialsManager.Auth0Module, 'getCredentials') |
107 | | - .mockImplementation(() => Promise.resolve(validToken)); |
| 102 | + A0Auth0.getCredentials = jest.fn(() => Promise.resolve(validToken)); |
108 | 103 | await expect(credentialsManager.getCredentials()).resolves.toEqual( |
109 | 104 | validToken |
110 | 105 | ); |
111 | | - newNativeModule.mockRestore(); |
112 | 106 | }); |
113 | 107 |
|
114 | 108 | it('passes along the forceRefresh parameter', async () => { |
115 | | - const newNativeModule = jest |
116 | | - .spyOn(credentialsManager.Auth0Module, 'getCredentials') |
117 | | - .mockImplementation(() => Promise.resolve(validToken)); |
| 109 | + A0Auth0.getCredentials = jest.fn(() => Promise.resolve(validToken)); |
118 | 110 |
|
119 | 111 | await credentialsManager.getCredentials(null, 0, {}, true); |
120 | 112 |
|
121 | | - expect( |
122 | | - credentialsManager.Auth0Module.getCredentials |
123 | | - ).toHaveBeenCalledWith(null, 0, {}, true); |
124 | | - |
125 | | - newNativeModule.mockRestore(); |
| 113 | + expect(A0Auth0.getCredentials).toHaveBeenCalledWith(null, 0, {}, true); |
126 | 114 | }); |
127 | 115 | }); |
128 | 116 |
|
129 | 117 | describe('test hasValidCredentials', () => { |
130 | 118 | it('returns false', async () => { |
131 | | - const newNativeModule = jest |
132 | | - .spyOn(credentialsManager.Auth0Module, 'hasValidCredentials') |
133 | | - .mockImplementation(() => Promise.resolve(true)); |
| 119 | + A0Auth0.hasValidCredentials = jest.fn(() => Promise.resolve(true)); |
134 | 120 | await expect(credentialsManager.hasValidCredentials()).resolves.toEqual( |
135 | 121 | true |
136 | 122 | ); |
137 | | - newNativeModule.mockRestore(); |
138 | 123 | }); |
139 | 124 |
|
140 | 125 | it('returns true', async () => { |
141 | | - const newNativeModule = jest |
142 | | - .spyOn(credentialsManager.Auth0Module, 'hasValidCredentials') |
143 | | - .mockImplementation(() => Promise.resolve(true)); |
| 126 | + A0Auth0.hasValidCredentials = jest.fn(() => Promise.resolve(true)); |
144 | 127 | await expect(credentialsManager.hasValidCredentials()).resolves.toEqual( |
145 | 128 | true |
146 | 129 | ); |
147 | | - newNativeModule.mockRestore(); |
148 | 130 | }); |
149 | 131 | }); |
150 | 132 |
|
151 | 133 | describe('test clearing credentials', () => { |
152 | 134 | it('returns false', async () => { |
153 | | - const newNativeModule = jest |
154 | | - .spyOn(credentialsManager.Auth0Module, 'clearCredentials') |
155 | | - .mockImplementation(() => Promise.resolve(true)); |
| 135 | + A0Auth0.clearCredentials = jest.fn(() => Promise.resolve(true)); |
156 | 136 | await expect(credentialsManager.clearCredentials()).resolves.toEqual( |
157 | 137 | true |
158 | 138 | ); |
159 | | - newNativeModule.mockRestore(); |
160 | 139 | }); |
161 | 140 |
|
162 | 141 | it('returns true', async () => { |
163 | | - const newNativeModule = jest |
164 | | - .spyOn(credentialsManager.Auth0Module, 'clearCredentials') |
165 | | - .mockImplementation(() => Promise.resolve(true)); |
| 142 | + A0Auth0.clearCredentials = jest.fn(() => Promise.resolve(true)); |
166 | 143 | await expect(credentialsManager.clearCredentials()).resolves.toEqual( |
167 | 144 | true |
168 | 145 | ); |
169 | | - newNativeModule.mockRestore(); |
170 | 146 | }); |
171 | 147 | }); |
172 | 148 |
|
|
0 commit comments