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

@@ -2,7 +2,6 @@ import React from 'react';
import { shallow } from 'enzyme';
import { identity } from 'ramda';
import { Card } from 'reactstrap';
import * as sinon from 'sinon';
import createShortUrlVisits from '../../src/visits/ShortUrlVisits';
import MutedMessage from '../../src/utils/MuttedMessage';
import GraphCard from '../../src/visits/GraphCard';
@@ -14,7 +13,7 @@ describe('<ShortUrlVisits />', () => {
const processStatsFromVisits = () => (
{ os: {}, browsers: {}, referrers: {}, countries: {}, cities: {}, citiesForMap: {} }
);
const getShortUrlVisitsMock = sinon.spy();
const getShortUrlVisitsMock = jest.fn();
const match = {
params: { shortCode: 'abc123' },
};
@@ -37,11 +36,8 @@ describe('<ShortUrlVisits />', () => {
};
afterEach(() => {
getShortUrlVisitsMock.resetHistory();
if (wrapper) {
wrapper.unmount();
}
getShortUrlVisitsMock.mockReset();
wrapper && wrapper.unmount();
});
it('renders a preloader when visits are loading', () => {
@@ -88,13 +84,12 @@ describe('<ShortUrlVisits />', () => {
it('reloads visits when selected dates change', () => {
const wrapper = createComponent({ loading: false, error: false, visits: [{}, {}, {}] });
const dateInput = wrapper.find(DateInput).first();
const expectedGetShortUrlVisitsCalls = 4;
dateInput.simulate('change', '2016-01-01T00:00:00+01:00');
dateInput.simulate('change', '2016-01-02T00:00:00+01:00');
dateInput.simulate('change', '2016-01-03T00:00:00+01:00');
expect(getShortUrlVisitsMock.callCount).toEqual(expectedGetShortUrlVisitsCalls);
expect(getShortUrlVisitsMock).toHaveBeenCalledTimes(4);
expect(wrapper.state('startDate')).toEqual('2016-01-03T00:00:00+01:00');
});

View File

@@ -1,4 +1,3 @@
import * as sinon from 'sinon';
import reducer, {
getShortUrlDetail,
GET_SHORT_URL_DETAIL_START,
@@ -36,49 +35,34 @@ describe('shortUrlDetailReducer', () => {
describe('getShortUrlDetail', () => {
const buildApiClientMock = (returned) => ({
getShortUrl: sinon.fake.returns(returned),
getShortUrl: jest.fn(() => returned),
});
const dispatchMock = sinon.spy();
const dispatchMock = jest.fn();
const getState = () => ({});
beforeEach(() => dispatchMock.resetHistory());
beforeEach(() => dispatchMock.mockReset());
it('dispatches start and error when promise is rejected', async () => {
const ShlinkApiClient = buildApiClientMock(Promise.reject());
const expectedDispatchCalls = 2;
await getShortUrlDetail(() => ShlinkApiClient)('abc123')(dispatchMock, getState);
const [ firstCallArg ] = dispatchMock.getCall(0).args;
const { type: firstCallType } = firstCallArg;
const [ secondCallArg ] = dispatchMock.getCall(1).args;
const { type: secondCallType } = secondCallArg;
expect(dispatchMock.callCount).toEqual(expectedDispatchCalls);
expect(ShlinkApiClient.getShortUrl.callCount).toEqual(1);
expect(firstCallType).toEqual(GET_SHORT_URL_DETAIL_START);
expect(secondCallType).toEqual(GET_SHORT_URL_DETAIL_ERROR);
expect(dispatchMock).toHaveBeenCalledTimes(2);
expect(dispatchMock).toHaveBeenNthCalledWith(1, { type: GET_SHORT_URL_DETAIL_START });
expect(dispatchMock).toHaveBeenNthCalledWith(2, { type: GET_SHORT_URL_DETAIL_ERROR });
expect(ShlinkApiClient.getShortUrl).toHaveBeenCalledTimes(1);
});
it('dispatches start and success when promise is resolved', async () => {
const resolvedShortUrl = { longUrl: 'foo', shortCode: 'bar' };
const ShlinkApiClient = buildApiClientMock(Promise.resolve(resolvedShortUrl));
const expectedDispatchCalls = 2;
await getShortUrlDetail(() => ShlinkApiClient)('abc123')(dispatchMock, getState);
const [ firstCallArg ] = dispatchMock.getCall(0).args;
const { type: firstCallType } = firstCallArg;
const [ secondCallArg ] = dispatchMock.getCall(1).args;
const { type: secondCallType, shortUrl } = secondCallArg;
expect(dispatchMock.callCount).toEqual(expectedDispatchCalls);
expect(ShlinkApiClient.getShortUrl.callCount).toEqual(1);
expect(firstCallType).toEqual(GET_SHORT_URL_DETAIL_START);
expect(secondCallType).toEqual(GET_SHORT_URL_DETAIL);
expect(shortUrl).toEqual(resolvedShortUrl);
expect(dispatchMock).toHaveBeenCalledTimes(2);
expect(dispatchMock).toHaveBeenNthCalledWith(1, { type: GET_SHORT_URL_DETAIL_START });
expect(dispatchMock).toHaveBeenNthCalledWith(2, { type: GET_SHORT_URL_DETAIL, shortUrl: resolvedShortUrl });
expect(ShlinkApiClient.getShortUrl).toHaveBeenCalledTimes(1);
});
});
});

View File

@@ -1,4 +1,3 @@
import * as sinon from 'sinon';
import reducer, {
getShortUrlVisits,
cancelGetShortUrlVisits,
@@ -53,57 +52,42 @@ describe('shortUrlVisitsReducer', () => {
describe('getShortUrlVisits', () => {
const buildApiClientMock = (returned) => ({
getShortUrlVisits: typeof returned === 'function' ? sinon.fake(returned) : sinon.fake.returns(returned),
getShortUrlVisits: jest.fn(typeof returned === 'function' ? returned : () => returned),
});
const dispatchMock = sinon.spy();
const dispatchMock = jest.fn();
const getState = () => ({
shortUrlVisits: { cancelVisits: false },
});
beforeEach(() => dispatchMock.resetHistory());
beforeEach(() => dispatchMock.mockReset());
it('dispatches start and error when promise is rejected', async () => {
const ShlinkApiClient = buildApiClientMock(Promise.reject());
const expectedDispatchCalls = 2;
await getShortUrlVisits(() => ShlinkApiClient)('abc123')(dispatchMock, getState);
const [ firstCallArg ] = dispatchMock.getCall(0).args;
const { type: firstCallType } = firstCallArg;
const [ secondCallArg ] = dispatchMock.getCall(1).args;
const { type: secondCallType } = secondCallArg;
expect(dispatchMock.callCount).toEqual(expectedDispatchCalls);
expect(ShlinkApiClient.getShortUrlVisits.callCount).toEqual(1);
expect(firstCallType).toEqual(GET_SHORT_URL_VISITS_START);
expect(secondCallType).toEqual(GET_SHORT_URL_VISITS_ERROR);
expect(dispatchMock).toHaveBeenCalledTimes(2);
expect(dispatchMock).toHaveBeenNthCalledWith(1, { type: GET_SHORT_URL_VISITS_START });
expect(dispatchMock).toHaveBeenNthCalledWith(2, { type: GET_SHORT_URL_VISITS_ERROR });
expect(ShlinkApiClient.getShortUrlVisits).toHaveBeenCalledTimes(1);
});
it('dispatches start and success when promise is resolved', async () => {
const resolvedVisits = [{}, {}];
const visits = [{}, {}];
const ShlinkApiClient = buildApiClientMock(Promise.resolve({
data: resolvedVisits,
data: visits,
pagination: {
currentPage: 1,
pagesCount: 1,
},
}));
const expectedDispatchCalls = 2;
await getShortUrlVisits(() => ShlinkApiClient)('abc123')(dispatchMock, getState);
const [ firstCallArg ] = dispatchMock.getCall(0).args;
const { type: firstCallType } = firstCallArg;
const [ secondCallArg ] = dispatchMock.getCall(1).args;
const { type: secondCallType, visits } = secondCallArg;
expect(dispatchMock.callCount).toEqual(expectedDispatchCalls);
expect(ShlinkApiClient.getShortUrlVisits.callCount).toEqual(1);
expect(firstCallType).toEqual(GET_SHORT_URL_VISITS_START);
expect(secondCallType).toEqual(GET_SHORT_URL_VISITS);
expect(visits).toEqual(resolvedVisits);
expect(dispatchMock).toHaveBeenCalledTimes(2);
expect(dispatchMock).toHaveBeenNthCalledWith(1, { type: GET_SHORT_URL_VISITS_START });
expect(dispatchMock).toHaveBeenNthCalledWith(2, { type: GET_SHORT_URL_VISITS, visits });
expect(ShlinkApiClient.getShortUrlVisits).toHaveBeenCalledTimes(1);
});
it('performs multiple API requests when response contains more pages', async () => {
@@ -119,11 +103,10 @@ describe('shortUrlVisitsReducer', () => {
await getShortUrlVisits(() => ShlinkApiClient)('abc123')(dispatchMock, getState);
const [ secondCallArg ] = dispatchMock.getCall(1).args;
const { visits } = secondCallArg;
expect(ShlinkApiClient.getShortUrlVisits.callCount).toEqual(expectedRequests);
expect(visits).toEqual([{}, {}, {}, {}, {}, {}]);
expect(ShlinkApiClient.getShortUrlVisits).toHaveBeenCalledTimes(expectedRequests);
expect(dispatchMock).toHaveBeenNthCalledWith(2, expect.objectContaining({
visits: [{}, {}, {}, {}, {}, {}],
}));
});
});