|
| 1 | +import LogtoClient from '@logto/browser'; |
| 2 | +import { App, readonly } from 'vue'; |
| 3 | + |
| 4 | +import { useLogto, useHandleSignInCallback, createLogto } from '.'; |
| 5 | +import { contextInjectionKey, logtoInjectionKey } from './consts'; |
| 6 | +import { createContext } from './context'; |
| 7 | +import { createPluginMethods } from './plugin'; |
| 8 | + |
| 9 | +const isSignInRedirected = jest.fn(() => false); |
| 10 | +const handleSignInCallback = jest.fn(async () => Promise.resolve()); |
| 11 | +const getAccessToken = jest.fn(() => { |
| 12 | + throw new Error('not authenticated'); |
| 13 | +}); |
| 14 | +const injectMock = jest.fn<any, string[]>((): any => { |
| 15 | + return undefined; |
| 16 | +}); |
| 17 | + |
| 18 | +jest.mock('@logto/browser', () => { |
| 19 | + return jest.fn().mockImplementation(() => { |
| 20 | + return { |
| 21 | + isAuthenticated: false, |
| 22 | + isSignInRedirected, |
| 23 | + handleSignInCallback, |
| 24 | + getAccessToken, |
| 25 | + signIn: jest.fn(async () => Promise.resolve()), |
| 26 | + signOut: jest.fn(async () => Promise.resolve()), |
| 27 | + }; |
| 28 | + }); |
| 29 | +}); |
| 30 | + |
| 31 | +jest.mock('vue', () => { |
| 32 | + return { |
| 33 | + ...jest.requireActual('vue'), |
| 34 | + inject: (key: string) => { |
| 35 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-return |
| 36 | + return injectMock(key); |
| 37 | + }, |
| 38 | + }; |
| 39 | +}); |
| 40 | + |
| 41 | +const appId = 'foo'; |
| 42 | +const endpoint = 'https://endpoint.com'; |
| 43 | + |
| 44 | +const appMock = { |
| 45 | + provide: jest.fn(), |
| 46 | +} as any as App; |
| 47 | + |
| 48 | +describe('createLogto.install', () => { |
| 49 | + test('should call LogtoClient constructor and provide Logto context data', async () => { |
| 50 | + createLogto.install(appMock, { appId, endpoint }); |
| 51 | + |
| 52 | + expect(LogtoClient).toHaveBeenCalledWith({ endpoint, appId }); |
| 53 | + expect(appMock.provide).toBeCalled(); |
| 54 | + }); |
| 55 | +}); |
| 56 | + |
| 57 | +describe('Logto plugin not installed', () => { |
| 58 | + test('should throw error if calling `useLogto` before install', () => { |
| 59 | + expect(() => { |
| 60 | + useLogto(); |
| 61 | + }).toThrowError('Must install Logto plugin first.'); |
| 62 | + }); |
| 63 | + |
| 64 | + test('should throw error if calling `useHandleSignInCallback` before install', () => { |
| 65 | + expect(() => { |
| 66 | + useHandleSignInCallback(); |
| 67 | + }).toThrowError('Must install Logto plugin first.'); |
| 68 | + }); |
| 69 | +}); |
| 70 | + |
| 71 | +describe('useLogto', () => { |
| 72 | + beforeEach(() => { |
| 73 | + const client = new LogtoClient({ appId, endpoint }); |
| 74 | + const context = createContext(client); |
| 75 | + const { isAuthenticated, isLoading, error } = context; |
| 76 | + |
| 77 | + injectMock.mockImplementationOnce(() => { |
| 78 | + return { |
| 79 | + isAuthenticated: readonly(isAuthenticated), |
| 80 | + isLoading: readonly(isLoading), |
| 81 | + error: readonly(error), |
| 82 | + ...createPluginMethods(context), |
| 83 | + }; |
| 84 | + }); |
| 85 | + }); |
| 86 | + |
| 87 | + test('should inject Logto context data', () => { |
| 88 | + const { |
| 89 | + isAuthenticated, |
| 90 | + isLoading, |
| 91 | + error, |
| 92 | + signIn, |
| 93 | + signOut, |
| 94 | + getAccessToken, |
| 95 | + getIdTokenClaims, |
| 96 | + fetchUserInfo, |
| 97 | + } = useLogto(); |
| 98 | + |
| 99 | + expect(isAuthenticated.value).toBe(false); |
| 100 | + expect(isLoading.value).toBe(false); |
| 101 | + expect(error?.value).toBeUndefined(); |
| 102 | + expect(signIn).toBeInstanceOf(Function); |
| 103 | + expect(signOut).toBeInstanceOf(Function); |
| 104 | + expect(getAccessToken).toBeInstanceOf(Function); |
| 105 | + expect(getIdTokenClaims).toBeInstanceOf(Function); |
| 106 | + expect(fetchUserInfo).toBeInstanceOf(Function); |
| 107 | + }); |
| 108 | + |
| 109 | + test('should return error when getAccessToken fails', async () => { |
| 110 | + const client = new LogtoClient({ appId, endpoint }); |
| 111 | + const context = createContext(client); |
| 112 | + const { getAccessToken } = createPluginMethods(context); |
| 113 | + const { error } = context; |
| 114 | + |
| 115 | + await getAccessToken(); |
| 116 | + expect(error.value).not.toBeUndefined(); |
| 117 | + expect(error.value?.message).toBe('not authenticated'); |
| 118 | + }); |
| 119 | +}); |
| 120 | + |
| 121 | +describe('useHandleSignInCallback', () => { |
| 122 | + beforeEach(() => { |
| 123 | + const client = new LogtoClient({ appId, endpoint }); |
| 124 | + const context = createContext(client); |
| 125 | + |
| 126 | + injectMock.mockImplementation((key: string) => { |
| 127 | + if (key === contextInjectionKey) { |
| 128 | + return context; |
| 129 | + } |
| 130 | + |
| 131 | + if (key === logtoInjectionKey) { |
| 132 | + const { isAuthenticated, isLoading, error } = context; |
| 133 | + |
| 134 | + return { |
| 135 | + isAuthenticated: readonly(isAuthenticated), |
| 136 | + isLoading: readonly(isLoading), |
| 137 | + error: readonly(error), |
| 138 | + ...createPluginMethods(context), |
| 139 | + }; |
| 140 | + } |
| 141 | + }); |
| 142 | + }); |
| 143 | + |
| 144 | + test('not in callback url should not call `handleSignInCallback`', async () => { |
| 145 | + const { signIn } = useLogto(); |
| 146 | + useHandleSignInCallback(); |
| 147 | + |
| 148 | + await signIn('https://example.com'); |
| 149 | + expect(handleSignInCallback).not.toHaveBeenCalled(); |
| 150 | + }); |
| 151 | + |
| 152 | + test('in callback url should call `handleSignInCallback`', async () => { |
| 153 | + isSignInRedirected.mockImplementationOnce(() => true); |
| 154 | + const { signIn } = useLogto(); |
| 155 | + useHandleSignInCallback(); |
| 156 | + |
| 157 | + await signIn('https://example.com'); |
| 158 | + |
| 159 | + expect(handleSignInCallback).toHaveBeenCalledTimes(1); |
| 160 | + }); |
| 161 | +}); |
0 commit comments