From 30ed170aacdbae7e37e093a2eaf71e4ed4609cb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20=C3=85str=C3=B6m?= Date: Fri, 8 May 2026 12:03:41 +0200 Subject: [PATCH] test: fill coverage gaps from @qlik/api migration (#1996) * test: add clientId openApp flow tests in connect.test.js * test: add getDocList error path test in connect.test.js * test: add unit tests for useDeauthorizePrevOAuthInstance * test: improve spy management and strengthen error assertion in tests --- commands/serve/web/__tests__/connect.test.js | 44 +++++++++++++++++ .../useDeauthorizePrevOAuthInstance.test.js | 48 +++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 commands/serve/web/hooks/__tests__/useDeauthorizePrevOAuthInstance.test.js diff --git a/commands/serve/web/__tests__/connect.test.js b/commands/serve/web/__tests__/connect.test.js index f60fd3bbf..b98ea018b 100644 --- a/commands/serve/web/__tests__/connect.test.js +++ b/commands/serve/web/__tests__/connect.test.js @@ -106,6 +106,12 @@ describe('connect.js', () => { expect(getItems).toHaveBeenCalledWith(expect.objectContaining({ resourceType: 'app', limit: 30 })); expect(docList).toEqual([{ qDocId: 'app-1', qTitle: 'My App' }]); }); + + test('getDocList should propagate errors from getItems', async () => { + getItems.mockRejectedValue(new Error('network error')); + const result = await connect(); + await expect(result.getDocList()).rejects.toThrow('network error'); + }); }); describe('connecting with `clientId` (OAuth2) flow', () => { @@ -261,6 +267,44 @@ describe('connect.js', () => { }); }); + describe('open app with clientId (OAuth2) flow', () => { + let getDocMock; + let appSessionMock; + + beforeEach(() => { + jsonResponseMock.mockImplementation(() => + Promise.resolve({ + clientId: 'someClientId', + enigma: { + secure: true, + host: 'some.eu.tenant.pte.qlikdev.com', + }, + }) + ); + getDocMock = jest.fn().mockResolvedValue({ id: appId }); + appSessionMock = { getDoc: getDocMock }; + openAppSession.mockReturnValue(appSessionMock); + }); + + test('should call `auth.setDefaultHostConfig` with oauth2 authType', async () => { + await openApp(appId); + expect(auth.setDefaultHostConfig).toHaveBeenCalledWith( + expect.objectContaining({ authType: 'oauth2', clientId: 'someClientId' }) + ); + }); + + test('should call `openAppSession` with the app id', async () => { + await openApp(appId); + expect(openAppSession).toHaveBeenCalledWith({ appId }); + }); + + test('should call `getDoc` and return the document', async () => { + const result = await openApp(appId); + expect(getDocMock).toHaveBeenCalledTimes(1); + expect(result).toEqual({ id: appId }); + }); + }); + describe('with Local engine flow', () => { beforeEach(() => { connectionResponse = { diff --git a/commands/serve/web/hooks/__tests__/useDeauthorizePrevOAuthInstance.test.js b/commands/serve/web/hooks/__tests__/useDeauthorizePrevOAuthInstance.test.js new file mode 100644 index 000000000..2b74d5f03 --- /dev/null +++ b/commands/serve/web/hooks/__tests__/useDeauthorizePrevOAuthInstance.test.js @@ -0,0 +1,48 @@ +import { renderHook } from '@testing-library/react'; +import auth from '@qlik/api/auth'; +import { useDeauthorizePrevOAuthInstance } from '../useDeauthorizePrevOAuthInstance'; +import * as RootContextModule from '../../contexts/RootContext'; + +jest.mock('@qlik/api/auth', () => ({ + __esModule: true, + default: { setDefaultHostConfig: jest.fn() }, +})); + +describe('useDeauthorizePrevOAuthInstance', () => { + let useRootContextSpy; + + beforeEach(() => { + useRootContextSpy = jest.spyOn(RootContextModule, 'useRootContext').mockReturnValue({ + cachedConnectionsData: { cachedConnections: [] }, + }); + }); + + afterEach(() => { + jest.restoreAllMocks(); + }); + + test('should call auth.setDefaultHostConfig(undefined) on mount', () => { + renderHook(() => useDeauthorizePrevOAuthInstance()); + expect(auth.setDefaultHostConfig).toHaveBeenCalledTimes(1); + expect(auth.setDefaultHostConfig).toHaveBeenCalledWith(undefined); + }); + + test('should call auth.setDefaultHostConfig(undefined) again when cachedConnections length changes', () => { + let connections = []; + useRootContextSpy.mockReturnValue({ + cachedConnectionsData: { cachedConnections: connections }, + }); + + const { rerender } = renderHook(() => useDeauthorizePrevOAuthInstance()); + expect(auth.setDefaultHostConfig).toHaveBeenCalledTimes(1); + + connections = ['ws://localhost:9000/new-connection']; + useRootContextSpy.mockReturnValue({ + cachedConnectionsData: { cachedConnections: connections }, + }); + rerender(); + + expect(auth.setDefaultHostConfig).toHaveBeenCalledTimes(2); + expect(auth.setDefaultHostConfig).toHaveBeenCalledWith(undefined); + }); +});