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