Created shortUrlDetail reducer test

This commit is contained in:
Alejandro Celaya 2018-09-07 20:41:21 +02:00
parent 3e63734e2b
commit bbce53ade6
4 changed files with 97 additions and 4 deletions

View file

@ -2,6 +2,7 @@
"extends": [
"adidas-env/browser",
"adidas-env/module",
"adidas-env/node",
"adidas-es6",
"adidas-babel",
"adidas-react"

View file

@ -4,9 +4,9 @@ import shlinkApiClient from '../../api/ShlinkApiClient';
import { shortUrlType } from '../../short-urls/reducers/shortUrlsList';
/* eslint-disable padding-line-between-statements, newline-after-var */
const GET_SHORT_URL_DETAIL_START = 'shlink/shortUrlDetail/GET_SHORT_URL_DETAIL_START';
const GET_SHORT_URL_DETAIL_ERROR = 'shlink/shortUrlDetail/GET_SHORT_URL_DETAIL_ERROR';
const GET_SHORT_URL_DETAIL = 'shlink/shortUrlDetail/GET_SHORT_URL_DETAIL';
export const GET_SHORT_URL_DETAIL_START = 'shlink/shortUrlDetail/GET_SHORT_URL_DETAIL_START';
export const GET_SHORT_URL_DETAIL_ERROR = 'shlink/shortUrlDetail/GET_SHORT_URL_DETAIL_ERROR';
export const GET_SHORT_URL_DETAIL = 'shlink/shortUrlDetail/GET_SHORT_URL_DETAIL';
/* eslint-enable padding-line-between-statements, newline-after-var */
export const shortUrlDetailType = PropTypes.shape({

View file

@ -5,7 +5,7 @@ import reducer, {
import { LIST_SHORT_URLS } from '../../../src/short-urls/reducers/shortUrlsList';
describe('shortUrlsListParamsReducer', () => {
describe('reducerr', () => {
describe('reducer', () => {
const defaultState = { page: '1' };
it('returns default value when action is unknown', () =>

View file

@ -0,0 +1,92 @@
import * as sinon from 'sinon';
import reducer, {
_getShortUrlDetail,
GET_SHORT_URL_DETAIL_START,
GET_SHORT_URL_DETAIL_ERROR,
GET_SHORT_URL_DETAIL,
} from '../../../src/visits/reducers/shortUrlDetail';
describe('shortUrlDetailReducer', () => {
describe('reducer', () => {
it('returns loading on GET_SHORT_URL_DETAIL_START', () => {
const state = reducer({ loading: false }, { type: GET_SHORT_URL_DETAIL_START });
const { loading } = state;
expect(loading).toEqual(true);
});
it('stops loading and returns error on GET_SHORT_URL_DETAIL_ERROR', () => {
const state = reducer({ loading: true, error: false }, { type: GET_SHORT_URL_DETAIL_ERROR });
const { loading, error } = state;
expect(loading).toEqual(false);
expect(error).toEqual(true);
});
it('return short URL on GET_SHORT_URL_DETAIL', () => {
const actionShortUrl = { longUrl: 'foo', shortCode: 'bar' };
const state = reducer({ loading: true, error: false }, { type: GET_SHORT_URL_DETAIL, shortUrl: actionShortUrl });
const { loading, error, shortUrl } = state;
expect(loading).toEqual(false);
expect(error).toEqual(false);
expect(shortUrl).toEqual(actionShortUrl);
});
it('returns default state on unknown action', () => {
const defaultState = {
shortUrl: {},
loading: false,
error: false,
};
const state = reducer(defaultState, { type: 'unknown' });
expect(state).toEqual(defaultState);
});
});
describe('getShortUrlDetail', () => {
const buildApiClientMock = (returned) => ({
getShortUrl: sinon.fake.returns(returned),
});
const dispatchMock = sinon.spy();
beforeEach(() => dispatchMock.resetHistory());
it('dispatches start and error when promise is rejected', async () => {
const ShlinkApiClient = buildApiClientMock(Promise.reject());
const expectedDispatchCalls = 2;
await _getShortUrlDetail(ShlinkApiClient, 'abc123')(dispatchMock);
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(firstCallType).toEqual(GET_SHORT_URL_DETAIL_START);
expect(secondCallType).toEqual(GET_SHORT_URL_DETAIL_ERROR);
});
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);
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(firstCallType).toEqual(GET_SHORT_URL_DETAIL_START);
expect(secondCallType).toEqual(GET_SHORT_URL_DETAIL);
expect(shortUrl).toEqual(resolvedShortUrl);
});
});
});