|
1 | 1 | import { Span, Transaction } from '../../src';
|
2 |
| -import { _startChild } from '../../src/browser/metrics'; |
| 2 | +import { _startChild, addResourceSpans, ResourceEntry } from '../../src/browser/metrics'; |
3 | 3 |
|
4 | 4 | describe('_startChild()', () => {
|
5 | 5 | it('creates a span with given properties', () => {
|
@@ -38,3 +38,132 @@ describe('_startChild()', () => {
|
38 | 38 | expect(transaction.startTimestamp).toEqual(123);
|
39 | 39 | });
|
40 | 40 | });
|
| 41 | + |
| 42 | +describe('addResourceSpans', () => { |
| 43 | + const transaction = new Transaction({ name: 'hello' }); |
| 44 | + beforeEach(() => { |
| 45 | + transaction.startChild = jest.fn(); |
| 46 | + }); |
| 47 | + |
| 48 | + // We already track xhr, we don't need to use |
| 49 | + it('does not create spans for xmlhttprequest', () => { |
| 50 | + const entry: ResourceEntry = { |
| 51 | + initiatorType: 'xmlhttprequest', |
| 52 | + transferSize: 256, |
| 53 | + encodedBodySize: 256, |
| 54 | + decodedBodySize: 256, |
| 55 | + }; |
| 56 | + addResourceSpans(transaction, entry, '/assets/to/me', 123, 456, 100); |
| 57 | + |
| 58 | + // eslint-disable-next-line @typescript-eslint/unbound-method |
| 59 | + expect(transaction.startChild).toHaveBeenCalledTimes(0); |
| 60 | + }); |
| 61 | + |
| 62 | + it('does not create spans for fetch', () => { |
| 63 | + const entry: ResourceEntry = { |
| 64 | + initiatorType: 'fetch', |
| 65 | + transferSize: 256, |
| 66 | + encodedBodySize: 256, |
| 67 | + decodedBodySize: 256, |
| 68 | + }; |
| 69 | + addResourceSpans(transaction, entry, '/assets/to/me', 123, 456, 100); |
| 70 | + |
| 71 | + // eslint-disable-next-line @typescript-eslint/unbound-method |
| 72 | + expect(transaction.startChild).toHaveBeenCalledTimes(0); |
| 73 | + }); |
| 74 | + |
| 75 | + it('creates spans for resource spans', () => { |
| 76 | + const entry: ResourceEntry = { |
| 77 | + initiatorType: 'css', |
| 78 | + transferSize: 256, |
| 79 | + encodedBodySize: 456, |
| 80 | + decodedBodySize: 593, |
| 81 | + }; |
| 82 | + |
| 83 | + const timeOrigin = 100; |
| 84 | + const startTime = 23; |
| 85 | + const duration = 356; |
| 86 | + |
| 87 | + const endTimestamp = addResourceSpans(transaction, entry, '/assets/to/css', startTime, duration, timeOrigin); |
| 88 | + |
| 89 | + // eslint-disable-next-line @typescript-eslint/unbound-method |
| 90 | + expect(transaction.startChild).toHaveBeenCalledTimes(1); |
| 91 | + // eslint-disable-next-line @typescript-eslint/unbound-method |
| 92 | + expect(transaction.startChild).toHaveBeenLastCalledWith({ |
| 93 | + data: { |
| 94 | + ['Decoded Body Size']: entry.decodedBodySize, |
| 95 | + ['Encoded Body Size']: entry.encodedBodySize, |
| 96 | + ['Transfer Size']: entry.transferSize, |
| 97 | + }, |
| 98 | + description: '/assets/to/css', |
| 99 | + endTimestamp: timeOrigin + startTime + duration, |
| 100 | + op: 'resource.css', |
| 101 | + startTimestamp: timeOrigin + startTime, |
| 102 | + }); |
| 103 | + |
| 104 | + expect(endTimestamp).toBe(timeOrigin + startTime + duration); |
| 105 | + }); |
| 106 | + |
| 107 | + it('creates a variety of resource spans', () => { |
| 108 | + const table = [ |
| 109 | + { |
| 110 | + initiatorType: undefined, |
| 111 | + op: 'resource', |
| 112 | + }, |
| 113 | + { |
| 114 | + initiatorType: '', |
| 115 | + op: 'resource', |
| 116 | + }, |
| 117 | + { |
| 118 | + initiatorType: 'css', |
| 119 | + op: 'resource.css', |
| 120 | + }, |
| 121 | + { |
| 122 | + initiatorType: 'image', |
| 123 | + op: 'resource.image', |
| 124 | + }, |
| 125 | + { |
| 126 | + initiatorType: 'script', |
| 127 | + op: 'resource.script', |
| 128 | + }, |
| 129 | + ]; |
| 130 | + |
| 131 | + for (const { initiatorType, op } of table) { |
| 132 | + const entry: ResourceEntry = { |
| 133 | + initiatorType, |
| 134 | + }; |
| 135 | + addResourceSpans(transaction, entry, '/assets/to/me', 123, 234, 465); |
| 136 | + |
| 137 | + // eslint-disable-next-line @typescript-eslint/unbound-method |
| 138 | + expect(transaction.startChild).toHaveBeenLastCalledWith( |
| 139 | + expect.objectContaining({ |
| 140 | + op, |
| 141 | + }), |
| 142 | + ); |
| 143 | + } |
| 144 | + }); |
| 145 | + |
| 146 | + it('allows for enter size of 0', () => { |
| 147 | + const entry: ResourceEntry = { |
| 148 | + initiatorType: 'css', |
| 149 | + transferSize: 0, |
| 150 | + encodedBodySize: 0, |
| 151 | + decodedBodySize: 0, |
| 152 | + }; |
| 153 | + |
| 154 | + addResourceSpans(transaction, entry, '/assets/to/css', 100, 23, 345); |
| 155 | + |
| 156 | + // eslint-disable-next-line @typescript-eslint/unbound-method |
| 157 | + expect(transaction.startChild).toHaveBeenCalledTimes(1); |
| 158 | + // eslint-disable-next-line @typescript-eslint/unbound-method |
| 159 | + expect(transaction.startChild).toHaveBeenLastCalledWith( |
| 160 | + expect.objectContaining({ |
| 161 | + data: { |
| 162 | + ['Decoded Body Size']: entry.decodedBodySize, |
| 163 | + ['Encoded Body Size']: entry.encodedBodySize, |
| 164 | + ['Transfer Size']: entry.transferSize, |
| 165 | + }, |
| 166 | + }), |
| 167 | + ); |
| 168 | + }); |
| 169 | +}); |
0 commit comments