Skip to content

Instantly share code, notes, and snippets.

@gr2m
Created March 25, 2012 06:21
Show Gist options
  • Save gr2m/2191748 to your computer and use it in GitHub Desktop.
Save gr2m/2191748 to your computer and use it in GitHub Desktop.

Revisions

  1. gr2m revised this gist Mar 25, 2012. 2 changed files with 0 additions and 0 deletions.
    File renamed without changes.
  2. gr2m created this gist Mar 25, 2012.
    49 changes: 49 additions & 0 deletions jasmine.promise-helpers.coffee
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,49 @@
    jasmine.Matchers.prototype.toBePromise = ->
    this.actual.done && !this.actual.resolve
    jasmine.Matchers.prototype.toBeRejected = -> this.actual.isRejected()
    jasmine.Matchers.prototype.toBeResolved = -> this.actual.isResolved()
    jasmine.Matchers.prototype.toBeResolvedWith = ->
    expectedArgs = jasmine.util.argsToArray(arguments);

    unless this.actual.done
    throw new Error('Expected a promise, but got ' + jasmine.pp(this.actual) + '.');

    done = jasmine.createSpy 'done'
    this.actual.done done

    this.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 this.env.contains_(done.argsForCall, expectedArgs);

    jasmine.Matchers.prototype.toBeRejectedWith = ->
    expectedArgs = jasmine.util.argsToArray(arguments);

    unless this.actual.fail
    throw new Error('Expected a promise, but got ' + jasmine.pp(this.actual) + '.');

    fail = jasmine.createSpy 'fail'
    this.actual.fail fail

    this.message = ->
    if fail.callCount == 0
    return [
    "Expected spy " + fail.identity + " to have been resolved with " + jasmine.pp(expectedArgs) + " but it was never resolved.",
    "Expected spy " + fail.identity + " not to have been resolved with " + jasmine.pp(expectedArgs) + " but it was."
    ];
    else
    return [
    "Expected spy " + fail.identity + " to have been resolved with " + jasmine.pp(expectedArgs) + " but was resolved with " + jasmine.pp(fail.argsForCall),
    "Expected spy " + fail.identity + " not to have been resolved with " + jasmine.pp(expectedArgs) + " but was resolved with " + jasmine.pp(fail.argsForCall)
    ];

    return this.env.contains_(fail.argsForCall, expectedArgs);
    47 changes: 47 additions & 0 deletions jasmine.promise-helpers.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    jasmine.Matchers.prototype.toBePromise = function() {
    return this.actual.done && !this.actual.resolve;
    };

    jasmine.Matchers.prototype.toBeRejected = function() {
    return this.actual.isRejected();
    };

    jasmine.Matchers.prototype.toBeResolved = function() {
    return this.actual.isResolved();
    };

    jasmine.Matchers.prototype.toBeResolvedWith = function() {
    var done, expectedArgs;
    expectedArgs = jasmine.util.argsToArray(arguments);
    if (!this.actual.done) {
    throw new Error('Expected a promise, but got ' + jasmine.pp(this.actual) + '.');
    }
    done = jasmine.createSpy('done');
    this.actual.done(done);
    this.message = function() {
    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 this.env.contains_(done.argsForCall, expectedArgs);
    };

    jasmine.Matchers.prototype.toBeRejectedWith = function() {
    var expectedArgs, fail;
    expectedArgs = jasmine.util.argsToArray(arguments);
    if (!this.actual.fail) {
    throw new Error('Expected a promise, but got ' + jasmine.pp(this.actual) + '.');
    }
    fail = jasmine.createSpy('fail');
    this.actual.fail(fail);
    this.message = function() {
    if (fail.callCount === 0) {
    return ["Expected spy " + fail.identity + " to have been resolved with " + jasmine.pp(expectedArgs) + " but it was never resolved.", "Expected spy " + fail.identity + " not to have been resolved with " + jasmine.pp(expectedArgs) + " but it was."];
    } else {
    return ["Expected spy " + fail.identity + " to have been resolved with " + jasmine.pp(expectedArgs) + " but was resolved with " + jasmine.pp(fail.argsForCall), "Expected spy " + fail.identity + " not to have been resolved with " + jasmine.pp(expectedArgs) + " but was resolved with " + jasmine.pp(fail.argsForCall)];
    }
    };
    return this.env.contains_(fail.argsForCall, expectedArgs);
    };