-
Notifications
You must be signed in to change notification settings - Fork 21
/
spec.js
174 lines (136 loc) · 7.25 KB
/
spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import { expect } from 'chai';
import createFilter, { createWhitelistFilter, createBlacklistFilter, persistFilter } from './index';
describe('redux-persist-transform-filter', () => {
describe('persistFilter', () => {
it('should be functions', () => {
expect(persistFilter).to.be.a('function');
});
it('should return a subset, given one key', () => {
expect(persistFilter({a:'a', b:'b', c:'c'}, 'a')).to.deep.equal({a:'a'});
});
it('should return a subset, given an array of keys', () => {
expect(persistFilter({a:'a', b:'b', c:'c'}, ['a'])).to.deep.equal({a:'a'});
expect(persistFilter({a:'a', b:'b', c:'c'}, ['a', 'b'])).to.deep.equal({a:'a', b:'b'});
});
it('should return a subset, given one key path', () => {
expect(persistFilter({a: {b:'b', c:'c'}, d:'d'}, 'a.b')).to.deep.equal({a: {b:'b'}});
expect(persistFilter({a: {b:'b', c:'c'}, d:'d'}, 'a.c')).to.deep.equal({a: {c:'c'}});
});
it('should return a subset, given an array of key paths', () => {
expect(persistFilter({a: {b:'b', c:'c'}, d:'d'}, ['a.b'])).to.deep.equal({a: {b:'b'}});
expect(persistFilter({a: {b:'b', c:'c'}, d:'d'}, [['a', 'b']])).to.deep.equal({a: {b:'b'}});
expect(persistFilter({a: {b:'b', c:'c'}, d:'d'}, ['a.b', 'a.c'])).to.deep.equal({a: {b:'b', c:'c'}});
expect(persistFilter({a: {b:'b', c:'c'}, d:'d'}, [['a', 'b'], ['a', 'c']])).to.deep.equal({a: {b:'b', c:'c'}});
});
it('should return a subset, given an object that contains a path and a filterFunction', () => {
const store = {a:{'id1':{x:true, b:'b'}, 'id2':{x:true, b:'bb'}, 'id3':{x:false, b:'bbb'}}, d:'d'};
expect(persistFilter(store, [{ path: 'a', filterFunction: item => item.x }])).to.deep.equal({a:{'id1':{x:true, b:'b'}, 'id2':{x:true, b:'bb'}}});
expect(persistFilter(store, [{ path: 'a', filterFunction: item => item.b === 'bb' }])).to.deep.equal({a:{'id2':{x:true, b:'bb'}}});
});
});
describe('persistFilter (blacklist)', () => {
it('should return a subset, given one key', () => {
expect(persistFilter({a:'a', b:'b', c:'c'}, 'a', 'blacklist')).to.deep.equal({b:'b', c:'c'});
});
it('should return a subset, given an array of keys', () => {
expect(persistFilter({a:'a', b:'b', c:'c'}, ['a'], 'blacklist')).to.deep.equal({b:'b', c:'c'});
expect(persistFilter({a:'a', b:'b', c:'c'}, ['a', 'b'], 'blacklist')).to.deep.equal({c:'c'});
});
it('should return a subset, given one key path', () => {
expect(persistFilter({a: {b:'b', c:'c'}, d:'d'}, 'a.b', 'blacklist')).to.deep.equal({a: {c:'c'}, d:'d'});
expect(persistFilter({a: {b:'b', c:'c'}, d:'d'}, 'a.c', 'blacklist')).to.deep.equal({a: {b:'b'}, d:'d'});
});
it('should return a subset, given an array of key paths', () => {
expect(persistFilter({a: {b:'b', c:'c'}, d:'d'}, ['a.b'], 'blacklist')).to.deep.equal({a: {c:'c'}, d:'d'});
expect(persistFilter({a: {b:'b', c:'c'}, d:'d'}, [['a', 'b']], 'blacklist')).to.deep.equal({a: {c:'c'}, d:'d'});
expect(persistFilter({a: {b:'b', c:'c'}, d:'d'}, ['a.b', 'a.c'], 'blacklist')).to.deep.equal({a:{}, d:'d'});
expect(persistFilter({a: {b:'b', c:'c'}, d:'d'}, [['a', 'b'], ['a', 'c']], 'blacklist')).to.deep.equal({a:{}, d:'d'});
});
it('should return a subset, given an object that contains a path and a filterFunction', () => {
const store = {a:{'id1':{x:true, b:'b'}, 'id2':{x:true, b:'bb'}, 'id3':{x:false, b:'bbb'}}, d:'d'};
expect(persistFilter(JSON.parse(JSON.stringify(store)), [{ path: 'a', filterFunction: item => item.x }], 'blacklist')).to.deep.equal({a:{'id3':{x:false, b:'bbb'}}, d:'d'});
expect(persistFilter(JSON.parse(JSON.stringify(store)), [{ path: 'a', filterFunction: item => item.b === 'bb' }], 'blacklist')).to.deep.equal({a:{'id1':{x:true, b:'b'}, 'id3':{x:false, b:'bbb'}}, d:'d'});
});
it('should return a subset, given an object that contains a path and a filterFunction to reduce array', () => {
const store = {a:[1,2,3], b:'b'};
expect(persistFilter(JSON.parse(JSON.stringify(store)), [{ path: 'a', filterFunction: () => true }], 'blacklist')).to.deep.equal({a:[], b:'b'});
expect(persistFilter(JSON.parse(JSON.stringify(store)), [{ path: 'a', filterFunction: () => true }], 'blacklist').a.length).to.equal(0);
});
});
describe('createFilter', () => {
it('should be a functions', () => {
expect(createFilter).to.be.a('function');
});
it('should return an object with in and out functions', () => {
const myFilter = createFilter('reducerName', 'a.b', 'a.c');
expect(myFilter).to.be.an('object');
expect(myFilter).to.have.all.keys(['in', 'out']);
expect(myFilter.in).to.be.a('function');
expect(myFilter.out).to.be.a('function');
});
it('should save a subset', () => {
const myFilter = createFilter('reducerName', ['a.b', 'd']);
// simulates a save
const result = myFilter.in({a: {b:'b', c:'c'}, d:'d'}, 'reducerName');
expect(result).to.deep.equal({a: {b:'b'}, d:'d'});
});
it('should load a subset', () => {
const myFilter = createFilter('reducerName', undefined, ['a.b', 'd']);
// simulates a load
const result = myFilter.out({a: {b:'b', c:'c'}, d:'d'}, 'reducerName');
expect(result).to.deep.equal({a: {b:'b'}, d:'d'});
});
});
describe('createWhitelistFilter', () => {
it('should be a functions', () => {
expect(createWhitelistFilter).to.be.a('function');
});
it('should return an object with in and out functions', () => {
const myFilter = createWhitelistFilter('reducerName', 'a.b', 'a.c');
expect(myFilter).to.be.an('object');
expect(myFilter).to.have.all.keys(['in', 'out']);
expect(myFilter.in).to.be.a('function');
expect(myFilter.out).to.be.a('function');
});
it('should save a subset', () => {
const myFilter = createWhitelistFilter('reducerName', ['a.b', 'd']);
// simulates a save
const result = myFilter.in({a: {b:'b', c:'c'}, d:'d'}, 'reducerName');
expect(result).to.deep.equal({a: {b:'b'}, d:'d'});
});
it('should load a subset', () => {
const myFilter = createWhitelistFilter('reducerName', undefined, ['a.b', 'd']);
// simulates a load
const result = myFilter.out({a: {b:'b', c:'c'}, d:'d'}, 'reducerName');
expect(result).to.deep.equal({a: {b:'b'}, d:'d'});
});
});
describe('createBlacklistFilter', () => {
it('should export functions', () => {
expect(createBlacklistFilter).to.be.a('function');
});
it('should return an object with in and out functions', () => {
const myFilter = createBlacklistFilter('reducerName', 'a.b', 'a.c');
expect(myFilter).to.be.an('object');
expect(myFilter).to.have.all.keys(['in', 'out']);
expect(myFilter.in).to.be.a('function');
expect(myFilter.out).to.be.a('function');
});
it('should save a subset', () => {
let state = {a: {b:'b', c:'c'}, d:'d'};
const myFilter = createBlacklistFilter('reducerName', ['a.b', 'd']);
// simulates a save
const result = myFilter.in(state, 'reducerName');
expect(result).to.deep.equal({a: {c:'c'}});
expect(state).to.deep.equal({a: {b:'b', c:'c'}, d:'d'});
});
it('should load a subset', () => {
let state = {a: {b:'b', c:'c'}, d:'d'};
const myFilter = createBlacklistFilter('reducerName', undefined, ['a.b', 'd']);
// simulates a load
const result = myFilter.out(state, 'reducerName');
expect(result).to.deep.equal({a: {c:'c'}});
expect(state).to.deep.equal({a: {b:'b', c:'c'}, d:'d'});
});
});
});