-
Notifications
You must be signed in to change notification settings - Fork 6
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
Implemented 'to have a call satisfying <object|function>'. #31
Conversation
Moved the code for recording spy calls performed by a function into a helper.
Some of the error messages can be improved, so consider this a WIP for now. I'm pretty sure the semantics are ok. |
The |
Show of hands: Should expect(spy, 'to have a call satisfying', [ 123 ]); be shorthand for expect(spy, 'to have a call satisfying', { args: [ 123 ] }); ? |
👍 for Ideally this same shortcut should work for expect(spy, 'to have calls satisfying', [
[ 123 ],
{ args: {1: 'foo' } },
{ args: [ 234, 'bar' ] }
]); |
I like shortcuts as long as they are not ambiguous. This one seems fair. |
I haven't read the rest of the API to know how well it composed. Agree with @sunesimonsen if it's not ambiguous. I'd be less of a fan if the asserting multiple calls is an array of arrays.. I write something similar on the other recent issue, not sure where that landed though :) |
@bruderstein - just read your comment. I'm advocating keeping the multiple and single call assertions distinct enough they can't be confused, so I'd rather the args: syntax for one or the other case. (side note thats why in thought a varargs style might be useful to disambiguate). |
Implemented shorthands:
The second shorthand kicks in whenever the object has only numerical properties. |
@bruderstein @sunesimonsen @alexjeffburke I implemented the shorthands for both |
Are you ok with others having that possibility? :) |
Haha I'm going to ask you a likely annoying Q but exactly which possibility do you mean? Want to make sure wires aren't crossed :) What I was getting at, or trying to, is I quite like the multiple calls case using Wish I could follow these discussions more but I will do what I can to adopt conventions in the few places I 'own'. |
@alexjeffburke Hehe, sorry for the confusing comment from me, I think I misread your previous comment :) I understand that you prefer the explicit version, which is perfectly fine. But it sounds like you think the availability of the shorthand introduces an ambiguity or risk? |
Oh no probs! (: Let me tell you where it came up for context. In -events, I used an array syntax for event matches [bunch, of, things]. Then I implemented multiple events, arrays of arrays and I found myself getting just a little more confused - see the last example in the README. The code seemed to suffer too - check supplied array contains an array. Do different lengths of the arrays inside the array matter? etc etc But, you know: a) I might be a little too close to the impl side as a basis for my concern b) maybe reading the assertion is enough if you are then very strict about what should go on the RHS. Lastly, side thought, this interfaces stuff is really tricky! |
@alexjeffburke Thanks for elaborating!
Right, I see what you mean. Aren't you missing a Maybe I'm in a little too deep at this point -- API design is really hard. In this case I think that as long as you have a habit of formatting the expect(mySpy, 'to have calls satisfying', [
[ 1, 2, 3 ],
[ 4, 5, 6 ]
]);
expect(mySpy, 'to have calls satisfying', [
{ 0: 1, 1: 2 },
{ 1: 5, 2: 6 }
]);
expect(mySpy, 'to have calls satisfying', [
{ args: [ 1, 2, 3 ] },
{ args: [ 4, 5, 6 ] }
]);
expect(mySpy, 'to have calls satisfying', [
{ args: { 0: 1, 1: 2 } },
{ args: { 1: 5, 2: 6 } }
]);
expect(mySpy, 'to have calls satisfying', {
1: [ 1, 2, 3 ],
3: [ 4, 5, 6 ]
});
expect(mySpy, 'to have calls satisfying', {
1: { 0: 1, 1: 2 },
3: { 1: 5, 2: 6 }
});
expect(mySpy, 'to have calls satisfying', {
1: { args: [ 1, 2, 3 ] },
3: { args: [ 4, 5, 6 ] }
});
expect(mySpy, 'to have calls satisfying', {
1: { args: { 0: 1, 1: 2 } },
3: { args: { 1: 5, 2: 6 } }
});
expect(mySpy, 'to have calls satisfying', function () {
mySpy(1, 2, 3);
mySpy(4, 5, 6);
}); As for whether they're writable in the sense that you don't find yourself referring to the docs all the time, that's harder to tell. For users that know the |
For completeness, here are the supported variants of expect(mySpy, 'to have a call satisfying', [ 1, 2, 3 ]);
expect(mySpy, 'to have a call satisfying', { 0: 1, 1: 2 });
expect(mySpy, 'to have a call satisfying', { args: [ 1, 2, 3 ] });
expect(mySpy, 'to have a call satisfying', { args: { 1: 2, 2: 3 } });
expect(mySpy, 'to have a call satisfying', function () {
mySpy(1, 2, 3);
}); |
👍 :) |
I really like how this turned out. Maybe we should just not touch 👍 |
Implemented 'to have a call satisfying <object|function>'.
Spinoff from #29.
// cc @sunesimonsen @gertsonderby @alexjeffburke @bruderstein