Skip to content

Commit fbedccd

Browse files
committed
Add v0.1.7 changes
2 parents d945ae3 + 4a3b735 commit fbedccd

File tree

7 files changed

+199
-39
lines changed

7 files changed

+199
-39
lines changed

.npmignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
*.test.js
2+
.idea
3+
coverage
4+
node_modules
5+
jest.config.json
6+
yarn.lock
7+
yarn-error.log

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 aleph-engineering
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

package.json

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
11
{
2-
"name": "@cuban-engineer/redux-storage-sync",
3-
"version": "0.1.0",
2+
"name": "@cuban-engineer/redux-storage-middleware",
3+
"version": "0.1.7",
44
"description": "Redux middleware for synchronizing store state with browser storages ",
5-
"main": "./dist/reduxStorageSync.js",
5+
"main": "./dist/reduxStorageMiddleware.js",
66
"scripts": {
7+
"clean": "rimraf dist",
78
"test": "jest --config=jest.config.json",
89
"test:coverage": "jest --config=jest.config.json --coverage",
910
"build": "webpack",
10-
"publish": "--access public"
11+
"publish": "--access public",
12+
"prepublish": "npm run clean && npm run test && npm run build"
1113
},
14+
"repository": {
15+
"type": "git",
16+
"url": "https://github.com/aleph-engineering/redux-storage-middleware.git"
17+
},
18+
"keywords": [
19+
"redux",
20+
"storage",
21+
"middleware",
22+
"redux-middleware"
23+
],
1224
"author": "",
1325
"license": "ISC",
1426
"devDependencies": {
@@ -18,8 +30,10 @@
1830
"eslint": "^4.19.1",
1931
"jest": "^22.4.3",
2032
"jest-localstorage-mock": "^2.2.0",
33+
"rimraf": "^2.6.2",
2134
"prettier": "^1.11.1",
2235
"eslint-plugin-prettier": "^2.6.0",
36+
"eslint-config-prettier": "^2.9.0",
2337
"redux-mock-store": "^1.5.1",
2438
"webpack": "^4.5.0",
2539
"webpack-cli": "^2.0.14"

readme.md

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
1+
# What makes the library better?
2+
[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
3+
14
# Installation
25
## Using npm:
36
```sh
4-
npm i --save redux-storage-middleware
7+
npm i --save @cuban-engineer/redux-storage-middleware
58
```
69

710
# Usage
811
## If you are using ES modules:
912
```javascript
10-
import reduxStorageMiddleware from 'redux-storage-middleware';
13+
import reduxStorageMiddleware from '@cuban-engineer/redux-storage-middleware';
1114
```
1215

1316
## Apply middleware to Redux:
1417
```javascript
1518
import { applyMiddleware, createStore } from 'redux';
16-
import reduxStorageMiddleware from 'redux-storage-middleware';
19+
import reduxStorageMiddleware from '@cuban-engineer/redux-storage-middleware';
1720
import rootReducer from './reducers/index';
1821

1922
const store = createStore(
@@ -24,15 +27,15 @@ const store = createStore(
2427

2528
# Describe what needs to be saved in your Redux actions
2629
## For simple Redux states
27-
Supose you already have this simple action for changing the logged user token in your application for sending requests to an API.
30+
Suppose you already have this simple action for changing the logged user token in your application for sending requests to an API.
2831
```javascript
2932
export const changeUserToken = userToken =>
3033
({
3134
type: 'CHANGE_USER_TOKEN',
3235
userToken
3336
});
3437
```
35-
Simply by adding the `sync` property which is an array of simple json objects that `redux-storage-middleware` understands you can define what and where to save after the Reducer handle your action.
38+
Simply by adding the `sync` property which is an array of simple json objects that `@cuban-engineer/redux-storage-middleware` understands you can define what and where to save after the Reducer handle your action.
3639
```javascript
3740
export const changeUserToken = userToken =>
3841
({
@@ -52,19 +55,19 @@ export const changeUserToken = userToken =>
5255
sync: [
5356
{
5457
// Key in which will be saved the data in the Browser storage.
55-
// If is not defined the key will be the @name attributte.
58+
// If is not defined the key will be the @name attribute.
5659
key: 'token',
5760
// Literal name of the object in the Redux State that wants to be saved to the Browser storage.
5861
name: 'userToken',
5962
// Array of string enumerating where to save the data in the Browser storage.
60-
// Currently only Session and Local Storage. Cookies storage is comming son.
61-
// If only want to save in a single place is also posible to define it as a simple string, like this:
63+
// Currently only Session and Local Storage. Cookies storage is coming son.
64+
// If only want to save in a single place is also possible to define it as a simple string, like this:
6265
// where: 'session'
6366
// If not defined, saving will occur in the Session Storage.
6467
where: ['session', 'local'],
6568
// For more complex Stores, if using combineReducers for instance, the entire tree hierarchy for
6669
// obtaining the object in the State can be defined in the hierarchy attribute.
67-
// If only one level is needed to reach the desire object from the state, is also posible to define
70+
// If only one level is needed to reach the desire object from the state, is also possible to define
6871
// the attribute as a simple string, like this hierarchy: 'ui'.
6972
// Example: state.user.userToken
7073
hierarchy: ['user'],

src/utils/sync/hierarchyResolver.test.js

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,39 @@ import hierarchyResolver from './hierarchyResolver';
22

33
describe('Suite of tests for the hierarchy resolver util', () => {
44
const root = 'children';
5-
const hierarchyString = 'dad';
6-
const hierarchyArray = ['grandpa', 'dad'];
7-
const stateDefault = {
8-
children: 'florian',
9-
};
10-
const stateSimple = {
11-
dad: {
12-
children: 'florian',
13-
},
14-
};
15-
const stateComplex = {
16-
grandpa: {
17-
dad: {
18-
children: 'tino',
19-
},
20-
},
21-
};
225

236
test('should return valid value with hierarchy as string provided', () => {
24-
expect(hierarchyResolver(root, hierarchyString, stateSimple)).toBe(
25-
stateSimple.dad.children,
7+
const hierarchyString = 'dad';
8+
const state = {
9+
dad: {
10+
children: 'florian',
11+
},
12+
};
13+
expect(hierarchyResolver(root, hierarchyString, state)).toBe(
14+
state.dad.children,
2615
);
2716
});
2817

2918
test('should return valid value with hierarchy as array provided', () => {
30-
expect(hierarchyResolver(root, hierarchyArray, stateComplex)).toBe(
31-
stateComplex.grandpa.dad.children,
19+
const hierarchyArray = ['grandpa', 'dad'];
20+
const state = {
21+
grandpa: {
22+
dad: {
23+
children: 'tino',
24+
},
25+
},
26+
};
27+
expect(hierarchyResolver(root, hierarchyArray, state)).toBe(
28+
state.grandpa.dad.children,
3229
);
3330
});
3431

3532
test('should return valid default value is hierarchy is not a string or array', () => {
36-
expect(hierarchyResolver(root, null, stateDefault)).toBe(
37-
stateDefault.children,
33+
const state = {
34+
children: 'florian',
35+
};
36+
expect(hierarchyResolver(root, null, state)).toBe(
37+
state.children,
3838
);
3939
});
4040
});

src/utils/sync/synchronizer.test.js

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import Sync from './synchronizer';
2+
import * as dataSaver from './../storage/dataSaver';
3+
import * as hierarchyResolver from './../sync/hierarchyResolver';
4+
5+
describe('Suite of tests for the synchronizer function', () => {
6+
const state = {
7+
token: 'abhy-12tt-jf78-ml23-jk34-1111',
8+
};
9+
10+
beforeEach(() => {
11+
dataSaver.default = jest.fn();
12+
hierarchyResolver.default = jest.fn();
13+
});
14+
15+
test('should not call hierarchyResolver when hierarchy is not defined in sync object', () => {
16+
const action = {
17+
type: 'CHANGE_USER_TOKEN',
18+
userToken: 'abhy-12tt-jf78-ml23-jk34',
19+
sync: [
20+
{
21+
name: 'token',
22+
where: ['session', 'local'],
23+
},
24+
],
25+
};
26+
Sync(action, state);
27+
expect(hierarchyResolver.default).toHaveBeenCalledTimes(0);
28+
});
29+
30+
test('should call hierarchyResolver when hierarchy is defined in sync object', () => {
31+
const action = {
32+
type: 'CHANGE_USER_TOKEN',
33+
userToken: 'abhy-12tt-jf78-ml23-jk34',
34+
sync: [
35+
{
36+
name: 'token',
37+
where: ['session', 'local'],
38+
hierarchy: ['user'],
39+
},
40+
],
41+
};
42+
Sync(action, state);
43+
expect(hierarchyResolver.default).toHaveBeenCalledTimes(action.sync.length);
44+
expect(hierarchyResolver.default).toHaveBeenCalledWith(
45+
action.sync[0].name,
46+
action.sync[0].hierarchy,
47+
state
48+
);
49+
});
50+
51+
test('should call dataSaver with none place parameter if where is not defined', () => {
52+
const action = {
53+
type: 'CHANGE_USER_TOKEN',
54+
userToken: 'abhy-12tt-jf78-ml23-jk34',
55+
sync: [
56+
{
57+
key: 'userToken',
58+
name: 'token',
59+
},
60+
],
61+
};
62+
Sync(action, state);
63+
expect(dataSaver.default).toHaveBeenCalledTimes(1);
64+
expect(dataSaver.default).toHaveBeenCalledWith({
65+
key: action.sync[0].key,
66+
value: state.token,
67+
});
68+
});
69+
70+
test('should call dataSaver with place parameter if where is of type string', () => {
71+
const action = {
72+
type: 'CHANGE_USER_TOKEN',
73+
userToken: 'abhy-12tt-jf78-ml23-jk34',
74+
sync: [
75+
{
76+
key: 'userToken',
77+
name: 'token',
78+
where: 'session',
79+
},
80+
],
81+
};
82+
Sync(action, state);
83+
expect(dataSaver.default).toHaveBeenCalledTimes(1);
84+
expect(dataSaver.default).toHaveBeenCalledWith(
85+
{ key: action.sync[0].key, value: state.token },
86+
action.sync[0].where
87+
);
88+
});
89+
90+
test('should call dataSaver with place parameter as many times as items has where is of type array', () => {
91+
const action = {
92+
type: 'CHANGE_USER_TOKEN',
93+
userToken: 'abhy-12tt-jf78-ml23-jk34',
94+
sync: [
95+
{
96+
key: 'userToken',
97+
name: 'token',
98+
where: ['session', 'local'],
99+
},
100+
],
101+
};
102+
Sync(action, state);
103+
expect(dataSaver.default).toHaveBeenCalledTimes(action.sync[0].where.length);
104+
expect(dataSaver.default).toHaveBeenCalledWith(
105+
{ key: action.sync[0].key, value: state.token },
106+
action.sync[0].where[0]
107+
);
108+
expect(dataSaver.default).toHaveBeenCalledWith(
109+
{ key: action.sync[0].key, value: state.token },
110+
action.sync[0].where[1]
111+
);
112+
});
113+
});

webpack.config.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ module.exports = {
55
entry: './src/index.js',
66
output: {
77
path: path.resolve(__dirname, 'dist'),
8-
filename: 'reduxStorageSync.js'
9-
}
10-
};
8+
filename: 'reduxStorageMiddleware.js',
9+
library: 'reduxStorageMiddleware',
10+
libraryTarget: 'umd',
11+
},
12+
};

0 commit comments

Comments
 (0)