This repository has been archived by the owner on Apr 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 789
/
mockLink.js
103 lines (103 loc) · 4.36 KB
/
mockLink.js
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
import * as tslib_1 from "tslib";
import { ApolloLink, Observable, } from 'apollo-link';
import { addTypenameToDocument, removeClientSetsFromDocument, removeConnectionDirectiveFromDocument, cloneDeep, isEqual, } from 'apollo-utilities';
import { print } from 'graphql/language/printer';
function requestToKey(request, addTypename) {
var queryString = request.query &&
print(addTypename ? addTypenameToDocument(request.query) : request.query);
var requestKey = { query: queryString };
return JSON.stringify(requestKey);
}
var MockLink = (function (_super) {
tslib_1.__extends(MockLink, _super);
function MockLink(mockedResponses, addTypename) {
if (addTypename === void 0) { addTypename = true; }
var _this = _super.call(this) || this;
_this.addTypename = true;
_this.mockedResponsesByKey = {};
_this.addTypename = addTypename;
if (mockedResponses)
mockedResponses.forEach(function (mockedResponse) {
_this.addMockedResponse(mockedResponse);
});
return _this;
}
MockLink.prototype.addMockedResponse = function (mockedResponse) {
var normalizedMockedResponse = this.normalizeMockedResponse(mockedResponse);
var key = requestToKey(normalizedMockedResponse.request, this.addTypename);
var mockedResponses = this.mockedResponsesByKey[key];
if (!mockedResponses) {
mockedResponses = [];
this.mockedResponsesByKey[key] = mockedResponses;
}
mockedResponses.push(normalizedMockedResponse);
};
MockLink.prototype.request = function (operation) {
var key = requestToKey(operation, this.addTypename);
var responseIndex;
var response = (this.mockedResponsesByKey[key] || []).find(function (res, index) {
var requestVariables = operation.variables || {};
var mockedResponseVariables = res.request.variables || {};
if (!isEqual(requestVariables, mockedResponseVariables)) {
return false;
}
responseIndex = index;
return true;
});
if (!response || typeof responseIndex === 'undefined') {
throw new Error("No more mocked responses for the query: " + print(operation.query) + ", variables: " + JSON.stringify(operation.variables));
}
this.mockedResponsesByKey[key].splice(responseIndex, 1);
var result = response.result, error = response.error, delay = response.delay, newData = response.newData;
if (newData) {
response.result = newData();
this.mockedResponsesByKey[key].push(response);
}
if (!result && !error) {
throw new Error("Mocked response should contain either result or error: " + key);
}
return new Observable(function (observer) {
var timer = setTimeout(function () {
if (error) {
observer.error(error);
}
else {
if (result) {
observer.next(typeof result === 'function'
? result()
: result);
}
observer.complete();
}
}, delay ? delay : 0);
return function () {
clearTimeout(timer);
};
});
};
MockLink.prototype.normalizeMockedResponse = function (mockedResponse) {
var newMockedResponse = cloneDeep(mockedResponse);
newMockedResponse.request.query = removeConnectionDirectiveFromDocument(newMockedResponse.request.query);
var query = removeClientSetsFromDocument(newMockedResponse.request.query);
if (query) {
newMockedResponse.request.query = query;
}
return newMockedResponse;
};
return MockLink;
}(ApolloLink));
export { MockLink };
export function mockSingleLink() {
var mockedResponses = [];
for (var _i = 0; _i < arguments.length; _i++) {
mockedResponses[_i] = arguments[_i];
}
var maybeTypename = mockedResponses[mockedResponses.length - 1];
var mocks = mockedResponses.slice(0, mockedResponses.length - 1);
if (typeof maybeTypename !== 'boolean') {
mocks = mockedResponses;
maybeTypename = true;
}
return new MockLink(mocks, maybeTypename);
}
//# sourceMappingURL=mockLink.js.map