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