mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2026-04-20 13:36:20 +00:00
Move shlink-web-component tests to their own folder
This commit is contained in:
60
shlink-web-component/test/mercure/helpers/index.test.tsx
Normal file
60
shlink-web-component/test/mercure/helpers/index.test.tsx
Normal file
@@ -0,0 +1,60 @@
|
||||
import { fromPartial } from '@total-typescript/shoehorn';
|
||||
import { EventSourcePolyfill } from 'event-source-polyfill';
|
||||
import { identity } from 'ramda';
|
||||
import { bindToMercureTopic } from '../../../src/mercure/helpers';
|
||||
import type { MercureInfo } from '../../../src/mercure/reducers/mercureInfo';
|
||||
|
||||
vi.mock('event-source-polyfill');
|
||||
|
||||
describe('helpers', () => {
|
||||
describe('bindToMercureTopic', () => {
|
||||
const onMessage = vi.fn();
|
||||
const onTokenExpired = vi.fn();
|
||||
|
||||
it.each([
|
||||
[fromPartial<MercureInfo>({ loading: true, error: false, mercureHubUrl: 'foo' })],
|
||||
[fromPartial<MercureInfo>({ loading: false, error: true, mercureHubUrl: 'foo' })],
|
||||
[fromPartial<MercureInfo>({ loading: true, error: true, mercureHubUrl: 'foo' })],
|
||||
[fromPartial<MercureInfo>({ loading: false, error: false, mercureHubUrl: undefined })],
|
||||
[fromPartial<MercureInfo>({ loading: true, error: true, mercureHubUrl: undefined })],
|
||||
])('does not bind an EventSource when loading, error or no hub URL', (mercureInfo) => {
|
||||
bindToMercureTopic(mercureInfo, [''], identity, () => {});
|
||||
|
||||
expect(EventSourcePolyfill).not.toHaveBeenCalled();
|
||||
expect(onMessage).not.toHaveBeenCalled();
|
||||
expect(onTokenExpired).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('binds an EventSource when mercure info is properly loaded', () => {
|
||||
const token = 'abc.123.efg';
|
||||
const mercureHubUrl = 'https://example.com/.well-known/mercure';
|
||||
const topic = 'foo';
|
||||
const hubUrl = new URL(mercureHubUrl);
|
||||
|
||||
hubUrl.searchParams.append('topic', topic);
|
||||
|
||||
const callback = bindToMercureTopic({
|
||||
loading: false,
|
||||
error: false,
|
||||
mercureHubUrl,
|
||||
token,
|
||||
}, [topic], onMessage, onTokenExpired);
|
||||
|
||||
expect(EventSourcePolyfill).toHaveBeenCalledWith(hubUrl, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
});
|
||||
|
||||
const [es] = (EventSourcePolyfill as any).mock.instances as EventSourcePolyfill[];
|
||||
|
||||
es.onmessage?.({ data: '{"foo": "bar"}' });
|
||||
es.onerror?.({ status: 401 });
|
||||
expect(onMessage).toHaveBeenCalledWith({ foo: 'bar' });
|
||||
expect(onTokenExpired).toHaveBeenCalled();
|
||||
|
||||
callback?.();
|
||||
expect(es.close).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,69 @@
|
||||
import { fromPartial } from '@total-typescript/shoehorn';
|
||||
import type { ShlinkApiClient } from '../../../../src/api/services/ShlinkApiClient';
|
||||
import type { GetState } from '../../../../src/container/types';
|
||||
import { mercureInfoReducerCreator } from '../../../src/mercure/reducers/mercureInfo';
|
||||
|
||||
describe('mercureInfoReducer', () => {
|
||||
const mercureInfo = {
|
||||
mercureHubUrl: 'http://example.com/.well-known/mercure',
|
||||
token: 'abc.123.def',
|
||||
};
|
||||
const getMercureInfo = vi.fn();
|
||||
const buildShlinkApiClient = () => fromPartial<ShlinkApiClient>({ mercureInfo: getMercureInfo });
|
||||
const { loadMercureInfo, reducer } = mercureInfoReducerCreator(buildShlinkApiClient);
|
||||
|
||||
describe('reducer', () => {
|
||||
it('returns loading on GET_MERCURE_INFO_START', () => {
|
||||
expect(reducer(undefined, loadMercureInfo.pending(''))).toEqual({
|
||||
loading: true,
|
||||
error: false,
|
||||
});
|
||||
});
|
||||
|
||||
it('returns error on GET_MERCURE_INFO_ERROR', () => {
|
||||
expect(reducer(undefined, loadMercureInfo.rejected(null, ''))).toEqual({
|
||||
loading: false,
|
||||
error: true,
|
||||
});
|
||||
});
|
||||
|
||||
it('returns mercure info on GET_MERCURE_INFO', () => {
|
||||
expect(reducer(undefined, loadMercureInfo.fulfilled(mercureInfo, ''))).toEqual(
|
||||
expect.objectContaining({ ...mercureInfo, loading: false, error: false }),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('loadMercureInfo', () => {
|
||||
const dispatch = vi.fn();
|
||||
const createGetStateMock = (enabled: boolean): GetState => vi.fn().mockReturnValue({
|
||||
settings: {
|
||||
realTimeUpdates: { enabled },
|
||||
},
|
||||
});
|
||||
|
||||
it('dispatches error when real time updates are disabled', async () => {
|
||||
getMercureInfo.mockResolvedValue(mercureInfo);
|
||||
const getState = createGetStateMock(false);
|
||||
|
||||
await loadMercureInfo()(dispatch, getState, {});
|
||||
|
||||
expect(getMercureInfo).not.toHaveBeenCalled();
|
||||
expect(dispatch).toHaveBeenCalledTimes(2);
|
||||
expect(dispatch).toHaveBeenLastCalledWith(expect.objectContaining({
|
||||
error: new Error('Real time updates not enabled'),
|
||||
}));
|
||||
});
|
||||
|
||||
it('calls API on success', async () => {
|
||||
getMercureInfo.mockResolvedValue(mercureInfo);
|
||||
const getState = createGetStateMock(true);
|
||||
|
||||
await loadMercureInfo()(dispatch, getState, {});
|
||||
|
||||
expect(getMercureInfo).toHaveBeenCalledTimes(1);
|
||||
expect(dispatch).toHaveBeenCalledTimes(2);
|
||||
expect(dispatch).toHaveBeenLastCalledWith(expect.objectContaining({ payload: mercureInfo }));
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user