|
1 | 1 | import * as useApi from '@/bcsc-theme/api/hooks/useApi'
|
2 | 2 | import * as useBCSCApiClient from '@/bcsc-theme/hooks/useBCSCApiClient'
|
3 |
| -import useDataLoader from '@/bcsc-theme/hooks/useDataLoader' |
| 3 | +import * as BcscCore from 'react-native-bcsc-core' |
| 4 | +import * as tokens from '@/bcsc-theme/utils/push-notification-tokens' |
4 | 5 | import { useQuickLoginURL } from '@/bcsc-theme/hooks/useQuickLoginUrl'
|
5 | 6 | import * as Bifold from '@bifold/core'
|
6 |
| -import { renderHook, waitFor } from '@testing-library/react-native' |
| 7 | +import { renderHook } from '@testing-library/react-native' |
7 | 8 |
|
8 | 9 | jest.mock('@bifold/core')
|
9 | 10 | jest.mock('@/bcsc-theme/api/hooks/useApi')
|
10 | 11 | jest.mock('@/bcsc-theme/hooks/useBCSCApiClient')
|
11 |
| -jest.mock('@/bcsc-theme/hooks/useDataLoader') |
| 12 | +jest.mock('@bcsc-theme/utils/push-notification-tokens') |
12 | 13 |
|
13 | 14 | describe('useQuickLoginURL', () => {
|
14 | 15 | beforeEach(() => {
|
15 | 16 | jest.resetAllMocks()
|
16 | 17 | })
|
17 | 18 |
|
18 |
| - it('should return no url or error when data loaders not ready', async () => { |
| 19 | + it('should return error when no initiate login uri', async () => { |
19 | 20 | const useApiMock = jest.mocked(useApi)
|
20 | 21 | const useClientMock = jest.mocked(useBCSCApiClient)
|
21 | 22 | const bifoldMock = jest.mocked(Bifold)
|
22 |
| - const useDataLoaderMock = jest.mocked(useDataLoader) |
23 | 23 |
|
24 | 24 | useApiMock.default.mockReturnValue({ jwks: { getFirstJwk: jest.fn() } } as any)
|
25 | 25 | useClientMock.useBCSCApiClient.mockReturnValue({} as any)
|
26 | 26 | bifoldMock.useServices.mockReturnValue([{ error: jest.fn() }] as any)
|
27 | 27 |
|
28 |
| - const jwkLoaderMock: any = { load: jest.fn(), isReady: false, data: 'A' } |
29 |
| - const tokensLoaderMock: any = { load: jest.fn(), isReady: true, data: 'B' } |
30 |
| - const accountLoaderMock: any = { load: jest.fn(), isReady: true, data: 'C' } |
| 28 | + const hook = renderHook(() => useQuickLoginURL()) |
| 29 | + const result = await hook.result.current({ client_ref_id: 'test' }) |
31 | 30 |
|
32 |
| - useDataLoaderMock |
33 |
| - .mockReturnValueOnce(jwkLoaderMock) |
34 |
| - .mockReturnValueOnce(tokensLoaderMock) |
35 |
| - .mockReturnValueOnce(accountLoaderMock) |
| 31 | + expect(result).toEqual({ success: false, error: expect.stringContaining('login unavailable') }) |
| 32 | + }) |
36 | 33 |
|
37 |
| - const hook = renderHook(() => useQuickLoginURL({ client_ref_id: '' })) |
| 34 | + it('should return error when no client access token', async () => { |
| 35 | + const useApiMock = jest.mocked(useApi) |
| 36 | + const useClientMock = jest.mocked(useBCSCApiClient) |
| 37 | + const bifoldMock = jest.mocked(Bifold) |
38 | 38 |
|
39 |
| - // Wait for useEffect to run |
40 |
| - await waitFor(() => { |
41 |
| - const [url, error] = hook.result.current |
| 39 | + useApiMock.default.mockReturnValue({ jwks: { getFirstJwk: jest.fn() } } as any) |
| 40 | + useClientMock.useBCSCApiClient.mockReturnValue({} as any) |
| 41 | + bifoldMock.useServices.mockReturnValue([{ error: jest.fn() }] as any) |
42 | 42 |
|
43 |
| - expect(url).toBeNull() |
44 |
| - expect(error).toBeNull() |
45 |
| - }) |
| 43 | + const hook = renderHook(() => useQuickLoginURL()) |
| 44 | + const result = await hook.result.current({ client_ref_id: 'test', initiate_login_uri: 'https://example.com' }) |
46 | 45 |
|
47 |
| - // Ensure load functions were called |
48 |
| - expect(jwkLoaderMock.load).toHaveBeenCalledTimes(1) |
49 |
| - expect(tokensLoaderMock.load).toHaveBeenCalledTimes(1) |
50 |
| - expect(accountLoaderMock.load).toHaveBeenCalledTimes(1) |
| 46 | + expect(result).toEqual({ success: false, error: expect.stringContaining('access token') }) |
51 | 47 | })
|
52 | 48 |
|
53 |
| - // TODO (MD): Add more tests (having difficulty with dataLoader mocks...) |
| 49 | + it('should return error when no notification tokens available', async () => { |
| 50 | + const useApiMock = jest.mocked(useApi) |
| 51 | + const useClientMock = jest.mocked(useBCSCApiClient) |
| 52 | + const bifoldMock = jest.mocked(Bifold) |
| 53 | + const bcscCoreMock = jest.mocked(BcscCore) |
| 54 | + const tokensMock = jest.mocked(tokens) |
| 55 | + |
| 56 | + const getFirstJwkMock = jest.fn() |
| 57 | + bcscCoreMock.getAccount = jest.fn() |
| 58 | + tokensMock.getNotificationTokens = jest.fn() |
| 59 | + |
| 60 | + useApiMock.default.mockReturnValue({ jwks: { getFirstJwk: getFirstJwkMock } } as any) |
| 61 | + useClientMock.useBCSCApiClient.mockReturnValue({ tokens: { access_token: true } } as any) |
| 62 | + bifoldMock.useServices.mockReturnValue([{ error: jest.fn() }] as any) |
| 63 | + |
| 64 | + const hook = renderHook(() => useQuickLoginURL()) |
| 65 | + const result = await hook.result.current({ client_ref_id: 'test', initiate_login_uri: 'https://example.com' }) |
| 66 | + |
| 67 | + expect(result).toEqual({ success: false, error: expect.stringContaining('notification tokens') }) |
| 68 | + |
| 69 | + expect(tokensMock.getNotificationTokens).toHaveBeenCalledTimes(1) |
| 70 | + expect(bcscCoreMock.getAccount).toHaveBeenCalledTimes(1) |
| 71 | + expect(getFirstJwkMock).toHaveBeenCalledTimes(1) |
| 72 | + }) |
| 73 | + |
| 74 | + it('should return error when no account available', async () => { |
| 75 | + const useApiMock = jest.mocked(useApi) |
| 76 | + const useClientMock = jest.mocked(useBCSCApiClient) |
| 77 | + const bifoldMock = jest.mocked(Bifold) |
| 78 | + const bcscCoreMock = jest.mocked(BcscCore) |
| 79 | + const tokensMock = jest.mocked(tokens) |
| 80 | + |
| 81 | + const getFirstJwkMock = jest.fn() |
| 82 | + bcscCoreMock.getAccount = jest.fn() |
| 83 | + tokensMock.getNotificationTokens = jest.fn().mockResolvedValue(true) |
| 84 | + |
| 85 | + useApiMock.default.mockReturnValue({ jwks: { getFirstJwk: getFirstJwkMock } } as any) |
| 86 | + useClientMock.useBCSCApiClient.mockReturnValue({ tokens: { access_token: true } } as any) |
| 87 | + bifoldMock.useServices.mockReturnValue([{ error: jest.fn() }] as any) |
| 88 | + |
| 89 | + const hook = renderHook(() => useQuickLoginURL()) |
| 90 | + const result = await hook.result.current({ client_ref_id: 'test', initiate_login_uri: 'https://example.com' }) |
| 91 | + |
| 92 | + expect(result).toEqual({ success: false, error: expect.stringContaining('account') }) |
| 93 | + |
| 94 | + expect(tokensMock.getNotificationTokens).toHaveBeenCalledTimes(1) |
| 95 | + expect(bcscCoreMock.getAccount).toHaveBeenCalledTimes(1) |
| 96 | + expect(getFirstJwkMock).toHaveBeenCalledTimes(1) |
| 97 | + }) |
| 98 | + |
| 99 | + it('should return error when no jwk available', async () => { |
| 100 | + const useApiMock = jest.mocked(useApi) |
| 101 | + const useClientMock = jest.mocked(useBCSCApiClient) |
| 102 | + const bifoldMock = jest.mocked(Bifold) |
| 103 | + const bcscCoreMock = jest.mocked(BcscCore) |
| 104 | + const tokensMock = jest.mocked(tokens) |
| 105 | + |
| 106 | + const getFirstJwkMock = jest.fn() |
| 107 | + bcscCoreMock.getAccount = jest.fn().mockResolvedValue(true) |
| 108 | + tokensMock.getNotificationTokens = jest.fn().mockResolvedValue(true) |
| 109 | + |
| 110 | + useApiMock.default.mockReturnValue({ jwks: { getFirstJwk: getFirstJwkMock } } as any) |
| 111 | + useClientMock.useBCSCApiClient.mockReturnValue({ tokens: { access_token: true } } as any) |
| 112 | + bifoldMock.useServices.mockReturnValue([{ error: jest.fn() }] as any) |
| 113 | + |
| 114 | + const hook = renderHook(() => useQuickLoginURL()) |
| 115 | + const result = await hook.result.current({ client_ref_id: 'test', initiate_login_uri: 'https://example.com' }) |
| 116 | + |
| 117 | + expect(result).toEqual({ success: false, error: expect.stringContaining('JWK') }) |
| 118 | + |
| 119 | + expect(tokensMock.getNotificationTokens).toHaveBeenCalledTimes(1) |
| 120 | + expect(bcscCoreMock.getAccount).toHaveBeenCalledTimes(1) |
| 121 | + expect(getFirstJwkMock).toHaveBeenCalledTimes(1) |
| 122 | + }) |
| 123 | + |
| 124 | + it('should return error when failed to create quick login JWT', async () => { |
| 125 | + const useApiMock = jest.mocked(useApi) |
| 126 | + const useClientMock = jest.mocked(useBCSCApiClient) |
| 127 | + const bifoldMock = jest.mocked(Bifold) |
| 128 | + const bcscCoreMock = jest.mocked(BcscCore) |
| 129 | + const tokensMock = jest.mocked(tokens) |
| 130 | + |
| 131 | + const getFirstJwkMock = jest.fn().mockResolvedValue(true) |
| 132 | + bcscCoreMock.getAccount = jest.fn().mockResolvedValue(true) |
| 133 | + tokensMock.getNotificationTokens = jest.fn().mockResolvedValue(true) |
| 134 | + bcscCoreMock.createQuickLoginJWT = jest.fn().mockRejectedValue(new Error('failed jwt')) |
| 135 | + |
| 136 | + useApiMock.default.mockReturnValue({ jwks: { getFirstJwk: getFirstJwkMock } } as any) |
| 137 | + useClientMock.useBCSCApiClient.mockReturnValue({ tokens: { access_token: true } } as any) |
| 138 | + bifoldMock.useServices.mockReturnValue([{ error: jest.fn() }] as any) |
| 139 | + |
| 140 | + const hook = renderHook(() => useQuickLoginURL()) |
| 141 | + const result = await hook.result.current({ client_ref_id: 'test', initiate_login_uri: 'https://example.com' }) |
| 142 | + |
| 143 | + expect(result).toEqual({ success: false, error: expect.stringContaining('failed jwt') }) |
| 144 | + |
| 145 | + expect(tokensMock.getNotificationTokens).toHaveBeenCalledTimes(1) |
| 146 | + expect(bcscCoreMock.getAccount).toHaveBeenCalledTimes(1) |
| 147 | + expect(getFirstJwkMock).toHaveBeenCalledTimes(1) |
| 148 | + expect(bcscCoreMock.createQuickLoginJWT).toHaveBeenCalledTimes(1) |
| 149 | + }) |
| 150 | + |
| 151 | + it('should return the quick login URL', async () => { |
| 152 | + const useApiMock = jest.mocked(useApi) |
| 153 | + const useClientMock = jest.mocked(useBCSCApiClient) |
| 154 | + const bifoldMock = jest.mocked(Bifold) |
| 155 | + const bcscCoreMock = jest.mocked(BcscCore) |
| 156 | + const tokensMock = jest.mocked(tokens) |
| 157 | + |
| 158 | + const getFirstJwkMock = jest.fn().mockResolvedValue('jwk') |
| 159 | + bcscCoreMock.getAccount = jest.fn().mockResolvedValue({ clientID: 'client-id', issuer: 'issuer' } as any) |
| 160 | + tokensMock.getNotificationTokens = jest.fn().mockResolvedValue({ fcmDeviceToken: 'fcm', apnsToken: 'apns' }) |
| 161 | + bcscCoreMock.createQuickLoginJWT = jest.fn().mockResolvedValue('test-jwt') |
| 162 | + |
| 163 | + useApiMock.default.mockReturnValue({ jwks: { getFirstJwk: getFirstJwkMock } } as any) |
| 164 | + useClientMock.useBCSCApiClient.mockReturnValue({ tokens: { access_token: 'access-token' } } as any) |
| 165 | + bifoldMock.useServices.mockReturnValue([{ error: jest.fn() }] as any) |
| 166 | + |
| 167 | + const hook = renderHook(() => useQuickLoginURL()) |
| 168 | + const result = await hook.result.current({ client_ref_id: 'test', initiate_login_uri: 'https://example.com' }) |
| 169 | + |
| 170 | + expect(result).toEqual({ success: true, url: `https://example.com?login_hint=${encodeURIComponent('test-jwt')}` }) |
| 171 | + |
| 172 | + expect(tokensMock.getNotificationTokens).toHaveBeenCalledTimes(1) |
| 173 | + expect(bcscCoreMock.getAccount).toHaveBeenCalledTimes(1) |
| 174 | + expect(getFirstJwkMock).toHaveBeenCalledTimes(1) |
| 175 | + expect(bcscCoreMock.createQuickLoginJWT).toHaveBeenCalledTimes(1) |
| 176 | + expect(bcscCoreMock.createQuickLoginJWT).toHaveBeenCalledWith( |
| 177 | + 'access-token', |
| 178 | + 'client-id', |
| 179 | + 'issuer', |
| 180 | + 'test', |
| 181 | + 'jwk', |
| 182 | + 'fcm', |
| 183 | + 'apns' |
| 184 | + ) |
| 185 | + }) |
54 | 186 | })
|
0 commit comments