Skip to content
This repository has been archived by the owner on Feb 4, 2024. It is now read-only.

Commit

Permalink
Merge pull request #3 from eugene-manuilov/release/1.0.0
Browse files Browse the repository at this point in the history
Release/1.0.0
  • Loading branch information
eugene-manuilov authored May 18, 2017
2 parents 284af66 + fd00acc commit 1760fb4
Show file tree
Hide file tree
Showing 5 changed files with 358 additions and 85 deletions.
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"rules": {
"func-names": 0,
"no-tabs": 0,
"class-methods-use-this": 0,
"indent": ["error", "tab"]
},
"globals": {
Expand Down
57 changes: 37 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,34 @@

[![npm version](https://badge.fury.io/js/redux-wordpress.svg)](https://badge.fury.io/js/redux-wordpress) [![Build Status](https://travis-ci.org/eugene-manuilov/redux-wordpress.svg?branch=master)](https://travis-ci.org/eugene-manuilov/redux-wordpress)

This package is intended to help to build Redux actions and reducers for WordPress REST API endpoints. **This package is not ready yet**, so please, don't use it in your projects.
This package is intended to help you to build Redux actions and reducers for WordPress REST API endpoints.

## The idea
## Installation

The main idea behind this package is to create helper functions which will allow us to easily generate action functions and reducers for custom WordPress REST API endpoints. It could be something like this:
NPM:

```
npm install redux-wordpress --save
```

Yarn:

```
yarn add redux-wordpress
```

## Usage

The package exports three function which you can use to create actions and build a reducer.

### createActions(name, host, endpoints, namespace)

Returns an object with a set of function which you can use to fetch data from REST API.

- **name** _(string)_ - Arbitrary name which will be used in action types to distinguish different actions.
- **host** _(string)_ - URL address to your API's root. Usually it will look like: `http://mysite.com/wp-json/`.
- **endpoints** _(array)_ - A list of endpoints which you want to build actions for. It could be something like `['posts', 'categories']`.
- **namespace** _(string)_ - Optional. The namespace for your endpoints. By default it is `wp/v2`.

```js
// actionCreators.js
Expand Down Expand Up @@ -36,6 +59,12 @@ export default actions;
// }
```

### createReducer(name)

Returns a reducer function which you can use to catch data returned by a fetch action.

- **name** _(string)_ - A name which will be used to catch proper actions. It should be the same name as you passed to `createActions` function.

```js
// reducers.js

Expand All @@ -47,25 +76,13 @@ const rootReducer = combineReducers({

export default rootReducer;
```
### createRequests(host, endpoints, namespace)

Generated reducer will listen to action types dispatched from actions and update state using endpoint names as holders for received data. It will look like this:

```json
{
"books": {
"total": 999,
"totalPages": 999,
"data": [{}, {}, {}]
},
"authors": {
"total": 999,
"totalPages": 999,
"data": [{}, {}, {}]
}
}
```
Helper function which generates request functions to endpoints which you can use to group multiple requests into one action:

This package also contains another helper function which generates request functions to endpoints which you can use to group multiple requests into one action:
- **host** _(string)_ - URL address to your API's root. Usually it will look like: `http://mysite.com/wp-json/`.
- **endpoints** _(array)_ - A list of endpoints which you want to build actions for. It could be something like `['posts', 'categories']`.
- **namespace** _(string)_ - Optional. The namespace for your endpoints. By default it is `wp/v2`.

```js
// actionCreators.js
Expand Down
232 changes: 188 additions & 44 deletions __tests__/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ import { createReducer } from '../lib/index';
const name = 'wp-api';
const reducer = createReducer(name);

const getTestData = () => {
const data = [];

for (let i = 0, len = faker.random.number({min: 1, max: 20}); i < len; i++) {
data.push({id: faker.random.number(), title: faker.lorem.sentence()});
}

return data;
};

test('Test createReducer creates a function', () => {
expect(typeof reducer).toBe('function');
});
Expand All @@ -17,67 +27,201 @@ test('Test original state has not changed on unsuccessful result', () => {
const state = {test: faker.random.number(), data: [1, 2, 3]};

expect(reducer(state, {type: `@@wp/${name}/fetched/books`, ok: false})).toEqual(state);
expect(reducer(state, {type: `@@wp/${name}/fetched/books/chapters`, ok: false})).toEqual(state);
expect(reducer(state, {type: `@@wp/${name}/fetched-all/books`, ok: false})).toEqual(state);
expect(reducer(state, {type: `@@wp/${name}/fetched-all/books/chapters`, ok: false})).toEqual(state);
expect(reducer(state, {type: `@@wp/${name}/fetched-by-id/books`, ok: false})).toEqual(state);
expect(reducer(state, {type: `@@wp/${name}/fetched-by-id/books/chapters`, ok: false})).toEqual(state);
expect(reducer(state, {type: `@@wp/${name}/fetched-all-by-id/books/chapters`, ok: false})).toEqual(state);
});

test('Test state change on fetch success', () => {
const authors = {
data: [],
total: faker.random.number(),
totalPages: faker.random.number()
describe('Fetch reducer', () => {
const initialState = {
authors: {
data: [],
total: faker.random.number(),
totalPages: faker.random.number()
}
};

const state = {
authors
};
test('state change on fetch success', () => {
const state = Object.assign({}, initialState);
const totalPages = faker.random.number();
const data = getTestData();

const totalPages = faker.random.number();
const data = [];
for (let i = 0, len = faker.random.number({min: 1, max: 20}); i < len; i++) {
data.push({id: faker.random.number(), title: faker.lorem.sentence()});
}
const action = {
type: `@@wp/${name}/fetched/books`,
ok: true,
results: data,
total: data.length,
totalPages
};

const action = {
type: `@@wp/${name}/fetched/books`,
ok: true,
results: data,
total: data.length,
totalPages
};
const result = Object.assign({}, state, {
books: {
data: data,
total: data.length,
totalPages
}
});

expect(reducer(state, action)).toEqual(result);
});

const result = Object.assign({}, state, {
books: {
data: data,
test('state change on fetch-all success', () => {
const state = Object.assign({}, initialState);
const totalPages = faker.random.number();
const data = getTestData();

const action = {
type: `@@wp/${name}/fetched-all/books`,
ok: true,
results: data,
total: data.length,
totalPages
}
});
};

expect(reducer(state, action)).toEqual(result);
const result = Object.assign({}, state, {
books: {
data: data,
total: data.length,
totalPages
}
});

expect(reducer(state, action)).toEqual(result);
});
});

test('Test state change on fetch-by-id success', () => {
const authors = {};
authors[faker.random.number()] = faker.lorem.sentence();
authors[faker.random.number()] = faker.lorem.sentence();
authors[faker.random.number()] = faker.lorem.sentence();
describe('Fetch-by-id reducer', () => {
test('Test state change on fetch-by-id success', () => {
const authors = {
data: [],
total: faker.random.number(),
totalPages: faker.random.number()
};

const state = {
authors
};

const id = faker.random.number();
const data = faker.lorem.sentence();

const state = {
authors
const action = {
type: `@@wp/${name}/fetched-by-id/books`,
ok: true,
id: id,
result: data
};

const obj = {};
obj[`books/${id}`] = { data };

expect(reducer(state, action)).toEqual(Object.assign({}, state, obj));
});
});

describe('Fetch-endpoint reducer', () => {
const initialState = {
authors: {
data: [],
total: faker.random.number(),
totalPages: faker.random.number()
}
};

const id = faker.random.number();
const data = faker.lorem.sentence();
test('state change on fetch-endpoint success', () => {
const state = Object.assign({}, initialState);
const totalPages = faker.random.number();
const data = getTestData();

const action = {
type: `@@wp/${name}/fetched/books/chapters`,
ok: true,
results: data,
total: data.length,
totalPages
};

const result = Object.assign({}, state, {
"books/chapters": {
data: data,
total: data.length,
totalPages
}
});

expect(reducer(state, action)).toEqual(result);
});

test('state change on fetch-all-enpoint success', () => {
const state = Object.assign({}, initialState);
const totalPages = faker.random.number();
const data = getTestData();

const action = {
type: `@@wp/${name}/fetched-all/books/chapters`,
ok: true,
results: data,
total: data.length,
totalPages
};

const result = Object.assign({}, state, {
"books/chapters": {
data: data,
total: data.length,
totalPages
}
});

const action = {
type: `@@wp/${name}/fetched-by-id/books`,
ok: true,
id: id,
result: data
expect(reducer(state, action)).toEqual(result);
});
});

describe('Fetch-endpoint-by-id reducer', () => {
const initialState = {
authors: {
data: [],
total: faker.random.number(),
totalPages: faker.random.number()
}
};

const result = Object.assign({}, state, {books: {}});
result.books[id] = data;
test('state change on fetch-endpoint success', () => {
const state = Object.assign({}, initialState);
const id = faker.random.number();
const data = faker.lorem.sentence();

expect(reducer(state, action)).toEqual(result);
});
const action = {
type: `@@wp/${name}/fetched-by-id/books/chapters`,
ok: true,
id: id,
result: data
};

const obj = {};
obj[`books/${id}/chapters`] = { data };

expect(reducer(state, action)).toEqual(Object.assign({}, state, obj));
});

test('state change on fetch-all-enpoint success', () => {
const state = Object.assign({}, initialState);
const id = faker.random.number();
const data = faker.lorem.sentence();

const action = {
type: `@@wp/${name}/fetched-all-by-id/books/chapters`,
ok: true,
id: id,
result: data
};

const obj = {};
obj[`books/${id}/chapters`] = { data };

expect(reducer(state, action)).toEqual(Object.assign({}, state, obj));
});
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"bugs": {
"url": "https://github.com/eugene-manuilov/redux-wordpress/issues"
},
"version": "0.1.0-dev.7",
"version": "1.0.0",
"main": "lib/index",
"files": [
"*.md",
Expand Down
Loading

0 comments on commit 1760fb4

Please sign in to comment.