Skip to content

Commit b65a469

Browse files
Merge pull request topcoder-platform#47 from suppermancool/code-30086294
code 30086294
2 parents b9919c5 + e13c3e2 commit b65a469

File tree

10 files changed

+217
-0
lines changed

10 files changed

+217
-0
lines changed

__tests__/__snapshots__/index.js.snap

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ Object {
5454
"getLookerDone": [Function],
5555
},
5656
"lookup": Object {
57+
"getCountriesDone": [Function],
58+
"getCountriesInit": [Function],
5759
"getSkillTagsDone": [Function],
5860
"getSkillTagsInit": [Function],
5961
},
@@ -201,14 +203,17 @@ Object {
201203
"countReset": [Function],
202204
"debug": [Function],
203205
"dir": [Function],
206+
"dirxml": [Function],
204207
"error": [Function],
205208
"group": [Function],
206209
"groupCollapsed": [Function],
207210
"groupEnd": [Function],
208211
"info": [Function],
209212
"log": [Function],
213+
"table": [Function],
210214
"time": [Function],
211215
"timeEnd": [Function],
216+
"timeLog": [Function],
212217
"trace": [Function],
213218
"warn": [Function],
214219
},

__tests__/actions/__snapshots__/lookup.js.snap

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,32 @@
33
exports[`Module exports 1`] = `
44
Object {
55
"lookup": Object {
6+
"getCountriesDone": [Function],
7+
"getCountriesInit": [Function],
68
"getSkillTagsDone": [Function],
79
"getSkillTagsInit": [Function],
810
},
911
}
1012
`;
1113

14+
exports[`lookup.getCountriesDone 1`] = `
15+
Object {
16+
"payload": Array [
17+
Object {
18+
"country": "Afghanistan",
19+
"countryCode": "AFG",
20+
},
21+
],
22+
"type": "LOOKUP/GET_COUNTRIES_DONE",
23+
}
24+
`;
25+
26+
exports[`lookup.getCountriesInit 1`] = `
27+
Object {
28+
"type": "LOOKUP/GET_COUNTRIES_INIT",
29+
}
30+
`;
31+
1232
exports[`lookup.getSkillTagsDone 1`] = `
1333
Object {
1434
"payload": Array [

__tests__/actions/lookup.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,15 @@ const tag = {
1010
status: 'APPROVED',
1111
};
1212

13+
const country = {
14+
country: 'Afghanistan',
15+
countryCode: 'AFG',
16+
};
17+
1318
// Mock services
1419
const mockLookupService = {
1520
getTags: jest.fn().mockReturnValue(Promise.resolve([tag])),
21+
getCountries: jest.fn().mockReturnValue(Promise.resolve([country])),
1622
};
1723
LookupService.getService = jest.fn().mockReturnValue(mockLookupService);
1824

@@ -28,3 +34,14 @@ test('lookup.getSkillTagsDone', async () => {
2834
expect(actionResult).toMatchSnapshot();
2935
expect(mockLookupService.getTags).toBeCalled();
3036
});
37+
38+
test('lookup.getCountriesInit', async () => {
39+
const actionResult = actions.lookup.getCountriesInit();
40+
expect(actionResult).toMatchSnapshot();
41+
});
42+
43+
test('lookup.getCountriesDone', async () => {
44+
const actionResult = await redux.resolveAction(actions.lookup.getCountriesDone());
45+
expect(actionResult).toMatchSnapshot();
46+
expect(mockLookupService.getCountries).toBeCalled();
47+
});

__tests__/reducers/__snapshots__/lookup.js.snap

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,50 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`Default reducer Get countries 1`] = `
4+
Object {
5+
"countries": Array [
6+
Object {
7+
"country": "Afghanistan",
8+
"countryCode": "AFG",
9+
},
10+
],
11+
"loadingCountriesError": false,
12+
"loadingSkillTagsError": true,
13+
"skillTags": Array [
14+
Object {
15+
"domain": "SKILLS",
16+
"id": 251,
17+
"name": "Jekyll",
18+
"status": "APPROVED",
19+
},
20+
],
21+
}
22+
`;
23+
24+
exports[`Default reducer Get countries error 1`] = `
25+
Object {
26+
"countries": Array [
27+
Object {
28+
"country": "Afghanistan",
29+
"countryCode": "AFG",
30+
},
31+
],
32+
"loadingCountriesError": true,
33+
"loadingSkillTagsError": true,
34+
"skillTags": Array [
35+
Object {
36+
"domain": "SKILLS",
37+
"id": 251,
38+
"name": "Jekyll",
39+
"status": "APPROVED",
40+
},
41+
],
42+
}
43+
`;
44+
345
exports[`Default reducer Get skill tags 1`] = `
446
Object {
47+
"countries": Array [],
548
"loadingSkillTagsError": false,
649
"skillTags": Array [
750
Object {
@@ -16,6 +59,7 @@ Object {
1659

1760
exports[`Default reducer Get skill tags error 1`] = `
1861
Object {
62+
"countries": Array [],
1963
"loadingSkillTagsError": true,
2064
"skillTags": Array [
2165
Object {
@@ -30,12 +74,56 @@ Object {
3074

3175
exports[`Default reducer Initial state 1`] = `
3276
Object {
77+
"countries": Array [],
3378
"skillTags": Array [],
3479
}
3580
`;
3681

82+
exports[`Factory without server side rendering Get countries 1`] = `
83+
Object {
84+
"countries": Array [
85+
Object {
86+
"country": "Afghanistan",
87+
"countryCode": "AFG",
88+
},
89+
],
90+
"loadingCountriesError": false,
91+
"loadingSkillTagsError": true,
92+
"skillTags": Array [
93+
Object {
94+
"domain": "SKILLS",
95+
"id": 251,
96+
"name": "Jekyll",
97+
"status": "APPROVED",
98+
},
99+
],
100+
}
101+
`;
102+
103+
exports[`Factory without server side rendering Get countries error 1`] = `
104+
Object {
105+
"countries": Array [
106+
Object {
107+
"country": "Afghanistan",
108+
"countryCode": "AFG",
109+
},
110+
],
111+
"loadingCountriesError": true,
112+
"loadingSkillTagsError": true,
113+
"skillTags": Array [
114+
Object {
115+
"domain": "SKILLS",
116+
"id": 251,
117+
"name": "Jekyll",
118+
"status": "APPROVED",
119+
},
120+
],
121+
}
122+
`;
123+
37124
exports[`Factory without server side rendering Get skill tags 1`] = `
38125
Object {
126+
"countries": Array [],
39127
"loadingSkillTagsError": false,
40128
"skillTags": Array [
41129
Object {
@@ -50,6 +138,7 @@ Object {
50138

51139
exports[`Factory without server side rendering Get skill tags error 1`] = `
52140
Object {
141+
"countries": Array [],
53142
"loadingSkillTagsError": true,
54143
"skillTags": Array [
55144
Object {
@@ -64,6 +153,7 @@ Object {
64153

65154
exports[`Factory without server side rendering Initial state 1`] = `
66155
Object {
156+
"countries": Array [],
67157
"skillTags": Array [],
68158
}
69159
`;

__tests__/reducers/lookup.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,19 @@ const tag = {
77
status: 'APPROVED',
88
};
99

10+
const country = {
11+
country: 'Afghanistan',
12+
countryCode: 'AFG',
13+
};
14+
1015
const mockActions = {
1116
lookup: {
1217
getSkillTagsInit: mockAction('LOOKUP/GET_SKILL_TAGS_INIT'),
1318
getSkillTagsDone: mockAction('LOOKUP/GET_SKILL_TAGS_DONE', [tag]),
1419
getSkillTagsDoneError: mockAction('LOOKUP/GET_SKILL_TAGS_DONE', null, 'Unknown error'),
20+
getCountriesInit: mockAction('LOOKUP/GET_COUNTRIES_INIT'),
21+
getCountriesDone: mockAction('LOOKUP/GET_COUNTRIES_DONE', [country]),
22+
getCountriesDoneError: mockAction('LOOKUP/GET_COUNTRIES_DONE', null, 'Unknown error'),
1523
},
1624
};
1725
jest.setMock(require.resolve('actions/lookup'), mockActions);
@@ -38,6 +46,17 @@ function testReducer() {
3846
state = reducer(state, mockActions.lookup.getSkillTagsDoneError());
3947
expect(state).toMatchSnapshot();
4048
});
49+
50+
test('Get countries', () => {
51+
state = reducer(state, mockActions.lookup.getCountriesInit());
52+
state = reducer(state, mockActions.lookup.getCountriesDone());
53+
expect(state).toMatchSnapshot();
54+
});
55+
56+
test('Get countries error', () => {
57+
state = reducer(state, mockActions.lookup.getCountriesDoneError());
58+
expect(state).toMatchSnapshot();
59+
});
4160
}
4261

4362
describe('Default reducer', () => {

docs/actions.lookup.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ Actions related to lookup data.
77
* [actions.lookup](#module_actions.lookup)
88
* [.getSkillTagsInit()](#module_actions.lookup.getSkillTagsInit) ⇒ <code>Action</code>
99
* [.getSkillTagsDone()](#module_actions.lookup.getSkillTagsDone) ⇒ <code>Action</code>
10+
* [.getCountriesInit()](#module_actions.lookup.getCountriesInit) ⇒ <code>Action</code>
11+
* [.getCountriesDone()](#module_actions.lookup.getCountriesDone) ⇒ <code>Action</code>
1012

1113
<a name="module_actions.lookup.getSkillTagsInit"></a>
1214

@@ -20,3 +22,9 @@ Creates an action that signals beginning of getting all skill tags.
2022
Creates an action that gets all skill tags.
2123

2224
**Kind**: static method of [<code>actions.lookup</code>](#module_actions.lookup)
25+
<a name="module_actions.lookup.getCountriesDone"></a>
26+
27+
### actions.lookup.getCountriesDone() ⇒ <code>Action</code>
28+
Creates an action that gets all countries.
29+
30+
**Kind**: static method of [<code>actions.lookup</code>](#module_actions.lookup)

docs/reducers.lookup.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ State segment managed by this reducer has the following structure:
1717
* [.factory()](#module_reducers.lookup.factory) ⇒ <code>Promise</code>
1818
* _inner_
1919
* [~onGetSkillTagsDone(state, action)](#module_reducers.lookup..onGetSkillTagsDone) ⇒ <code>Object</code>
20+
* [~onGetCountriesDone(state, action)](#module_reducers.lookup..onGetCountriesDone) ⇒ <code>Object</code>
2021
* [~create(initialState)](#module_reducers.lookup..create) ⇒ <code>function</code>
2122

2223
<a name="module_reducers.lookup.default"></a>
@@ -37,6 +38,13 @@ Factory which creates a new reducer.
3738
### reducers.lookup~onGetSkillTagsDone(state, action) ⇒ <code>Object</code>
3839
Handles LOOKUP/GET_SKILL_TAGS_DONE action.
3940

41+
**Kind**: static method of [<code>reducers.lookup</code>](#module_reducers.lookup)
42+
**Resolves**: <code>Function(state, action): state</code> New reducer.
43+
<a name="module_reducers.lookup..onGetCountriesDone"></a>
44+
45+
### reducers.lookup~onGetCountriesDone(state, action) ⇒ <code>Object</code>
46+
Handles LOOKUP/GET_COUNTRIES_DONE action.
47+
4048
**Kind**: inner method of [<code>reducers.lookup</code>](#module_reducers.lookup)
4149
**Returns**: <code>Object</code> - New state
4250

src/actions/lookup.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,27 @@ function getSkillTagsDone() {
2626
return getService().getTags(params);
2727
}
2828

29+
/**
30+
* @static
31+
* @desc Creates an action that signals beginning of getting all countries.
32+
* @return {Action}
33+
*/
34+
function getCountriesInit() {}
35+
36+
/**
37+
* @static
38+
* @desc Creates an action that gets all countries.
39+
* @return {Action}
40+
*/
41+
function getCountriesDone() {
42+
return getService().getCountries();
43+
}
44+
2945
export default createActions({
3046
LOOKUP: {
3147
GET_SKILL_TAGS_INIT: getSkillTagsInit,
3248
GET_SKILL_TAGS_DONE: getSkillTagsDone,
49+
GET_COUNTRIES_INIT: getCountriesInit,
50+
GET_COUNTRIES_DONE: getCountriesDone,
3351
},
3452
});

src/reducers/lookup.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,25 @@ function onGetSkillTagsDone(state, { payload, error }) {
2929
});
3030
}
3131

32+
/**
33+
* Handles LOOKUP/GET_COUNTRIES_DONE action.
34+
* @param {Object} state
35+
* @param {Object} action Payload will be JSON from api call
36+
* @return {Object} New state
37+
*/
38+
function onGetCountriesDone(state, { payload, error }) {
39+
if (error) {
40+
logger.error('Failed to get countries', payload);
41+
return { ...state, loadingCountriesError: true };
42+
}
43+
44+
return ({
45+
...state,
46+
loadingCountriesError: false,
47+
countries: payload,
48+
});
49+
}
50+
3251
/**
3352
* Creates a new Lookup reducer with the specified initial state.
3453
* @param {Object} initialState Optional. Initial state.
@@ -39,8 +58,11 @@ function create(initialState = {}) {
3958
return handleActions({
4059
[a.getSkillTagsInit]: state => state,
4160
[a.getSkillTagsDone]: onGetSkillTagsDone,
61+
[a.getCountriesInit]: state => state,
62+
[a.getCountriesDone]: onGetCountriesDone,
4263
}, _.defaults(initialState, {
4364
skillTags: [],
65+
countries: [],
4466
}));
4567
}
4668

src/services/lookup.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ class LookupService {
2727
const res = await this.private.api.get(`/tags/?${qs.stringify(params)}`);
2828
return getApiResponsePayload(res);
2929
}
30+
31+
/**
32+
* Gets countries.
33+
* @param {Object} params Parameters
34+
* @return {Promise} Resolves to the countries.
35+
*/
36+
async getCountries() {
37+
const res = await this.private.api.get('/members/lookup/countries');
38+
return getApiResponsePayload(res);
39+
}
3040
}
3141

3242
let lastInstance = null;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy