mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2026-03-17 21:13:48 +00:00
Simplified code making it easier to read
This commit is contained in:
@@ -55,22 +55,19 @@ describe('shortUrlCreationReducer', () => {
|
||||
afterEach(() => dispatch.mockReset());
|
||||
|
||||
it('calls API on success', async () => {
|
||||
const expectedDispatchCalls = 2;
|
||||
const result = 'foo';
|
||||
const apiClientMock = createApiClientMock(Promise.resolve(result));
|
||||
const dispatchable = createShortUrl(() => apiClientMock)({});
|
||||
|
||||
await dispatchable(dispatch, getState);
|
||||
const [ firstDispatchCallArgs, secondDispatchCallArgs ] = dispatch.mock.calls;
|
||||
|
||||
expect(apiClientMock.createShortUrl).toHaveBeenCalledTimes(1);
|
||||
expect(dispatch).toHaveBeenCalledTimes(expectedDispatchCalls);
|
||||
expect(firstDispatchCallArgs).toEqual([{ type: CREATE_SHORT_URL_START }]);
|
||||
expect(secondDispatchCallArgs).toEqual([{ type: CREATE_SHORT_URL, result }]);
|
||||
expect(dispatch).toHaveBeenCalledTimes(2);
|
||||
expect(dispatch).toHaveBeenNthCalledWith(1, { type: CREATE_SHORT_URL_START });
|
||||
expect(dispatch).toHaveBeenNthCalledWith(2, { type: CREATE_SHORT_URL, result });
|
||||
});
|
||||
|
||||
it('throws on error', async () => {
|
||||
const expectedDispatchCalls = 2;
|
||||
const error = 'Error';
|
||||
const apiClientMock = createApiClientMock(Promise.reject(error));
|
||||
const dispatchable = createShortUrl(() => apiClientMock)({});
|
||||
@@ -80,12 +77,11 @@ describe('shortUrlCreationReducer', () => {
|
||||
} catch (e) {
|
||||
expect(e).toEqual(error);
|
||||
}
|
||||
const [ firstDispatchCallArgs, secondDispatchCallArgs ] = dispatch.mock.calls;
|
||||
|
||||
expect(apiClientMock.createShortUrl).toHaveBeenCalledTimes(1);
|
||||
expect(dispatch).toHaveBeenCalledTimes(expectedDispatchCalls);
|
||||
expect(firstDispatchCallArgs).toEqual([{ type: CREATE_SHORT_URL_START }]);
|
||||
expect(secondDispatchCallArgs).toEqual([{ type: CREATE_SHORT_URL_ERROR }]);
|
||||
expect(dispatch).toHaveBeenCalledTimes(2);
|
||||
expect(dispatch).toHaveBeenNthCalledWith(1, { type: CREATE_SHORT_URL_START });
|
||||
expect(dispatch).toHaveBeenNthCalledWith(2, { type: CREATE_SHORT_URL_ERROR });
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -70,17 +70,15 @@ describe('shortUrlDeletionReducer', () => {
|
||||
deleteShortUrl: jest.fn(() => ''),
|
||||
};
|
||||
const shortCode = 'abc123';
|
||||
const expectedDispatchCalls = 2;
|
||||
|
||||
await deleteShortUrl(() => apiClientMock)(shortCode)(dispatch, getState);
|
||||
const [ firstDispatchCallArgs, secondDispatchCallArgs ] = dispatch.mock.calls;
|
||||
|
||||
expect(dispatch).toHaveBeenCalledTimes(expectedDispatchCalls);
|
||||
expect(firstDispatchCallArgs).toEqual([{ type: DELETE_SHORT_URL_START }]);
|
||||
expect(secondDispatchCallArgs).toEqual([{ type: DELETE_SHORT_URL, shortCode }]);
|
||||
expect(dispatch).toHaveBeenCalledTimes(2);
|
||||
expect(dispatch).toHaveBeenNthCalledWith(1, { type: DELETE_SHORT_URL_START });
|
||||
expect(dispatch).toHaveBeenNthCalledWith(2, { type: DELETE_SHORT_URL, shortCode });
|
||||
|
||||
expect(apiClientMock.deleteShortUrl).toHaveBeenCalledTimes(1);
|
||||
expect(apiClientMock.deleteShortUrl.mock.calls[0]).toEqual([ shortCode ]);
|
||||
expect(apiClientMock.deleteShortUrl).toHaveBeenCalledWith(shortCode);
|
||||
});
|
||||
|
||||
it('dispatches proper actions if API client request fails', async () => {
|
||||
@@ -90,21 +88,19 @@ describe('shortUrlDeletionReducer', () => {
|
||||
deleteShortUrl: jest.fn(() => Promise.reject(error)),
|
||||
};
|
||||
const shortCode = 'abc123';
|
||||
const expectedDispatchCalls = 2;
|
||||
|
||||
try {
|
||||
await deleteShortUrl(() => apiClientMock)(shortCode)(dispatch, getState);
|
||||
} catch (e) {
|
||||
expect(e).toEqual(error);
|
||||
}
|
||||
const [ firstDispatchCallArgs, secondDispatchCallArgs ] = dispatch.mock.calls;
|
||||
|
||||
expect(dispatch).toHaveBeenCalledTimes(expectedDispatchCalls);
|
||||
expect(firstDispatchCallArgs).toEqual([{ type: DELETE_SHORT_URL_START }]);
|
||||
expect(secondDispatchCallArgs).toEqual([{ type: DELETE_SHORT_URL_ERROR, errorData: data }]);
|
||||
expect(dispatch).toHaveBeenCalledTimes(2);
|
||||
expect(dispatch).toHaveBeenNthCalledWith(1, { type: DELETE_SHORT_URL_START });
|
||||
expect(dispatch).toHaveBeenNthCalledWith(2, { type: DELETE_SHORT_URL_ERROR, errorData: data });
|
||||
|
||||
expect(apiClientMock.deleteShortUrl).toHaveBeenCalledTimes(1);
|
||||
expect(apiClientMock.deleteShortUrl.mock.calls[0]).toEqual([ shortCode ]);
|
||||
expect(apiClientMock.deleteShortUrl).toHaveBeenCalledWith(shortCode);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -84,14 +84,12 @@ describe('shortUrlsListReducer', () => {
|
||||
const apiClientMock = {
|
||||
listShortUrls: jest.fn().mockResolvedValue([]),
|
||||
};
|
||||
const expectedDispatchCalls = 2;
|
||||
|
||||
await listShortUrls(() => apiClientMock)()(dispatch, getState);
|
||||
const [ firstDispatchCallArgs, secondDispatchCallArgs ] = dispatch.mock.calls;
|
||||
|
||||
expect(dispatch).toHaveBeenCalledTimes(expectedDispatchCalls);
|
||||
expect(firstDispatchCallArgs).toEqual([{ type: LIST_SHORT_URLS_START }]);
|
||||
expect(secondDispatchCallArgs).toEqual([{ type: LIST_SHORT_URLS, shortUrls: [], params: {} }]);
|
||||
expect(dispatch).toHaveBeenCalledTimes(2);
|
||||
expect(dispatch).toHaveBeenNthCalledWith(1, { type: LIST_SHORT_URLS_START });
|
||||
expect(dispatch).toHaveBeenNthCalledWith(2, { type: LIST_SHORT_URLS, shortUrls: [], params: {} });
|
||||
|
||||
expect(apiClientMock.listShortUrls).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
@@ -100,14 +98,12 @@ describe('shortUrlsListReducer', () => {
|
||||
const apiClientMock = {
|
||||
listShortUrls: jest.fn().mockRejectedValue(),
|
||||
};
|
||||
const expectedDispatchCalls = 2;
|
||||
|
||||
await listShortUrls(() => apiClientMock)()(dispatch, getState);
|
||||
const [ firstDispatchCallArgs, secondDispatchCallArgs ] = dispatch.mock.calls;
|
||||
|
||||
expect(dispatch).toHaveBeenCalledTimes(expectedDispatchCalls);
|
||||
expect(firstDispatchCallArgs).toEqual([{ type: LIST_SHORT_URLS_START }]);
|
||||
expect(secondDispatchCallArgs).toEqual([{ type: LIST_SHORT_URLS_ERROR, params: {} }]);
|
||||
expect(dispatch).toHaveBeenCalledTimes(2);
|
||||
expect(dispatch).toHaveBeenNthCalledWith(1, { type: LIST_SHORT_URLS_START });
|
||||
expect(dispatch).toHaveBeenNthCalledWith(2, { type: LIST_SHORT_URLS_ERROR, params: {} });
|
||||
|
||||
expect(apiClientMock.listShortUrls).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user