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
This commit is contained in:
Tobias Åström
2026-05-08 12:03:41 +02:00
committed by GitHub
parent f43dbf256b
commit 30ed170aac
2 changed files with 92 additions and 0 deletions

View File

@@ -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 = {

View File

@@ -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);
});
});