tempus-api-graphql
Version:
A GraphQL wrapper for the tempus api (tempus2.xyz)
384 lines (356 loc) • 12.4 kB
JavaScript
"use strict";
require("regenerator-runtime/runtime");
var _graphql = require("graphql");
var _axios = _interopRequireDefault(require("axios"));
var _schema = require("../schema");
var _tempus = require("../tempus");
var _fetch_response_by_url = _interopRequireDefault(require("../utils/fetch_response_by_url"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
jest.mock("axios");
var tempus = (0, _tempus.newCachedTempusFetcher)(_fetch_response_by_url["default"]);
function request(_x) {
return _request.apply(this, arguments);
}
function _request() {
_request = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee8(query) {
return regeneratorRuntime.wrap(function _callee8$(_context8) {
while (1) {
switch (_context8.prev = _context8.next) {
case 0:
return _context8.abrupt("return", (0, _graphql.graphql)((0, _schema.newTempusSchema)(tempus), query));
case 1:
case "end":
return _context8.stop();
}
}
}, _callee8);
}));
return _request.apply(this, arguments);
}
var requestStubs = {};
var BASE_URL = "https://tempus2.xyz/api/v0/";
beforeEach(function () {
jest.clearAllMocks();
_axios["default"].get.mockImplementation(function (url) {
var returnValue = requestStubs[url];
if (!returnValue) throw new Error("Unstubbed URL: '".concat(url, "'"));
return Promise.resolve(returnValue);
});
tempus.clearCache();
requestStubs = {};
});
function stubRequest(url, result, error) {
if (error) {
requestStubs["".concat(BASE_URL).concat(url)] = {
error: result
};
} else {
requestStubs["".concat(BASE_URL).concat(url)] = {
data: result
};
}
}
describe("server list", function () {
test("it requests the correct endpoint and works with 0 servers", /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
var r;
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
stubRequest("servers/statusList", []);
_context.next = 3;
return request("{ servers { name }} ");
case 3:
r = _context.sent;
expect(_axios["default"].get).toHaveBeenCalledWith("".concat(BASE_URL, "servers/statusList"));
expect(r).toEqual({
data: {
servers: []
}
});
case 6:
case "end":
return _context.stop();
}
}
}, _callee);
})));
});
describe("activity", function () {
test("requests the correct endpoint and saves the base fields", /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2() {
var r;
return regeneratorRuntime.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
stubRequest("activity", {
map_wrs: [{
map_info: {
name: "jump_rush"
},
player_info: {
name: "Player"
},
record_info: {
duration: 45.67
}
}]
});
_context2.next = 3;
return request("{ activity { mapWrs { duration player { name } map { name } } }} ");
case 3:
r = _context2.sent;
expect(_axios["default"].get).toHaveBeenCalledWith("".concat(BASE_URL, "activity"));
expect(r).toEqual({
data: {
activity: {
mapWrs: [{
player: {
name: "Player"
},
map: {
name: "jump_rush"
},
duration: 45.67
}]
}
}
});
case 6:
case "end":
return _context2.stop();
}
}
}, _callee2);
})));
test("requests related fields", /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3() {
var r;
return regeneratorRuntime.wrap(function _callee3$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
stubRequest("activity", {
map_wrs: [{
map_info: {
name: "jump_rush"
},
player_info: {
name: "Player",
id: 2
},
record_info: {
duration: 45.67
}
}]
});
stubRequest("players/id/2/stats", {
player_info: {
country: "Australia"
}
});
stubRequest("maps/name/jump_rush/fullOverview2", {
map_info: {
id: 8,
name: "jump_rush"
},
authors: [{
name: "QuBa"
}]
});
_context3.next = 5;
return request("{ activity { mapWrs { duration player { name country } map { name authors { name }} } }} ");
case 5:
r = _context3.sent;
expect(r).toEqual({
data: {
activity: {
mapWrs: [{
player: {
name: "Player",
country: "Australia"
},
map: {
name: "jump_rush",
authors: [{
name: "QuBa"
}]
},
duration: 45.67
}]
}
}
});
expect(_axios["default"].get).toHaveBeenCalledWith("".concat(BASE_URL, "activity"));
expect(_axios["default"].get).toHaveBeenCalledWith("".concat(BASE_URL, "players/id/2/stats"));
expect(_axios["default"].get).toHaveBeenCalledWith("".concat(BASE_URL, "maps/name/jump_rush/fullOverview2"));
case 10:
case "end":
return _context3.stop();
}
}
}, _callee3);
})));
});
describe("map", function () {
test("requests the correct endpoint and saves the base fields", /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee4() {
var r;
return regeneratorRuntime.wrap(function _callee4$(_context4) {
while (1) {
switch (_context4.prev = _context4.next) {
case 0:
stubRequest("maps/name/jump_rush/fullOverview2", {
map_info: {
name: "jump_rush",
id: 111,
date_added: 100
},
tier_info: {
soldier: 1,
demoman: 1
},
videos: {
soldier: "url",
demoman: null
},
authors: [{
name: "Bob",
id: 2,
map_count: 7,
player_info: {
id: 201,
name: "bob",
steamid: "steeeeamid"
}
}]
});
_context4.next = 3;
return request('{ map(name: "jump_rush") { id name tiers { soldier demoman } videos { soldier demoman } authors { name id mapCount player { name id steamId }}}}');
case 3:
r = _context4.sent;
expect(_axios["default"].get).toHaveBeenCalledWith("".concat(BASE_URL, "maps/name/jump_rush/fullOverview2"));
expect(r).toEqual({
data: {
map: {
id: "111",
name: "jump_rush",
tiers: {
soldier: 1,
demoman: 1
},
videos: {
soldier: "https://youtube.com/watch?v=url",
demoman: null
},
authors: [{
name: "Bob",
id: 2,
mapCount: 7,
player: {
name: "bob",
id: 201,
steamId: "steeeeamid"
}
}]
}
}
});
case 6:
case "end":
return _context4.stop();
}
}
}, _callee4);
})));
test.skip("requests map runs", /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee5() {
return regeneratorRuntime.wrap(function _callee5$(_context5) {
while (1) {
switch (_context5.prev = _context5.next) {
case 0:
case "end":
return _context5.stop();
}
}
}, _callee5);
})));
test.skip("requests records", /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee6() {
return regeneratorRuntime.wrap(function _callee6$(_context6) {
while (1) {
switch (_context6.prev = _context6.next) {
case 0:
case "end":
return _context6.stop();
}
}
}, _callee6);
})));
test("request map wrs", /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee7() {
var r;
return regeneratorRuntime.wrap(function _callee7$(_context7) {
while (1) {
switch (_context7.prev = _context7.next) {
case 0:
stubRequest("maps/name/jump_rush/wrs", {
soldier: {
wr: {
duration: 5.0,
zone_id: 1,
splits: [{
type: "checkpoint",
zoneindex: 1,
duration: 1.0,
compared_duration: 2.0
}]
},
rank2: {
duration: 10.0
}
}
});
stubRequest("maps/name/jump_rush/fullOverview2", {
map_info: {
name: "jump_rush",
id: 111
}
});
_context7.next = 4;
return request('{ map(name: "jump_rush") { wr(class: SOLDIER) { zone { id type map { name id } } splits { type zoneindex duration comparedDuration } } }}');
case 4:
r = _context7.sent;
expect(_axios["default"].get).toHaveBeenCalledWith("".concat(BASE_URL, "maps/name/jump_rush/wrs"));
expect(r).toEqual({
data: {
map: {
wr: {
zone: {
id: 1,
type: "map",
map: {
name: "jump_rush",
id: "111"
}
},
splits: [{
type: "checkpoint",
zoneindex: 1,
duration: 1.0,
comparedDuration: 2.0
}, {
type: "map",
zoneindex: 1,
duration: 5.0,
comparedDuration: 10.0
}]
}
}
}
});
case 7:
case "end":
return _context7.stop();
}
}
}, _callee7);
})));
});