Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prioritize newer mock handlers #453

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion addon/mocks/mock-create-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export default class MockCreateRequest extends AttributeMatcher(
super(modelName, 'createRecord');
this.model = model;
this.returnArgs = {};
this.matchArgs = {};
this.setupHandler();
}

Expand Down
8 changes: 8 additions & 0 deletions addon/mocks/mock-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ export default class {
return this;
}

hasSomeParams() {
return !isEmptyObject(this.someQueryParams);
}

paramsMatch(request) {
if (!isEmptyObject(this.someQueryParams)) {
return isPartOf(
Expand Down Expand Up @@ -179,6 +183,10 @@ export default class {
return true;
}

hasMatch() {
return this.matchArgs != null;
}

// mockId holds the url for this mock request
oldUrl() {
return this.mockId && this.mockId.url;
Expand Down
1 change: 0 additions & 1 deletion addon/mocks/mock-update-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export default class MockUpdateRequest extends MaybeIdUrlMatch(
this.id = id;
this.model = model;
this.returnArgs = {};
this.matchArgs = {};
this.setupHandler();
}

Expand Down
7 changes: 5 additions & 2 deletions addon/mocks/request-wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,15 @@ export default class RequestWrapper {
*/
getHandlers() {
return this.handlers.sort(
(a, b) => b.hasQueryParams() - a.hasQueryParams()
(a, b) =>
b.hasMatch() - a.hasMatch() ||
b.hasQueryParams() - a.hasQueryParams() ||
b.hasSomeParams() - a.hasSomeParams()
);
}

addHandler(handler) {
this.handlers.push(handler);
this.handlers.unshift(handler);
return this.index++;
}

Expand Down
2 changes: 1 addition & 1 deletion tests/unit/mocks/mock-create-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ module('MockCreate', function (hooks) {
let user1 = build('user');
let user2 = build('user');

mockCreate('user').returns({ attrs: { id: user2.get('id') } });
mockCreate('user')
.returns({ attrs: { id: user1.get('id') } })
.singleUse();
mockCreate('user').returns({ attrs: { id: user2.get('id') } });

let model1 = FactoryGuy.store.createRecord('user', user1.get());
await model1.save();
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/mocks/mock-find-all-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ module('MockFindAll', function (hooks) {
let wrapper = RequestManager.findWrapper({ type: 'GET', url: '/users' });
let ids = wrapper.getHandlers().map((h) => h.mockId);
assert.deepEqual(ids, [
{ type: 'GET', url: '/users', num: 0 },
{ type: 'GET', url: '/users', num: 1 },
{ type: 'GET', url: '/users', num: 0 },
]);
});

Expand Down
29 changes: 22 additions & 7 deletions tests/unit/mocks/request-wrapper-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
import { mockFindAll, mockQuery } from 'ember-data-factory-guy';
import { mockFindAll, mockCreate } from 'ember-data-factory-guy';
import { inlineSetup } from '../../helpers/utility-methods';
import RequestManager from 'ember-data-factory-guy/mocks/request-manager';

Expand All @@ -10,12 +10,27 @@ module('RequestWrapper', function (hooks) {
setupTest(hooks);
inlineSetup(hooks, serializerType);

test('#getHandlers', function (assert) {
let mockF = mockFindAll('user', 2),
mockFQ = mockFindAll('user', 2).withParams({ foo: true }),
mockQ = mockQuery('user', { moo: true }),
wrapper = RequestManager.findWrapper({ handler: mockF });
test('#getHandlers should prioritize handlers with match and query params', function (assert) {
const withoutParams = mockCreate('user', 2);
const withParams = mockCreate('user').withParams({ foo: true });
const withSomeParams = mockCreate('user').withSomeParams({ foo: true });
const withMatch = mockCreate('user').match({ foo: true });
const wrapper = RequestManager.findWrapper({ handler: withoutParams });

assert.deepEqual(wrapper.getHandlers(), [mockFQ, mockQ, mockF]);
assert.deepEqual(wrapper.getHandlers(), [
withMatch,
withParams,
withSomeParams,
withoutParams,
]);
});

test('#getHandlers should prioritize newer handlers', function (assert) {
const first = mockFindAll('user', 2);
const second = mockFindAll('user', 2);
const third = mockFindAll('user', 2);
const wrapper = RequestManager.findWrapper({ handler: first });

assert.deepEqual(wrapper.getHandlers(), [third, second, first]);
});
});