Removed remaining usages of sinon

This commit is contained in:
Alejandro Celaya
2019-04-19 12:41:59 +02:00
parent f8de069567
commit 28ca54547e
24 changed files with 231 additions and 402 deletions

View File

@@ -1,7 +1,6 @@
import React from 'react';
import { shallow } from 'enzyme';
import moment from 'moment';
import * as sinon from 'sinon';
import { identity } from 'ramda';
import createShortUrlsCreator from '../../src/short-urls/CreateShortUrl';
import DateInput from '../../src/utils/DateInput';
@@ -12,7 +11,7 @@ describe('<CreateShortUrl />', () => {
const shortUrlCreationResult = {
loading: false,
};
const createShortUrl = sinon.spy();
const createShortUrl = jest.fn();
beforeEach(() => {
const CreateShortUrl = createShortUrlsCreator(TagsSelector, () => '');
@@ -23,7 +22,7 @@ describe('<CreateShortUrl />', () => {
});
afterEach(() => {
wrapper.unmount();
createShortUrl.resetHistory();
createShortUrl.mockReset();
});
it('saves short URL with data set in form controls', (done) => {
@@ -49,8 +48,8 @@ describe('<CreateShortUrl />', () => {
const form = wrapper.find('form');
form.simulate('submit', { preventDefault: identity });
expect(createShortUrl.callCount).toEqual(1);
expect(createShortUrl.getCall(0).args).toEqual(
expect(createShortUrl).toHaveBeenCalledTimes(1);
expect(createShortUrl.mock.calls[0]).toEqual(
[
{
longUrl: 'https://long-domain.com/foo/bar',

View File

@@ -1,21 +1,17 @@
import React from 'react';
import { shallow } from 'enzyme';
import sinon from 'sinon';
import searchBarCreator from '../../src/short-urls/SearchBar';
import SearchField from '../../src/utils/SearchField';
import Tag from '../../src/tags/helpers/Tag';
describe('<SearchBar />', () => {
let wrapper;
const listShortUrlsMock = sinon.spy();
const listShortUrlsMock = jest.fn();
const SearchBar = searchBarCreator({});
afterEach(() => {
listShortUrlsMock.resetHistory();
if (wrapper) {
wrapper.unmount();
}
listShortUrlsMock.mockReset();
wrapper && wrapper.unmount();
});
it('renders a SearchField', () => {
@@ -42,9 +38,9 @@ describe('<SearchBar />', () => {
wrapper = shallow(<SearchBar shortUrlsListParams={{}} listShortUrls={listShortUrlsMock} />);
const searchField = wrapper.find(SearchField);
expect(listShortUrlsMock.callCount).toEqual(0);
expect(listShortUrlsMock).not.toHaveBeenCalled();
searchField.simulate('change');
expect(listShortUrlsMock.callCount).toEqual(1);
expect(listShortUrlsMock).toHaveBeenCalledTimes(1);
});
it('updates short URLs list when a tag is removed', () => {
@@ -53,8 +49,8 @@ describe('<SearchBar />', () => {
);
const tag = wrapper.find(Tag).first();
expect(listShortUrlsMock.callCount).toEqual(0);
expect(listShortUrlsMock).not.toHaveBeenCalled();
tag.simulate('close');
expect(listShortUrlsMock.callCount).toEqual(1);
expect(listShortUrlsMock).toHaveBeenCalledTimes(1);
});
});

View File

@@ -3,12 +3,11 @@ import { shallow } from 'enzyme';
import { identity } from 'ramda';
import { CopyToClipboard } from 'react-copy-to-clipboard';
import { Tooltip } from 'reactstrap';
import * as sinon from 'sinon';
import createCreateShortUrlResult from '../../../src/short-urls/helpers/CreateShortUrlResult';
describe('<CreateShortUrlResult />', () => {
let wrapper;
const stateFlagTimeout = sinon.spy();
const stateFlagTimeout = jest.fn();
const createWrapper = (result, error = false) => {
const CreateShortUrlResult = createCreateShortUrlResult(stateFlagTimeout);
@@ -18,7 +17,7 @@ describe('<CreateShortUrlResult />', () => {
};
afterEach(() => {
stateFlagTimeout.resetHistory();
stateFlagTimeout.mockReset();
wrapper && wrapper.unmount();
});
@@ -48,8 +47,8 @@ describe('<CreateShortUrlResult />', () => {
const wrapper = createWrapper({ shortUrl: 'https://doma.in/abc123' });
const copyBtn = wrapper.find(CopyToClipboard);
expect(stateFlagTimeout.callCount).toEqual(0);
expect(stateFlagTimeout).not.toHaveBeenCalled();
copyBtn.simulate('copy');
expect(stateFlagTimeout.callCount).toEqual(1);
expect(stateFlagTimeout).toHaveBeenCalledTimes(1);
});
});

View File

@@ -1,7 +1,6 @@
import React from 'react';
import { shallow } from 'enzyme';
import { identity } from 'ramda';
import * as sinon from 'sinon';
import DeleteShortUrlModal from '../../../src/short-urls/helpers/DeleteShortUrlModal';
describe('<DeleteShortUrlModal />', () => {
@@ -11,7 +10,7 @@ describe('<DeleteShortUrlModal />', () => {
shortCode: 'abc123',
originalUrl: 'https://long-domain.com/foo/bar',
};
const deleteShortUrl = sinon.fake.returns(Promise.resolve());
const deleteShortUrl = jest.fn(() => Promise.resolve());
const createWrapper = (shortUrlDeletion) => {
wrapper = shallow(
<DeleteShortUrlModal
@@ -30,7 +29,7 @@ describe('<DeleteShortUrlModal />', () => {
afterEach(() => {
wrapper && wrapper.unmount();
deleteShortUrl.resetHistory();
deleteShortUrl.mockClear();
});
it('shows threshold error message when threshold error occurs', () => {
@@ -106,9 +105,9 @@ describe('<DeleteShortUrlModal />', () => {
setImmediate(() => {
const form = wrapper.find('form');
expect(deleteShortUrl.callCount).toEqual(0);
expect(deleteShortUrl).not.toHaveBeenCalled();
form.simulate('submit', { preventDefault: identity });
expect(deleteShortUrl.callCount).toEqual(1);
expect(deleteShortUrl).toHaveBeenCalledTimes(1);
done();
});
});

View File

@@ -1,6 +1,5 @@
import React from 'react';
import { shallow } from 'enzyme';
import * as sinon from 'sinon';
import { Modal } from 'reactstrap';
import createEditTagsModal from '../../../src/short-urls/helpers/EditTagsModal';
@@ -8,10 +7,10 @@ describe('<EditTagsModal />', () => {
let wrapper;
const shortCode = 'abc123';
const TagsSelector = () => '';
const editShortUrlTags = sinon.fake.resolves();
const shortUrlTagsEdited = sinon.fake();
const resetShortUrlsTags = sinon.fake();
const toggle = sinon.fake();
const editShortUrlTags = jest.fn(() => Promise.resolve());
const shortUrlTagsEdited = jest.fn();
const resetShortUrlsTags = jest.fn();
const toggle = jest.fn();
const createWrapper = (shortUrlTags) => {
const EditTagsModal = createEditTagsModal(TagsSelector);
@@ -37,10 +36,10 @@ describe('<EditTagsModal />', () => {
afterEach(() => {
wrapper && wrapper.unmount();
editShortUrlTags.resetHistory();
shortUrlTagsEdited.resetHistory();
resetShortUrlsTags.resetHistory();
toggle.resetHistory();
editShortUrlTags.mockClear();
shortUrlTagsEdited.mockReset();
resetShortUrlsTags.mockReset();
toggle.mockReset();
});
it('resets tags when component is mounted', () => {
@@ -51,7 +50,7 @@ describe('<EditTagsModal />', () => {
error: false,
});
expect(resetShortUrlsTags.callCount).toEqual(1);
expect(resetShortUrlsTags).toHaveBeenCalledTimes(1);
});
it('renders tags selector and save button when loaded', () => {
@@ -92,12 +91,12 @@ describe('<EditTagsModal />', () => {
saveBtn.simulate('click');
expect(editShortUrlTags.callCount).toEqual(1);
expect(editShortUrlTags.getCall(0).args).toEqual([ shortCode, []]);
expect(editShortUrlTags).toHaveBeenCalledTimes(1);
expect(editShortUrlTags.mock.calls[0]).toEqual([ shortCode, []]);
// Wrap this expect in a setImmediate since it is called as a result of an inner promise
setImmediate(() => {
expect(toggle.callCount).toEqual(1);
expect(toggle).toHaveBeenCalledTimes(1);
done();
});
});
@@ -112,7 +111,7 @@ describe('<EditTagsModal />', () => {
const modal = wrapper.find(Modal);
modal.simulate('closed');
expect(shortUrlTagsEdited.callCount).toEqual(0);
expect(shortUrlTagsEdited).not.toHaveBeenCalled();
});
it('notifies tags have been edited when window is closed after saving', (done) => {
@@ -130,8 +129,8 @@ describe('<EditTagsModal />', () => {
// Wrap this expect in a setImmediate since it is called as a result of an inner promise
setImmediate(() => {
modal.simulate('closed');
expect(shortUrlTagsEdited.callCount).toEqual(1);
expect(shortUrlTagsEdited.getCall(0).args).toEqual([ shortCode, []]);
expect(shortUrlTagsEdited).toHaveBeenCalledTimes(1);
expect(shortUrlTagsEdited.mock.calls[0]).toEqual([ shortCode, []]);
done();
});
});
@@ -146,6 +145,6 @@ describe('<EditTagsModal />', () => {
const cancelBtn = wrapper.find('.btn-link');
cancelBtn.simulate('click');
expect(toggle.callCount).toEqual(1);
expect(toggle).toHaveBeenCalledTimes(1);
});
});

View File

@@ -3,7 +3,6 @@ import { shallow } from 'enzyme';
import moment from 'moment';
import Moment from 'react-moment';
import { assoc, toString } from 'ramda';
import * as sinon from 'sinon';
import createShortUrlsRow from '../../../src/short-urls/helpers/ShortUrlsRow';
import ExternalLink from '../../../src/utils/ExternalLink';
import Tag from '../../../src/tags/helpers/Tag';
@@ -12,7 +11,7 @@ describe('<ShortUrlsRow />', () => {
let wrapper;
const mockFunction = () => '';
const ShortUrlsRowMenu = mockFunction;
const stateFlagTimeout = sinon.spy();
const stateFlagTimeout = jest.fn();
const colorGenerator = {
getColorForKey: mockFunction,
setColorForKey: mockFunction,
@@ -92,9 +91,9 @@ describe('<ShortUrlsRow />', () => {
const menu = col.find(ShortUrlsRowMenu);
expect(menu).toHaveLength(1);
expect(stateFlagTimeout.called).toEqual(false);
expect(stateFlagTimeout).not.toHaveBeenCalled();
menu.simulate('copyToClipboard');
expect(stateFlagTimeout.calledOnce).toEqual(true);
expect(stateFlagTimeout).toHaveBeenCalledTimes(1);
});
it('shows copy hint when state prop is true', () => {

View File

@@ -1,6 +1,5 @@
import React from 'react';
import { shallow } from 'enzyme';
import * as sinon from 'sinon';
import { ButtonDropdown, DropdownItem } from 'reactstrap';
import createShortUrlsRowMenu from '../../../src/short-urls/helpers/ShortUrlsRowMenu';
import PreviewModal from '../../../src/short-urls/helpers/PreviewModal';
@@ -10,7 +9,7 @@ describe('<ShortUrlsRowMenu />', () => {
let wrapper;
const DeleteShortUrlModal = () => '';
const EditTagsModal = () => '';
const onCopyToClipboard = sinon.spy();
const onCopyToClipboard = jest.fn();
const selectedServer = { id: 'abc123' };
const shortUrl = {
shortCode: 'abc123',

View File

@@ -1,4 +1,3 @@
import * as sinon from 'sinon';
import reducer, {
CREATE_SHORT_URL_START,
CREATE_SHORT_URL_ERROR,
@@ -48,12 +47,12 @@ describe('shortUrlCreationReducer', () => {
describe('createShortUrl', () => {
const createApiClientMock = (result) => ({
createShortUrl: sinon.fake.returns(result),
createShortUrl: jest.fn(() => result),
});
const dispatch = sinon.spy();
const dispatch = jest.fn();
const getState = () => ({});
afterEach(() => dispatch.resetHistory());
afterEach(() => dispatch.mockReset());
it('calls API on success', async () => {
const expectedDispatchCalls = 2;
@@ -62,12 +61,12 @@ describe('shortUrlCreationReducer', () => {
const dispatchable = createShortUrl(() => apiClientMock)({});
await dispatchable(dispatch, getState);
const [ firstDispatchCallArgs, secondDispatchCallArgs ] = dispatch.mock.calls;
expect(apiClientMock.createShortUrl.callCount).toEqual(1);
expect(dispatch.callCount).toEqual(expectedDispatchCalls);
expect(dispatch.getCall(0).args).toEqual([{ type: CREATE_SHORT_URL_START }]);
expect(dispatch.getCall(1).args).toEqual([{ type: CREATE_SHORT_URL, result }]);
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 }]);
});
it('throws on error', async () => {
@@ -81,12 +80,12 @@ describe('shortUrlCreationReducer', () => {
} catch (e) {
expect(e).toEqual(error);
}
const [ firstDispatchCallArgs, secondDispatchCallArgs ] = dispatch.mock.calls;
expect(apiClientMock.createShortUrl.callCount).toEqual(1);
expect(dispatch.callCount).toEqual(expectedDispatchCalls);
expect(dispatch.getCall(0).args).toEqual([{ type: CREATE_SHORT_URL_START }]);
expect(dispatch.getCall(1).args).toEqual([{ type: CREATE_SHORT_URL_ERROR }]);
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 }]);
});
});
});

View File

@@ -1,4 +1,3 @@
import * as sinon from 'sinon';
import reducer, {
DELETE_SHORT_URL, DELETE_SHORT_URL_ERROR,
DELETE_SHORT_URL_START,
@@ -58,36 +57,37 @@ describe('shortUrlDeletionReducer', () => {
});
describe('deleteShortUrl', () => {
const dispatch = sinon.spy();
const getState = sinon.fake.returns({ selectedServer: {} });
const dispatch = jest.fn();
const getState = jest.fn().mockReturnValue({ selectedServer: {} });
afterEach(() => {
dispatch.resetHistory();
getState.resetHistory();
dispatch.mockReset();
getState.mockClear();
});
it('dispatches proper actions if API client request succeeds', async () => {
const apiClientMock = {
deleteShortUrl: sinon.fake.resolves(''),
deleteShortUrl: jest.fn(() => ''),
};
const shortCode = 'abc123';
const expectedDispatchCalls = 2;
await deleteShortUrl(() => apiClientMock)(shortCode)(dispatch, getState);
const [ firstDispatchCallArgs, secondDispatchCallArgs ] = dispatch.mock.calls;
expect(dispatch.callCount).toEqual(expectedDispatchCalls);
expect(dispatch.getCall(0).args).toEqual([{ type: DELETE_SHORT_URL_START }]);
expect(dispatch.getCall(1).args).toEqual([{ type: DELETE_SHORT_URL, shortCode }]);
expect(dispatch).toHaveBeenCalledTimes(expectedDispatchCalls);
expect(firstDispatchCallArgs).toEqual([{ type: DELETE_SHORT_URL_START }]);
expect(secondDispatchCallArgs).toEqual([{ type: DELETE_SHORT_URL, shortCode }]);
expect(apiClientMock.deleteShortUrl.callCount).toEqual(1);
expect(apiClientMock.deleteShortUrl.getCall(0).args).toEqual([ shortCode ]);
expect(apiClientMock.deleteShortUrl).toHaveBeenCalledTimes(1);
expect(apiClientMock.deleteShortUrl.mock.calls[0]).toEqual([ shortCode ]);
});
it('dispatches proper actions if API client request fails', async () => {
const data = { foo: 'bar' };
const error = { response: { data } };
const apiClientMock = {
deleteShortUrl: sinon.fake.returns(Promise.reject(error)),
deleteShortUrl: jest.fn(() => Promise.reject(error)),
};
const shortCode = 'abc123';
const expectedDispatchCalls = 2;
@@ -97,13 +97,14 @@ describe('shortUrlDeletionReducer', () => {
} catch (e) {
expect(e).toEqual(error);
}
const [ firstDispatchCallArgs, secondDispatchCallArgs ] = dispatch.mock.calls;
expect(dispatch.callCount).toEqual(expectedDispatchCalls);
expect(dispatch.getCall(0).args).toEqual([{ type: DELETE_SHORT_URL_START }]);
expect(dispatch.getCall(1).args).toEqual([{ type: DELETE_SHORT_URL_ERROR, errorData: data }]);
expect(dispatch).toHaveBeenCalledTimes(expectedDispatchCalls);
expect(firstDispatchCallArgs).toEqual([{ type: DELETE_SHORT_URL_START }]);
expect(secondDispatchCallArgs).toEqual([{ type: DELETE_SHORT_URL_ERROR, errorData: data }]);
expect(apiClientMock.deleteShortUrl.callCount).toEqual(1);
expect(apiClientMock.deleteShortUrl.getCall(0).args).toEqual([ shortCode ]);
expect(apiClientMock.deleteShortUrl).toHaveBeenCalledTimes(1);
expect(apiClientMock.deleteShortUrl.mock.calls[0]).toEqual([ shortCode ]);
});
});
});

View File

@@ -1,4 +1,3 @@
import * as sinon from 'sinon';
import reducer, {
LIST_SHORT_URLS,
LIST_SHORT_URLS_ERROR,
@@ -73,42 +72,44 @@ describe('shortUrlsListReducer', () => {
});
describe('listShortUrls', () => {
const dispatch = sinon.spy();
const getState = sinon.fake.returns({ selectedServer: {} });
const dispatch = jest.fn();
const getState = jest.fn().mockReturnValue({ selectedServer: {} });
afterEach(() => {
dispatch.resetHistory();
getState.resetHistory();
dispatch.mockReset();
getState.mockClear();
});
it('dispatches proper actions if API client request succeeds', async () => {
const apiClientMock = {
listShortUrls: sinon.fake.resolves([]),
listShortUrls: jest.fn().mockResolvedValue([]),
};
const expectedDispatchCalls = 2;
await listShortUrls(() => apiClientMock)()(dispatch, getState);
const [ firstDispatchCallArgs, secondDispatchCallArgs ] = dispatch.mock.calls;
expect(dispatch.callCount).toEqual(expectedDispatchCalls);
expect(dispatch.getCall(0).args).toEqual([{ type: LIST_SHORT_URLS_START }]);
expect(dispatch.getCall(1).args).toEqual([{ type: LIST_SHORT_URLS, shortUrls: [], params: {} }]);
expect(dispatch).toHaveBeenCalledTimes(expectedDispatchCalls);
expect(firstDispatchCallArgs).toEqual([{ type: LIST_SHORT_URLS_START }]);
expect(secondDispatchCallArgs).toEqual([{ type: LIST_SHORT_URLS, shortUrls: [], params: {} }]);
expect(apiClientMock.listShortUrls.callCount).toEqual(1);
expect(apiClientMock.listShortUrls).toHaveBeenCalledTimes(1);
});
it('dispatches proper actions if API client request fails', async () => {
const apiClientMock = {
listShortUrls: sinon.fake.rejects(),
listShortUrls: jest.fn().mockRejectedValue(),
};
const expectedDispatchCalls = 2;
await listShortUrls(() => apiClientMock)()(dispatch, getState);
const [ firstDispatchCallArgs, secondDispatchCallArgs ] = dispatch.mock.calls;
expect(dispatch.callCount).toEqual(expectedDispatchCalls);
expect(dispatch.getCall(0).args).toEqual([{ type: LIST_SHORT_URLS_START }]);
expect(dispatch.getCall(1).args).toEqual([{ type: LIST_SHORT_URLS_ERROR, params: {} }]);
expect(dispatch).toHaveBeenCalledTimes(expectedDispatchCalls);
expect(firstDispatchCallArgs).toEqual([{ type: LIST_SHORT_URLS_START }]);
expect(secondDispatchCallArgs).toEqual([{ type: LIST_SHORT_URLS_ERROR, params: {} }]);
expect(apiClientMock.listShortUrls.callCount).toEqual(1);
expect(apiClientMock.listShortUrls).toHaveBeenCalledTimes(1);
});
});
});