Skip to content

Commit 9f0dfc3

Browse files
authored
Merge pull request #6 from log-oscon/tests/reducer
Restructures tests and adds some reducer tests to the lib
2 parents ae2d994 + 1e0daf1 commit 9f0dfc3

17 files changed

+413
-245
lines changed

.eslintrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"extends": [ "airbnb" ],
3838

3939
"rules": {
40+
"no-underscore-dangle": 0,
4041
"no-unused-vars": [2, { "vars": "all", "args": "after-used" }],
4142
"quotes": [2, "single"],
4243
"strict": [2, "never"],

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Some guidelines in reading this document:
1111

1212
## [new release]
1313

14+
* Implements reducer tests ([#6](https://github.com/log-oscon/redux-wpapi/pull/6))
1415
* Draft on Contributions and the introduction of this `CHANGELOG.md` file ([#5](https://github.com/log-oscon/redux-wpapi/pull/5))
1516
* Fix FAILURE handling ([#3](https://github.com/log-oscon/redux-wpapi/pull/3))
1617

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# redux-wpapi
2+
[![npm version](https://img.shields.io/npm/v/redux.svg?style=flat-square)](https://www.npmjs.com/package/redux-wpapi)
3+
24
A [node-wpapi](https://github.com/WP-API/node-wpapi) integration for a Redux based Application.
35

46
## How it Works

src/ReduxWPAPI.js

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import capitalize from 'lodash/capitalize';
1111
import Immutable from 'immutable';
1212
import isArray from 'lodash/isArray';
1313
import isUndefined from 'lodash/isUndefined';
14-
import namedGroupRegex from './named-group-regexp';
14+
import namedGroupRegex from './namedGroupRegexp';
1515
import { selectQuery } from './selectors';
1616

1717
import {
@@ -61,7 +61,7 @@ export default class ReduxWPAPI {
6161
};
6262
},
6363

64-
// FIXME provisional method while waiting WP-API/node-wpapi#213
64+
// FIXME provisional method while WAITING WP-API/node-wpapi#213
6565
getIndexes(request) {
6666
/* eslint-disable no-param-reassign */
6767
// Internal mutations inside reduce function
@@ -194,23 +194,23 @@ export default class ReduxWPAPI {
194194
if (meta.operation === 'get') {
195195
let cache;
196196
let lastCacheUpdate;
197-
let cachePath;
197+
let data;
198198
const state = store.getState().wp;
199199
const indexes = this.settings.getIndexes(request, this.settings);
200200
const localID = this.getEntityLocalID(state, meta.aggregator, indexes);
201201
payload.uid = this.settings.getRequestUID(request, this.settings);
202202
payload.page = parseInt(this.settings.getRequestedPage(request, this.settings) || 1, 10);
203203

204204
if (localID) {
205-
cachePath = localID;
206-
cache = state.getIn(['entities', cachePath]);
205+
cache = state.getIn(['entities', localID]);
206+
data = [localID];
207207
}
208208

209209
if (cache) {
210210
lastCacheUpdate = cache.lastCacheUpdate;
211211
} else {
212-
cachePath = ['requestsByQuery', payload.uid];
213-
cache = state.getIn([...cachePath, payload.page]);
212+
cache = state.getIn(['requestsByQuery', payload.uid, payload.page]);
213+
data = cache.get('data');
214214
}
215215

216216
if (cache && (localID || (isUndefined(localID) && !cache.get('error')))) {
@@ -222,7 +222,7 @@ export default class ReduxWPAPI {
222222
uid: payload.uid,
223223
page: payload.page,
224224
lastCacheUpdate,
225-
cachePath,
225+
data,
226226
},
227227
});
228228

@@ -263,26 +263,28 @@ export default class ReduxWPAPI {
263263
reducer = (state = initialReducerState, action) => {
264264
switch (action.type) {
265265
case REDUX_WP_API_CACHE_HIT: {
266-
const { cachePath, page, uid } = action.payload;
267-
268-
const newState = state.mergeIn(['requestsByName', action.meta.name], { page, uid });
269-
if (newState.getIn(['requestsByQuery', uid, page])) {
270-
return newState;
271-
}
266+
const { data, page, uid } = action.payload;
267+
let newState = state.mergeIn(
268+
['requestsByName', action.meta.name],
269+
{ page, uid }
270+
);
272271

273-
return (
274-
newState.mergeIn(
275-
['requestsByQuery', uid, 1],
276-
{
272+
if (!newState.getIn(['requestsByQuery', uid, page])) {
273+
newState = (
274+
newState
275+
.mergeIn(['requestsByQuery', uid, page], {
277276
status: READY,
278277
operation: action.meta.operation,
279278
error: false,
280279
requestAt: action.payload.lastCacheUpdate,
281280
responseAt: action.payload.lastCacheUpdate,
282-
data: [cachePath],
283-
}
284-
)
285-
);
281+
})
282+
.setIn(['requestsByQuery', uid, 1, 'data'], data)
283+
);
284+
}
285+
286+
287+
return newState;
286288
}
287289

288290
case REDUX_WP_API_REQUEST: {

src/constants/requestStatus.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
export const WAITING = 'WAITING';
22
export const READY = 'READY';
33
export const ERROR = 'ERROR';
4+
5+
export default {
6+
WAITING,
7+
READY,
8+
ERROR,
9+
};
File renamed without changes.

test/data/collectionResponse.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
const collectionResponse = [
2+
{
3+
id: 2,
4+
slug: 'dumb2',
5+
dumbAttr: 'dumb2',
6+
_links: {
7+
self: [{ href: 'http://dumb.url/wp-json/namespace/any/2' }],
8+
collection: [{ href: 'http://dumb.url/wp-json/namespace/any' }],
9+
parent: [{
10+
embeddable: true,
11+
href: 'http://dumb.url/wp-json/namespace/any/1',
12+
}],
13+
author: [{
14+
embeddable: true,
15+
href: 'http://dumb.url/wp-json/wp/v2/users/1',
16+
}],
17+
},
18+
_embedded: {
19+
author: [{
20+
id: 1,
21+
name: 'admin',
22+
link: 'http://km.nos.dev/author/admin/',
23+
slug: 'admin',
24+
_links: {
25+
self: [{ href: 'http://km.nos.dev/wp-json/wp/v2/users/1' }],
26+
collection: [{ href: 'http://km.nos.dev/wp-json/wp/v2/users' }],
27+
},
28+
}],
29+
parent: [{
30+
id: 1,
31+
slug: 'dumb1',
32+
dumbAttr: 'dumb1',
33+
_links: {
34+
self: [{ href: 'http://dumb.url/wp-json/namespace/any/1' }],
35+
collection: [{ href: 'http://dumb.url/wp-json/namespace/any' }],
36+
parent: [{
37+
embeddable: true,
38+
href: 'http://dumb.url/wp-json/namespace/any/1',
39+
}],
40+
author: [{
41+
embeddable: true,
42+
href: 'http://dumb.url/wp-json/wp/v2/users/2',
43+
}],
44+
},
45+
}],
46+
},
47+
},
48+
{
49+
id: 1,
50+
slug: 'dumb1',
51+
dumbAttr: 'dumb1',
52+
_links: {
53+
self: [{ href: 'http://dumb.url/wp-json/namespace/any/1' }],
54+
collection: [{ href: 'http://dumb.url/wp-json/namespace/any' }],
55+
parent: [{
56+
embeddable: true,
57+
href: 'http://dumb.url/wp-json/namespace/any/1',
58+
}],
59+
author: [{
60+
embeddable: true,
61+
href: 'http://dumb.url/wp-json/wp/v2/users/2',
62+
}],
63+
},
64+
_embedded: {
65+
author: [{
66+
id: 2,
67+
name: 'edygar',
68+
link: 'http://km.nos.dev/author/edygar/',
69+
slug: 'edygar',
70+
_links: {
71+
self: [{ href: 'http://km.nos.dev/wp-json/wp/v2/users/2' }],
72+
collection: [{ href: 'http://km.nos.dev/wp-json/wp/v2/users' }],
73+
},
74+
}],
75+
},
76+
},
77+
];
78+
79+
collectionResponse._paging = {
80+
total: 2,
81+
totalPages: 1,
82+
};
83+
84+
export default collectionResponse;

test/data/queryBySlugResponse.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const queryBySlugResponse = [
2+
{
3+
id: 1,
4+
slug: 'dumb1-modified',
5+
dumbAttr: 'dumb1 - modified',
6+
_links: {
7+
self: [{ href: 'http://dumb.url/wp-json/namespace/any/1' }],
8+
collection: [{ href: 'http://dumb.url/wp-json/namespace/any' }],
9+
parent: [{
10+
embeddable: true,
11+
href: 'http://dumb.url/wp-json/namespace/any/1',
12+
}],
13+
author: [{
14+
embeddable: true,
15+
href: 'http://dumb.url/wp-json/wp/v2/users/2',
16+
}],
17+
},
18+
_embedded: {
19+
author: [{
20+
id: 2,
21+
name: 'edygar',
22+
link: 'http://km.nos.dev/author/edygar/',
23+
slug: 'edygar',
24+
_links: {
25+
self: [{ href: 'http://km.nos.dev/wp-json/wp/v2/users/2' }],
26+
collection: [{ href: 'http://km.nos.dev/wp-json/wp/v2/users' }],
27+
},
28+
}],
29+
},
30+
},
31+
];
32+
33+
queryBySlugResponse._paging = {
34+
total: 1,
35+
totalPages: 2,
36+
};
37+
38+
export default queryBySlugResponse;
39+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { REDUX_WP_API_CACHE_HIT } from '../../src/constants/actions';
2+
3+
export default {
4+
type: REDUX_WP_API_CACHE_HIT,
5+
payload: {
6+
uid: '/namespace/any',
7+
page: 1,
8+
lastCacheUpdate: Date.now(),
9+
data: [3, 1],
10+
},
11+
meta: {
12+
name: 'test',
13+
aggregator: 'any',
14+
operation: 'get',
15+
requestAt: Date.now(),
16+
},
17+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { REDUX_WP_API_CACHE_HIT } from '../../src/constants/actions';
2+
3+
export default {
4+
type: REDUX_WP_API_CACHE_HIT,
5+
payload: {
6+
uid: '/namespace/any?slug=dumb2',
7+
page: 1,
8+
lastCacheUpdate: Date.now(),
9+
data: [3],
10+
},
11+
meta: {
12+
name: 'test',
13+
aggregator: 'any',
14+
operation: 'get',
15+
requestAt: Date.now(),
16+
},
17+
};

0 commit comments

Comments
 (0)