mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
Fix unmock qs module, Backend-test refactored to not time out on error
This commit is contained in:
parent
287809ec3b
commit
30d161625f
@ -320,12 +320,14 @@ class Backend {
|
|||||||
{ setFromData: (refinedSpec.method.toLowerCase() === 'get') }
|
{ setFromData: (refinedSpec.method.toLowerCase() === 'get') }
|
||||||
);
|
);
|
||||||
|
|
||||||
const encodedData = encode(
|
const encodedData = (refinedSpec.method.toLowerCase() !== 'get')
|
||||||
refinedSpec.payloadFormat,
|
? encode(
|
||||||
// Filter raw data through the defined schema,
|
refinedSpec.payloadFormat,
|
||||||
// potentially removing keys because they're
|
// Filter raw data through the defined schema,
|
||||||
applySchemaToData(refinedSpec.payloadSchema, mergedData)
|
// potentially removing keys because they're
|
||||||
);
|
applySchemaToData(refinedSpec.payloadSchema, mergedData)
|
||||||
|
)
|
||||||
|
: '';
|
||||||
|
|
||||||
const args = refinedSpec.method.toLowerCase() === 'get'
|
const args = refinedSpec.method.toLowerCase() === 'get'
|
||||||
? [url, mergedHeaders]
|
? [url, mergedHeaders]
|
||||||
|
@ -8,6 +8,18 @@ jest.unmock('merge');
|
|||||||
|
|
||||||
import backend from '../Backend';
|
import backend from '../Backend';
|
||||||
|
|
||||||
|
// Mock out the get/post/put/delete methods in the backend
|
||||||
|
// So that we can isolate our test to the behaviour of createEndpointFetcher()
|
||||||
|
// The mocked getters will pass returnValue to the resulting promise's then() call
|
||||||
|
function getBackendMock(returnValue) {
|
||||||
|
return Object.assign(backend, {
|
||||||
|
get: getMockPromise(returnValue),
|
||||||
|
post: getMockPromise(returnValue),
|
||||||
|
put: getMockPromise(returnValue),
|
||||||
|
delete: getMockPromise(returnValue),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return a mock function that returns a promise
|
* Return a mock function that returns a promise
|
||||||
*/
|
*/
|
||||||
@ -117,26 +129,19 @@ describe('Backend', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('createEndpointFetcher()', () => {
|
describe('createEndpointFetcher()', () => {
|
||||||
// Mock out the get/post/put/delete methods in the backend
|
let mock = null;
|
||||||
// So that we can isolate our test to the behaviour of createEndpointFetcher()
|
|
||||||
// The mocked getters will pass returnValue to the resulting promise's then() call
|
beforeEach(() => {
|
||||||
function getBackendMock(returnValue) {
|
mock = getBackendMock({
|
||||||
return Object.assign(backend, {
|
// mock response body and headers
|
||||||
get: getMockPromise(returnValue),
|
text: () => Promise.resolve('{"status":"ok"}'),
|
||||||
post: getMockPromise(returnValue),
|
headers: {
|
||||||
put: getMockPromise(returnValue),
|
get: () => 'application/json',
|
||||||
delete: getMockPromise(returnValue),
|
},
|
||||||
});
|
});
|
||||||
}
|
});
|
||||||
|
|
||||||
it('should add querystring to the URL for GET requests', (done) => {
|
it('should add querystring to the URL for GET requests', (done) => {
|
||||||
const mock = getBackendMock({
|
|
||||||
text: () => Promise.resolve('{"status":"ok","message":"happy"}'),
|
|
||||||
headers: new Headers({
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
|
|
||||||
const endpoint = mock.createEndpointFetcher({
|
const endpoint = mock.createEndpointFetcher({
|
||||||
url: 'http://example.org',
|
url: 'http://example.org',
|
||||||
method: 'get',
|
method: 'get',
|
||||||
@ -145,25 +150,21 @@ describe('Backend', () => {
|
|||||||
|
|
||||||
const promise = endpoint({ id: 1, values: { a: 'aye', b: 'bee' } });
|
const promise = endpoint({ id: 1, values: { a: 'aye', b: 'bee' } });
|
||||||
|
|
||||||
return promise.then(() => {
|
expect(mock.get)
|
||||||
expect(mock.get.mock.calls[0][0])
|
.toBeCalledWith(
|
||||||
.toEqual('http://example.org?id=1&values%5Ba%5D=aye&values%5Bb%5D=bee');
|
'http://example.org?id=1&values%5Ba%5D=aye&values%5Bb%5D=bee',
|
||||||
expect(mock.get.mock.calls[0][1])
|
{
|
||||||
.toEqual({
|
|
||||||
Accept: 'application/json',
|
Accept: 'application/json',
|
||||||
'Content-Type': 'application/x-www-form-urlencoded',
|
'Content-Type': 'application/x-www-form-urlencoded',
|
||||||
});
|
}
|
||||||
done();
|
);
|
||||||
});
|
|
||||||
|
return promise
|
||||||
|
.catch((e) => expect(e).toBeFalsy())
|
||||||
|
.then(done);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should pass a JSON payload', (done) => {
|
it('should pass a JSON payload', (done) => {
|
||||||
const mock = getBackendMock({
|
|
||||||
text: () => Promise.resolve('{"status":"ok","message":"happy"}'),
|
|
||||||
headers: new Headers({
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
const endpoint = mock.createEndpointFetcher({
|
const endpoint = mock.createEndpointFetcher({
|
||||||
url: 'http://example.org',
|
url: 'http://example.org',
|
||||||
method: 'post',
|
method: 'post',
|
||||||
@ -173,25 +174,22 @@ describe('Backend', () => {
|
|||||||
|
|
||||||
const promise = endpoint({ id: 1, values: { a: 'aye', b: 'bee' } });
|
const promise = endpoint({ id: 1, values: { a: 'aye', b: 'bee' } });
|
||||||
|
|
||||||
return promise.then((result) => {
|
expect(mock.post)
|
||||||
expect(mock.post.mock.calls[0][0]).toEqual('http://example.org');
|
.toBeCalledWith(
|
||||||
expect(mock.post.mock.calls[0][1]).toEqual('{"id":1,"values":{"a":"aye","b":"bee"}}');
|
'http://example.org',
|
||||||
expect(mock.post.mock.calls[0][2]).toEqual({
|
'{"id":1,"values":{"a":"aye","b":"bee"}}',
|
||||||
Accept: 'application/json',
|
{
|
||||||
'Content-Type': 'application/json',
|
Accept: 'application/json',
|
||||||
});
|
'Content-Type': 'application/json',
|
||||||
expect(result).toEqual({ status: 'ok', message: 'happy' });
|
}
|
||||||
done();
|
);
|
||||||
});
|
|
||||||
|
return promise
|
||||||
|
.catch((e) => expect(e).toBeFalsy())
|
||||||
|
.then(done);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should replace url template parameters', (done) => {
|
it('should replace url template parameters', (done) => {
|
||||||
const mock = getBackendMock({
|
|
||||||
text: () => Promise.resolve('{"status":"ok"}'),
|
|
||||||
headers: new Headers({
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
const endpoint = mock.createEndpointFetcher({
|
const endpoint = mock.createEndpointFetcher({
|
||||||
url: 'http://example.com/:one/:two/?foo=bar',
|
url: 'http://example.com/:one/:two/?foo=bar',
|
||||||
method: 'post',
|
method: 'post',
|
||||||
@ -206,20 +204,22 @@ describe('Backend', () => {
|
|||||||
three: 3,
|
three: 3,
|
||||||
});
|
});
|
||||||
|
|
||||||
return promise.then(() => {
|
expect(mock.post)
|
||||||
expect(mock.post.mock.calls[0][0]).toEqual('http://example.com/1/2/?foo=bar');
|
.toBeCalledWith(
|
||||||
expect(mock.post.mock.calls[0][1]).toEqual('two=2&three=3');
|
'http://example.com/1/2/?foo=bar',
|
||||||
done();
|
'two=2&three=3',
|
||||||
});
|
{
|
||||||
|
Accept: 'application/json',
|
||||||
|
'Content-Type': 'application/x-www-form-urlencoded',
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
return promise
|
||||||
|
.catch((e) => expect(e).toBeFalsy())
|
||||||
|
.then(done);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should add query parameters from spec for non-GET data', (done) => {
|
it('should add query parameters from spec for non-GET data', (done) => {
|
||||||
const mock = getBackendMock({
|
|
||||||
text: () => Promise.resolve('{"status":"ok"}'),
|
|
||||||
headers: new Headers({
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
const endpoint = mock.createEndpointFetcher({
|
const endpoint = mock.createEndpointFetcher({
|
||||||
url: 'http://example.com/:one/:two/?foo=bar',
|
url: 'http://example.com/:one/:two/?foo=bar',
|
||||||
method: 'post',
|
method: 'post',
|
||||||
@ -236,20 +236,22 @@ describe('Backend', () => {
|
|||||||
three: 3,
|
three: 3,
|
||||||
});
|
});
|
||||||
|
|
||||||
return promise.then(() => {
|
expect(mock.post)
|
||||||
expect(mock.post.mock.calls[0][0]).toEqual('http://example.com/1/2/?foo=bar&three=3');
|
.toBeCalledWith(
|
||||||
expect(mock.post.mock.calls[0][1]).toEqual('{"two":2}');
|
'http://example.com/1/2/?foo=bar&three=3',
|
||||||
done();
|
'{"two":2}',
|
||||||
});
|
{
|
||||||
|
Accept: 'application/json',
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
return promise
|
||||||
|
.catch((e) => expect(e).toBeFalsy())
|
||||||
|
.then(done);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should add query parameters from payload for GET data', (done) => {
|
it('should add query parameters from payload for GET data', (done) => {
|
||||||
const mock = getBackendMock({
|
|
||||||
text: () => Promise.resolve('{"status":"ok"}'),
|
|
||||||
headers: new Headers({
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
const endpoint = mock.createEndpointFetcher({
|
const endpoint = mock.createEndpointFetcher({
|
||||||
url: 'http://example.com/:one/:two/?foo=bar',
|
url: 'http://example.com/:one/:two/?foo=bar',
|
||||||
method: 'get',
|
method: 'get',
|
||||||
@ -265,23 +267,21 @@ describe('Backend', () => {
|
|||||||
three: 3,
|
three: 3,
|
||||||
});
|
});
|
||||||
|
|
||||||
return promise.then(() => {
|
expect(mock.get)
|
||||||
expect(mock.get.mock.calls[0][0]).toEqual('http://example.com/1/2/?foo=bar&two=2&three=3');
|
.toBeCalledWith(
|
||||||
expect(mock.get.mock.calls[0][1]).toEqual({
|
'http://example.com/1/2/?foo=bar&two=2&three=3',
|
||||||
Accept: 'application/json',
|
{
|
||||||
'Content-Type': 'application/x-www-form-urlencoded',
|
Accept: 'application/json',
|
||||||
});
|
'Content-Type': 'application/x-www-form-urlencoded',
|
||||||
done();
|
}
|
||||||
});
|
);
|
||||||
|
|
||||||
|
return promise
|
||||||
|
.catch((e) => expect(e).toBeFalsy())
|
||||||
|
.then(done);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should merge defaultData into data argument', (done) => {
|
it('should merge defaultData into data argument', (done) => {
|
||||||
const mock = getBackendMock({
|
|
||||||
text: () => Promise.resolve('{"status":"ok"}'),
|
|
||||||
headers: new Headers({
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
const endpoint = mock.createEndpointFetcher({
|
const endpoint = mock.createEndpointFetcher({
|
||||||
url: 'http://example.com/',
|
url: 'http://example.com/',
|
||||||
method: 'post',
|
method: 'post',
|
||||||
@ -294,16 +294,24 @@ describe('Backend', () => {
|
|||||||
four: { fourTwo: true },
|
four: { fourTwo: true },
|
||||||
});
|
});
|
||||||
|
|
||||||
return promise.then(() => {
|
expect(mock.post)
|
||||||
expect(mock.post.mock.calls[0][0]).toEqual('http://example.com/');
|
.toBeCalledWith(
|
||||||
expect(mock.post.mock.calls[0][1]).toEqual(JSON.stringify({
|
'http://example.com/',
|
||||||
one: 1,
|
JSON.stringify({
|
||||||
two: 'updated',
|
one: 1,
|
||||||
four: { fourOne: true, fourTwo: true },
|
two: 'updated',
|
||||||
three: 3,
|
four: { fourOne: true, fourTwo: true },
|
||||||
}));
|
three: 3,
|
||||||
done();
|
}),
|
||||||
});
|
{
|
||||||
|
Accept: 'application/json',
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
return promise
|
||||||
|
.catch((e) => expect(e).toBeFalsy())
|
||||||
|
.then(done);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -102,7 +102,8 @@
|
|||||||
"testDirectoryName": "tests",
|
"testDirectoryName": "tests",
|
||||||
"mocksPattern": "mocks",
|
"mocksPattern": "mocks",
|
||||||
"unmockedModulePathPatterns": [
|
"unmockedModulePathPatterns": [
|
||||||
"<rootDir>/node_modules/react"
|
"<rootDir>/node_modules/react",
|
||||||
|
"<rootDir>/node_modules/qs"
|
||||||
],
|
],
|
||||||
"bail": true,
|
"bail": true,
|
||||||
"testRunner": "<rootDir>/node_modules/jest-cli/src/testRunners/jasmine/jasmine2.js"
|
"testRunner": "<rootDir>/node_modules/jest-cli/src/testRunners/jasmine/jasmine2.js"
|
||||||
|
Loading…
Reference in New Issue
Block a user