|
9 | 9 | */
|
10 | 10 |
|
11 | 11 | 'use strict';
|
| 12 | + |
12 | 13 | jest.unmock('../../Utilities/Platform');
|
| 14 | +jest.mock('../../Utilities/GlobalPerformanceLogger'); |
| 15 | + |
13 | 16 | const Platform = require('../../Utilities/Platform');
|
| 17 | +const GlobalPerformanceLogger = require('../../Utilities/GlobalPerformanceLogger'); |
| 18 | +const createPerformanceLogger = require('../../Utilities/createPerformanceLogger'); |
14 | 19 | let requestId = 1;
|
15 | 20 |
|
16 | 21 | function setRequestId(id) {
|
@@ -71,6 +76,8 @@ describe('XMLHttpRequest', function() {
|
71 | 76 | xhr.addEventListener('load', handleLoad);
|
72 | 77 | xhr.addEventListener('loadend', handleLoadEnd);
|
73 | 78 | xhr.addEventListener('readystatechange', handleReadyStateChange);
|
| 79 | + |
| 80 | + jest.clearAllMocks(); |
74 | 81 | });
|
75 | 82 |
|
76 | 83 | afterEach(() => {
|
@@ -238,4 +245,52 @@ describe('XMLHttpRequest', function() {
|
238 | 245 | 'Content-Type: text/plain; charset=utf-8\r\n' + 'Content-Length: 32',
|
239 | 246 | );
|
240 | 247 | });
|
| 248 | + |
| 249 | + it('should log to GlobalPerformanceLogger if a custom performance logger is not set', () => { |
| 250 | + xhr.open('GET', 'blabla'); |
| 251 | + xhr.send(); |
| 252 | + |
| 253 | + expect(GlobalPerformanceLogger.startTimespan).toHaveBeenCalledWith( |
| 254 | + 'network_XMLHttpRequest_blabla', |
| 255 | + ); |
| 256 | + expect(GlobalPerformanceLogger.stopTimespan).not.toHaveBeenCalled(); |
| 257 | + |
| 258 | + setRequestId(8); |
| 259 | + xhr.__didReceiveResponse(requestId, 200, { |
| 260 | + 'Content-Type': 'text/plain; charset=utf-8', |
| 261 | + 'Content-Length': '32', |
| 262 | + }); |
| 263 | + |
| 264 | + expect(GlobalPerformanceLogger.stopTimespan).toHaveBeenCalledWith( |
| 265 | + 'network_XMLHttpRequest_blabla', |
| 266 | + ); |
| 267 | + }); |
| 268 | + |
| 269 | + it('should log to a custom performance logger if set', () => { |
| 270 | + const performanceLogger = createPerformanceLogger(); |
| 271 | + jest.spyOn(performanceLogger, 'startTimespan'); |
| 272 | + jest.spyOn(performanceLogger, 'stopTimespan'); |
| 273 | + |
| 274 | + xhr.setPerformanceLogger(performanceLogger); |
| 275 | + |
| 276 | + xhr.open('GET', 'blabla'); |
| 277 | + xhr.send(); |
| 278 | + |
| 279 | + expect(performanceLogger.startTimespan).toHaveBeenCalledWith( |
| 280 | + 'network_XMLHttpRequest_blabla', |
| 281 | + ); |
| 282 | + expect(GlobalPerformanceLogger.startTimespan).not.toHaveBeenCalled(); |
| 283 | + expect(performanceLogger.stopTimespan).not.toHaveBeenCalled(); |
| 284 | + |
| 285 | + setRequestId(9); |
| 286 | + xhr.__didReceiveResponse(requestId, 200, { |
| 287 | + 'Content-Type': 'text/plain; charset=utf-8', |
| 288 | + 'Content-Length': '32', |
| 289 | + }); |
| 290 | + |
| 291 | + expect(performanceLogger.stopTimespan).toHaveBeenCalledWith( |
| 292 | + 'network_XMLHttpRequest_blabla', |
| 293 | + ); |
| 294 | + expect(GlobalPerformanceLogger.stopTimespan).not.toHaveBeenCalled(); |
| 295 | + }); |
241 | 296 | });
|
0 commit comments