Forked from gr2m/jasmine.promise-matchers.coffee
Last active
December 31, 2015 07:19
-
-
Save hmayer00/7953530 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The base is stolen from @gist: https://gist.github.com/gr2m/2191748, and then substantially modified. | |
beforeEach -> | |
@addMatchers | |
toBePromise: -> | |
@actual.done && [email protected] | |
toBeRejected: -> | |
@actual.state() == "rejected" | |
toBeResolved: -> | |
@actual.state() == "resolved" | |
toBeResolvedWith: -> | |
expectedArgs = jasmine.util.argsToArray(arguments) | |
unless @actual.done | |
throw new Error("Expected a promise, but got #{jasmine.pp(@actual)}.") | |
done = jasmine.createSpy 'done' | |
@actual.done done | |
@message = -> | |
if done.callCount == 0 | |
return [ | |
"Expected spy #{done.identity} to have been resolved with #{jasmine.pp(expectedArgs)} but it was never resolved.", | |
"Expected spy #{done.identity} not to have been resolved with #{jasmine.pp(expectedArgs)} but it was." | |
] | |
else | |
return [ | |
"Expected spy #{done.identity} to have been resolved with #{jasmine.pp(expectedArgs)} but was resolved with #{jasmine.pp(done.argsForCall)}", | |
"Expected spy #{done.identity} not to have been resolved with #{jasmine.pp(expectedArgs)} but was resolved with #{jasmine.pp(done.argsForCall)}" | |
] | |
return @env.contains_(done.argsForCall, expectedArgs) | |
toBeRejectedWith: -> | |
expectedArgs = jasmine.util.argsToArray(arguments) | |
unless @actual.fail | |
throw new Error("Expected a promise, but got #{jasmine.pp(@actual)}") | |
fail = jasmine.createSpy 'fail' | |
@actual.fail fail | |
@message = -> | |
if fail.callCount == 0 | |
return [ | |
"Expected spy #{fail.identity} to have been rejected with #{jasmine.pp(expectedArgs)} but it was never rejected.", | |
"Expected spy #{fail.identity} not to have been rejected with #{jasmine.pp(expectedArgs)} but it was." | |
] | |
else | |
return [ | |
"Expected spy #{fail.identity} to have been rejected with #{jasmine.pp(expectedArgs)} but was rejected with #{jasmine.pp(fail.argsForCall)}", | |
"Expected spy #{fail.identity} not to have been rejected with #{jasmine.pp(expectedArgs)} but was rejected with #{jasmine.pp(fail.argsForCall)}" | |
] | |
return @env.contains_(fail.argsForCall, expectedArgs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment