-
-
Notifications
You must be signed in to change notification settings - Fork 22
Add resolves() and rejects() method #85
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
Conversation
ffMathy
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor changes needed. Good idea.
src/Transformations.ts
Outdated
| type BaseMockObjectMixin<TReturnType> = { | ||
| returns: (...args: TReturnType[]) => void; | ||
| throws: (exception: any) => void; | ||
| resolves: (...args: Unpacked<TReturnType>[]) => void; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
resolves should only be available functions that return promises.
|
Also, add documentation. |
|
I'm not sure if this is the best implementation to restrict the method only for those that return a promise, but I'm open to suggestions. |
|
Really cool! Could you also implement in the same way |
src/states/FunctionState.ts
Outdated
| if (property === SubstituteMethods.resolves) { | ||
| returns = returns.map(value => Promise.resolve(value)) | ||
| } else if(property === SubstituteMethods.rejects) { | ||
| returns = returns.map(value => Promise.reject(value)) | ||
| } | ||
| this.returns.push({ | ||
| returnValues: returns, | ||
| returnIndex: 0, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice how you refactored it! But I don't think it's a good idea to reassigning variables, I think something like this would be better:
| if (property === SubstituteMethods.resolves) { | |
| returns = returns.map(value => Promise.resolve(value)) | |
| } else if(property === SubstituteMethods.rejects) { | |
| returns = returns.map(value => Promise.reject(value)) | |
| } | |
| this.returns.push({ | |
| returnValues: returns, | |
| returnIndex: 0, | |
| const returnMock: Partial<ReturnMock> = { returnIndex: 0, args: this._lastArgs }; | |
| switch (property) { | |
| case SubstituteMethods.returns: | |
| returnMock.returnValues = returns; | |
| break; | |
| case SubstituteMethods.resolves: | |
| returnMock.returnValues = returns.map(value => Promise.resolve(value)); | |
| break; | |
| case SubstituteMethods.rejects: | |
| returnMock.returnValues = returns.map(value => Promise.reject(value)); | |
| break; | |
| default: | |
| throw SubstituteException.generic( | |
| `Expected one of the following methods: "${SubstituteMethods.returns}", "${SubstituteMethods.resolves}" or "${SubstituteMethods.rejects}"` | |
| ); | |
| } | |
| this.returns.push(<ReturnMock>returnMock); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice! I just removed the default: throw from the code. It will never be reached in this case because of the if condition above :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True, the if prevents the default case. I added it in the suggestion for the future, in case new methods are added and the switch case doesn't get updated. It makes it easier to debug
|
If all tests pass, I'd say I have no more comments. Nicely done. @notanengineercom when you are satisfied (and thanks for your review so far), feel free to merge. |
Usually in other test libraries we have available the method
resolves(value)to be alias toreturns(Promise.resolve(value))becomes:
References: