|
4 | 4 | import type { SeverityLevel } from '@sentry/core'; |
5 | 5 | import * as core from '@sentry/core'; |
6 | 6 | import { TouchEventBoundary } from '../src/js/touchevents'; |
| 7 | +import * as userInteractionModule from '../src/js/tracing/integrations/userInteraction'; |
7 | 8 | import { getDefaultTestClientOptions, TestClient } from './mocks/client'; |
8 | 9 |
|
9 | 10 | describe('TouchEventBoundary._onTouchStart', () => { |
@@ -310,4 +311,243 @@ describe('TouchEventBoundary._onTouchStart', () => { |
310 | 311 | type: defaultProps.breadcrumbType, |
311 | 312 | }); |
312 | 313 | }); |
| 314 | + |
| 315 | + describe('sentry-span-attributes', () => { |
| 316 | + it('sets custom attributes from prop on user interaction span', () => { |
| 317 | + const { defaultProps } = TouchEventBoundary; |
| 318 | + const boundary = new TouchEventBoundary(defaultProps); |
| 319 | + |
| 320 | + const mockSpan = { |
| 321 | + setAttribute: jest.fn(), |
| 322 | + setAttributes: jest.fn(), |
| 323 | + }; |
| 324 | + jest.spyOn(userInteractionModule, 'startUserInteractionSpan').mockReturnValue(mockSpan as any); |
| 325 | + |
| 326 | + const event = { |
| 327 | + _targetInst: { |
| 328 | + elementType: { displayName: 'Button' }, |
| 329 | + memoizedProps: { |
| 330 | + 'sentry-label': 'checkout', |
| 331 | + 'sentry-span-attributes': { |
| 332 | + 'user.subscription': 'premium', |
| 333 | + 'cart.items': '3', |
| 334 | + 'feature.enabled': true, |
| 335 | + }, |
| 336 | + }, |
| 337 | + }, |
| 338 | + }; |
| 339 | + |
| 340 | + // @ts-expect-error Calling private member |
| 341 | + boundary._onTouchStart(event); |
| 342 | + |
| 343 | + expect(mockSpan.setAttributes).toHaveBeenCalledWith({ |
| 344 | + 'user.subscription': 'premium', |
| 345 | + 'cart.items': '3', |
| 346 | + 'feature.enabled': true, |
| 347 | + }); |
| 348 | + }); |
| 349 | + |
| 350 | + it('handles multiple attribute types (string, number, boolean)', () => { |
| 351 | + const { defaultProps } = TouchEventBoundary; |
| 352 | + const boundary = new TouchEventBoundary(defaultProps); |
| 353 | + |
| 354 | + const mockSpan = { |
| 355 | + setAttribute: jest.fn(), |
| 356 | + setAttributes: jest.fn(), |
| 357 | + }; |
| 358 | + jest.spyOn(userInteractionModule, 'startUserInteractionSpan').mockReturnValue(mockSpan as any); |
| 359 | + |
| 360 | + const event = { |
| 361 | + _targetInst: { |
| 362 | + elementType: { displayName: 'Button' }, |
| 363 | + memoizedProps: { |
| 364 | + 'sentry-label': 'test', |
| 365 | + 'sentry-span-attributes': { |
| 366 | + 'string.value': 'test', |
| 367 | + 'number.value': 42, |
| 368 | + 'boolean.value': false, |
| 369 | + 'array.value': ['a', 'b', 'c'], |
| 370 | + }, |
| 371 | + }, |
| 372 | + }, |
| 373 | + }; |
| 374 | + |
| 375 | + // @ts-expect-error Calling private member |
| 376 | + boundary._onTouchStart(event); |
| 377 | + |
| 378 | + expect(mockSpan.setAttributes).toHaveBeenCalledWith({ |
| 379 | + 'string.value': 'test', |
| 380 | + 'number.value': 42, |
| 381 | + 'boolean.value': false, |
| 382 | + 'array.value': ['a', 'b', 'c'], |
| 383 | + }); |
| 384 | + }); |
| 385 | + |
| 386 | + it('handles invalid span attributes gracefully (null)', () => { |
| 387 | + const { defaultProps } = TouchEventBoundary; |
| 388 | + const boundary = new TouchEventBoundary(defaultProps); |
| 389 | + |
| 390 | + const mockSpan = { |
| 391 | + setAttribute: jest.fn(), |
| 392 | + setAttributes: jest.fn(), |
| 393 | + }; |
| 394 | + jest.spyOn(userInteractionModule, 'startUserInteractionSpan').mockReturnValue(mockSpan as any); |
| 395 | + |
| 396 | + const event = { |
| 397 | + _targetInst: { |
| 398 | + elementType: { displayName: 'Button' }, |
| 399 | + memoizedProps: { |
| 400 | + 'sentry-label': 'test', |
| 401 | + 'sentry-span-attributes': null, |
| 402 | + }, |
| 403 | + }, |
| 404 | + }; |
| 405 | + |
| 406 | + // @ts-expect-error Calling private member |
| 407 | + boundary._onTouchStart(event); |
| 408 | + |
| 409 | + expect(mockSpan.setAttributes).not.toHaveBeenCalled(); |
| 410 | + }); |
| 411 | + |
| 412 | + it('handles invalid span attributes gracefully (array)', () => { |
| 413 | + const { defaultProps } = TouchEventBoundary; |
| 414 | + const boundary = new TouchEventBoundary(defaultProps); |
| 415 | + |
| 416 | + const mockSpan = { |
| 417 | + setAttribute: jest.fn(), |
| 418 | + setAttributes: jest.fn(), |
| 419 | + }; |
| 420 | + jest.spyOn(userInteractionModule, 'startUserInteractionSpan').mockReturnValue(mockSpan as any); |
| 421 | + |
| 422 | + const event = { |
| 423 | + _targetInst: { |
| 424 | + elementType: { displayName: 'Button' }, |
| 425 | + memoizedProps: { |
| 426 | + 'sentry-label': 'test', |
| 427 | + 'sentry-span-attributes': ['invalid', 'array'], |
| 428 | + }, |
| 429 | + }, |
| 430 | + }; |
| 431 | + |
| 432 | + // @ts-expect-error Calling private member |
| 433 | + boundary._onTouchStart(event); |
| 434 | + |
| 435 | + expect(mockSpan.setAttributes).not.toHaveBeenCalled(); |
| 436 | + }); |
| 437 | + |
| 438 | + it('handles empty object gracefully', () => { |
| 439 | + const { defaultProps } = TouchEventBoundary; |
| 440 | + const boundary = new TouchEventBoundary(defaultProps); |
| 441 | + |
| 442 | + const mockSpan = { |
| 443 | + setAttribute: jest.fn(), |
| 444 | + setAttributes: jest.fn(), |
| 445 | + }; |
| 446 | + jest.spyOn(userInteractionModule, 'startUserInteractionSpan').mockReturnValue(mockSpan as any); |
| 447 | + |
| 448 | + const event = { |
| 449 | + _targetInst: { |
| 450 | + elementType: { displayName: 'Button' }, |
| 451 | + memoizedProps: { |
| 452 | + 'sentry-label': 'test', |
| 453 | + 'sentry-span-attributes': {}, |
| 454 | + }, |
| 455 | + }, |
| 456 | + }; |
| 457 | + |
| 458 | + // @ts-expect-error Calling private member |
| 459 | + boundary._onTouchStart(event); |
| 460 | + |
| 461 | + expect(mockSpan.setAttributes).not.toHaveBeenCalled(); |
| 462 | + }); |
| 463 | + |
| 464 | + it('works with sentry-label', () => { |
| 465 | + const { defaultProps } = TouchEventBoundary; |
| 466 | + const boundary = new TouchEventBoundary(defaultProps); |
| 467 | + |
| 468 | + const mockSpan = { |
| 469 | + setAttribute: jest.fn(), |
| 470 | + setAttributes: jest.fn(), |
| 471 | + }; |
| 472 | + jest.spyOn(userInteractionModule, 'startUserInteractionSpan').mockReturnValue(mockSpan as any); |
| 473 | + |
| 474 | + const event = { |
| 475 | + _targetInst: { |
| 476 | + elementType: { displayName: 'Button' }, |
| 477 | + memoizedProps: { |
| 478 | + 'sentry-label': 'checkout-button', |
| 479 | + 'sentry-span-attributes': { |
| 480 | + 'custom.key': 'value', |
| 481 | + }, |
| 482 | + }, |
| 483 | + }, |
| 484 | + }; |
| 485 | + |
| 486 | + // @ts-expect-error Calling private member |
| 487 | + boundary._onTouchStart(event); |
| 488 | + |
| 489 | + expect(userInteractionModule.startUserInteractionSpan).toHaveBeenCalledWith({ |
| 490 | + elementId: 'checkout-button', |
| 491 | + op: 'ui.action.touch', |
| 492 | + }); |
| 493 | + expect(mockSpan.setAttributes).toHaveBeenCalledWith({ |
| 494 | + 'custom.key': 'value', |
| 495 | + }); |
| 496 | + }); |
| 497 | + |
| 498 | + it('finds attributes in component tree', () => { |
| 499 | + const { defaultProps } = TouchEventBoundary; |
| 500 | + const boundary = new TouchEventBoundary(defaultProps); |
| 501 | + |
| 502 | + const mockSpan = { |
| 503 | + setAttribute: jest.fn(), |
| 504 | + setAttributes: jest.fn(), |
| 505 | + }; |
| 506 | + jest.spyOn(userInteractionModule, 'startUserInteractionSpan').mockReturnValue(mockSpan as any); |
| 507 | + |
| 508 | + const event = { |
| 509 | + _targetInst: { |
| 510 | + elementType: { displayName: 'Text' }, |
| 511 | + return: { |
| 512 | + elementType: { displayName: 'Button' }, |
| 513 | + memoizedProps: { |
| 514 | + 'sentry-label': 'parent-button', |
| 515 | + 'sentry-span-attributes': { |
| 516 | + 'found.in': 'parent', |
| 517 | + }, |
| 518 | + }, |
| 519 | + }, |
| 520 | + }, |
| 521 | + }; |
| 522 | + |
| 523 | + // @ts-expect-error Calling private member |
| 524 | + boundary._onTouchStart(event); |
| 525 | + |
| 526 | + expect(mockSpan.setAttributes).toHaveBeenCalledWith({ |
| 527 | + 'found.in': 'parent', |
| 528 | + }); |
| 529 | + }); |
| 530 | + |
| 531 | + it('does not call setAttributes when no span is created', () => { |
| 532 | + const { defaultProps } = TouchEventBoundary; |
| 533 | + const boundary = new TouchEventBoundary(defaultProps); |
| 534 | + |
| 535 | + jest.spyOn(userInteractionModule, 'startUserInteractionSpan').mockReturnValue(undefined); |
| 536 | + |
| 537 | + const event = { |
| 538 | + _targetInst: { |
| 539 | + elementType: { displayName: 'Button' }, |
| 540 | + memoizedProps: { |
| 541 | + 'sentry-label': 'test', |
| 542 | + 'sentry-span-attributes': { |
| 543 | + 'custom.key': 'value', |
| 544 | + }, |
| 545 | + }, |
| 546 | + }, |
| 547 | + }; |
| 548 | + |
| 549 | + // @ts-expect-error Calling private member |
| 550 | + expect(() => boundary._onTouchStart(event)).not.toThrow(); |
| 551 | + }); |
| 552 | + }); |
313 | 553 | }); |
0 commit comments