react-relay-network-layer
Version:
Network Layer for React Relay and Express (Batch Queries, AuthToken, Logging, Retry)
113 lines (110 loc) • 3.4 kB
JavaScript
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); }); }; }
import fetchMock from 'fetch-mock';
import { RelayNetworkLayer } from '..';
import { mockReq } from '../__mocks__/mockReq';
describe('Queries tests', () => {
const middlewares = [];
const rnl = new RelayNetworkLayer(middlewares);
beforeEach(() => {
fetchMock.restore();
});
it('should make a successfull query',
/*#__PURE__*/
_asyncToGenerator(function* () {
fetchMock.mock({
matcher: '/graphql',
response: {
status: 200,
body: {
data: {
ok: 1
}
},
sendAsJson: true
},
method: 'POST'
});
const req = mockReq();
yield rnl.sendQueries([req]);
expect(req.payload).toEqual({
response: {
ok: 1
}
});
}));
it('should fail correctly on network failure',
/*#__PURE__*/
_asyncToGenerator(function* () {
fetchMock.mock({
matcher: '/graphql',
response: {
throws: new Error('Network connection error')
},
method: 'POST'
});
const req1 = mockReq();
expect.assertions(2);
try {
yield rnl.sendQueries([req1]);
} catch (e) {
expect(e instanceof Error).toBeTruthy();
expect(e.toString()).toMatch('Network connection error');
}
}));
it('should handle error response',
/*#__PURE__*/
_asyncToGenerator(function* () {
fetchMock.mock({
matcher: '/graphql',
response: {
status: 200,
body: {
errors: [{
location: 1,
message: 'major error'
}]
}
},
method: 'POST'
});
const req1 = mockReq(1);
yield rnl.sendQueries([req1]).catch(() => {});
expect(req1.error instanceof Error).toBeTruthy();
}));
it('should handle server non-2xx errors',
/*#__PURE__*/
_asyncToGenerator(function* () {
fetchMock.mock({
matcher: '/graphql',
response: {
status: 500,
body: 'Something went completely wrong.'
},
method: 'POST'
});
const req1 = mockReq(1);
yield rnl.sendQueries([req1]).catch(() => {});
const error = req1.error;
expect(error instanceof Error).toBeTruthy();
expect(error.message).toEqual('Something went completely wrong.');
expect(error.fetchResponse.status).toEqual(500);
}));
it('should fail on missing `data` property',
/*#__PURE__*/
_asyncToGenerator(function* () {
fetchMock.mock({
matcher: '/graphql',
response: {
status: 200,
body: {},
sendAsJson: true
},
method: 'POST'
});
const req = mockReq();
yield rnl.sendQueries([req]).catch(() => {});
const error = req.error;
expect(error.toString()).toMatch('Server return empty `response.data`');
}));
});