Simplified code making it easier to read

This commit is contained in:
Alejandro Celaya
2019-04-19 12:52:55 +02:00
parent 28ca54547e
commit 33d67cbe3d
8 changed files with 40 additions and 74 deletions

View File

@@ -39,20 +39,12 @@ describe('selectedServerReducer', () => {
it('dispatches proper actions', () => { it('dispatches proper actions', () => {
const dispatch = jest.fn(); const dispatch = jest.fn();
const expectedDispatchCalls = 2;
selectServer(ServersServiceMock)(serverId)(dispatch); selectServer(ServersServiceMock)(serverId)(dispatch);
const [ firstCallArgs, secondCallArgs ] = dispatch.mock.calls; expect(dispatch).toHaveBeenCalledTimes(2);
expect(dispatch).toHaveBeenNthCalledWith(1, { type: RESET_SHORT_URL_PARAMS });
expect(dispatch).toHaveBeenCalledTimes(expectedDispatchCalls); expect(dispatch).toHaveBeenNthCalledWith(2, { type: SELECT_SERVER, selectedServer });
expect(firstCallArgs).toEqual([{ type: RESET_SHORT_URL_PARAMS }]);
expect(secondCallArgs).toEqual([
{
type: SELECT_SERVER,
selectedServer,
},
]);
}); });
it('invokes dependencies', () => { it('invokes dependencies', () => {

View File

@@ -52,7 +52,7 @@ describe('serverReducer', () => {
expect(result).toEqual(expectedFetchServersResult); expect(result).toEqual(expectedFetchServersResult);
expect(ServersServiceMock.createServer).toHaveBeenCalledTimes(1); expect(ServersServiceMock.createServer).toHaveBeenCalledTimes(1);
expect(ServersServiceMock.createServer.mock.calls[0]).toEqual([ serverToCreate ]); expect(ServersServiceMock.createServer).toHaveBeenCalledWith(serverToCreate);
expect(ServersServiceMock.listServers).not.toHaveBeenCalled(); expect(ServersServiceMock.listServers).not.toHaveBeenCalled();
expect(ServersServiceMock.deleteServer).not.toHaveBeenCalled(); expect(ServersServiceMock.deleteServer).not.toHaveBeenCalled();
expect(ServersServiceMock.createServers).not.toHaveBeenCalled(); expect(ServersServiceMock.createServers).not.toHaveBeenCalled();
@@ -69,7 +69,7 @@ describe('serverReducer', () => {
expect(ServersServiceMock.createServer).not.toHaveBeenCalled(); expect(ServersServiceMock.createServer).not.toHaveBeenCalled();
expect(ServersServiceMock.createServers).not.toHaveBeenCalled(); expect(ServersServiceMock.createServers).not.toHaveBeenCalled();
expect(ServersServiceMock.deleteServer).toHaveBeenCalledTimes(1); expect(ServersServiceMock.deleteServer).toHaveBeenCalledTimes(1);
expect(ServersServiceMock.deleteServer.mock.calls[0]).toEqual([ serverToDelete ]); expect(ServersServiceMock.deleteServer).toHaveBeenCalledWith(serverToDelete);
}); });
}); });
@@ -82,7 +82,7 @@ describe('serverReducer', () => {
expect(ServersServiceMock.listServers).not.toHaveBeenCalled(); expect(ServersServiceMock.listServers).not.toHaveBeenCalled();
expect(ServersServiceMock.createServer).not.toHaveBeenCalled(); expect(ServersServiceMock.createServer).not.toHaveBeenCalled();
expect(ServersServiceMock.createServers).toHaveBeenCalledTimes(1); expect(ServersServiceMock.createServers).toHaveBeenCalledTimes(1);
expect(ServersServiceMock.createServers.mock.calls[0]).toEqual([ serversToCreate ]); expect(ServersServiceMock.createServers).toHaveBeenCalledWith(serversToCreate);
expect(ServersServiceMock.deleteServer).not.toHaveBeenCalled(); expect(ServersServiceMock.deleteServer).not.toHaveBeenCalled();
}); });
}); });

View File

@@ -1,4 +1,3 @@
import { last } from 'ramda';
import ServersService from '../../../src/servers/services/ServersService'; import ServersService from '../../../src/servers/services/ServersService';
describe('ServersService', () => { describe('ServersService', () => {
@@ -68,10 +67,7 @@ describe('ServersService', () => {
expect(storageMock.get).toHaveBeenCalledTimes(1); expect(storageMock.get).toHaveBeenCalledTimes(1);
expect(storageMock.set).toHaveBeenCalledTimes(1); expect(storageMock.set).toHaveBeenCalledTimes(1);
expect(storageMock.set).toHaveBeenCalledWith(expect.anything(), {
const setLastCallLastArg = last(last(storageMock.set.mock.calls));
expect(setLastCallLastArg).toEqual({
abc123: { id: 'abc123' }, abc123: { id: 'abc123' },
def456: { id: 'def456' }, def456: { id: 'def456' },
ghi789: { id: 'ghi789' }, ghi789: { id: 'ghi789' },
@@ -88,10 +84,7 @@ describe('ServersService', () => {
expect(storageMock.get).toHaveBeenCalledTimes(1); expect(storageMock.get).toHaveBeenCalledTimes(1);
expect(storageMock.set).toHaveBeenCalledTimes(1); expect(storageMock.set).toHaveBeenCalledTimes(1);
expect(storageMock.set).toHaveBeenCalledWith(expect.anything(), {
const setLastCallLastArg = last(last(storageMock.set.mock.calls));
expect(setLastCallLastArg).toEqual({
abc123: { id: 'abc123' }, abc123: { id: 'abc123' },
def456: { id: 'def456' }, def456: { id: 'def456' },
ghi789: { id: 'ghi789' }, ghi789: { id: 'ghi789' },
@@ -109,10 +102,7 @@ describe('ServersService', () => {
expect(storageMock.get).toHaveBeenCalledTimes(1); expect(storageMock.get).toHaveBeenCalledTimes(1);
expect(storageMock.set).toHaveBeenCalledTimes(1); expect(storageMock.set).toHaveBeenCalledTimes(1);
expect(storageMock.set).toHaveBeenCalledWith(expect.anything(), {
const setLastCallLastArg = last(last(storageMock.set.mock.calls));
expect(setLastCallLastArg).toEqual({
def456: { id: 'def456' }, def456: { id: 'def456' },
}); });
}); });

View File

@@ -49,19 +49,15 @@ describe('<CreateShortUrl />', () => {
form.simulate('submit', { preventDefault: identity }); form.simulate('submit', { preventDefault: identity });
expect(createShortUrl).toHaveBeenCalledTimes(1); expect(createShortUrl).toHaveBeenCalledTimes(1);
expect(createShortUrl.mock.calls[0]).toEqual( expect(createShortUrl).toHaveBeenCalledWith({
[ longUrl: 'https://long-domain.com/foo/bar',
{ tags: [ 'tag_foo', 'tag_bar' ],
longUrl: 'https://long-domain.com/foo/bar', customSlug: 'my-slug',
tags: [ 'tag_foo', 'tag_bar' ], validSince: validSince.format(),
customSlug: 'my-slug', validUntil: validUntil.format(),
validSince: validSince.format(), maxVisits: '20',
validUntil: validUntil.format(), findIfExists: false,
maxVisits: '20', });
findIfExists: false,
},
]
);
done(); done();
}); });
}); });

View File

@@ -92,7 +92,7 @@ describe('<EditTagsModal />', () => {
saveBtn.simulate('click'); saveBtn.simulate('click');
expect(editShortUrlTags).toHaveBeenCalledTimes(1); expect(editShortUrlTags).toHaveBeenCalledTimes(1);
expect(editShortUrlTags.mock.calls[0]).toEqual([ shortCode, []]); expect(editShortUrlTags).toHaveBeenCalledWith(shortCode, []);
// Wrap this expect in a setImmediate since it is called as a result of an inner promise // Wrap this expect in a setImmediate since it is called as a result of an inner promise
setImmediate(() => { setImmediate(() => {
@@ -130,7 +130,7 @@ describe('<EditTagsModal />', () => {
setImmediate(() => { setImmediate(() => {
modal.simulate('closed'); modal.simulate('closed');
expect(shortUrlTagsEdited).toHaveBeenCalledTimes(1); expect(shortUrlTagsEdited).toHaveBeenCalledTimes(1);
expect(shortUrlTagsEdited.mock.calls[0]).toEqual([ shortCode, []]); expect(shortUrlTagsEdited).toHaveBeenCalledWith(shortCode, []);
done(); done();
}); });
}); });

View File

@@ -55,22 +55,19 @@ describe('shortUrlCreationReducer', () => {
afterEach(() => dispatch.mockReset()); afterEach(() => dispatch.mockReset());
it('calls API on success', async () => { it('calls API on success', async () => {
const expectedDispatchCalls = 2;
const result = 'foo'; const result = 'foo';
const apiClientMock = createApiClientMock(Promise.resolve(result)); const apiClientMock = createApiClientMock(Promise.resolve(result));
const dispatchable = createShortUrl(() => apiClientMock)({}); const dispatchable = createShortUrl(() => apiClientMock)({});
await dispatchable(dispatch, getState); await dispatchable(dispatch, getState);
const [ firstDispatchCallArgs, secondDispatchCallArgs ] = dispatch.mock.calls;
expect(apiClientMock.createShortUrl).toHaveBeenCalledTimes(1); expect(apiClientMock.createShortUrl).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenCalledTimes(expectedDispatchCalls); expect(dispatch).toHaveBeenCalledTimes(2);
expect(firstDispatchCallArgs).toEqual([{ type: CREATE_SHORT_URL_START }]); expect(dispatch).toHaveBeenNthCalledWith(1, { type: CREATE_SHORT_URL_START });
expect(secondDispatchCallArgs).toEqual([{ type: CREATE_SHORT_URL, result }]); expect(dispatch).toHaveBeenNthCalledWith(2, { type: CREATE_SHORT_URL, result });
}); });
it('throws on error', async () => { it('throws on error', async () => {
const expectedDispatchCalls = 2;
const error = 'Error'; const error = 'Error';
const apiClientMock = createApiClientMock(Promise.reject(error)); const apiClientMock = createApiClientMock(Promise.reject(error));
const dispatchable = createShortUrl(() => apiClientMock)({}); const dispatchable = createShortUrl(() => apiClientMock)({});
@@ -80,12 +77,11 @@ describe('shortUrlCreationReducer', () => {
} catch (e) { } catch (e) {
expect(e).toEqual(error); expect(e).toEqual(error);
} }
const [ firstDispatchCallArgs, secondDispatchCallArgs ] = dispatch.mock.calls;
expect(apiClientMock.createShortUrl).toHaveBeenCalledTimes(1); expect(apiClientMock.createShortUrl).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenCalledTimes(expectedDispatchCalls); expect(dispatch).toHaveBeenCalledTimes(2);
expect(firstDispatchCallArgs).toEqual([{ type: CREATE_SHORT_URL_START }]); expect(dispatch).toHaveBeenNthCalledWith(1, { type: CREATE_SHORT_URL_START });
expect(secondDispatchCallArgs).toEqual([{ type: CREATE_SHORT_URL_ERROR }]); expect(dispatch).toHaveBeenNthCalledWith(2, { type: CREATE_SHORT_URL_ERROR });
}); });
}); });
}); });

View File

@@ -70,17 +70,15 @@ describe('shortUrlDeletionReducer', () => {
deleteShortUrl: jest.fn(() => ''), deleteShortUrl: jest.fn(() => ''),
}; };
const shortCode = 'abc123'; const shortCode = 'abc123';
const expectedDispatchCalls = 2;
await deleteShortUrl(() => apiClientMock)(shortCode)(dispatch, getState); await deleteShortUrl(() => apiClientMock)(shortCode)(dispatch, getState);
const [ firstDispatchCallArgs, secondDispatchCallArgs ] = dispatch.mock.calls;
expect(dispatch).toHaveBeenCalledTimes(expectedDispatchCalls); expect(dispatch).toHaveBeenCalledTimes(2);
expect(firstDispatchCallArgs).toEqual([{ type: DELETE_SHORT_URL_START }]); expect(dispatch).toHaveBeenNthCalledWith(1, { type: DELETE_SHORT_URL_START });
expect(secondDispatchCallArgs).toEqual([{ type: DELETE_SHORT_URL, shortCode }]); expect(dispatch).toHaveBeenNthCalledWith(2, { type: DELETE_SHORT_URL, shortCode });
expect(apiClientMock.deleteShortUrl).toHaveBeenCalledTimes(1); 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 () => { it('dispatches proper actions if API client request fails', async () => {
@@ -90,21 +88,19 @@ describe('shortUrlDeletionReducer', () => {
deleteShortUrl: jest.fn(() => Promise.reject(error)), deleteShortUrl: jest.fn(() => Promise.reject(error)),
}; };
const shortCode = 'abc123'; const shortCode = 'abc123';
const expectedDispatchCalls = 2;
try { try {
await deleteShortUrl(() => apiClientMock)(shortCode)(dispatch, getState); await deleteShortUrl(() => apiClientMock)(shortCode)(dispatch, getState);
} catch (e) { } catch (e) {
expect(e).toEqual(error); expect(e).toEqual(error);
} }
const [ firstDispatchCallArgs, secondDispatchCallArgs ] = dispatch.mock.calls;
expect(dispatch).toHaveBeenCalledTimes(expectedDispatchCalls); expect(dispatch).toHaveBeenCalledTimes(2);
expect(firstDispatchCallArgs).toEqual([{ type: DELETE_SHORT_URL_START }]); expect(dispatch).toHaveBeenNthCalledWith(1, { type: DELETE_SHORT_URL_START });
expect(secondDispatchCallArgs).toEqual([{ type: DELETE_SHORT_URL_ERROR, errorData: data }]); expect(dispatch).toHaveBeenNthCalledWith(2, { type: DELETE_SHORT_URL_ERROR, errorData: data });
expect(apiClientMock.deleteShortUrl).toHaveBeenCalledTimes(1); expect(apiClientMock.deleteShortUrl).toHaveBeenCalledTimes(1);
expect(apiClientMock.deleteShortUrl.mock.calls[0]).toEqual([ shortCode ]); expect(apiClientMock.deleteShortUrl).toHaveBeenCalledWith(shortCode);
}); });
}); });
}); });

View File

@@ -84,14 +84,12 @@ describe('shortUrlsListReducer', () => {
const apiClientMock = { const apiClientMock = {
listShortUrls: jest.fn().mockResolvedValue([]), listShortUrls: jest.fn().mockResolvedValue([]),
}; };
const expectedDispatchCalls = 2;
await listShortUrls(() => apiClientMock)()(dispatch, getState); await listShortUrls(() => apiClientMock)()(dispatch, getState);
const [ firstDispatchCallArgs, secondDispatchCallArgs ] = dispatch.mock.calls;
expect(dispatch).toHaveBeenCalledTimes(expectedDispatchCalls); expect(dispatch).toHaveBeenCalledTimes(2);
expect(firstDispatchCallArgs).toEqual([{ type: LIST_SHORT_URLS_START }]); expect(dispatch).toHaveBeenNthCalledWith(1, { type: LIST_SHORT_URLS_START });
expect(secondDispatchCallArgs).toEqual([{ type: LIST_SHORT_URLS, shortUrls: [], params: {} }]); expect(dispatch).toHaveBeenNthCalledWith(2, { type: LIST_SHORT_URLS, shortUrls: [], params: {} });
expect(apiClientMock.listShortUrls).toHaveBeenCalledTimes(1); expect(apiClientMock.listShortUrls).toHaveBeenCalledTimes(1);
}); });
@@ -100,14 +98,12 @@ describe('shortUrlsListReducer', () => {
const apiClientMock = { const apiClientMock = {
listShortUrls: jest.fn().mockRejectedValue(), listShortUrls: jest.fn().mockRejectedValue(),
}; };
const expectedDispatchCalls = 2;
await listShortUrls(() => apiClientMock)()(dispatch, getState); await listShortUrls(() => apiClientMock)()(dispatch, getState);
const [ firstDispatchCallArgs, secondDispatchCallArgs ] = dispatch.mock.calls;
expect(dispatch).toHaveBeenCalledTimes(expectedDispatchCalls); expect(dispatch).toHaveBeenCalledTimes(2);
expect(firstDispatchCallArgs).toEqual([{ type: LIST_SHORT_URLS_START }]); expect(dispatch).toHaveBeenNthCalledWith(1, { type: LIST_SHORT_URLS_START });
expect(secondDispatchCallArgs).toEqual([{ type: LIST_SHORT_URLS_ERROR, params: {} }]); expect(dispatch).toHaveBeenNthCalledWith(2, { type: LIST_SHORT_URLS_ERROR, params: {} });
expect(apiClientMock.listShortUrls).toHaveBeenCalledTimes(1); expect(apiClientMock.listShortUrls).toHaveBeenCalledTimes(1);
}); });