index.test.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. "use strict";
  2. var __assign = (this && this.__assign) || function () {
  3. __assign = Object.assign || function(t) {
  4. for (var s, i = 1, n = arguments.length; i < n; i++) {
  5. s = arguments[i];
  6. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  7. t[p] = s[p];
  8. }
  9. return t;
  10. };
  11. return __assign.apply(this, arguments);
  12. };
  13. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  14. return new (P || (P = Promise))(function (resolve, reject) {
  15. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  16. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  17. function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
  18. step((generator = generator.apply(thisArg, _arguments || [])).next());
  19. });
  20. };
  21. var __generator = (this && this.__generator) || function (thisArg, body) {
  22. var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
  23. return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
  24. function verb(n) { return function (v) { return step([n, v]); }; }
  25. function step(op) {
  26. if (f) throw new TypeError("Generator is already executing.");
  27. while (_) try {
  28. if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
  29. if (y = 0, t) op = [op[0] & 2, t.value];
  30. switch (op[0]) {
  31. case 0: case 1: t = op; break;
  32. case 4: _.label++; return { value: op[1], done: false };
  33. case 5: _.label++; y = op[1]; op = [0]; continue;
  34. case 7: op = _.ops.pop(); _.trys.pop(); continue;
  35. default:
  36. if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
  37. if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
  38. if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
  39. if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
  40. if (t[2]) _.ops.pop();
  41. _.trys.pop(); continue;
  42. }
  43. op = body.call(thisArg, _);
  44. } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
  45. if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
  46. }
  47. };
  48. var __rest = (this && this.__rest) || function (s, e) {
  49. var t = {};
  50. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
  51. t[p] = s[p];
  52. if (s != null && typeof Object.getOwnPropertySymbols === "function")
  53. for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) if (e.indexOf(p[i]) < 0)
  54. t[p[i]] = s[p[i]];
  55. return t;
  56. };
  57. var _this = this;
  58. Object.defineProperty(exports, "__esModule", { value: true });
  59. var ava_1 = require("ava");
  60. var fetchMock = require("fetch-mock");
  61. var index_1 = require("../src/index");
  62. ava_1.default('minimal query', function (t) { return __awaiter(_this, void 0, void 0, function () {
  63. var data;
  64. var _this = this;
  65. return __generator(this, function (_a) {
  66. switch (_a.label) {
  67. case 0:
  68. data = {
  69. viewer: {
  70. id: 'some-id',
  71. },
  72. };
  73. return [4 /*yield*/, mock({ body: { data: data } }, function () { return __awaiter(_this, void 0, void 0, function () {
  74. var _a, _b;
  75. return __generator(this, function (_c) {
  76. switch (_c.label) {
  77. case 0:
  78. _b = (_a = t).deepEqual;
  79. return [4 /*yield*/, index_1.request('https://mock-api.com/graphql', "{ viewer { id } }")];
  80. case 1:
  81. _b.apply(_a, [_c.sent(), data]);
  82. return [2 /*return*/];
  83. }
  84. });
  85. }); })];
  86. case 1:
  87. _a.sent();
  88. return [2 /*return*/];
  89. }
  90. });
  91. }); });
  92. ava_1.default('minimal raw query', function (t) { return __awaiter(_this, void 0, void 0, function () {
  93. var data, extensions;
  94. var _this = this;
  95. return __generator(this, function (_a) {
  96. switch (_a.label) {
  97. case 0:
  98. data = {
  99. viewer: {
  100. id: 'some-id',
  101. },
  102. };
  103. extensions = {
  104. version: '1',
  105. };
  106. return [4 /*yield*/, mock({ body: { data: data, extensions: extensions } }, function () { return __awaiter(_this, void 0, void 0, function () {
  107. var _a, headers, result;
  108. return __generator(this, function (_b) {
  109. switch (_b.label) {
  110. case 0: return [4 /*yield*/, index_1.rawRequest('https://mock-api.com/graphql', "{ viewer { id } }")];
  111. case 1:
  112. _a = _b.sent(), headers = _a.headers, result = __rest(_a, ["headers"]);
  113. t.deepEqual(result, { data: data, extensions: extensions, status: 200 });
  114. return [2 /*return*/];
  115. }
  116. });
  117. }); })];
  118. case 1:
  119. _a.sent();
  120. return [2 /*return*/];
  121. }
  122. });
  123. }); });
  124. ava_1.default('minimal raw query with response headers', function (t) { return __awaiter(_this, void 0, void 0, function () {
  125. var data, extensions, reqHeaders;
  126. var _this = this;
  127. return __generator(this, function (_a) {
  128. switch (_a.label) {
  129. case 0:
  130. data = {
  131. viewer: {
  132. id: 'some-id',
  133. },
  134. };
  135. extensions = {
  136. version: '1',
  137. };
  138. reqHeaders = {
  139. 'Content-Type': 'application/json',
  140. 'X-Custom-Header': 'test-custom-header',
  141. };
  142. return [4 /*yield*/, mock({ headers: reqHeaders, body: { data: data, extensions: extensions } }, function () { return __awaiter(_this, void 0, void 0, function () {
  143. var _a, headers, result;
  144. return __generator(this, function (_b) {
  145. switch (_b.label) {
  146. case 0: return [4 /*yield*/, index_1.rawRequest('https://mock-api.com/graphql', "{ viewer { id } }")];
  147. case 1:
  148. _a = _b.sent(), headers = _a.headers, result = __rest(_a, ["headers"]);
  149. t.deepEqual(result, { data: data, extensions: extensions, status: 200 });
  150. t.deepEqual(headers.get('X-Custom-Header'), reqHeaders['X-Custom-Header']);
  151. return [2 /*return*/];
  152. }
  153. });
  154. }); })];
  155. case 1:
  156. _a.sent();
  157. return [2 /*return*/];
  158. }
  159. });
  160. }); });
  161. ava_1.default('basic error', function (t) { return __awaiter(_this, void 0, void 0, function () {
  162. var errors;
  163. var _this = this;
  164. return __generator(this, function (_a) {
  165. switch (_a.label) {
  166. case 0:
  167. errors = {
  168. message: 'Syntax Error GraphQL request (1:1) Unexpected Name "x"\n\n1: x\n ^\n',
  169. locations: [
  170. {
  171. line: 1,
  172. column: 1,
  173. },
  174. ],
  175. };
  176. return [4 /*yield*/, mock({ body: { errors: errors } }, function () { return __awaiter(_this, void 0, void 0, function () {
  177. var err;
  178. return __generator(this, function (_a) {
  179. switch (_a.label) {
  180. case 0: return [4 /*yield*/, t.throwsAsync(index_1.request('https://mock-api.com/graphql', "x"))];
  181. case 1:
  182. err = _a.sent();
  183. t.deepEqual(err.response.errors, errors);
  184. return [2 /*return*/];
  185. }
  186. });
  187. }); })];
  188. case 1:
  189. _a.sent();
  190. return [2 /*return*/];
  191. }
  192. });
  193. }); });
  194. ava_1.default('raw request error', function (t) { return __awaiter(_this, void 0, void 0, function () {
  195. var errors;
  196. var _this = this;
  197. return __generator(this, function (_a) {
  198. switch (_a.label) {
  199. case 0:
  200. errors = {
  201. message: 'Syntax Error GraphQL request (1:1) Unexpected Name "x"\n\n1: x\n ^\n',
  202. locations: [
  203. {
  204. line: 1,
  205. column: 1,
  206. },
  207. ],
  208. };
  209. return [4 /*yield*/, mock({ body: { errors: errors } }, function () { return __awaiter(_this, void 0, void 0, function () {
  210. var err;
  211. return __generator(this, function (_a) {
  212. switch (_a.label) {
  213. case 0: return [4 /*yield*/, t.throwsAsync(index_1.rawRequest('https://mock-api.com/graphql', "x"))];
  214. case 1:
  215. err = _a.sent();
  216. t.deepEqual(err.response.errors, errors);
  217. return [2 /*return*/];
  218. }
  219. });
  220. }); })];
  221. case 1:
  222. _a.sent();
  223. return [2 /*return*/];
  224. }
  225. });
  226. }); });
  227. ava_1.default('content-type with charset', function (t) { return __awaiter(_this, void 0, void 0, function () {
  228. var data;
  229. var _this = this;
  230. return __generator(this, function (_a) {
  231. switch (_a.label) {
  232. case 0:
  233. data = {
  234. viewer: {
  235. id: 'some-id',
  236. },
  237. };
  238. return [4 /*yield*/, mock({
  239. headers: { 'Content-Type': 'application/json; charset=utf-8' },
  240. body: { data: data },
  241. }, function () { return __awaiter(_this, void 0, void 0, function () {
  242. var _a, _b;
  243. return __generator(this, function (_c) {
  244. switch (_c.label) {
  245. case 0:
  246. _b = (_a = t).deepEqual;
  247. return [4 /*yield*/, index_1.request('https://mock-api.com/graphql', "{ viewer { id } }")];
  248. case 1:
  249. _b.apply(_a, [_c.sent(), data]);
  250. return [2 /*return*/];
  251. }
  252. });
  253. }); })];
  254. case 1:
  255. _a.sent();
  256. return [2 /*return*/];
  257. }
  258. });
  259. }); });
  260. ava_1.default('extra fetch options', function (t) { return __awaiter(_this, void 0, void 0, function () {
  261. var options, client;
  262. var _this = this;
  263. return __generator(this, function (_a) {
  264. switch (_a.label) {
  265. case 0:
  266. options = {
  267. credentials: 'include',
  268. mode: 'cors',
  269. cache: 'reload',
  270. };
  271. client = new index_1.GraphQLClient('https://mock-api.com/graphql', options);
  272. return [4 /*yield*/, mock({
  273. body: { data: { test: 'test' } },
  274. }, function () { return __awaiter(_this, void 0, void 0, function () {
  275. var actualOptions, name_1;
  276. return __generator(this, function (_a) {
  277. switch (_a.label) {
  278. case 0: return [4 /*yield*/, client.request('{ test }')];
  279. case 1:
  280. _a.sent();
  281. actualOptions = fetchMock.lastCall()[1];
  282. for (name_1 in options) {
  283. t.deepEqual(actualOptions[name_1], options[name_1]);
  284. }
  285. return [2 /*return*/];
  286. }
  287. });
  288. }); })];
  289. case 1:
  290. _a.sent();
  291. return [2 /*return*/];
  292. }
  293. });
  294. }); });
  295. function mock(response, testFn) {
  296. return __awaiter(this, void 0, void 0, function () {
  297. return __generator(this, function (_a) {
  298. switch (_a.label) {
  299. case 0:
  300. fetchMock.mock({
  301. matcher: '*',
  302. response: {
  303. headers: __assign({ 'Content-Type': 'application/json' }, response.headers),
  304. body: JSON.stringify(response.body),
  305. },
  306. });
  307. return [4 /*yield*/, testFn()];
  308. case 1:
  309. _a.sent();
  310. fetchMock.restore();
  311. return [2 /*return*/];
  312. }
  313. });
  314. });
  315. }
  316. //# sourceMappingURL=index.test.js.map