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

Allow mockCreate/mockUpdate to receive a matching function #263

Merged

Conversation

lorcan
Copy link
Contributor

@lorcan lorcan commented Dec 12, 2016

tl;dr Allow match to accept a function or attribute(s) to determine whether a create or update matched.

The function passed to match is given the requestData and can perform bespoke logic to determine whether a request matched or not. If the function returns true then a match is recorded, otherwise it is not.

We have a use-case where we're updating a complex, nested model called Parent. Parent hasMany Child relationships, each of which has multiple attributes. We're mocking an update to Parent but what we really care about is one change to an attribute of Child, let's say description. Using the existing match function we would need to pass in the entire child structure, which is quite unwieldy. By allowing match to receive a function the responsibility of determining whether a match was made belongs to the test, e.g.,:

      let done = assert.async();
      let customDescription = "special description";
      let parent = make('parent');

      let updateMock = mockUpdate(parent);
     
      updateMock.match(function(requestData) {
        return requestData.children[0].description === customDescription;
      });
      profile.set('description', customDescription);
      profile.save().then(function(parent) {
        equal(updateMock.timesCalled, 1);
        done();
      });

One thing I don't like about this approach is that the caller needs to be aware of the adaptor type and process the requestData themselves to do the match. I think it's a reasonable compromise to get the flexibility of performing arbitrary matching.

I've given a stab at writing documentation and tests for this functionality. Happy to start a discussion and rework this PR if it seems like a sensible addition.

This function is passed the `requestData` and can perform bespoke logic
to determine whether a request matched or not. If the function returns
`true` then a match is recorded, otherwise it is not.
@danielspaniel
Copy link
Collaborator

I guess this is not too bad an idea, and you pointed out the right flaws. But if you really need to get this down deep, there is no choice.
I can't right now think of another way to do this without the function.

And I really like / am shocked that you lay out the tests quite thoroughly and update the documentation. This almost never happens, so I just got a bruise when I fell out of my chair.

@danielspaniel danielspaniel merged commit de598bc into adopted-ember-addons:master Dec 12, 2016
@lorcan
Copy link
Contributor Author

lorcan commented Dec 12, 2016

Lol! Awesome! :-D
We're huge fans of ember-data-factory-guy!

@lorcan lorcan deleted the allow-function-matcher branch December 12, 2016 19:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants