Skip to content
This repository has been archived by the owner on Feb 19, 2022. It is now read-only.

Commit

Permalink
More tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan-roemer committed Feb 28, 2015
1 parent b751528 commit f0174e9
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 18 deletions.
17 changes: 15 additions & 2 deletions simple-console.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
(function (root) {
// Memoizations.
var _console;
var _bind;

// Patches
var EMPTY_OBJ = {};
Expand All @@ -26,6 +27,7 @@
var SimpleConsole = function () {
var self = this;
var con = this._getConsole();
var bind = this._getBind();
var noConsole = !con;
con = con || {};
var i;
Expand All @@ -42,10 +44,10 @@
if (noConsole || !con[meth]) {
// No console or method: Noop it.
self[meth] = NOOP;
} else if (Function.prototype.bind) {
} else if (bind) {
// IE9 and most others: Bind to our create real function.
// Should work if `console.FOO` is `function` or `object`.
self[meth] = Function.prototype.bind.call(con[meth], con);
self[meth] = bind.call(con[meth], con);
} else {
// IE8: No bind, so even more tortured.
self[meth] = function () {
Expand All @@ -68,6 +70,17 @@
return _console;
};

/**
* Accessor to bind object. (Cached).
*
* @returns {Object} the console object or `null` if unavailable.
*/
SimpleConsole.prototype._getBind = function () {
if (typeof _bind !== "undefined") { return _bind; }
_bind = Function.prototype.bind || null;
return _bind;
};

// UMD wrapper: Borrowed from webpack version.
/* istanbul ignore next */
function umd() {
Expand Down
64 changes: 48 additions & 16 deletions test/spec/simple-console.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,57 @@ describe("simple-console", function () {
});
});

describe("invocations", function () {
var con;
// These specs will log out to console.
// This is generally disfavored in tests, but we're _testing_ a logger
// and want to have things blow up if anything is wrong.
describe("actually log out", function () {
describe("no function bind", function () {
var bindStub,
con;

beforeEach(function () {
con = new SimpleConsole();
beforeEach(function () {
// By default, returns `undefined`.
bindStub = sinon.stub(SimpleConsole.prototype, "_getBind");
con = new SimpleConsole();
});

afterEach(function () {
bindStub.restore();
});

it("should invoke lots of functions and not die", function () {
con.log("log");
con.log.apply(con, ["log apply"]);
con.log.call(con, "log call");
con.warn("warn");
con.warn.apply(con, ["warn apply"]);
con.warn.call(con, "warn call");
con.error("error");
con.error.apply(con, ["error apply"]);
con.error.call(con, "error call");
});
});

// Just do a lot of invocations and see if anything dies.
// This _will_ clog the output, so do them last...
it("should invoke lots of functions and maybe log out", function () {
con.log("log");
con.log.apply(con, ["log apply"]);
con.log.call(con, "log call");
con.warn("warn");
con.warn.apply(con, ["warn apply"]);
con.warn.call(con, "warn call");
con.error("error");
con.error.apply(con, ["error apply"]);
con.error.call(con, "error call");
describe("native functionality", function () {
var con;

beforeEach(function () {
con = new SimpleConsole();
});

// Just do a lot of invocations and see if anything dies.
// This _will_ clog the output, so do them last...
it("should invoke lots of functions and maybe log", function () {
con.log("log");
con.log.apply(con, ["log apply"]);
con.log.call(con, "log call");
con.warn("warn");
con.warn.apply(con, ["warn apply"]);
con.warn.call(con, "warn call");
con.error("error");
con.error.apply(con, ["error apply"]);
con.error.call(con, "error call");
});
});
});
});

0 comments on commit f0174e9

Please sign in to comment.