diff --git a/src/js/lib.js b/src/js/lib.js index 3d035a2ab..1857966d3 100644 --- a/src/js/lib.js +++ b/src/js/lib.js @@ -756,15 +756,6 @@ vjs.parseUrl = function(url) { return details; }; -// if there's no console then don't try to output messages -// they will still be stored in vjs.log.history -var _noop = function(){}; -var _console = window['console'] || { - 'log': _noop, - 'warn': _noop, - 'error': _noop -}; - /** * Log messags to the console and history based on the type of message * @@ -773,8 +764,20 @@ var _console = window['console'] || { * @private */ function _logType(type, args){ + var argsArray, noop, console; + // convert args to an array to get array functions - var argsArray = Array.prototype.slice.call(args); + argsArray = Array.prototype.slice.call(args); + // if there's no console then don't try to output messages + // they will still be stored in vjs.log.history + // Was setting these once outside of this function, but containing them + // in the function makes it easier to test cases where console doesn't exist + noop = function(){}; + console = window['console'] || { + 'log': noop, + 'warn': noop, + 'error': noop + }; if (type) { // add the type to the front of the message @@ -791,11 +794,11 @@ function _logType(type, args){ argsArray.unshift('VIDEOJS:'); // call appropriate log function - if (_console[type].apply) { - _console[type].apply(_console, argsArray); + if (console[type].apply) { + console[type].apply(console, argsArray); } else { // ie8 doesn't allow error.apply, but it will just join() the array anyway - _console[type](argsArray.join(' ')); + console[type](argsArray.join(' ')); } } diff --git a/test/unit/lib.js b/test/unit/lib.js index 080659c00..0376661cc 100644 --- a/test/unit/lib.js +++ b/test/unit/lib.js @@ -294,56 +294,54 @@ test('vjs.findPosition should find top and left position', function() { // LOG TESTS test('should confirm logging functions work', function() { - var console = window['console']; - var origLog = console.log; - var origWarn = console.warn; - var origError = console.error; + var console, log, error, warn, origConsole, origLog, origWarn, origError; + origConsole = window['console']; + // replace the native console for testing // in ie8 console.log is apparently not a 'function' so sinon chokes on it // https://github.com/cjohansen/Sinon.JS/issues/386 - // instead we'll temporarily replace them with functions - if (typeof origLog === 'object') { - console.log = function(){}; - console.warn = function(){}; - console.error = function(){}; - } + // instead we'll temporarily replace them with no-op functions + console = window['console'] = { + log: function(){}, + warn: function(){}, + error: function(){} + }; // stub the global log functions - var log = sinon.stub(console, 'log'); - var error = sinon.stub(console, 'error'); - var warn = sinon.stub(console, 'warn'); + log = sinon.stub(console, 'log'); + error = sinon.stub(console, 'error'); + warn = sinon.stub(console, 'warn'); + + vjs.log('log1', 'log2'); + vjs.log.warn('warn1', 'warn2'); + vjs.log.error('error1', 'error2'); - vjs.log('asdf', 'fdsa'); ok(log.called, 'log was called'); equal(log.firstCall.args[0], 'VIDEOJS:'); - equal(log.firstCall.args[1], 'asdf'); - equal(log.firstCall.args[2], 'fdsa'); + equal(log.firstCall.args[1], 'log1'); + equal(log.firstCall.args[2], 'log2'); - vjs.log.warn('asdf', 'fdsa'); ok(warn.called, 'warn was called'); equal(warn.firstCall.args[0], 'VIDEOJS:'); equal(warn.firstCall.args[1], 'WARN:'); - equal(warn.firstCall.args[2], 'asdf'); - equal(warn.firstCall.args[3], 'fdsa'); + equal(warn.firstCall.args[2], 'warn1'); + equal(warn.firstCall.args[3], 'warn2'); - vjs.log.error('asdf', 'fdsa'); ok(error.called, 'error was called'); equal(error.firstCall.args[0], 'VIDEOJS:'); equal(error.firstCall.args[1], 'ERROR:'); - equal(error.firstCall.args[2], 'asdf'); - equal(error.firstCall.args[3], 'fdsa'); + equal(error.firstCall.args[2], 'error1'); + equal(error.firstCall.args[3], 'error2'); + + ok(vjs.log.history.length === 3, 'there should be three messages in the log history'); // tear down sinon log.restore(); error.restore(); warn.restore(); - // restore ie8 - if (typeof origLog === 'object') { - console.log = origLog; - console.warn = origWarn; - console.error = origError; - } + // restore the native console + window['console'] = origConsole; }); test('should loop through each element of an array', function() {