diff --git a/__tests__/__snapshots__/index.js.snap b/__tests__/__snapshots__/index.js.snap index 3c3af962..151af59b 100644 --- a/__tests__/__snapshots__/index.js.snap +++ b/__tests__/__snapshots__/index.js.snap @@ -54,6 +54,8 @@ Object { "getLookerDone": [Function], }, "lookup": Object { + "getCountriesDone": [Function], + "getCountriesInit": [Function], "getSkillTagsDone": [Function], "getSkillTagsInit": [Function], }, @@ -201,14 +203,17 @@ Object { "countReset": [Function], "debug": [Function], "dir": [Function], + "dirxml": [Function], "error": [Function], "group": [Function], "groupCollapsed": [Function], "groupEnd": [Function], "info": [Function], "log": [Function], + "table": [Function], "time": [Function], "timeEnd": [Function], + "timeLog": [Function], "trace": [Function], "warn": [Function], }, diff --git a/__tests__/actions/__snapshots__/lookup.js.snap b/__tests__/actions/__snapshots__/lookup.js.snap index c29d03c4..8940c5c6 100644 --- a/__tests__/actions/__snapshots__/lookup.js.snap +++ b/__tests__/actions/__snapshots__/lookup.js.snap @@ -3,12 +3,32 @@ exports[`Module exports 1`] = ` Object { "lookup": Object { + "getCountriesDone": [Function], + "getCountriesInit": [Function], "getSkillTagsDone": [Function], "getSkillTagsInit": [Function], }, } `; +exports[`lookup.getCountriesDone 1`] = ` +Object { + "payload": Array [ + Object { + "country": "Afghanistan", + "countryCode": "AFG", + }, + ], + "type": "LOOKUP/GET_COUNTRIES_DONE", +} +`; + +exports[`lookup.getCountriesInit 1`] = ` +Object { + "type": "LOOKUP/GET_COUNTRIES_INIT", +} +`; + exports[`lookup.getSkillTagsDone 1`] = ` Object { "payload": Array [ diff --git a/__tests__/actions/lookup.js b/__tests__/actions/lookup.js index 9bf3a83e..55da0d49 100644 --- a/__tests__/actions/lookup.js +++ b/__tests__/actions/lookup.js @@ -10,9 +10,15 @@ const tag = { status: 'APPROVED', }; +const country = { + country: 'Afghanistan', + countryCode: 'AFG', +}; + // Mock services const mockLookupService = { getTags: jest.fn().mockReturnValue(Promise.resolve([tag])), + getCountries: jest.fn().mockReturnValue(Promise.resolve([country])), }; LookupService.getService = jest.fn().mockReturnValue(mockLookupService); @@ -28,3 +34,14 @@ test('lookup.getSkillTagsDone', async () => { expect(actionResult).toMatchSnapshot(); expect(mockLookupService.getTags).toBeCalled(); }); + +test('lookup.getCountriesInit', async () => { + const actionResult = actions.lookup.getCountriesInit(); + expect(actionResult).toMatchSnapshot(); +}); + +test('lookup.getCountriesDone', async () => { + const actionResult = await redux.resolveAction(actions.lookup.getCountriesDone()); + expect(actionResult).toMatchSnapshot(); + expect(mockLookupService.getCountries).toBeCalled(); +}); diff --git a/__tests__/reducers/__snapshots__/lookup.js.snap b/__tests__/reducers/__snapshots__/lookup.js.snap index 3f923ead..a6e9710e 100644 --- a/__tests__/reducers/__snapshots__/lookup.js.snap +++ b/__tests__/reducers/__snapshots__/lookup.js.snap @@ -1,7 +1,50 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`Default reducer Get countries 1`] = ` +Object { + "countries": Array [ + Object { + "country": "Afghanistan", + "countryCode": "AFG", + }, + ], + "loadingCountriesError": false, + "loadingSkillTagsError": true, + "skillTags": Array [ + Object { + "domain": "SKILLS", + "id": 251, + "name": "Jekyll", + "status": "APPROVED", + }, + ], +} +`; + +exports[`Default reducer Get countries error 1`] = ` +Object { + "countries": Array [ + Object { + "country": "Afghanistan", + "countryCode": "AFG", + }, + ], + "loadingCountriesError": true, + "loadingSkillTagsError": true, + "skillTags": Array [ + Object { + "domain": "SKILLS", + "id": 251, + "name": "Jekyll", + "status": "APPROVED", + }, + ], +} +`; + exports[`Default reducer Get skill tags 1`] = ` Object { + "countries": Array [], "loadingSkillTagsError": false, "skillTags": Array [ Object { @@ -16,6 +59,7 @@ Object { exports[`Default reducer Get skill tags error 1`] = ` Object { + "countries": Array [], "loadingSkillTagsError": true, "skillTags": Array [ Object { @@ -30,12 +74,56 @@ Object { exports[`Default reducer Initial state 1`] = ` Object { + "countries": Array [], "skillTags": Array [], } `; +exports[`Factory without server side rendering Get countries 1`] = ` +Object { + "countries": Array [ + Object { + "country": "Afghanistan", + "countryCode": "AFG", + }, + ], + "loadingCountriesError": false, + "loadingSkillTagsError": true, + "skillTags": Array [ + Object { + "domain": "SKILLS", + "id": 251, + "name": "Jekyll", + "status": "APPROVED", + }, + ], +} +`; + +exports[`Factory without server side rendering Get countries error 1`] = ` +Object { + "countries": Array [ + Object { + "country": "Afghanistan", + "countryCode": "AFG", + }, + ], + "loadingCountriesError": true, + "loadingSkillTagsError": true, + "skillTags": Array [ + Object { + "domain": "SKILLS", + "id": 251, + "name": "Jekyll", + "status": "APPROVED", + }, + ], +} +`; + exports[`Factory without server side rendering Get skill tags 1`] = ` Object { + "countries": Array [], "loadingSkillTagsError": false, "skillTags": Array [ Object { @@ -50,6 +138,7 @@ Object { exports[`Factory without server side rendering Get skill tags error 1`] = ` Object { + "countries": Array [], "loadingSkillTagsError": true, "skillTags": Array [ Object { @@ -64,6 +153,7 @@ Object { exports[`Factory without server side rendering Initial state 1`] = ` Object { + "countries": Array [], "skillTags": Array [], } `; diff --git a/__tests__/reducers/lookup.js b/__tests__/reducers/lookup.js index c7baeab9..e7ae3c96 100644 --- a/__tests__/reducers/lookup.js +++ b/__tests__/reducers/lookup.js @@ -7,11 +7,19 @@ const tag = { status: 'APPROVED', }; +const country = { + country: 'Afghanistan', + countryCode: 'AFG', +}; + const mockActions = { lookup: { getSkillTagsInit: mockAction('LOOKUP/GET_SKILL_TAGS_INIT'), getSkillTagsDone: mockAction('LOOKUP/GET_SKILL_TAGS_DONE', [tag]), getSkillTagsDoneError: mockAction('LOOKUP/GET_SKILL_TAGS_DONE', null, 'Unknown error'), + getCountriesInit: mockAction('LOOKUP/GET_COUNTRIES_INIT'), + getCountriesDone: mockAction('LOOKUP/GET_COUNTRIES_DONE', [country]), + getCountriesDoneError: mockAction('LOOKUP/GET_COUNTRIES_DONE', null, 'Unknown error'), }, }; jest.setMock(require.resolve('actions/lookup'), mockActions); @@ -38,6 +46,17 @@ function testReducer() { state = reducer(state, mockActions.lookup.getSkillTagsDoneError()); expect(state).toMatchSnapshot(); }); + + test('Get countries', () => { + state = reducer(state, mockActions.lookup.getCountriesInit()); + state = reducer(state, mockActions.lookup.getCountriesDone()); + expect(state).toMatchSnapshot(); + }); + + test('Get countries error', () => { + state = reducer(state, mockActions.lookup.getCountriesDoneError()); + expect(state).toMatchSnapshot(); + }); } describe('Default reducer', () => { diff --git a/docs/actions.lookup.md b/docs/actions.lookup.md index 1db62d1e..7d0b385d 100644 --- a/docs/actions.lookup.md +++ b/docs/actions.lookup.md @@ -7,6 +7,8 @@ Actions related to lookup data. * [actions.lookup](#module_actions.lookup) * [.getSkillTagsInit()](#module_actions.lookup.getSkillTagsInit) ⇒ <code>Action</code> * [.getSkillTagsDone()](#module_actions.lookup.getSkillTagsDone) ⇒ <code>Action</code> + * [.getCountriesInit()](#module_actions.lookup.getCountriesInit) ⇒ <code>Action</code> + * [.getCountriesDone()](#module_actions.lookup.getCountriesDone) ⇒ <code>Action</code> <a name="module_actions.lookup.getSkillTagsInit"></a> @@ -20,3 +22,9 @@ Creates an action that signals beginning of getting all skill tags. Creates an action that gets all skill tags. **Kind**: static method of [<code>actions.lookup</code>](#module_actions.lookup) +<a name="module_actions.lookup.getCountriesDone"></a> + +### actions.lookup.getCountriesDone() ⇒ <code>Action</code> +Creates an action that gets all countries. + +**Kind**: static method of [<code>actions.lookup</code>](#module_actions.lookup) diff --git a/docs/reducers.lookup.md b/docs/reducers.lookup.md index 340e2d25..1a08f131 100644 --- a/docs/reducers.lookup.md +++ b/docs/reducers.lookup.md @@ -17,6 +17,7 @@ State segment managed by this reducer has the following structure: * [.factory()](#module_reducers.lookup.factory) ⇒ <code>Promise</code> * _inner_ * [~onGetSkillTagsDone(state, action)](#module_reducers.lookup..onGetSkillTagsDone) ⇒ <code>Object</code> + * [~onGetCountriesDone(state, action)](#module_reducers.lookup..onGetCountriesDone) ⇒ <code>Object</code> * [~create(initialState)](#module_reducers.lookup..create) ⇒ <code>function</code> <a name="module_reducers.lookup.default"></a> @@ -37,6 +38,13 @@ Factory which creates a new reducer. ### reducers.lookup~onGetSkillTagsDone(state, action) ⇒ <code>Object</code> Handles LOOKUP/GET_SKILL_TAGS_DONE action. +**Kind**: static method of [<code>reducers.lookup</code>](#module_reducers.lookup) +**Resolves**: <code>Function(state, action): state</code> New reducer. +<a name="module_reducers.lookup..onGetCountriesDone"></a> + +### reducers.lookup~onGetCountriesDone(state, action) ⇒ <code>Object</code> +Handles LOOKUP/GET_COUNTRIES_DONE action. + **Kind**: inner method of [<code>reducers.lookup</code>](#module_reducers.lookup) **Returns**: <code>Object</code> - New state diff --git a/src/actions/lookup.js b/src/actions/lookup.js index b37ed92e..ec20cc9f 100644 --- a/src/actions/lookup.js +++ b/src/actions/lookup.js @@ -26,9 +26,27 @@ function getSkillTagsDone() { return getService().getTags(params); } +/** + * @static + * @desc Creates an action that signals beginning of getting all countries. + * @return {Action} + */ +function getCountriesInit() {} + +/** + * @static + * @desc Creates an action that gets all countries. + * @return {Action} + */ +function getCountriesDone() { + return getService().getCountries(); +} + export default createActions({ LOOKUP: { GET_SKILL_TAGS_INIT: getSkillTagsInit, GET_SKILL_TAGS_DONE: getSkillTagsDone, + GET_COUNTRIES_INIT: getCountriesInit, + GET_COUNTRIES_DONE: getCountriesDone, }, }); diff --git a/src/reducers/lookup.js b/src/reducers/lookup.js index a2cdff2a..db53ddf1 100644 --- a/src/reducers/lookup.js +++ b/src/reducers/lookup.js @@ -29,6 +29,25 @@ function onGetSkillTagsDone(state, { payload, error }) { }); } +/** + * Handles LOOKUP/GET_COUNTRIES_DONE action. + * @param {Object} state + * @param {Object} action Payload will be JSON from api call + * @return {Object} New state + */ +function onGetCountriesDone(state, { payload, error }) { + if (error) { + logger.error('Failed to get countries', payload); + return { ...state, loadingCountriesError: true }; + } + + return ({ + ...state, + loadingCountriesError: false, + countries: payload, + }); +} + /** * Creates a new Lookup reducer with the specified initial state. * @param {Object} initialState Optional. Initial state. @@ -39,8 +58,11 @@ function create(initialState = {}) { return handleActions({ [a.getSkillTagsInit]: state => state, [a.getSkillTagsDone]: onGetSkillTagsDone, + [a.getCountriesInit]: state => state, + [a.getCountriesDone]: onGetCountriesDone, }, _.defaults(initialState, { skillTags: [], + countries: [], })); } diff --git a/src/services/lookup.js b/src/services/lookup.js index 2b98bf8b..76c6a792 100644 --- a/src/services/lookup.js +++ b/src/services/lookup.js @@ -27,6 +27,16 @@ class LookupService { const res = await this.private.api.get(`/tags/?${qs.stringify(params)}`); return getApiResponsePayload(res); } + + /** + * Gets countries. + * @param {Object} params Parameters + * @return {Promise} Resolves to the countries. + */ + async getCountries() { + const res = await this.private.api.get('/members/lookup/countries'); + return getApiResponsePayload(res); + } } let lastInstance = null;