Skip to content

Commit

Permalink
Fix withSomeParams for GET requests (#397)
Browse files Browse the repository at this point in the history
thanks again form being diligent Adam
  • Loading branch information
misterbyrne authored and danielspaniel committed Jun 24, 2019
1 parent 5f7f1d2 commit 359853b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 41 deletions.
13 changes: 4 additions & 9 deletions addon/mocks/mock-any-request.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import MockRequest from './mock-request';
import {
isEmptyObject, isEquivalent, isPartOf, paramsFromRequestBody, param, toParams
isEmptyObject, isEquivalent, isPartOf, paramsFromRequestBody, toParams
} from "../utils/helper-functions";

export default class MockAnyRequest extends MockRequest {
Expand Down Expand Up @@ -46,17 +46,12 @@ export default class MockAnyRequest extends MockRequest {
}

_tryMatchParams(request, handlerParams, comparisonFunction) {
if (this.type === 'GET') {
return comparisonFunction(param(request.queryParams), param(handlerParams));
}
let requestParams = request.queryParams;

if (/POST|PUT|PATCH/.test(this.type)) {
const requestBody = request.requestBody,
requestParams = paramsFromRequestBody(requestBody);
return comparisonFunction(toParams(requestParams), toParams(handlerParams));
requestParams = paramsFromRequestBody(request.requestBody);
}

return false;
return comparisonFunction(toParams(requestParams), toParams(handlerParams));
}

}
50 changes: 18 additions & 32 deletions tests/unit/mocks/mock-any-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,42 +164,28 @@ module('MockAny', function(hooks) {
assert.equal(theMock.timesCalled, 1, 'mock#timesCalled is called once');
});

test("#withParams", async function(assert) {
const method = 'POST',
url = '/api/post/some-stuff',
dataFoo = {foo: 'foo'},
dataBar = {bar: 'bar'};

let fooMock = mock({url: '/api/post/some-stuff', type: method}).withParams(dataFoo);
let barMock = mock({url: '/api/post/some-stuff', type: method}).withParams(dataBar);
assert.equal(fooMock.timesCalled, 0, 'fooMock#timesCalled is initially 0');
assert.equal(barMock.timesCalled, 0, 'barMock#timesCalled is initially 0');

await fetchJSON({url, params: dataFoo, method});
await fetchJSON({url, params: dataBar, method});

assert.equal(fooMock.timesCalled, 1, 'fooMock#timesCalled is called once');
assert.equal(barMock.timesCalled, 1, 'barMock#timesCalled is called once');
});

test("#withSomeParams", async function(assert) {
const method = 'POST',
url = '/api/post/some-stuff',
dataFoo = {foo: 'foo'},
dataBar = {bar: 'bar'};
function testWithSomeParamsForMethod(method) {
test(`#withSomeParams works with complex parameters for ${method} requests`, async function(assert) {
const url = '/api/post/some-stuff',
dataFoo = {foo: 'foo', array: ['a', { b: 123 }, 'c'], obj: { a: 321 } },
dataBar = {bar: 'bar', array: [1, { x: 2 }, 3], obj: { x: 321 } };

let fooMock = mock({url: '/api/post/some-stuff', type: method}).withSomeParams(dataFoo);
let barMock = mock({url: '/api/post/some-stuff', type: method}).withSomeParams(dataBar);
let fooMock = mock({url, type: method}).withSomeParams(dataFoo);
let barMock = mock({url, type: method}).withSomeParams(dataBar);
assert.equal(fooMock.timesCalled, 0, 'fooMock#timesCalled is initially 0');
assert.equal(barMock.timesCalled, 0, 'barMock#timesCalled is initially 0');

assert.equal(fooMock.timesCalled, 0, 'fooMock#timesCalled is initially 0');
assert.equal(barMock.timesCalled, 0, 'barMock#timesCalled is initially 0');
await fetchJSON({url, method, params: Object.assign({ extra: 123 }, dataFoo)});
assert.equal(fooMock.timesCalled, 1, 'fooMock#timesCalled is called once');
assert.equal(barMock.timesCalled, 0, 'barMock#timesCalled is called once');

await fetchJSON({url, params: dataFoo, method});
await fetchJSON({url, params: dataBar, method});
await fetchJSON({url, method, params: Object.assign({ extra: 123 }, dataBar)});
assert.equal(fooMock.timesCalled, 1, 'fooMock#timesCalled is called once');
assert.equal(barMock.timesCalled, 1, 'barMock#timesCalled is called once');
});
}

assert.equal(fooMock.timesCalled, 1, 'fooMock#timesCalled is called once');
assert.equal(barMock.timesCalled, 1, 'barMock#timesCalled is called once');
});
['GET', 'POST', 'PATCH', 'PUT'].forEach(testWithSomeParamsForMethod);

function testWithParamsForMethod(method) {
test(`#withParams works with complex parameters for ${method} requests`, async function(assert) {
Expand Down

0 comments on commit 359853b

Please sign in to comment.