Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 57b837b

Browse files
committed
test($log): run all $log tests in IE9 & non-IE9 logging mode (#15995)
In IE 9 console methods don't inherit from Function.prototype and, hence, don't have apply. Until recently IE 9 logging in AngularJS was restricted to the first 2 parameters but that changed as we could just reuse Function.prototype.apply everywhere, creating one code path for all browsers. Therefore, we can now run all tests in modes where apply exists on logging methods and where it doesn't. Ref #15911 Ref b277e3e Closes #15995
1 parent 6daca02 commit 57b837b

File tree

1 file changed

+126
-116
lines changed

1 file changed

+126
-116
lines changed

test/ng/logSpec.js

+126-116
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
describe('$log', function() {
55
var $window, logger, log, warn, info, error, debug;
66

7-
8-
97
beforeEach(module(function($provide) {
108
$window = {
119
navigator: {userAgent: window.navigator.userAgent},
@@ -67,147 +65,159 @@ describe('$log', function() {
6765
}
6866
));
6967

70-
it('should work if $window.navigator not defined', inject(
71-
function() {
72-
delete $window.navigator;
73-
},
74-
function($log) {}
75-
));
68+
runTests({ie9Mode: false});
69+
runTests({ie9Mode: true});
70+
71+
function runTests(options) {
72+
var ie9Mode = options.ie9Mode;
7673

77-
describe('IE logging behavior', function() {
78-
function removeApplyFunctionForIE() {
79-
log.apply = log.call =
74+
function attachMockConsoleTo$window() {
75+
// Support: IE 9 only
76+
// Simulate missing apply on console methods in IE 9.
77+
if (ie9Mode) {
78+
log.apply = log.call =
8079
warn.apply = warn.call =
8180
info.apply = info.call =
8281
error.apply = error.call =
8382
debug.apply = debug.call = null;
83+
}
8484

85-
$window.console = {log: log,
85+
$window.console = {
86+
log: log,
8687
warn: warn,
8788
info: info,
8889
error: error,
89-
debug: debug};
90+
debug: debug
91+
};
9092
}
9193

92-
it('should work in IE where console.error doesn\'t have an apply method', inject(
93-
removeApplyFunctionForIE,
94-
function($log) {
94+
describe(ie9Mode ? 'IE 9 logging behavior' : 'Modern browsers\' logging behavior', function() {
95+
beforeEach(module(attachMockConsoleTo$window));
96+
97+
it('should work if $window.navigator not defined', inject(
98+
function() {
99+
delete $window.navigator;
100+
},
101+
function($log) {}
102+
));
103+
104+
it('should have a working apply method', inject(function($log) {
95105
$log.log.apply($log);
96106
$log.warn.apply($log);
97107
$log.info.apply($log);
98108
$log.error.apply($log);
99109
$log.debug.apply($log);
100110
expect(logger).toEqual('log;warn;info;error;debug;');
101-
})
102-
);
103-
104-
// Support: Safari 9.1 only, iOS 9.3 only
105-
// For some reason Safari thinks there is always 1 parameter passed here.
106-
if (!/\b9\.\d(\.\d+)* safari/i.test(window.navigator.userAgent) &&
107-
!/\biphone os 9_/i.test(window.navigator.userAgent)) {
108-
it('should not attempt to log the second argument in IE if it is not specified', inject(
109-
function() {
110-
log = function(arg1, arg2) { logger += 'log,' + arguments.length + ';'; };
111-
warn = function(arg1, arg2) { logger += 'warn,' + arguments.length + ';'; };
112-
info = function(arg1, arg2) { logger += 'info,' + arguments.length + ';'; };
113-
error = function(arg1, arg2) { logger += 'error,' + arguments.length + ';'; };
114-
debug = function(arg1, arg2) { logger += 'debug,' + arguments.length + ';'; };
115-
},
116-
removeApplyFunctionForIE,
117-
function($log) {
118-
$log.log();
119-
$log.warn();
120-
$log.info();
121-
$log.error();
122-
$log.debug();
123-
expect(logger).toEqual('log,0;warn,0;info,0;error,0;debug,0;');
124-
})
125-
);
126-
}
127-
});
128-
129-
describe('$log.debug', function() {
130-
131-
beforeEach(initService(false));
132-
133-
it('should skip debugging output if disabled', inject(
134-
function() {
135-
$window.console = {log: log,
136-
warn: warn,
137-
info: info,
138-
error: error,
139-
debug: debug};
140-
},
141-
function($log) {
142-
$log.log();
143-
$log.warn();
144-
$log.info();
145-
$log.error();
146-
$log.debug();
147-
expect(logger).toEqual('log;warn;info;error;');
111+
}));
112+
113+
// Support: Safari 9.1 only, iOS 9.3 only
114+
// For some reason Safari thinks there is always 1 parameter passed here.
115+
if (!/\b9\.\d(\.\d+)* safari/i.test(window.navigator.userAgent) &&
116+
!/\biphone os 9_/i.test(window.navigator.userAgent)) {
117+
it('should not attempt to log the second argument in IE if it is not specified', inject(
118+
function() {
119+
log = function(arg1, arg2) { logger += 'log,' + arguments.length + ';'; };
120+
warn = function(arg1, arg2) { logger += 'warn,' + arguments.length + ';'; };
121+
info = function(arg1, arg2) { logger += 'info,' + arguments.length + ';'; };
122+
error = function(arg1, arg2) { logger += 'error,' + arguments.length + ';'; };
123+
debug = function(arg1, arg2) { logger += 'debug,' + arguments.length + ';'; };
124+
},
125+
attachMockConsoleTo$window,
126+
function($log) {
127+
$log.log();
128+
$log.warn();
129+
$log.info();
130+
$log.error();
131+
$log.debug();
132+
expect(logger).toEqual('log,0;warn,0;info,0;error,0;debug,0;');
133+
})
134+
);
148135
}
149-
));
150-
151-
});
152136

153-
describe('$log.error', function() {
154-
var e, $log;
155-
156-
function TestError() {
157-
Error.prototype.constructor.apply(this, arguments);
158-
this.message = undefined;
159-
this.sourceURL = undefined;
160-
this.line = undefined;
161-
this.stack = undefined;
162-
}
163-
TestError.prototype = Object.create(Error.prototype);
164-
TestError.prototype.constructor = TestError;
165-
166-
beforeEach(inject(
167-
function() {
168-
e = new TestError('');
169-
$window.console = {
170-
error: jasmine.createSpy('error')
171-
};
172-
},
173-
174-
function(_$log_) {
175-
$log = _$log_;
176-
}
177-
));
178-
179-
it('should pass error if does not have trace', function() {
180-
$log.error('abc', e);
181-
expect($window.console.error).toHaveBeenCalledWith('abc', e);
182-
});
137+
describe('$log.debug', function() {
138+
139+
beforeEach(initService(false));
140+
141+
it('should skip debugging output if disabled', inject(
142+
function() {
143+
$window.console = {log: log,
144+
warn: warn,
145+
info: info,
146+
error: error,
147+
debug: debug};
148+
},
149+
function($log) {
150+
$log.log();
151+
$log.warn();
152+
$log.info();
153+
$log.error();
154+
$log.debug();
155+
expect(logger).toEqual('log;warn;info;error;');
156+
}
157+
));
183158

184-
if (msie || /\bEdge\//.test(window.navigator.userAgent)) {
185-
it('should print stack', function() {
186-
e.stack = 'stack';
187-
$log.error('abc', e);
188-
expect($window.console.error).toHaveBeenCalledWith('abc', 'stack');
189159
});
190-
} else {
191-
it('should print a raw error', function() {
192-
e.stack = 'stack';
193-
$log.error('abc', e);
194-
expect($window.console.error).toHaveBeenCalledWith('abc', e);
195-
});
196-
}
197160

198-
it('should print line', function() {
199-
e.message = 'message';
200-
e.sourceURL = 'sourceURL';
201-
e.line = '123';
202-
$log.error('abc', e);
203-
expect($window.console.error).toHaveBeenCalledWith('abc', 'message\nsourceURL:123');
161+
describe('$log.error', function() {
162+
var e, $log;
163+
164+
function TestError() {
165+
Error.prototype.constructor.apply(this, arguments);
166+
this.message = undefined;
167+
this.sourceURL = undefined;
168+
this.line = undefined;
169+
this.stack = undefined;
170+
}
171+
TestError.prototype = Object.create(Error.prototype);
172+
TestError.prototype.constructor = TestError;
173+
174+
beforeEach(inject(
175+
function() {
176+
e = new TestError('');
177+
$window.console = {
178+
error: jasmine.createSpy('error')
179+
};
180+
},
181+
182+
function(_$log_) {
183+
$log = _$log_;
184+
}
185+
));
186+
187+
it('should pass error if does not have trace', function() {
188+
$log.error('abc', e);
189+
expect($window.console.error).toHaveBeenCalledWith('abc', e);
190+
});
191+
192+
if (msie || /\bEdge\//.test(window.navigator.userAgent)) {
193+
it('should print stack', function() {
194+
e.stack = 'stack';
195+
$log.error('abc', e);
196+
expect($window.console.error).toHaveBeenCalledWith('abc', 'stack');
197+
});
198+
} else {
199+
it('should print a raw error', function() {
200+
e.stack = 'stack';
201+
$log.error('abc', e);
202+
expect($window.console.error).toHaveBeenCalledWith('abc', e);
203+
});
204+
}
205+
206+
it('should print line', function() {
207+
e.message = 'message';
208+
e.sourceURL = 'sourceURL';
209+
e.line = '123';
210+
$log.error('abc', e);
211+
expect($window.console.error).toHaveBeenCalledWith('abc', 'message\nsourceURL:123');
212+
});
213+
});
204214
});
205-
});
215+
}
216+
206217

207218
function initService(debugEnabled) {
208219
return module(function($logProvider) {
209220
$logProvider.debugEnabled(debugEnabled);
210221
});
211222
}
212-
213223
});

0 commit comments

Comments
 (0)