Skip to content

Commit

Permalink
Convert tests for same assertion to unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mroderick committed Oct 2, 2020
1 parent 727336b commit ef15eee
Showing 1 changed file with 107 additions and 70 deletions.
177 changes: 107 additions & 70 deletions lib/assertions/same.test.js
@@ -1,95 +1,132 @@
"use strict";

var assert = require("assert");
var referee = require("../referee");
var proxyquire = require("proxyquire").noCallThru();
var sinon = require("sinon");

var obj = { id: 42 };
var obj2 = { id: 42 };

describe("assert.same", function() {
it("should pass when comparing object to itself", function() {
referee.assert.same(obj, obj);
});
describe("same factory", function() {
beforeEach(function() {
this.fakeActualAndExpectedMessageValues =
"866c5b01-0028-4cf2-8dd7-9ef99bbc53b0";

it("should pass when comparing strings", function() {
referee.assert.same("Hey", "Hey");
});
var factory = proxyquire("./same", {
"../actual-and-expected-message-values": this
.fakeActualAndExpectedMessageValues
});

it("should pass when comparing booleans", function() {
referee.assert.same(true, true);
});
this.fakeReferee = {
add: sinon.fake()
};

it("should pass when comparing infinity", function() {
referee.assert.same(Infinity, Infinity);
});
factory(this.fakeReferee);

it("should pass when comparing numbers", function() {
referee.assert.same(32, 32);
this.options = this.fakeReferee.add.args[0][1];
});

it("should pass when comparing null to null", function() {
referee.assert.same(null, null);
it("calls referee.add with 'same' as name", function() {
assert(this.fakeReferee.add.calledWith("same"));
});

it("should pass when comparing undefined to undefined", function() {
referee.assert.same(undefined, undefined);
describe(".assert", function() {
context("when comparing object to itself", function() {
it("returns true", function() {
assert(this.options.assert(obj, obj));
});
});

context("when comparing strings", function() {
it("returns true", function() {
assert(this.options.assert("Hey", "Hey"));
});
});

context("when comparing booleans", function() {
it("returns true", function() {
assert(this.options.assert(true, true));
assert(this.options.assert(false, false));
});
});

context("when comparing Infinity", function() {
it("returns true", function() {
assert(this.options.assert(Infinity, Infinity));
});
});

context("when comparing numbers", function() {
it("returns true", function() {
assert(this.options.assert(32, 32));
});
});

context("when comparing null with null", function() {
it("returns true", function() {
assert(this.options.assert(null, null));
});
});

context("when comparing undefined with undefined", function() {
it("returns true", function() {
assert(this.options.assert(undefined, undefined));
});
});

context("when comparing NaN with NaN", function() {
it("returns true", function() {
assert(this.options.assert(NaN, NaN));
});
});

context("when comparing different objects", function() {
it("returns false", function() {
assert.equal(this.options.assert(obj, obj2), false);
});
});

context("when comparing without coercion", function() {
it("returns false", function() {
assert.equal(this.options.assert(666, "666"), false);
assert.equal(this.options.assert("666", 666), false);
});
});

context("when comparing -0 to +0", function() {
it("returns false", function() {
assert.equal(this.options.assert(-0, +0), false);
});
});
});

it("should pass when comparing NaN to NaN", function() {
referee.assert.same(NaN, NaN);
describe(".assertMessage", function() {
it("is '${customMessage}${actual} expected to be the same object as ${expected}'", function() {
assert.equal(
this.options.assertMessage,
"${customMessage}${actual} expected to be the same object as ${expected}"
);
});
});

it("should fail when comparing different objects", function() {
assert.throws(
function() {
referee.assert.same(obj, obj2);
},
function(error) {
assert.equal(error.code, "ERR_ASSERTION");
assert.equal(
error.message,
"[assert.same] { id: 42 } expected to be the same object as { id: 42 }"
);
assert.equal(error.name, "AssertionError");
assert.equal(error.operator, "assert.same");
return true;
}
);
describe(".refuteMessage", function() {
it("is '${customMessage}${actual} expected not to be the same object as ${expected}'", function() {
assert.equal(
this.options.refuteMessage,
"${customMessage}${actual} expected not to be the same object as ${expected}"
);
});
});

it("should fail when comparing without coercion", function() {
assert.throws(
function() {
referee.assert.same(666, "666");
},
function(error) {
assert.equal(error.code, "ERR_ASSERTION");
assert.equal(
error.message,
"[assert.same] 666 expected to be the same object as '666'"
);
assert.equal(error.name, "AssertionError");
assert.equal(error.operator, "assert.same");
return true;
}
);
describe(".expectation", function() {
it("is 'toBe'", function() {
assert.equal(this.options.expectation, "toBe");
});
});

it("should fail when comparing -0 to +0", function() {
assert.throws(
function() {
referee.assert.same(-0, +0);
},
function(error) {
assert.equal(error.code, "ERR_ASSERTION");
assert.equal(
error.message,
"[assert.same] -0 expected to be the same object as 0"
);
assert.equal(error.name, "AssertionError");
assert.equal(error.operator, "assert.same");
return true;
}
);
describe(".values", function() {
it("delegates to '../actual-and-expected-message-values'", function() {
assert.equal(this.options.values, this.fakeActualAndExpectedMessageValues);
});
});
});

0 comments on commit ef15eee

Please sign in to comment.