|
| 1 | +import { mocked } from 'ts-jest/utils'; |
| 2 | + |
| 3 | +import authorize from '../api/authorize'; |
| 4 | +import onboard from '../api/onboard'; |
| 5 | +import logout from '../api/logout'; |
| 6 | +import openService from '../api/open-service'; |
| 7 | +import requestMagicLink from '../api/request-magic-link'; |
| 8 | +import exchangeToken from '../api/exchange-token'; |
| 9 | +import tokenInfo from '../api/token-info'; |
| 10 | +import mtLinkSdk, { MtLinkSdk } from '..'; |
| 11 | + |
| 12 | +jest.mock('../api/authorize'); |
| 13 | +jest.mock('../api/onboard'); |
| 14 | +jest.mock('../api/logout'); |
| 15 | +jest.mock('../api/open-service'); |
| 16 | +jest.mock('../api/request-magic-link'); |
| 17 | +jest.mock('../api/exchange-token'); |
| 18 | +jest.mock('../api/token-info'); |
| 19 | + |
| 20 | +describe('index', () => { |
| 21 | + test('MtLinkSdk', async () => { |
| 22 | + const instance = new MtLinkSdk(); |
| 23 | + |
| 24 | + instance.init('clientId', { |
| 25 | + redirectUri: 'redirectUri' |
| 26 | + }); |
| 27 | + |
| 28 | + const options = instance.storedOptions; |
| 29 | + const storedOptions = { |
| 30 | + clientId: options.clientId, |
| 31 | + codeVerifier: options.codeVerifier, |
| 32 | + mode: options.mode, |
| 33 | + redirectUri: options.redirectUri, |
| 34 | + state: options.state |
| 35 | + }; |
| 36 | + |
| 37 | + const result1 = instance.authorize({ scopes: 'scopes' }); |
| 38 | + expect(result1).toBeUndefined(); |
| 39 | + expect(authorize).toBeCalledWith(storedOptions, { scopes: 'scopes' }); |
| 40 | + |
| 41 | + const result2 = instance.onboard({ scopes: 'scopes' }); |
| 42 | + expect(result2).toBeUndefined(); |
| 43 | + expect(onboard).toBeCalledWith(storedOptions, { scopes: 'scopes' }); |
| 44 | + |
| 45 | + const result3 = instance.logout({ backTo: 'backTo' }); |
| 46 | + expect(result3).toBeUndefined(); |
| 47 | + expect(logout).toBeCalledWith(storedOptions, { backTo: 'backTo' }); |
| 48 | + |
| 49 | + const result4 = instance.openService('test'); |
| 50 | + expect(result4).toBeUndefined(); |
| 51 | + expect(openService).toBeCalledWith(storedOptions, 'test', undefined); |
| 52 | + |
| 53 | + const result5 = await instance.requestMagicLink({ magicLinkTo: 'magicLinkTo' }); |
| 54 | + expect(result5).toBeUndefined(); |
| 55 | + expect(requestMagicLink).toBeCalledWith(storedOptions, { magicLinkTo: 'magicLinkTo' }); |
| 56 | + |
| 57 | + mocked(exchangeToken).mockResolvedValueOnce('test'); |
| 58 | + const result6 = await instance.exchangeToken({ code: 'code' }); |
| 59 | + expect(result6).toBe('test'); |
| 60 | + expect(exchangeToken).toBeCalledWith(storedOptions, { code: 'code' }); |
| 61 | + |
| 62 | + // @ts-ignore |
| 63 | + mocked(tokenInfo).mockResolvedValueOnce('test'); |
| 64 | + const result7 = await instance.tokenInfo('test'); |
| 65 | + expect(result7).toBe('test'); |
| 66 | + expect(tokenInfo).toBeCalledWith(storedOptions, 'test', undefined); |
| 67 | + }); |
| 68 | + |
| 69 | + test('mtLinkSdk', () => { |
| 70 | + const instance = new MtLinkSdk(); |
| 71 | + |
| 72 | + expect(instance).not.toBe(mtLinkSdk); |
| 73 | + }); |
| 74 | + |
| 75 | + test('init without clientId', () => { |
| 76 | + expect(() => { |
| 77 | + mtLinkSdk.init(''); |
| 78 | + }).toThrow('[mt-link-sdk] Missing parameter `client_id` in `init`.'); |
| 79 | + }); |
| 80 | + |
| 81 | + test('init options', () => { |
| 82 | + mtLinkSdk.init('clientId', { |
| 83 | + mode: 'local', |
| 84 | + state: 'state', |
| 85 | + codeVerifier: 'codeVerifier' |
| 86 | + }); |
| 87 | + |
| 88 | + expect(mtLinkSdk.storedOptions.mode).toBe('local'); |
| 89 | + expect(mtLinkSdk.storedOptions.state).toBe('state'); |
| 90 | + expect(mtLinkSdk.storedOptions.codeVerifier).toBe('codeVerifier'); |
| 91 | + }); |
| 92 | + |
| 93 | + test('invalid mode default to production', () => { |
| 94 | + mtLinkSdk.init('clientId', { |
| 95 | + // @ts-ignore |
| 96 | + mode: 'invalid' |
| 97 | + }); |
| 98 | + |
| 99 | + expect(mtLinkSdk.storedOptions.mode).toBe('production'); |
| 100 | + }); |
| 101 | +}); |
0 commit comments