From f8eb5fb022bce0dcfa839b1252c1f23c9e030c39 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 12 Aug 2018 18:49:57 +0200 Subject: [PATCH] Creates shortUrlsListParams reducer test --- .../reducers/shortUrlCreationResult.js | 4 ++- src/short-urls/reducers/shortUrlVisits.js | 4 ++- src/short-urls/reducers/shortUrlsList.js | 4 ++- .../reducers/shortUrlsListParams.test.js | 32 +++++++++++++++++++ 4 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 test/shortUrls/reducers/shortUrlsListParams.test.js diff --git a/src/short-urls/reducers/shortUrlCreationResult.js b/src/short-urls/reducers/shortUrlCreationResult.js index 88675f6c..c74b97d5 100644 --- a/src/short-urls/reducers/shortUrlCreationResult.js +++ b/src/short-urls/reducers/shortUrlCreationResult.js @@ -1,4 +1,5 @@ import ShlinkApiClient from '../../api/ShlinkApiClient'; +import { curry } from 'ramda'; const CREATE_SHORT_URL_START = 'shlink/createShortUrl/CREATE_SHORT_URL_START'; const CREATE_SHORT_URL_ERROR = 'shlink/createShortUrl/CREATE_SHORT_URL_ERROR'; @@ -37,7 +38,7 @@ export default function reducer(state = defaultState, action) { } } -export const createShortUrl = data => async dispatch => { +export const _createShortUrl = (ShlinkApiClient, data) => async dispatch => { dispatch({ type: CREATE_SHORT_URL_START }); try { @@ -47,5 +48,6 @@ export const createShortUrl = data => async dispatch => { dispatch({ type: CREATE_SHORT_URL_ERROR }); } }; +export const createShortUrl = curry(_createShortUrl)(ShlinkApiClient); export const resetCreateShortUrl = () => ({ type: RESET_CREATE_SHORT_URL }); diff --git a/src/short-urls/reducers/shortUrlVisits.js b/src/short-urls/reducers/shortUrlVisits.js index 0f9c8ed4..a247fa2a 100644 --- a/src/short-urls/reducers/shortUrlVisits.js +++ b/src/short-urls/reducers/shortUrlVisits.js @@ -1,4 +1,5 @@ import ShlinkApiClient from '../../api/ShlinkApiClient'; +import { curry } from 'ramda'; const GET_SHORT_URL_VISITS_START = 'shlink/shortUrlVisits/GET_SHORT_URL_VISITS_START'; const GET_SHORT_URL_VISITS_ERROR = 'shlink/shortUrlVisits/GET_SHORT_URL_VISITS_ERROR'; @@ -36,7 +37,7 @@ export default function dispatch (state = initialState, action) { } } -export const getShortUrlVisits = (shortCode, dates) => dispatch => { +export const _getShortUrlVisits = (ShlinkApiClient, shortCode, dates) => dispatch => { dispatch({ type: GET_SHORT_URL_VISITS_START }); Promise.all([ @@ -46,3 +47,4 @@ export const getShortUrlVisits = (shortCode, dates) => dispatch => { .then(([visits, shortUrl]) => dispatch({ visits, shortUrl, type: GET_SHORT_URL_VISITS })) .catch(() => dispatch({ type: GET_SHORT_URL_VISITS_ERROR })); }; +export const getShortUrlVisits = curry(_getShortUrlVisits)(ShlinkApiClient); diff --git a/src/short-urls/reducers/shortUrlsList.js b/src/short-urls/reducers/shortUrlsList.js index f9771eae..728d53b0 100644 --- a/src/short-urls/reducers/shortUrlsList.js +++ b/src/short-urls/reducers/shortUrlsList.js @@ -1,4 +1,5 @@ import ShlinkApiClient from '../../api/ShlinkApiClient'; +import { curry } from 'ramda'; const LIST_SHORT_URLS_START = 'shlink/shortUrlsList/LIST_SHORT_URLS_START'; const LIST_SHORT_URLS_ERROR = 'shlink/shortUrlsList/LIST_SHORT_URLS_ERROR'; @@ -30,7 +31,7 @@ export default function reducer(state = initialState, action) { } } -export const listShortUrls = (params = {}) => async dispatch => { +export const _listShortUrls = (ShlinkApiClient, params = {}) => async dispatch => { dispatch({ type: LIST_SHORT_URLS_START }); try { @@ -40,3 +41,4 @@ export const listShortUrls = (params = {}) => async dispatch => { dispatch({ type: LIST_SHORT_URLS_ERROR, params }); } }; +export const listShortUrls = curry(_listShortUrls)(ShlinkApiClient); diff --git a/test/shortUrls/reducers/shortUrlsListParams.test.js b/test/shortUrls/reducers/shortUrlsListParams.test.js new file mode 100644 index 00000000..f22507d3 --- /dev/null +++ b/test/shortUrls/reducers/shortUrlsListParams.test.js @@ -0,0 +1,32 @@ +import reduce, { + RESET_SHORT_URL_PARAMS, + resetShortUrlParams, +} from '../../../src/short-urls/reducers/shortUrlsListParams'; +import { LIST_SHORT_URLS } from '../../../src/short-urls/reducers/shortUrlsList'; + +describe('shortUrlsListParamsReducer', () => { + describe('reduce', () => { + const defaultState = { page: '1' }; + + it('returns default value when action is anknown', () => + expect(reduce(defaultState, { type: 'unknown' })).toEqual(defaultState) + ); + + it('returns params when action is LIST_SHORT_URLS', () => + expect(reduce(defaultState, { type: LIST_SHORT_URLS, params: { searchTerm: 'foo' } })).toEqual({ + ...defaultState, + searchTerm: 'foo' + }) + ); + + it('returns default value when action is RESET_SHORT_URL_PARAMS', () => + expect(reduce(defaultState, { type: RESET_SHORT_URL_PARAMS })).toEqual(defaultState) + ); + }); + + describe('resetShortUrlParams', () => { + it('returns proper action', () => + expect(resetShortUrlParams()).toEqual({ type: RESET_SHORT_URL_PARAMS }) + ); + }); +});