1 | import { ApolloModule, Apollo } from 'apollo-angular';
|
2 | import { Observable, ApolloLink } from 'apollo-link';
|
3 | import { InMemoryCache } from 'apollo-cache-inmemory';
|
4 | import { ApolloCache } from 'apollo-cache';
|
5 | import { Injectable, InjectionToken, NgModule, Optional, Inject } from '@angular/core';
|
6 | import { print } from 'graphql';
|
7 | import { ApolloError } from 'apollo-client';
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | class ApolloTestingController {
|
16 | }
|
17 |
|
18 | const isApolloError = (err) => err && err.hasOwnProperty('graphQLErrors');
|
19 | const ɵ0 = isApolloError;
|
20 | class TestOperation {
|
21 | constructor(operation, observer) {
|
22 | this.operation = operation;
|
23 | this.observer = observer;
|
24 | }
|
25 | flush(result) {
|
26 | if (isApolloError(result)) {
|
27 | this.observer.error(result);
|
28 | }
|
29 | else {
|
30 | this.observer.next(result);
|
31 | this.observer.complete();
|
32 | }
|
33 | }
|
34 | networkError(error) {
|
35 | const apolloError = new ApolloError({
|
36 | networkError: error,
|
37 | });
|
38 | this.flush(apolloError);
|
39 | }
|
40 | graphqlErrors(errors) {
|
41 | this.flush({
|
42 | errors,
|
43 | });
|
44 | }
|
45 | }
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 | class ApolloTestingBackend {
|
56 | constructor() {
|
57 | |
58 |
|
59 |
|
60 | this.open = [];
|
61 | }
|
62 | |
63 |
|
64 |
|
65 | handle(op) {
|
66 | return new Observable((observer) => {
|
67 | const testOp = new TestOperation(op, observer);
|
68 | this.open.push(testOp);
|
69 | });
|
70 | }
|
71 | |
72 |
|
73 |
|
74 | _match(match) {
|
75 | if (typeof match === 'string') {
|
76 | return this.open.filter(testOp => testOp.operation.operationName === match);
|
77 | }
|
78 | else if (typeof match === 'function') {
|
79 | return this.open.filter(testOp => match(testOp.operation));
|
80 | }
|
81 | else {
|
82 | if (this.isDocumentNode(match)) {
|
83 | return this.open.filter(testOp => print(testOp.operation.query) === print(match));
|
84 | }
|
85 | return this.open.filter(testOp => this.matchOp(match, testOp));
|
86 | }
|
87 | }
|
88 | matchOp(match, testOp) {
|
89 | const variables = JSON.stringify(match.variables);
|
90 | const extensions = JSON.stringify(match.extensions);
|
91 | const sameName = this.compare(match.operationName, testOp.operation.operationName);
|
92 | const sameVariables = this.compare(variables, testOp.operation.variables);
|
93 | const sameQuery = print(testOp.operation.query) === print(match.query);
|
94 | const sameExtensions = this.compare(extensions, testOp.operation.extensions);
|
95 | return sameName && sameVariables && sameQuery && sameExtensions;
|
96 | }
|
97 | compare(expected, value) {
|
98 | const prepare = (val) => typeof val === 'string' ? val : JSON.stringify(val);
|
99 | const received = prepare(value);
|
100 | return !expected || received === expected;
|
101 | }
|
102 | |
103 |
|
104 |
|
105 |
|
106 | match(match) {
|
107 | const results = this._match(match);
|
108 | results.forEach(result => {
|
109 | const index = this.open.indexOf(result);
|
110 | if (index !== -1) {
|
111 | this.open.splice(index, 1);
|
112 | }
|
113 | });
|
114 | return results;
|
115 | }
|
116 | |
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 | expectOne(match, description) {
|
124 | description = description || this.descriptionFromMatcher(match);
|
125 | const matches = this.match(match);
|
126 | if (matches.length > 1) {
|
127 | throw new Error(`Expected one matching operation for criteria "${description}", found ${matches.length} operations.`);
|
128 | }
|
129 | if (matches.length === 0) {
|
130 | throw new Error(`Expected one matching operation for criteria "${description}", found none.`);
|
131 | }
|
132 | return matches[0];
|
133 | }
|
134 | |
135 |
|
136 |
|
137 |
|
138 | expectNone(match, description) {
|
139 | description = description || this.descriptionFromMatcher(match);
|
140 | const matches = this.match(match);
|
141 | if (matches.length > 0) {
|
142 | throw new Error(`Expected zero matching operations for criteria "${description}", found ${matches.length}.`);
|
143 | }
|
144 | }
|
145 | |
146 |
|
147 |
|
148 | verify() {
|
149 | const open = this.open;
|
150 | if (open.length > 0) {
|
151 |
|
152 | const operations = open
|
153 | .map(testOp => testOp.operation.operationName)
|
154 | .join(', ');
|
155 | throw new Error(`Expected no open operations, found ${open.length}: ${operations}`);
|
156 | }
|
157 | }
|
158 | isDocumentNode(docOrOp) {
|
159 | return !docOrOp.operationName;
|
160 | }
|
161 | descriptionFromMatcher(matcher) {
|
162 | if (typeof matcher === 'string') {
|
163 | return `Match operationName: ${matcher}`;
|
164 | }
|
165 | else if (typeof matcher === 'object') {
|
166 | if (this.isDocumentNode(matcher)) {
|
167 | return `Match DocumentNode`;
|
168 | }
|
169 | const name = matcher.operationName || '(any)';
|
170 | const variables = JSON.stringify(matcher.variables) || '(any)';
|
171 | return `Match operation: ${name}, variables: ${variables}`;
|
172 | }
|
173 | else {
|
174 | return `Match by function: ${matcher.name}`;
|
175 | }
|
176 | }
|
177 | }
|
178 | ApolloTestingBackend.decorators = [
|
179 | { type: Injectable }
|
180 | ];
|
181 |
|
182 | const APOLLO_TESTING_CACHE = new InjectionToken('apollo-angular/testing cache');
|
183 | const APOLLO_TESTING_NAMED_CACHE = new InjectionToken('apollo-angular/testing named cache');
|
184 | const APOLLO_TESTING_CLIENTS = new InjectionToken('apollo-angular/testing named clients');
|
185 | function addClient(name, op) {
|
186 | op.clientName = name;
|
187 | return op;
|
188 | }
|
189 | class ApolloTestingModuleCore {
|
190 | constructor(apollo, backend, namedClients, cache, namedCaches) {
|
191 | function createOptions(name, c) {
|
192 | return {
|
193 | link: new ApolloLink((operation) => backend.handle(addClient(name, operation))),
|
194 | cache: c ||
|
195 | new InMemoryCache({
|
196 | addTypename: false,
|
197 | }),
|
198 | };
|
199 | }
|
200 | apollo.create(createOptions('default', cache));
|
201 | if (namedClients && namedClients.length) {
|
202 | namedClients.forEach((name) => {
|
203 | const caches = namedCaches && typeof namedCaches === 'object' ? namedCaches : {};
|
204 | apollo.createNamed(name, createOptions(name, caches[name]));
|
205 | });
|
206 | }
|
207 | }
|
208 | }
|
209 | ApolloTestingModuleCore.decorators = [
|
210 | { type: NgModule, args: [{
|
211 | imports: [ApolloModule],
|
212 | providers: [
|
213 | ApolloTestingBackend,
|
214 | { provide: ApolloTestingController, useExisting: ApolloTestingBackend },
|
215 | ],
|
216 | },] }
|
217 | ];
|
218 | ApolloTestingModuleCore.ctorParameters = () => [
|
219 | { type: Apollo },
|
220 | { type: ApolloTestingBackend },
|
221 | { type: Array, decorators: [{ type: Optional }, { type: Inject, args: [APOLLO_TESTING_CLIENTS,] }] },
|
222 | { type: ApolloCache, decorators: [{ type: Optional }, { type: Inject, args: [APOLLO_TESTING_CACHE,] }] },
|
223 | { type: undefined, decorators: [{ type: Optional }, { type: Inject, args: [APOLLO_TESTING_NAMED_CACHE,] }] }
|
224 | ];
|
225 | class ApolloTestingModule {
|
226 | static withClients(names) {
|
227 | return {
|
228 | ngModule: ApolloTestingModuleCore,
|
229 | providers: [
|
230 | {
|
231 | provide: APOLLO_TESTING_CLIENTS,
|
232 | useValue: names,
|
233 | },
|
234 | ],
|
235 | };
|
236 | }
|
237 | }
|
238 | ApolloTestingModule.decorators = [
|
239 | { type: NgModule, args: [{
|
240 | imports: [ApolloTestingModuleCore],
|
241 | },] }
|
242 | ];
|
243 |
|
244 |
|
245 |
|
246 |
|
247 |
|
248 | export { APOLLO_TESTING_CACHE, APOLLO_TESTING_NAMED_CACHE, ApolloTestingController, ApolloTestingModule, TestOperation, APOLLO_TESTING_CLIENTS as ɵa, ApolloTestingModuleCore as ɵb, ApolloTestingBackend as ɵc };
|
249 |
|