Removed duplicated code when binding to mercure by checking if enabled first

This commit is contained in:
Alejandro Celaya
2020-06-06 09:24:05 +02:00
parent 05e3e87653
commit c46d5187c1
15 changed files with 47 additions and 53 deletions

View File

@@ -11,12 +11,11 @@ describe('helpers', () => {
const onTokenExpired = jest.fn();
it.each([
[{ loading: true, error: false }, { enabled: true }],
[{ loading: false, error: true }, { enabled: true }],
[{ loading: true, error: true }, { enabled: true }],
[{ loading: false, error: false }, { enabled: false }],
])('does not bind an EventSource when disabled, loading or error', (mercureInfo, realTimeUpdates) => {
bindToMercureTopic(mercureInfo, realTimeUpdates)();
[{ 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();
@@ -36,7 +35,7 @@ describe('helpers', () => {
error: false,
mercureHubUrl,
token,
}, { enabled: true }, topic, onMessage, onTokenExpired)();
}, topic, onMessage, onTokenExpired)();
expect(EventSource).toHaveBeenCalledWith(hubUrl, {
headers: {

View File

@@ -40,14 +40,31 @@ describe('mercureInfoReducer', () => {
mercureInfo: jest.fn(() => result),
});
const dispatch = jest.fn();
const getState = () => ({});
const createGetStateMock = (enabled) => jest.fn(() => ({
settings: {
realTimeUpdates: { enabled },
},
}));
afterEach(jest.resetAllMocks);
it('dispatches error when real time updates are disabled', async () => {
const apiClientMock = createApiClientMock(Promise.resolve(mercureInfo));
const getState = createGetStateMock(false);
await loadMercureInfo(() => apiClientMock)()(dispatch, getState);
expect(apiClientMock.mercureInfo).not.toHaveBeenCalled();
expect(dispatch).toHaveBeenCalledTimes(2);
expect(dispatch).toHaveBeenNthCalledWith(1, { type: GET_MERCURE_INFO_START });
expect(dispatch).toHaveBeenNthCalledWith(2, { type: GET_MERCURE_INFO_ERROR });
});
it('calls API on success', async () => {
const apiClientMock = createApiClientMock(Promise.resolve(mercureInfo));
const getState = createGetStateMock(true);
await loadMercureInfo(() => apiClientMock)()(dispatch, getState());
await loadMercureInfo(() => apiClientMock)()(dispatch, getState);
expect(apiClientMock.mercureInfo).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenCalledTimes(2);
@@ -58,8 +75,9 @@ describe('mercureInfoReducer', () => {
it('throws error on failure', async () => {
const error = 'Error';
const apiClientMock = createApiClientMock(Promise.reject(error));
const getState = createGetStateMock(true);
await loadMercureInfo(() => apiClientMock)()(dispatch, getState());
await loadMercureInfo(() => apiClientMock)()(dispatch, getState);
expect(apiClientMock.mercureInfo).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenCalledTimes(2);

View File

@@ -9,7 +9,6 @@ describe('<ShortUrlsList />', () => {
const ShortUrlsRow = () => '';
const listShortUrlsMock = jest.fn();
const resetShortUrlParamsMock = jest.fn();
const realTimeUpdates = { enabled: true };
const ShortUrlsList = shortUrlsListCreator(ShortUrlsRow);
@@ -38,7 +37,6 @@ describe('<ShortUrlsList />', () => {
]
}
mercureInfo={{ loading: true }}
settings={{ realTimeUpdates }}
/>
);
});

View File

@@ -15,7 +15,7 @@ describe('<TagsList />', () => {
const TagsList = createTagsList(TagCard);
wrapper = shallow(
<TagsList forceListTags={identity} filterTags={filterTags} match={{ params }} tagsList={tagsList} settings={{}} />
<TagsList forceListTags={identity} filterTags={filterTags} match={{ params }} tagsList={tagsList} />
);
return wrapper;

View File

@@ -14,7 +14,6 @@ describe('<ShortUrlVisits />', () => {
const history = {
goBack: jest.fn(),
};
const realTimeUpdates = { enabled: true };
const VisitsStats = jest.fn();
beforeEach(() => {
@@ -31,7 +30,6 @@ describe('<ShortUrlVisits />', () => {
shortUrlDetail={{}}
cancelGetShortUrlVisits={identity}
matchMedia={() => ({ matches: false })}
settings={{ realTimeUpdates }}
/>
);
});

View File

@@ -13,7 +13,6 @@ describe('<TagVisits />', () => {
const history = {
goBack: jest.fn(),
};
const realTimeUpdates = { enabled: true };
const VisitsStats = jest.fn();
beforeEach(() => {
@@ -26,7 +25,6 @@ describe('<TagVisits />', () => {
history={history}
tagVisits={{ loading: true, visits: [] }}
cancelGetTagVisits={identity}
settings={{ realTimeUpdates }}
/>
);
});