UNPKG

@thoughtspot/visual-embed-sdk

Version:
88 lines 3.95 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const tokenizedFetchModule = tslib_1.__importStar(require("../../tokenizedFetch")); const tokenizedAuthService_1 = require("./tokenizedAuthService"); const logger_1 = require("../logger"); const authService_1 = require("./authService"); const thoughtspotHost = 'http://thoughtspotHost'; describe('tokenizedAuthService', () => { afterEach(() => { jest.clearAllMocks(); jest.restoreAllMocks(); }); test('isActiveService if fetch returns ok', async () => { jest.spyOn(tokenizedFetchModule, 'tokenizedFetch').mockResolvedValueOnce({ ok: true, }); const isActiveResp = await (0, tokenizedAuthService_1.isActiveService)('http://thoughtspotHost'); expect(isActiveResp).toEqual(true); }); test('isActiveService if fetch returns not ok', async () => { jest.spyOn(tokenizedFetchModule, 'tokenizedFetch').mockResolvedValueOnce({ ok: false, }); const isActiveResp = await (0, tokenizedAuthService_1.isActiveService)('http://thoughtspotHost'); expect(isActiveResp).toEqual(false); }); test('isActiveService if fetch fails', async () => { jest.spyOn(tokenizedFetchModule, 'tokenizedFetch').mockRejectedValueOnce({ ok: false, }); jest.spyOn(logger_1.logger, 'warn'); const isActiveResp = await (0, tokenizedAuthService_1.isActiveService)('http://thoughtspotHost'); expect(isActiveResp).toEqual(false); expect(logger_1.logger.warn).toHaveBeenCalled(); }); }); describe('fetchPreauthInfoService', () => { afterEach(() => { jest.clearAllMocks(); jest.restoreAllMocks(); }); test('fetchPreauthInfoService if fetch returns ok', async () => { const mockFetch = jest.spyOn(tokenizedFetchModule, 'tokenizedFetch'); // Mock for fetchPreauthInfoService mockFetch .mockResolvedValueOnce({ ok: true, headers: new Headers({ 'content-type': 'application/json' }), status: 200, statusText: 'Ok', json: jest.fn().mockResolvedValue({ info: { configInfo: { mixpanelConfig: { devSdkKey: 'devSdkKey', }, }, userGUID: 'userGUID', }, }), }); const result = await (0, tokenizedAuthService_1.fetchPreauthInfoService)(thoughtspotHost); const response = await result.json(); expect(mockFetch).toHaveBeenCalledTimes(1); expect(mockFetch).toHaveBeenNthCalledWith(1, `${thoughtspotHost}${authService_1.EndPoints.PREAUTH_INFO}`, {}); expect(response).toHaveProperty('info'); }); it('fetchPreauthInfoService if fetch fails', async () => { const mockFetch = jest.spyOn(tokenizedFetchModule, 'tokenizedFetch'); // Prevent logger.error from reaching console.error (which throws in test env) jest.spyOn(logger_1.logger, 'error').mockImplementation(() => { }); mockFetch.mockResolvedValueOnce({ ok: false, status: 500, statusText: 'Internal Server Error', json: jest.fn().mockResolvedValue({}), text: jest.fn().mockResolvedValue('Internal Server Error'), }); const response = await (0, tokenizedAuthService_1.fetchPreauthInfoService)(thoughtspotHost); expect(response.ok).toBe(false); expect(response.status).toBe(500); expect(logger_1.logger.error).toHaveBeenCalled(); expect(mockFetch).toHaveBeenCalledTimes(1); expect(mockFetch).toHaveBeenCalledWith(`${thoughtspotHost}${authService_1.EndPoints.PREAUTH_INFO}`, {}); }); }); //# sourceMappingURL=tokenizedAuthService.spec.js.map