-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert tests for isMap to unit test
- Loading branch information
Showing
1 changed file
with
68 additions
and
164 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,186 +1,90 @@ | ||
"use strict"; | ||
|
||
var assert = require("assert"); | ||
var referee = require("../referee"); | ||
var captureArgs = require("../test-helper/capture-args"); | ||
var proxyquire = require("proxyquire").noCallThru(); | ||
var sinon = require("sinon"); | ||
|
||
describe("assert.isMap", function() { | ||
it("should pass for Map", function() { | ||
referee.assert.isMap(new Map()); | ||
}); | ||
|
||
it("should fail for String", function() { | ||
assert.throws( | ||
function() { | ||
referee.assert.isMap("apple pie"); | ||
}, | ||
function(error) { | ||
assert.equal(error.code, "ERR_ASSERTION"); | ||
assert.equal( | ||
error.message, | ||
"[assert.isMap] Expected 'apple pie' to be a Map" | ||
); | ||
assert.equal(error.name, "AssertionError"); | ||
assert.equal(error.operator, "assert.isMap"); | ||
return true; | ||
} | ||
); | ||
}); | ||
describe("isMap factory", function() { | ||
beforeEach(function() { | ||
this.fakeActualMessageValues = "a4bf1905-489e-4f10-a47e-5b79b7cf173a"; | ||
|
||
it("should fail for Array", function() { | ||
assert.throws( | ||
function() { | ||
referee.assert.isMap([]); | ||
}, | ||
function(error) { | ||
assert.equal(error.code, "ERR_ASSERTION"); | ||
assert.equal( | ||
error.message, | ||
"[assert.isMap] Expected [] to be a Map" | ||
); | ||
assert.equal(error.name, "AssertionError"); | ||
assert.equal(error.operator, "assert.isMap"); | ||
return true; | ||
} | ||
); | ||
}); | ||
this.factory = proxyquire("./is-map", { | ||
"../actual-message-values": this.fakeActualMessageValues | ||
}); | ||
|
||
it("should fail for WeakMap", function() { | ||
assert.throws( | ||
function() { | ||
// eslint-disable-next-line ie11/no-weak-collections | ||
referee.assert.isMap(new WeakMap()); | ||
}, | ||
function(error) { | ||
assert.equal(error.code, "ERR_ASSERTION"); | ||
assert.equal( | ||
error.message, | ||
"[assert.isMap] Expected WeakMap { <items unknown> } to be a Map" | ||
); | ||
assert.equal(error.name, "AssertionError"); | ||
assert.equal(error.operator, "assert.isMap"); | ||
return true; | ||
} | ||
); | ||
}); | ||
this.fakeReferee = { | ||
add: sinon.fake() | ||
}; | ||
|
||
it("should fail for Object", function() { | ||
assert.throws( | ||
function() { | ||
referee.assert.isMap({}); | ||
}, | ||
function(error) { | ||
assert.equal(error.code, "ERR_ASSERTION"); | ||
assert.equal( | ||
error.message, | ||
"[assert.isMap] Expected {} to be a Map" | ||
); | ||
assert.equal(error.name, "AssertionError"); | ||
assert.equal(error.operator, "assert.isMap"); | ||
return true; | ||
} | ||
); | ||
}); | ||
|
||
it("should fail for arguments", function() { | ||
assert.throws( | ||
function() { | ||
referee.assert.isMap(captureArgs()); | ||
}, | ||
function(error) { | ||
assert.equal(error.code, "ERR_ASSERTION"); | ||
assert.equal( | ||
error.message, | ||
"[assert.isMap] Expected [Arguments] {} to be a Map" | ||
); | ||
assert.equal(error.name, "AssertionError"); | ||
assert.equal(error.operator, "assert.isMap"); | ||
return true; | ||
} | ||
); | ||
}); | ||
|
||
it("should fail with custom message", function() { | ||
var message = "4eb2174d-3faa-4095-92d1-cd8dfb7e2a58"; | ||
|
||
assert.throws( | ||
function() { | ||
referee.assert.isMap("apple pie", message); | ||
}, | ||
function(error) { | ||
assert.equal(error.code, "ERR_ASSERTION"); | ||
assert.equal( | ||
error.message, | ||
"[assert.isMap] " + | ||
message + | ||
": Expected 'apple pie' to be a Map" | ||
); | ||
assert.equal(error.name, "AssertionError"); | ||
assert.equal(error.operator, "assert.isMap"); | ||
return true; | ||
} | ||
); | ||
}); | ||
}); | ||
this.factory(this.fakeReferee); | ||
|
||
describe("refute.isMap", function() { | ||
it("should fail for Map", function() { | ||
assert.throws( | ||
function() { | ||
referee.refute.isMap(new Map()); | ||
}, | ||
function(error) { | ||
assert.equal(error.code, "ERR_ASSERTION"); | ||
assert.equal( | ||
error.message, | ||
"[refute.isMap] Expected Map {} not to be a Map" | ||
); | ||
assert.equal(error.name, "AssertionError"); | ||
assert.equal(error.operator, "refute.isMap"); | ||
return true; | ||
} | ||
); | ||
this.options = this.fakeReferee.add.args[0][1]; | ||
}); | ||
|
||
it("should pass for String", function() { | ||
referee.refute.isMap("apple pie"); | ||
it("calls referee.add with 'isMap' as name", function() { | ||
assert(this.fakeReferee.add.calledWith("isMap")); | ||
}); | ||
|
||
it("should pass for Array", function() { | ||
referee.refute.isMap([]); | ||
describe(".assert", function() { | ||
describe("when actual is an instance of Map", function() { | ||
it("returns true", function() { | ||
var result = this.options.assert(new Map()); | ||
|
||
assert.equal(result, true); | ||
}); | ||
}); | ||
|
||
describe("when actual is not an instance of Map", function() { | ||
it("returns false", function() { | ||
var t = this; | ||
|
||
[ | ||
Map, | ||
WeakMap, | ||
Set, | ||
// eslint-disable-next-line ie11/no-weak-collections | ||
new WeakMap(), | ||
[], | ||
{}, | ||
"apple pie", | ||
123, | ||
new Date() | ||
].forEach(function(value) { | ||
var result = t.options.assert(value); | ||
|
||
assert.equal(result, false); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
it("should pass for WeakMap", function() { | ||
// eslint-disable-next-line ie11/no-weak-collections | ||
referee.refute.isMap(new WeakMap()); | ||
describe(".assertMessage", function() { | ||
it("is '${customMessage}Expected ${actual} to be a Map'", function() { | ||
assert.equal( | ||
this.options.assertMessage, | ||
"${customMessage}Expected ${actual} to be a Map" | ||
); | ||
}); | ||
}); | ||
|
||
it("should pass for Object", function() { | ||
referee.refute.isMap({}); | ||
describe(".refuteMessage", function() { | ||
it("is '${customMessage}Expected ${actual} not to be a Map'", function() { | ||
assert.equal( | ||
this.options.refuteMessage, | ||
"${customMessage}Expected ${actual} not to be a Map" | ||
); | ||
}); | ||
}); | ||
|
||
it("should pass for arguments", function() { | ||
referee.refute.isMap(captureArgs()); | ||
describe(".expectation", function() { | ||
it("is 'toBeMap'", function() { | ||
assert.equal(this.options.expectation, "toBeMap"); | ||
}); | ||
}); | ||
|
||
it("should fail with custom message", function() { | ||
var message = "f84e51dd-d5af-4ef0-81ec-2d575eadd735"; | ||
assert.throws( | ||
function() { | ||
referee.refute.isMap(new Map(), message); | ||
}, | ||
function(error) { | ||
assert.equal(error.code, "ERR_ASSERTION"); | ||
assert.equal( | ||
error.message, | ||
"[refute.isMap] " + | ||
message + | ||
": Expected Map {} not to be a Map" | ||
); | ||
assert.equal(error.name, "AssertionError"); | ||
assert.equal(error.operator, "refute.isMap"); | ||
return true; | ||
} | ||
); | ||
describe(".values", function() { | ||
it("delegates to '../actual-message-values'", function() { | ||
assert.equal(this.options.values, this.fakeActualMessageValues); | ||
}); | ||
}); | ||
}); |