From 4ff1a65031e985bf930f6761c1ecf46e4db98d6e Mon Sep 17 00:00:00 2001 From: Colin Casey Date: Fri, 20 Sep 2013 21:33:16 -0300 Subject: [PATCH] fix(log): prevent logging `undefined` for $log in IE Closes #1705 --- src/ng/log.js | 2 +- test/ng/logSpec.js | 60 +++++++++++++++++++++++++++++++--------------- 2 files changed, 42 insertions(+), 20 deletions(-) diff --git a/src/ng/log.js b/src/ng/log.js index e0a1aec363f7..0678ac21e746 100644 --- a/src/ng/log.js +++ b/src/ng/log.js @@ -151,7 +151,7 @@ function $LogProvider(){ // we are IE which either doesn't have window.console => this is noop and we do nothing, // or we are IE where console.log doesn't have apply so we log at least first 2 args return function(arg1, arg2) { - logFn(arg1, arg2); + logFn(arg1, arg2 == null ? '' : arg2); } } }]; diff --git a/test/ng/logSpec.js b/test/ng/logSpec.js index b416e044d499..4cac5c5cba44 100644 --- a/test/ng/logSpec.js +++ b/test/ng/logSpec.js @@ -69,21 +69,23 @@ describe('$log', function() { } )); + describe("IE logging behavior", function() { + function removeApplyFunctionForIE() { + log.apply = log.call = + warn.apply = warn.call = + info.apply = info.call = + error.apply = error.call = + debug.apply = debug.call = null; - it("should work in IE where console.error doesn't have apply method", inject( - function() { - log.apply = log.call = - warn.apply = warn.call = - info.apply = info.call = - error.apply = error.call = - debug.apply = debug.call = null; - - $window.console = {log: log, - warn: warn, - info: info, - error: error, - debug: debug}; - }, + $window.console = {log: log, + warn: warn, + info: info, + error: error, + debug: debug}; + } + + it("should work in IE where console.error doesn't have an apply method", inject( + removeApplyFunctionForIE, function($log) { $log.log.apply($log); $log.warn.apply($log); @@ -92,12 +94,32 @@ describe('$log', function() { $log.debug.apply($log); expect(logger).toEqual('log;warn;info;error;debug;'); }) - ); + ); + + it("should not attempt to log the second argument in IE if it is not specified", inject( + function() { + log = function(arg1, arg2) { logger+= 'log;' + arg2; }; + warn = function(arg1, arg2) { logger+= 'warn;' + arg2; }; + info = function(arg1, arg2) { logger+= 'info;' + arg2; }; + error = function(arg1, arg2) { logger+= 'error;' + arg2; }; + debug = function(arg1, arg2) { logger+= 'debug;' + arg2; }; + }, + removeApplyFunctionForIE, + function($log) { + $log.log(); + $log.warn(); + $log.info(); + $log.error(); + $log.debug(); + expect(logger).toEqual('log;warn;info;error;debug;'); + }) + ); + }); describe("$log.debug", function () { - + beforeEach(initService(false)); - + it("should skip debugging output if disabled", inject( function(){ $window.console = {log: log, @@ -105,7 +127,7 @@ describe('$log', function() { info: info, error: error, debug: debug}; - }, + }, function($log) { $log.log(); $log.warn(); @@ -115,7 +137,7 @@ describe('$log', function() { expect(logger).toEqual('log;warn;info;error;'); } )); - + }); describe('$log.error', function() {