|
7 | 7 | BrowserTracing, |
8 | 8 | BrowserTracingOptions, |
9 | 9 | DEFAULT_MAX_TRANSACTION_DURATION_SECONDS, |
| 10 | + getHeaderContext, |
10 | 11 | getMetaContent, |
11 | 12 | } from '../../src/browser/browsertracing'; |
12 | 13 | import { defaultRequestInstrumentionOptions } from '../../src/browser/request'; |
@@ -177,14 +178,15 @@ describe('BrowserTracing', () => { |
177 | 178 | expect(mockBeforeNavigation).toHaveBeenCalledTimes(1); |
178 | 179 | }); |
179 | 180 |
|
180 | | - it('does not create a transaction if it returns undefined', () => { |
| 181 | + // TODO add this back in once getTransaction() returns sampled = false transactions, too |
| 182 | + it.skip('creates a transaction with sampled = false if it returns undefined', () => { |
181 | 183 | const mockBeforeNavigation = jest.fn().mockReturnValue(undefined); |
182 | 184 | createBrowserTracing(true, { |
183 | 185 | beforeNavigate: mockBeforeNavigation, |
184 | 186 | routingInstrumentation: customRoutingInstrumentation, |
185 | 187 | }); |
186 | 188 | const transaction = getActiveTransaction(hub) as IdleTransaction; |
187 | | - expect(transaction).not.toBeDefined(); |
| 189 | + expect(transaction.sampled).toBe(false); |
188 | 190 |
|
189 | 191 | expect(mockBeforeNavigation).toHaveBeenCalledTimes(1); |
190 | 192 | }); |
@@ -379,5 +381,67 @@ describe('BrowserTracing', () => { |
379 | 381 | expect(metaTagValue).toBe(content); |
380 | 382 | }); |
381 | 383 | }); |
| 384 | + |
| 385 | + describe('getHeaderContext', () => { |
| 386 | + it('correctly parses a valid sentry-trace meta header', () => { |
| 387 | + document.head.innerHTML = `<meta name="sentry-trace" content="12312012123120121231201212312012-1121201211212012-0">`; |
| 388 | + |
| 389 | + const headerContext = getHeaderContext(); |
| 390 | + |
| 391 | + expect(headerContext).toBeDefined(); |
| 392 | + expect(headerContext!.traceId).toEqual('12312012123120121231201212312012'); |
| 393 | + expect(headerContext!.parentSpanId).toEqual('1121201211212012'); |
| 394 | + expect(headerContext!.parentSampled).toEqual(false); |
| 395 | + }); |
| 396 | + |
| 397 | + it('returns undefined if the header is malformed', () => { |
| 398 | + document.head.innerHTML = `<meta name="sentry-trace" content="12312012-112120121-0">`; |
| 399 | + |
| 400 | + const headerContext = getHeaderContext(); |
| 401 | + |
| 402 | + expect(headerContext).toBeUndefined(); |
| 403 | + }); |
| 404 | + |
| 405 | + it("returns undefined if the header isn't there", () => { |
| 406 | + document.head.innerHTML = `<meta name="dogs" content="12312012123120121231201212312012-1121201211212012-0">`; |
| 407 | + |
| 408 | + const headerContext = getHeaderContext(); |
| 409 | + |
| 410 | + expect(headerContext).toBeUndefined(); |
| 411 | + }); |
| 412 | + }); |
| 413 | + |
| 414 | + describe('using the data', () => { |
| 415 | + // TODO add this back in once getTransaction() returns sampled = false transactions, too |
| 416 | + it.skip('uses the data for pageload transactions', () => { |
| 417 | + // make sampled false here, so we can see that it's being used rather than the tracesSampleRate-dictated one |
| 418 | + document.head.innerHTML = `<meta name="sentry-trace" content="12312012123120121231201212312012-1121201211212012-0">`; |
| 419 | + |
| 420 | + // pageload transactions are created as part of the BrowserTracing integration's initialization |
| 421 | + createBrowserTracing(true); |
| 422 | + const transaction = getActiveTransaction(hub) as IdleTransaction; |
| 423 | + |
| 424 | + expect(transaction).toBeDefined(); |
| 425 | + expect(transaction.op).toBe('pageload'); |
| 426 | + expect(transaction.traceId).toEqual('12312012123120121231201212312012'); |
| 427 | + expect(transaction.parentSpanId).toEqual('1121201211212012'); |
| 428 | + expect(transaction.sampled).toBe(false); |
| 429 | + }); |
| 430 | + |
| 431 | + it('ignores the data for navigation transactions', () => { |
| 432 | + mockChangeHistory = () => undefined; |
| 433 | + document.head.innerHTML = `<meta name="sentry-trace" content="12312012123120121231201212312012-1121201211212012-0">`; |
| 434 | + |
| 435 | + createBrowserTracing(true); |
| 436 | + |
| 437 | + mockChangeHistory({ to: 'here', from: 'there' }); |
| 438 | + const transaction = getActiveTransaction(hub) as IdleTransaction; |
| 439 | + |
| 440 | + expect(transaction).toBeDefined(); |
| 441 | + expect(transaction.op).toBe('navigation'); |
| 442 | + expect(transaction.traceId).not.toEqual('12312012123120121231201212312012'); |
| 443 | + expect(transaction.parentSpanId).toBeUndefined(); |
| 444 | + }); |
| 445 | + }); |
382 | 446 | }); |
383 | 447 | }); |
0 commit comments