|
| 1 | +/** |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance |
| 5 | + * with the License. A copy of the License is located at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * or in the 'license' file accompanying this file. This file is distributed on an 'AS IS' BASIS, WITHOUT WARRANTIES |
| 10 | + * OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific language governing permissions |
| 11 | + * and limitations under the License. |
| 12 | + */ |
| 13 | +import { MockObserver } from './MockObserver'; |
| 14 | + |
| 15 | +export function setUpBrowserPerformance() { |
| 16 | + (global as any).PerformanceObserver = MockObserver; |
| 17 | + setPerformanceEntries(false); |
| 18 | +} |
| 19 | + |
| 20 | +export function setPerformanceEntries(isLoaded = true) { |
| 21 | + Object.defineProperty(window, 'performance', { |
| 22 | + writable: true, |
| 23 | + value: { |
| 24 | + getEntriesByType: jest |
| 25 | + .fn() |
| 26 | + .mockImplementation( |
| 27 | + isLoaded ? getEntriesByType : getEntriesByTypeUnload |
| 28 | + ), |
| 29 | + }, |
| 30 | + }); |
| 31 | +} |
| 32 | + |
| 33 | +function getEntriesByType(): PerformanceEntryList { |
| 34 | + return <PerformanceEntry[]>(<unknown>[ |
| 35 | + { |
| 36 | + name: 'https://aws.amazon.com/cn/', |
| 37 | + entryType: 'navigation', |
| 38 | + startTime: 0, |
| 39 | + duration: 3444.4000000059605, |
| 40 | + initiatorType: 'navigation', |
| 41 | + deliveryType: 'indirect', |
| 42 | + nextHopProtocol: 'h2', |
| 43 | + renderBlockingStatus: 'non-blocking', |
| 44 | + workerStart: 0, |
| 45 | + redirectStart: 2, |
| 46 | + redirectEnd: 2.2, |
| 47 | + fetchStart: 2.2000000178813934, |
| 48 | + domainLookupStart: 2.2000000178813934, |
| 49 | + domainLookupEnd: 2.2000000178813934, |
| 50 | + connectStart: 2.2000000178813934, |
| 51 | + secureConnectionStart: 2.2000000178813934, |
| 52 | + connectEnd: 2.2000000178813934, |
| 53 | + requestStart: 745.9000000059605, |
| 54 | + responseStart: 1006.7000000178814, |
| 55 | + firstInterimResponseStart: 0, |
| 56 | + responseEnd: 1321.300000011921, |
| 57 | + transferSize: 167553, |
| 58 | + encodedBodySize: 167253, |
| 59 | + decodedBodySize: 1922019, |
| 60 | + responseStatus: 200, |
| 61 | + serverTiming: [ |
| 62 | + { |
| 63 | + name: 'cache', |
| 64 | + duration: 0, |
| 65 | + description: 'hit-front', |
| 66 | + }, |
| 67 | + { |
| 68 | + name: 'host', |
| 69 | + duration: 0, |
| 70 | + description: 'cp3062', |
| 71 | + }, |
| 72 | + ], |
| 73 | + unloadEventStart: 1011.9000000059605, |
| 74 | + unloadEventEnd: 1011.9000000059605, |
| 75 | + domInteractive: 1710.9000000059605, |
| 76 | + domContentLoadedEventStart: 1712.7000000178814, |
| 77 | + domContentLoadedEventEnd: 1714.7000000178814, |
| 78 | + domComplete: 3440.4000000059605, |
| 79 | + loadEventStart: 3444.2000000178814, |
| 80 | + loadEventEnd: 3444.4000000059605, |
| 81 | + type: 'reload', |
| 82 | + redirectCount: 0, |
| 83 | + activationStart: 0, |
| 84 | + criticalCHRestart: 0, |
| 85 | + }, |
| 86 | + ]); |
| 87 | +} |
| 88 | + |
| 89 | +function getEntriesByTypeUnload(): PerformanceEntryList { |
| 90 | + return <PerformanceEntry[]>(<unknown>[ |
| 91 | + { |
| 92 | + name: 'https://aws.amazon.com/cn/', |
| 93 | + entryType: 'navigation', |
| 94 | + startTime: 0, |
| 95 | + duration: 0, |
| 96 | + }, |
| 97 | + ]); |
| 98 | +} |
0 commit comments