|
4 | 4 | describe('$log', function() {
|
5 | 5 | var $window, logger, log, warn, info, error, debug;
|
6 | 6 |
|
7 |
| - |
8 |
| - |
9 | 7 | beforeEach(module(function($provide) {
|
10 | 8 | $window = {
|
11 | 9 | navigator: {userAgent: window.navigator.userAgent},
|
@@ -67,147 +65,159 @@ describe('$log', function() {
|
67 | 65 | }
|
68 | 66 | ));
|
69 | 67 |
|
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; |
76 | 73 |
|
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 = |
80 | 79 | warn.apply = warn.call =
|
81 | 80 | info.apply = info.call =
|
82 | 81 | error.apply = error.call =
|
83 | 82 | debug.apply = debug.call = null;
|
| 83 | + } |
84 | 84 |
|
85 |
| - $window.console = {log: log, |
| 85 | + $window.console = { |
| 86 | + log: log, |
86 | 87 | warn: warn,
|
87 | 88 | info: info,
|
88 | 89 | error: error,
|
89 |
| - debug: debug}; |
| 90 | + debug: debug |
| 91 | + }; |
90 | 92 | }
|
91 | 93 |
|
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) { |
95 | 105 | $log.log.apply($log);
|
96 | 106 | $log.warn.apply($log);
|
97 | 107 | $log.info.apply($log);
|
98 | 108 | $log.error.apply($log);
|
99 | 109 | $log.debug.apply($log);
|
100 | 110 | 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 | + ); |
148 | 135 | }
|
149 |
| - )); |
150 |
| - |
151 |
| - }); |
152 | 136 |
|
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 | + )); |
183 | 158 |
|
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'); |
189 | 159 | });
|
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 |
| - } |
197 | 160 |
|
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 | + }); |
204 | 214 | });
|
205 |
| - }); |
| 215 | + } |
| 216 | + |
206 | 217 |
|
207 | 218 | function initService(debugEnabled) {
|
208 | 219 | return module(function($logProvider) {
|
209 | 220 | $logProvider.debugEnabled(debugEnabled);
|
210 | 221 | });
|
211 | 222 | }
|
212 |
| - |
213 | 223 | });
|
0 commit comments