Skip to content

Commit

Permalink
Merge branch 'master' of github.com:danielspaniel/ember-data-factory-guy
Browse files Browse the repository at this point in the history
  • Loading branch information
danielspaniel committed May 31, 2018
2 parents 4fbd6f7 + ecff43a commit 34b76c9
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 5 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1760,7 +1760,10 @@ Usage:
// using match() method to specify a matching function
let profile = make('profile');
profile.set('name', "woo");
let mock = mockUpdate(profile).match(requestData => requestData.name === "moo");
let mock = mockUpdate(profile).match((requestBody) => {
// this example uses a JSONAPI Adapter
return requestBody.data.attributes.name === "moo"
});
profile.save(); // will not be mocked since the mock you set requires the request's top level attribute "name" to equal "moo"

// either set the name to "moo" which will now be mocked correctly
Expand Down
7 changes: 3 additions & 4 deletions addon/mocks/attribute-matcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,9 @@ const AttributeMatcher = (superclass) => class extends superclass {

extraRequestMatches(request) {
if (this.matchArgs) {
let requestBody = JSON.parse(request.requestBody),
requestData = requestBody[this.modelName];
let requestBody = JSON.parse(request.requestBody);
if (typeof this.matchArgs === 'function') {
return this.matchArgs(requestData);
return this.matchArgs(requestBody);
} else {
return this.attributesMatch(requestBody);
}
Expand Down Expand Up @@ -101,4 +100,4 @@ const AttributeMatcher = (superclass) => class extends superclass {
}
};

export default AttributeMatcher;
export default AttributeMatcher;
36 changes: 36 additions & 0 deletions tests/acceptance/user-view-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,42 @@ module('Acceptance | User View', function(hooks) {
assert.dom('li.project').exists('One project shown');
});

test("Add a project to a user with mockCreate and a custom matching function", async function(assert) {
// mockFindRecord will build a default user for the json payload
let mock = mockFindRecord('user');

await visit('/user/' + mock.get('id'));

let newProjectTitle = "Gonzo Project";

// should be no projects
assert.dom('li.project').doesNotExist('No projects shown');

await fillIn('input.project-title', newProjectTitle);

// You can also implement your own custom matching function
mockCreate('project').match((requestBody) => {
return requestBody.data.attributes.title === newProjectTitle;
});

/**
Let's say that clicking this a button, triggers action in the
view to create project record and looks something like this:
actions: {
addProject: function (user) {
let title = this.$('input.project-title').val();
let store = this.get('store');
store.createRecord('project', {title, user}).save();
}
}
*/
await click('button.add-project');

assert.dom('li.project').exists('One project shown');
});

test("Request failure shows a error message", async function(assert) {
let mock = mockFindRecord('user').fails({status: 404});
await visit('/user/' + mock.get('id'));
Expand Down
23 changes: 23 additions & 0 deletions tests/unit/shared-adapter-behaviour.js
Original file line number Diff line number Diff line change
Expand Up @@ -1198,6 +1198,29 @@ SharedBehavior.mockCreateTests = function() {
});
});

test("match can take a function - supplied parameter is the json request body", async function(assert) {
assert.expect(2);
run(async () => {
let mock = mockCreate('profile');

mock.match(function(requestBody) {
assert.ok(true, 'matching function is called');
const description = (
(requestBody.description) || // DRFSerializer, RESTAdapter, JSONSerializer
(requestBody.profile && requestBody.profile.description) || // RESTSerializer, ActiveModelSerializer
(requestBody.data && requestBody.data.attributes.description) // JSONAPISerializer
);
return description === 'match me!';
});

let profile = FactoryGuy.store.createRecord('profile');
profile.set('description', 'match me!');
await profile.save();

assert.equal(mock.timesCalled, 1);
});
});

test("match some attributes", async function(assert) {
run(async () => {
let customDescription = "special description",
Expand Down

0 comments on commit 34b76c9

Please sign in to comment.