|
9 | 9 | SEMANTIC_ATTRIBUTE_SENTRY_SOURCE, |
10 | 10 | setCurrentClient, |
11 | 11 | } from '@sentry/core'; |
12 | | -import { render } from '@testing-library/react'; |
| 12 | +import { act, render, waitFor } from '@testing-library/react'; |
13 | 13 | import * as React from 'react'; |
14 | 14 | import { |
15 | 15 | createMemoryRouter, |
@@ -607,4 +607,276 @@ describe('React Router cross usage of wrappers', () => { |
607 | 607 | }); |
608 | 608 | }); |
609 | 609 | }); |
| 610 | + |
| 611 | + describe('consecutive navigations to different routes', () => { |
| 612 | + it('should create separate transactions for consecutive navigations to different routes', async () => { |
| 613 | + const client = createMockBrowserClient(); |
| 614 | + setCurrentClient(client); |
| 615 | + |
| 616 | + client.addIntegration( |
| 617 | + reactRouterV6BrowserTracingIntegration({ |
| 618 | + useEffect: React.useEffect, |
| 619 | + useLocation, |
| 620 | + useNavigationType, |
| 621 | + createRoutesFromChildren, |
| 622 | + matchRoutes, |
| 623 | + }), |
| 624 | + ); |
| 625 | + |
| 626 | + const createSentryMemoryRouter = wrapCreateMemoryRouterV6(createMemoryRouter); |
| 627 | + |
| 628 | + const router = createSentryMemoryRouter( |
| 629 | + [ |
| 630 | + { |
| 631 | + children: [ |
| 632 | + { path: '/users', element: <div>Users</div> }, |
| 633 | + { path: '/settings', element: <div>Settings</div> }, |
| 634 | + { path: '/profile', element: <div>Profile</div> }, |
| 635 | + ], |
| 636 | + }, |
| 637 | + ], |
| 638 | + { initialEntries: ['/users'] }, |
| 639 | + ); |
| 640 | + |
| 641 | + render( |
| 642 | + <React.StrictMode> |
| 643 | + <RouterProvider router={router} /> |
| 644 | + </React.StrictMode>, |
| 645 | + ); |
| 646 | + |
| 647 | + expect(mockStartBrowserTracingNavigationSpan).not.toHaveBeenCalled(); |
| 648 | + |
| 649 | + await act(async () => { |
| 650 | + router.navigate('/settings'); |
| 651 | + await waitFor(() => expect(mockStartBrowserTracingNavigationSpan).toHaveBeenCalledTimes(1)); |
| 652 | + }); |
| 653 | + |
| 654 | + expect(mockStartBrowserTracingNavigationSpan).toHaveBeenLastCalledWith(expect.any(BrowserClient), { |
| 655 | + name: '/settings', |
| 656 | + attributes: { |
| 657 | + [SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'route', |
| 658 | + [SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'navigation', |
| 659 | + [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.navigation.react.reactrouter_v6', |
| 660 | + }, |
| 661 | + }); |
| 662 | + |
| 663 | + await act(async () => { |
| 664 | + router.navigate('/profile'); |
| 665 | + await waitFor(() => expect(mockStartBrowserTracingNavigationSpan).toHaveBeenCalledTimes(2)); |
| 666 | + }); |
| 667 | + |
| 668 | + expect(mockStartBrowserTracingNavigationSpan).toHaveBeenCalledTimes(2); |
| 669 | + |
| 670 | + const calls = mockStartBrowserTracingNavigationSpan.mock.calls; |
| 671 | + expect(calls[0]![1].name).toBe('/settings'); |
| 672 | + expect(calls[1]![1].name).toBe('/profile'); |
| 673 | + expect(calls[0]![1].attributes[SEMANTIC_ATTRIBUTE_SENTRY_OP]).toBe('navigation'); |
| 674 | + expect(calls[1]![1].attributes[SEMANTIC_ATTRIBUTE_SENTRY_OP]).toBe('navigation'); |
| 675 | + }); |
| 676 | + |
| 677 | + it('should create separate transactions for rapid consecutive navigations', async () => { |
| 678 | + const client = createMockBrowserClient(); |
| 679 | + setCurrentClient(client); |
| 680 | + |
| 681 | + client.addIntegration( |
| 682 | + reactRouterV6BrowserTracingIntegration({ |
| 683 | + useEffect: React.useEffect, |
| 684 | + useLocation, |
| 685 | + useNavigationType, |
| 686 | + createRoutesFromChildren, |
| 687 | + matchRoutes, |
| 688 | + }), |
| 689 | + ); |
| 690 | + |
| 691 | + const createSentryMemoryRouter = wrapCreateMemoryRouterV6(createMemoryRouter); |
| 692 | + |
| 693 | + const router = createSentryMemoryRouter( |
| 694 | + [ |
| 695 | + { |
| 696 | + children: [ |
| 697 | + { path: '/a', element: <div>A</div> }, |
| 698 | + { path: '/b', element: <div>B</div> }, |
| 699 | + { path: '/c', element: <div>C</div> }, |
| 700 | + ], |
| 701 | + }, |
| 702 | + ], |
| 703 | + { initialEntries: ['/a'] }, |
| 704 | + ); |
| 705 | + |
| 706 | + render( |
| 707 | + <React.StrictMode> |
| 708 | + <RouterProvider router={router} /> |
| 709 | + </React.StrictMode>, |
| 710 | + ); |
| 711 | + |
| 712 | + await act(async () => { |
| 713 | + router.navigate('/b'); |
| 714 | + router.navigate('/c'); |
| 715 | + await waitFor(() => expect(mockStartBrowserTracingNavigationSpan).toHaveBeenCalledTimes(2)); |
| 716 | + }); |
| 717 | + |
| 718 | + expect(mockStartBrowserTracingNavigationSpan).toHaveBeenCalledTimes(2); |
| 719 | + |
| 720 | + const calls = mockStartBrowserTracingNavigationSpan.mock.calls; |
| 721 | + expect(calls[0]![1].name).toBe('/b'); |
| 722 | + expect(calls[1]![1].name).toBe('/c'); |
| 723 | + }); |
| 724 | + |
| 725 | + it('should NOT create duplicate spans for same route name (even with different params)', async () => { |
| 726 | + const client = createMockBrowserClient(); |
| 727 | + setCurrentClient(client); |
| 728 | + |
| 729 | + client.addIntegration( |
| 730 | + reactRouterV6BrowserTracingIntegration({ |
| 731 | + useEffect: React.useEffect, |
| 732 | + useLocation, |
| 733 | + useNavigationType, |
| 734 | + createRoutesFromChildren, |
| 735 | + matchRoutes, |
| 736 | + }), |
| 737 | + ); |
| 738 | + |
| 739 | + const createSentryMemoryRouter = wrapCreateMemoryRouterV6(createMemoryRouter); |
| 740 | + |
| 741 | + const router = createSentryMemoryRouter( |
| 742 | + [ |
| 743 | + { |
| 744 | + children: [{ path: '/user/:id', element: <div>User</div> }], |
| 745 | + }, |
| 746 | + ], |
| 747 | + { initialEntries: ['/user/1'] }, |
| 748 | + ); |
| 749 | + |
| 750 | + render( |
| 751 | + <React.StrictMode> |
| 752 | + <RouterProvider router={router} /> |
| 753 | + </React.StrictMode>, |
| 754 | + ); |
| 755 | + |
| 756 | + await act(async () => { |
| 757 | + router.navigate('/user/2'); |
| 758 | + await waitFor(() => expect(mockStartBrowserTracingNavigationSpan).toHaveBeenCalledTimes(1)); |
| 759 | + }); |
| 760 | + |
| 761 | + expect(mockStartBrowserTracingNavigationSpan).toHaveBeenCalledTimes(1); |
| 762 | + expect(mockStartBrowserTracingNavigationSpan).toHaveBeenCalledWith(expect.any(BrowserClient), { |
| 763 | + name: '/user/:id', |
| 764 | + attributes: { |
| 765 | + [SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'route', |
| 766 | + [SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'navigation', |
| 767 | + [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.navigation.react.reactrouter_v6', |
| 768 | + }, |
| 769 | + }); |
| 770 | + |
| 771 | + await act(async () => { |
| 772 | + router.navigate('/user/3'); |
| 773 | + await new Promise(resolve => setTimeout(resolve, 100)); |
| 774 | + }); |
| 775 | + |
| 776 | + expect(mockStartBrowserTracingNavigationSpan).toHaveBeenCalledTimes(1); |
| 777 | + }); |
| 778 | + |
| 779 | + it('should handle mixed cross-usage and consecutive navigations correctly', async () => { |
| 780 | + const client = createMockBrowserClient(); |
| 781 | + setCurrentClient(client); |
| 782 | + |
| 783 | + client.addIntegration( |
| 784 | + reactRouterV6BrowserTracingIntegration({ |
| 785 | + useEffect: React.useEffect, |
| 786 | + useLocation, |
| 787 | + useNavigationType, |
| 788 | + createRoutesFromChildren, |
| 789 | + matchRoutes, |
| 790 | + }), |
| 791 | + ); |
| 792 | + |
| 793 | + const createSentryMemoryRouter = wrapCreateMemoryRouterV6(createMemoryRouter); |
| 794 | + const sentryUseRoutes = wrapUseRoutesV6(useRoutes); |
| 795 | + |
| 796 | + const UsersRoute: React.FC = () => sentryUseRoutes([{ path: '/', element: <div>Users</div> }]); |
| 797 | + |
| 798 | + const SettingsRoute: React.FC = () => sentryUseRoutes([{ path: '/', element: <div>Settings</div> }]); |
| 799 | + |
| 800 | + const router = createSentryMemoryRouter( |
| 801 | + [ |
| 802 | + { |
| 803 | + children: [ |
| 804 | + { path: '/users/*', element: <UsersRoute /> }, |
| 805 | + { path: '/settings/*', element: <SettingsRoute /> }, |
| 806 | + ], |
| 807 | + }, |
| 808 | + ], |
| 809 | + { initialEntries: ['/users'] }, |
| 810 | + ); |
| 811 | + |
| 812 | + render( |
| 813 | + <React.StrictMode> |
| 814 | + <RouterProvider router={router} /> |
| 815 | + </React.StrictMode>, |
| 816 | + ); |
| 817 | + |
| 818 | + expect(mockStartBrowserTracingNavigationSpan).not.toHaveBeenCalled(); |
| 819 | + |
| 820 | + await act(async () => { |
| 821 | + router.navigate('/settings'); |
| 822 | + await waitFor(() => expect(mockStartBrowserTracingNavigationSpan).toHaveBeenCalledTimes(1)); |
| 823 | + }); |
| 824 | + |
| 825 | + expect(mockStartBrowserTracingNavigationSpan).toHaveBeenCalledTimes(1); |
| 826 | + expect(mockStartBrowserTracingNavigationSpan).toHaveBeenLastCalledWith(expect.any(BrowserClient), { |
| 827 | + name: '/settings/*', |
| 828 | + attributes: expect.objectContaining({ |
| 829 | + [SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'navigation', |
| 830 | + }), |
| 831 | + }); |
| 832 | + }); |
| 833 | + |
| 834 | + it('should not create duplicate spans for cross-usage on same route', async () => { |
| 835 | + const client = createMockBrowserClient(); |
| 836 | + setCurrentClient(client); |
| 837 | + |
| 838 | + client.addIntegration( |
| 839 | + reactRouterV6BrowserTracingIntegration({ |
| 840 | + useEffect: React.useEffect, |
| 841 | + useLocation, |
| 842 | + useNavigationType, |
| 843 | + createRoutesFromChildren, |
| 844 | + matchRoutes, |
| 845 | + }), |
| 846 | + ); |
| 847 | + |
| 848 | + const createSentryMemoryRouter = wrapCreateMemoryRouterV6(createMemoryRouter); |
| 849 | + const sentryUseRoutes = wrapUseRoutesV6(useRoutes); |
| 850 | + |
| 851 | + const NestedRoute: React.FC = () => sentryUseRoutes([{ path: '/', element: <div>Details</div> }]); |
| 852 | + |
| 853 | + const router = createSentryMemoryRouter( |
| 854 | + [ |
| 855 | + { |
| 856 | + children: [{ path: '/details/*', element: <NestedRoute /> }], |
| 857 | + }, |
| 858 | + ], |
| 859 | + { initialEntries: ['/home'] }, |
| 860 | + ); |
| 861 | + |
| 862 | + render( |
| 863 | + <React.StrictMode> |
| 864 | + <RouterProvider router={router} /> |
| 865 | + </React.StrictMode>, |
| 866 | + ); |
| 867 | + |
| 868 | + await act(async () => { |
| 869 | + router.navigate('/details'); |
| 870 | + await waitFor(() => expect(mockStartBrowserTracingNavigationSpan).toHaveBeenCalled()); |
| 871 | + }); |
| 872 | + |
| 873 | + expect(mockStartBrowserTracingNavigationSpan).toHaveBeenCalledTimes(1); |
| 874 | + expect(mockStartBrowserTracingNavigationSpan).toHaveBeenCalledWith(expect.any(BrowserClient), { |
| 875 | + name: '/details/*', |
| 876 | + attributes: expect.objectContaining({ |
| 877 | + [SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'navigation', |
| 878 | + }), |
| 879 | + }); |
| 880 | + }); |
| 881 | + }); |
610 | 882 | }); |
0 commit comments