mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2024-12-23 09:30:31 +03:00
Added test for mercure info helpers
This commit is contained in:
parent
a485d0b507
commit
d8ae69e861
2 changed files with 58 additions and 1 deletions
57
test/mercure/helpers/index.test.js
Normal file
57
test/mercure/helpers/index.test.js
Normal file
|
@ -0,0 +1,57 @@
|
|||
import { EventSourcePolyfill as EventSource } from 'event-source-polyfill';
|
||||
import { bindToMercureTopic } from '../../../src/mercure/helpers';
|
||||
|
||||
jest.mock('event-source-polyfill');
|
||||
|
||||
describe('helpers', () => {
|
||||
afterEach(jest.resetAllMocks);
|
||||
|
||||
describe('bindToMercureTopic', () => {
|
||||
const onMessage = jest.fn();
|
||||
const onTokenExpired = jest.fn();
|
||||
|
||||
it.each([
|
||||
[{ loading: true, error: false }],
|
||||
[{ loading: false, error: true }],
|
||||
[{ loading: true, error: true }],
|
||||
])('does not bind an EventSource when loading or error', (mercureInfo) => {
|
||||
bindToMercureTopic(mercureInfo)();
|
||||
|
||||
expect(EventSource).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(EventSource).toHaveBeenCalledWith(hubUrl, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
});
|
||||
|
||||
const [ es ] = EventSource.mock.instances;
|
||||
|
||||
es.onmessage({ data: '{"foo": "bar"}' });
|
||||
es.onerror({ status: 401 });
|
||||
expect(onMessage).toHaveBeenCalledWith({ foo: 'bar' });
|
||||
expect(onTokenExpired).toHaveBeenCalled();
|
||||
|
||||
callback();
|
||||
expect(es.close).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
|
@ -3,7 +3,7 @@ import reducer, {
|
|||
GET_MERCURE_INFO_ERROR,
|
||||
GET_MERCURE_INFO,
|
||||
loadMercureInfo,
|
||||
} from '../../src/mercure/reducers/mercureInfo.js';
|
||||
} from '../../../src/mercure/reducers/mercureInfo.js';
|
||||
|
||||
describe('mercureInfoReducer', () => {
|
||||
const mercureInfo = {
|
Loading…
Reference in a new issue