2023-04-13 22:48:29 +03:00
|
|
|
import { fromPartial } from '@total-typescript/shoehorn';
|
2021-02-28 19:43:41 +03:00
|
|
|
import { EventSourcePolyfill } from 'event-source-polyfill';
|
2020-08-29 14:51:53 +03:00
|
|
|
import { identity } from 'ramda';
|
2020-04-18 13:49:03 +03:00
|
|
|
import { bindToMercureTopic } from '../../../src/mercure/helpers';
|
2023-02-18 12:40:37 +03:00
|
|
|
import type { MercureInfo } from '../../../src/mercure/reducers/mercureInfo';
|
2020-04-18 13:49:03 +03:00
|
|
|
|
2023-05-27 12:57:26 +03:00
|
|
|
vi.mock('event-source-polyfill');
|
2020-04-18 13:49:03 +03:00
|
|
|
|
|
|
|
describe('helpers', () => {
|
|
|
|
describe('bindToMercureTopic', () => {
|
2023-05-27 12:57:26 +03:00
|
|
|
const onMessage = vi.fn();
|
|
|
|
const onTokenExpired = vi.fn();
|
2020-04-18 13:49:03 +03:00
|
|
|
|
|
|
|
it.each([
|
2023-04-13 22:48:29 +03:00
|
|
|
[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 })],
|
2020-08-29 14:51:53 +03:00
|
|
|
])('does not bind an EventSource when loading, error or no hub URL', (mercureInfo) => {
|
2022-03-26 14:17:42 +03:00
|
|
|
bindToMercureTopic(mercureInfo, [''], identity, () => {});
|
2020-04-18 13:49:03 +03:00
|
|
|
|
2021-02-28 19:43:41 +03:00
|
|
|
expect(EventSourcePolyfill).not.toHaveBeenCalled();
|
2020-04-18 13:49:03 +03:00
|
|
|
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,
|
2022-03-26 14:17:42 +03:00
|
|
|
}, [topic], onMessage, onTokenExpired);
|
2020-04-18 13:49:03 +03:00
|
|
|
|
2021-02-28 19:43:41 +03:00
|
|
|
expect(EventSourcePolyfill).toHaveBeenCalledWith(hubUrl, {
|
2020-04-18 13:49:03 +03:00
|
|
|
headers: {
|
|
|
|
Authorization: `Bearer ${token}`,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2022-03-26 14:17:42 +03:00
|
|
|
const [es] = (EventSourcePolyfill as any).mock.instances as EventSourcePolyfill[];
|
2020-04-18 13:49:03 +03:00
|
|
|
|
2021-02-28 19:43:41 +03:00
|
|
|
es.onmessage?.({ data: '{"foo": "bar"}' });
|
|
|
|
es.onerror?.({ status: 401 });
|
2020-04-18 13:49:03 +03:00
|
|
|
expect(onMessage).toHaveBeenCalledWith({ foo: 'bar' });
|
|
|
|
expect(onTokenExpired).toHaveBeenCalled();
|
|
|
|
|
2020-08-29 14:51:53 +03:00
|
|
|
callback?.();
|
2020-04-18 13:49:03 +03:00
|
|
|
expect(es.close).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|