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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190 | 1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
| import ApolloPassport from './index';
import chai from 'chai';
import 'regenerator-runtime/runtime';
const token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ';
const decodedToken = {
"sub": "1234567890",
"name": "John Doe",
"admin": true
};
global.window = global;
const localStorage = window.localStorage = {
items: {},
getItem: (name) => localStorage.items[name],
setItem: (name, value) => localStorage.items[name] = value,
removeItem: (name) => delete localStorage.items[name]
};
const should = chai.should();
describe('ApolloPassport - client', () => {
describe('- constructor', () => {
it('instantiates correctly', () => {
const apolloClient = {};
const ap = new ApolloPassport({ apolloClient });
ap.apolloClient.should.equal(apolloClient);
});
it('runs assertToken if a token exists in localStorage', (done) => {
localStorage.setItem('apToken', token);
const ap = new ApolloPassport({});
ap.assertToken = function() {
ap._token.should.equal(token);
done();
};
});
});
describe('- modules', () => {
it('can be used', () => {
const ap = new ApolloPassport({});
const TestStrategy = function() {};
ap.use('test', TestStrategy);
ap.strategies.test.should.be.an.instanceof(TestStrategy);
});
it('can extend the instance', () => {
const ap = new ApolloPassport({});
const methods = { a: {} };
ap.extendWith(methods);
ap.a.should.equal(methods.a);
});
});
describe('- actions -', () => {
it('loginStart()', () => {
});
describe('loginComplete()', () => {
it('handles errors', () => {
const ap = new ApolloPassport({});
const result = {
errors: []
};
ap.loginComplete(result, 'unused');
// Nothing to test for for now.
});
it('sets state', () => {
const ap = new ApolloPassport({});
const result = {
data: {
passportLoginEmail: { token }
}
};
ap.loginComplete(result, 'passportLoginEmail');
ap.getState().should.deep.equal({
data: decodedToken,
verified: true,
error: null
});
});
});
it('logout() clears state & local storage', () => {
const ap = new ApolloPassport({});
localStorage.setItem('apToken', 'a');
ap.setState({ data: decodedToken, verified: true, error: null });
ap.logout();
const state = ap.getState();
state.data.should.deep.equal({});
state.verified.should.be.false;
});
});
describe('- state', () => {
it('allows removal subscribers', () => {
const ap = new ApolloPassport({});
const test = function() {};
ap.subscribe(test);
ap.unsubscribe(test);
ap._subscribers.has(test).should.be.false;
});
// subscribe+emitState tested in redux suite below.
});
describe('- optional redux support', () => {
describe('- reducer', () => {
const ap = new ApolloPassport({});
const reducer = ap.reducer();
const state = {};
it('changes state on APOLLO_PASSPORT_UPDATE', () => {
reducer({}, {
type: 'APOLLO_PASSPORT_UPDATE',
state: state
}).should.equal(state);
});
it('should return given state on non matching action', () => {
reducer(state, {}).should.equal(state);
});
it('should return getState as initial value', () => {
reducer(undefined, {}).should.equal(ap.getState());
});
});
describe('- middleware', () => {
it('keeps ref to store and returns identity middleware', () => {
const ap = new ApolloPassport({});
const middleware = ap.middleware();
const store = {};
const m = middleware(store);
ap.store.should.equal(store);
// test the identity middleware
const action = {};
m(() => action.test = 1)(action);
action.test.should.equal(1);
});
it('updates redux state on apolloPassport state changes', (done) => {
const ap = new ApolloPassport({});
const middleware = ap.middleware();
const nextState = { data: {}, verified: true, error: null };
const store = {
dispatch(action) {
action.type.should.equal('APOLLO_PASSPORT_UPDATE');
action.state.should.equal(nextState);
done();
}
};
middleware(store);
ap.setState(nextState);
});
});
});
}); |