1
0
mirror of https://github.com/videojs/video.js.git synced 2025-02-02 11:34:50 +02:00

Fixed console log tests that were breaking in IE8

This commit is contained in:
Steve Heffernan 2014-08-14 16:15:13 -07:00
parent dfdbdbc9bb
commit 487c05628c
2 changed files with 42 additions and 41 deletions

View File

@ -756,15 +756,6 @@ vjs.parseUrl = function(url) {
return details; 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 * Log messags to the console and history based on the type of message
* *
@ -773,8 +764,20 @@ var _console = window['console'] || {
* @private * @private
*/ */
function _logType(type, args){ function _logType(type, args){
var argsArray, noop, console;
// convert args to an array to get array functions // 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) { if (type) {
// add the type to the front of the message // add the type to the front of the message
@ -791,11 +794,11 @@ function _logType(type, args){
argsArray.unshift('VIDEOJS:'); argsArray.unshift('VIDEOJS:');
// call appropriate log function // call appropriate log function
if (_console[type].apply) { if (console[type].apply) {
_console[type].apply(_console, argsArray); console[type].apply(console, argsArray);
} else { } else {
// ie8 doesn't allow error.apply, but it will just join() the array anyway // ie8 doesn't allow error.apply, but it will just join() the array anyway
_console[type](argsArray.join(' ')); console[type](argsArray.join(' '));
} }
} }

View File

@ -294,56 +294,54 @@ test('vjs.findPosition should find top and left position', function() {
// LOG TESTS // LOG TESTS
test('should confirm logging functions work', function() { test('should confirm logging functions work', function() {
var console = window['console']; var console, log, error, warn, origConsole, origLog, origWarn, origError;
var origLog = console.log;
var origWarn = console.warn;
var origError = console.error;
origConsole = window['console'];
// replace the native console for testing
// in ie8 console.log is apparently not a 'function' so sinon chokes on it // in ie8 console.log is apparently not a 'function' so sinon chokes on it
// https://github.com/cjohansen/Sinon.JS/issues/386 // https://github.com/cjohansen/Sinon.JS/issues/386
// instead we'll temporarily replace them with functions // instead we'll temporarily replace them with no-op functions
if (typeof origLog === 'object') { console = window['console'] = {
console.log = function(){}; log: function(){},
console.warn = function(){}; warn: function(){},
console.error = function(){}; error: function(){}
} };
// stub the global log functions // stub the global log functions
var log = sinon.stub(console, 'log'); log = sinon.stub(console, 'log');
var error = sinon.stub(console, 'error'); error = sinon.stub(console, 'error');
var warn = sinon.stub(console, 'warn'); 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'); ok(log.called, 'log was called');
equal(log.firstCall.args[0], 'VIDEOJS:'); equal(log.firstCall.args[0], 'VIDEOJS:');
equal(log.firstCall.args[1], 'asdf'); equal(log.firstCall.args[1], 'log1');
equal(log.firstCall.args[2], 'fdsa'); equal(log.firstCall.args[2], 'log2');
vjs.log.warn('asdf', 'fdsa');
ok(warn.called, 'warn was called'); ok(warn.called, 'warn was called');
equal(warn.firstCall.args[0], 'VIDEOJS:'); equal(warn.firstCall.args[0], 'VIDEOJS:');
equal(warn.firstCall.args[1], 'WARN:'); equal(warn.firstCall.args[1], 'WARN:');
equal(warn.firstCall.args[2], 'asdf'); equal(warn.firstCall.args[2], 'warn1');
equal(warn.firstCall.args[3], 'fdsa'); equal(warn.firstCall.args[3], 'warn2');
vjs.log.error('asdf', 'fdsa');
ok(error.called, 'error was called'); ok(error.called, 'error was called');
equal(error.firstCall.args[0], 'VIDEOJS:'); equal(error.firstCall.args[0], 'VIDEOJS:');
equal(error.firstCall.args[1], 'ERROR:'); equal(error.firstCall.args[1], 'ERROR:');
equal(error.firstCall.args[2], 'asdf'); equal(error.firstCall.args[2], 'error1');
equal(error.firstCall.args[3], 'fdsa'); equal(error.firstCall.args[3], 'error2');
ok(vjs.log.history.length === 3, 'there should be three messages in the log history');
// tear down sinon // tear down sinon
log.restore(); log.restore();
error.restore(); error.restore();
warn.restore(); warn.restore();
// restore ie8 // restore the native console
if (typeof origLog === 'object') { window['console'] = origConsole;
console.log = origLog;
console.warn = origWarn;
console.error = origError;
}
}); });
test('should loop through each element of an array', function() { test('should loop through each element of an array', function() {