|
| 1 | +import createSdk, { |
| 2 | + refresh, |
| 3 | + getJwtPermissions, |
| 4 | + getJwtRoles, |
| 5 | + getRefreshToken, |
| 6 | + getSessionToken, |
| 7 | + isSessionTokenExpired, |
| 8 | + isRefreshTokenExpired |
| 9 | +} from '../src/sdk'; |
| 10 | + |
| 11 | +jest.mock('@descope/web-js-sdk', () => () => ({ |
| 12 | + getSessionToken: jest.fn(), |
| 13 | + getRefreshToken: jest.fn(), |
| 14 | + isJwtExpired: jest.fn(), |
| 15 | + getJwtPermissions: jest.fn(), |
| 16 | + getJwtRoles: jest.fn(), |
| 17 | + refresh: jest.fn() |
| 18 | +})); |
| 19 | + |
| 20 | +const sdk = createSdk({ projectId: 'test' }); |
| 21 | + |
| 22 | +describe('utility functions', () => { |
| 23 | + it('should call getSessionToken from sdk', () => { |
| 24 | + getSessionToken(); |
| 25 | + expect(sdk.getSessionToken).toHaveBeenCalled(); |
| 26 | + }); |
| 27 | + |
| 28 | + it('should warn when using getSessionToken in non browser environment', () => { |
| 29 | + const warnSpy = jest.spyOn(console, 'warn'); |
| 30 | + |
| 31 | + const origWindow = window; |
| 32 | + Object.defineProperty(global, 'window', { |
| 33 | + value: undefined, |
| 34 | + writable: true, |
| 35 | + configurable: true |
| 36 | + }); |
| 37 | + |
| 38 | + jest.resetModules(); |
| 39 | + |
| 40 | + // eslint-disable-next-line @typescript-eslint/no-var-requires |
| 41 | + const { getSessionToken: getSessionTokenLocal } = require('../src/sdk'); |
| 42 | + |
| 43 | + getSessionTokenLocal(); |
| 44 | + |
| 45 | + global.window = origWindow; |
| 46 | + jest.resetModules(); |
| 47 | + |
| 48 | + expect(warnSpy).toHaveBeenCalledWith( |
| 49 | + 'Get session token is not supported in SSR' |
| 50 | + ); |
| 51 | + expect(sdk.getSessionToken).not.toHaveBeenCalled(); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should call getRefreshToken from sdk', () => { |
| 55 | + getRefreshToken(); |
| 56 | + expect(sdk.getRefreshToken).toHaveBeenCalled(); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should warn when using getRefreshToken in non browser environment', () => { |
| 60 | + const warnSpy = jest.spyOn(console, 'warn'); |
| 61 | + |
| 62 | + const origWindow = window; |
| 63 | + Object.defineProperty(global, 'window', { |
| 64 | + value: undefined, |
| 65 | + writable: true, |
| 66 | + configurable: true |
| 67 | + }); |
| 68 | + |
| 69 | + jest.resetModules(); |
| 70 | + |
| 71 | + // eslint-disable-next-line @typescript-eslint/no-var-requires |
| 72 | + const { getRefreshToken: getRefreshTokenLocal } = require('../src/sdk'); |
| 73 | + |
| 74 | + getRefreshTokenLocal(); |
| 75 | + |
| 76 | + global.window = origWindow; |
| 77 | + jest.resetModules(); |
| 78 | + |
| 79 | + expect(warnSpy).toHaveBeenCalledWith( |
| 80 | + 'Get refresh token is not supported in SSR' |
| 81 | + ); |
| 82 | + expect(sdk.getRefreshToken).not.toHaveBeenCalled(); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should call refresh token with the session token', async () => { |
| 86 | + (sdk.refresh as jest.Mock).getMockImplementation(); |
| 87 | + await refresh('test'); |
| 88 | + expect(sdk.refresh).toHaveBeenCalledWith('test'); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should call getJwtPermissions with the session token when not provided', () => { |
| 92 | + (sdk.getSessionToken as jest.Mock).mockReturnValueOnce('session'); |
| 93 | + getJwtPermissions(); |
| 94 | + expect(sdk.getJwtPermissions).toHaveBeenCalledWith('session', undefined); |
| 95 | + }); |
| 96 | + |
| 97 | + it('should call isSessionJwtExpired with the session token when not provided', () => { |
| 98 | + (sdk.getSessionToken as jest.Mock).mockReturnValueOnce('session'); |
| 99 | + jest.spyOn(sdk, 'isJwtExpired').mockReturnValueOnce(false); |
| 100 | + isSessionTokenExpired(); |
| 101 | + expect(sdk.isJwtExpired).toHaveBeenCalledWith('session'); |
| 102 | + }); |
| 103 | + |
| 104 | + it('should call isRefreshJwtExpired with the refresh token when not provided', () => { |
| 105 | + (sdk.getRefreshToken as jest.Mock).mockReturnValueOnce('refresh'); |
| 106 | + jest.spyOn(sdk, 'isJwtExpired').mockReturnValueOnce(false); |
| 107 | + isRefreshTokenExpired(); |
| 108 | + expect(sdk.isJwtExpired).toHaveBeenCalledWith('refresh'); |
| 109 | + }); |
| 110 | + |
| 111 | + it('should call getJwtRoles with the session token when not provided', () => { |
| 112 | + (sdk.getSessionToken as jest.Mock).mockReturnValueOnce('session'); |
| 113 | + jest.spyOn(sdk, 'getJwtRoles').mockReturnValueOnce([]); |
| 114 | + getJwtRoles(); |
| 115 | + expect(sdk.getJwtRoles).toHaveBeenCalledWith('session', undefined); |
| 116 | + }); |
| 117 | + |
| 118 | + it('should call getJwtRoles with the session token when not provided', () => { |
| 119 | + jest.spyOn(console, 'error').mockImplementation(() => {}); // eslint-disable-line @typescript-eslint/no-empty-function |
| 120 | + jest.spyOn(sdk, 'getJwtRoles').mockImplementation(() => { |
| 121 | + throw new Error('session token'); |
| 122 | + }); |
| 123 | + getJwtRoles(); |
| 124 | + expect(console.error).toHaveBeenCalled(); // eslint-disable-line no-console |
| 125 | + }); |
| 126 | +}); |
0 commit comments