mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2026-04-19 21:16:18 +00:00
Removed duplicated code when binding to mercure by checking if enabled first
This commit is contained in:
@@ -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: {
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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 }}
|
||||
/>
|
||||
);
|
||||
});
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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 }}
|
||||
/>
|
||||
);
|
||||
});
|
||||
|
||||
@@ -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 }}
|
||||
/>
|
||||
);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user