diff --git a/test/mercure/helpers/index.test.js b/test/mercure/helpers/index.test.js new file mode 100644 index 00000000..1e6fd3df --- /dev/null +++ b/test/mercure/helpers/index.test.js @@ -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(); + }); + }); +}); diff --git a/test/mercure/mercureInfo.test.js b/test/mercure/reducers/mercureInfo.test.js similarity index 97% rename from test/mercure/mercureInfo.test.js rename to test/mercure/reducers/mercureInfo.test.js index 908480ee..fa93636e 100644 --- a/test/mercure/mercureInfo.test.js +++ b/test/mercure/reducers/mercureInfo.test.js @@ -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 = {