-
Notifications
You must be signed in to change notification settings - Fork 12
/
RxObservableQuery.ts
206 lines (170 loc) · 5.73 KB
/
RxObservableQuery.ts
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import { assert } from 'chai';
import { spy, stub } from 'sinon';
import { ApolloClient, ObservableQuery, ApolloQueryResult } from 'apollo-client';
import * as heroes from './fixtures/heroes';
import { RxObservableQuery } from '../src/RxObservableQuery';
import { ObservableQueryRef } from '../src/utils/ObservableQueryRef';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/do';
import 'rxjs/add/operator/filter';
describe('RxObservableQuery', () => {
let obsQuery: ObservableQuery<heroes.AllHeroesQueryResult>;
let rxObsQuery: RxObservableQuery<heroes.AllHeroesQueryResult>;
let client: ApolloClient;
beforeEach(() => {
const mocked = heroes.mockClient();
client = mocked.client;
obsQuery = mocked.obsQuery;
rxObsQuery = mocked.rxObsQuery;
});
it('should handle ObservableQueryRef', (done) => {
const mockedClient = {
refetch() { done(); },
};
const obsQRef = new ObservableQueryRef(mockedClient as ObservableQuery<any>);
const rxObs = new RxObservableQuery(obsQRef);
rxObs.refetch();
});
describe('regular', () => {
it('should be able to subscribe', () => {
assert.doesNotThrow(() => {
rxObsQuery.subscribe({
next() {
//
},
});
}, Error);
});
it('should be able to receive data', (done: MochaDone) => {
rxObsQuery.subscribe({
next(result) {
assert.deepEqual(result.data, heroes.data);
done();
},
error(error) {
done(error);
},
});
});
it('should be able to use a operator', (done: MochaDone) => {
rxObsQuery
.map(result => result.data)
.subscribe({
next(result) {
assert.deepEqual(result, heroes.data);
done();
},
error() {
done(new Error('should not be called'));
},
});
});
it('should be able to use multiple operators', (done: MochaDone) => {
function onlyFoo(hero: heroes.Hero): boolean {
return !!hero.name.match(/foo/i);
}
rxObsQuery
.map<ApolloQueryResult<heroes.AllHeroesQueryResult>, heroes.Hero[]>
(result => result.data.allHeroes.heroes)
.map(heroes => heroes.filter(onlyFoo))
.subscribe({
next(result) {
assert.deepEqual(result, heroes.data.allHeroes.heroes.filter(onlyFoo));
done();
},
error() {
done(new Error('should not be called'));
},
});
});
it('should be chainable', (done: MochaDone) => {
const counter = { calls: 0 };
function justDoIt() {
counter.calls++;
}
rxObsQuery
.do(justDoIt)
.do(justDoIt)
.do(justDoIt)
.subscribe({
next(result) {
assert.deepEqual(result.data, heroes.data);
assert.equal(counter.calls, 3);
done();
},
error() {
done(new Error('should not be called'));
},
});
});
});
describe('apollo-specific', () => {
it('should be able to refech', () => {
const stubbed = stub(obsQuery, 'refetch').returns('promise');
const promise = rxObsQuery.refetch(heroes.variables);
assert.deepEqual(stubbed.args[0], [heroes.variables]);
assert.equal(promise, 'promise' as any);
});
it('should be able to startPolling', () => {
const stubbed = stub(obsQuery, 'startPolling');
const p = 234;
rxObsQuery.startPolling(p);
assert.deepEqual(stubbed.args[0], [p]);
});
it('should be able to stopPolling', () => {
const spied = spy(obsQuery, 'stopPolling');
rxObsQuery.stopPolling();
assert.isTrue(spied.calledOnce);
});
it('should be able to fetchMore', () => {
const stubbed = stub(obsQuery, 'fetchMore').returns('promise');
const options = {};
const promise = rxObsQuery.fetchMore(options);
assert.deepEqual(stubbed.args[0], [options]);
assert.equal(promise, 'promise' as any);
});
it('should be able to subscribeToMore', () => {
const stubbed = stub(obsQuery, 'subscribeToMore').returns('fn');
const options = {};
const fn = rxObsQuery.subscribeToMore(options);
assert.deepEqual(stubbed.args[0], [options]);
assert.equal(fn, 'fn' as any);
});
it('should be able to updateQuery', () => {
const stubbed = stub(obsQuery, 'updateQuery').returns('void');
const mapFn = () => {
//
};
const result = rxObsQuery.updateQuery(mapFn);
assert.deepEqual(stubbed.args[0], [mapFn]);
assert.equal(result, undefined);
});
it('should be able to use result', () => {
stub(obsQuery, 'result').returns('promise');
const promise = rxObsQuery.result();
assert.equal(promise, 'promise' as any);
});
it('should be able to use currentResult', () => {
stub(obsQuery, 'currentResult').returns('ApolloQueryResult');
const result = rxObsQuery.currentResult();
assert.equal(result, 'ApolloQueryResult');
});
it('should be able to get variables', () => {
assert.isTrue(rxObsQuery.variables === obsQuery.variables);
});
it('should be able to setOptions', () => {
const stubbed = stub(obsQuery, 'setOptions').returns('promise');
const options = {};
const promise = rxObsQuery.setOptions(options);
assert.deepEqual(stubbed.args[0], [options]);
assert.equal(promise, 'promise' as any);
});
it('should be able to setVariables', () => {
const stubbed = stub(obsQuery, 'setVariables').returns('promise');
const variables = {};
const promise = rxObsQuery.setVariables(variables);
assert.deepEqual(stubbed.args[0], [variables, false]);
assert.equal(promise, 'promise' as any);
});
});
});