Browse Source

test(rr6): Remove a lot of browserHistory from tests (#82586)

Instead consistently test that `router.{push,replace}` is called. This
works even when the actual implementation is using browserHistory since
we assign the same mocks that the router functions use to the
browserHistory object.
Evan Purkhiser 2 months ago
parent
commit
48c36d9530

+ 3 - 8
static/app/utils/replays/hooks/useActiveReplayTab.spec.tsx

@@ -5,8 +5,6 @@ import {renderHook} from 'sentry-test/reactTestingLibrary';
 import {browserHistory} from 'sentry/utils/browserHistory';
 import useActiveReplayTab, {TabKey} from 'sentry/utils/replays/hooks/useActiveReplayTab';
 
-const mockPush = jest.mocked(browserHistory.push);
-
 function mockLocation(query: string = '') {
   window.location.search = qs.stringify({query});
 }
@@ -14,13 +12,10 @@ function mockLocation(query: string = '') {
 describe('useActiveReplayTab', () => {
   beforeEach(() => {
     mockLocation();
-    mockPush.mockReset();
   });
 
   it('should use Breadcrumbs as a default', () => {
-    const {result} = renderHook(useActiveReplayTab, {
-      initialProps: {},
-    });
+    const {result} = renderHook(useActiveReplayTab, {initialProps: {}});
 
     expect(result.current.getActiveTab()).toBe(TabKey.BREADCRUMBS);
   });
@@ -42,7 +37,7 @@ describe('useActiveReplayTab', () => {
     expect(result.current.getActiveTab()).toBe(TabKey.BREADCRUMBS);
 
     result.current.setActiveTab('nEtWoRk');
-    expect(mockPush).toHaveBeenLastCalledWith({
+    expect(browserHistory.push).toHaveBeenLastCalledWith({
       pathname: '/',
       state: undefined,
       query: {query: '', t_main: TabKey.NETWORK},
@@ -56,7 +51,7 @@ describe('useActiveReplayTab', () => {
     expect(result.current.getActiveTab()).toBe(TabKey.BREADCRUMBS);
 
     result.current.setActiveTab('foo bar');
-    expect(mockPush).toHaveBeenLastCalledWith({
+    expect(browserHistory.push).toHaveBeenLastCalledWith({
       pathname: '/',
       query: {query: '', t_main: TabKey.BREADCRUMBS},
     });

+ 8 - 5
static/app/views/acceptOrganizationInvite/index.spec.tsx

@@ -1,11 +1,11 @@
 import {OrganizationFixture} from 'sentry-fixture/organization';
 import {RouteComponentPropsFixture} from 'sentry-fixture/routeComponentPropsFixture';
+import {RouterFixture} from 'sentry-fixture/routerFixture';
 
 import {render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
 
 import {logout} from 'sentry/actionCreators/account';
 import ConfigStore from 'sentry/stores/configStore';
-import {browserHistory} from 'sentry/utils/browserHistory';
 import AcceptOrganizationInvite from 'sentry/views/acceptOrganizationInvite';
 
 jest.mock('sentry/actionCreators/account');
@@ -25,6 +25,7 @@ const getJoinButton = () => {
 };
 
 describe('AcceptOrganizationInvite', function () {
+  const router = RouterFixture();
   const organization = OrganizationFixture({slug: 'org-slug'});
   const configState = ConfigStore.getState();
 
@@ -46,7 +47,8 @@ describe('AcceptOrganizationInvite', function () {
       <AcceptOrganizationInvite
         {...RouteComponentPropsFixture()}
         params={{orgId: 'org-slug', memberId: '1', token: 'abc'}}
-      />
+      />,
+      {router}
     );
 
     const acceptMock = MockApiClient.addMockResponse({
@@ -58,7 +60,7 @@ describe('AcceptOrganizationInvite', function () {
 
     await userEvent.click(joinButton!);
     expect(acceptMock).toHaveBeenCalled();
-    expect(browserHistory.replace).toHaveBeenCalledWith('/org-slug/');
+    expect(router.replace).toHaveBeenCalledWith('/org-slug/');
   });
 
   it('can accept invitation on customer-domains', async function () {
@@ -85,7 +87,8 @@ describe('AcceptOrganizationInvite', function () {
       <AcceptOrganizationInvite
         {...RouteComponentPropsFixture()}
         params={{memberId: '1', token: 'abc'}}
-      />
+      />,
+      {router}
     );
 
     const acceptMock = MockApiClient.addMockResponse({
@@ -97,7 +100,7 @@ describe('AcceptOrganizationInvite', function () {
 
     await userEvent.click(joinButton!);
     expect(acceptMock).toHaveBeenCalled();
-    expect(browserHistory.replace).toHaveBeenCalledWith('/org-slug/');
+    expect(router.replace).toHaveBeenCalledWith('/org-slug/');
   });
 
   it('renders error message', function () {

+ 1 - 2
static/app/views/alerts/incidentRedirect.spec.tsx

@@ -4,7 +4,6 @@ import {initializeOrg} from 'sentry-test/initializeOrg';
 import {render, waitFor} from 'sentry-test/reactTestingLibrary';
 
 import {trackAnalytics} from 'sentry/utils/analytics';
-import {browserHistory} from 'sentry/utils/browserHistory';
 
 import IncidentRedirect from './incidentRedirect';
 
@@ -44,7 +43,7 @@ describe('IncidentRedirect', () => {
     );
 
     await waitFor(() => {
-      expect(browserHistory.replace).toHaveBeenCalledWith({
+      expect(router.replace).toHaveBeenCalledWith({
         pathname: '/organizations/org-slug/alerts/rules/details/4/',
         query: {
           alert: '123',

+ 1 - 2
static/app/views/alerts/rules/issue/details/ruleDetails.spec.tsx

@@ -9,7 +9,6 @@ import {initializeOrg} from 'sentry-test/initializeOrg';
 import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
 
 import ProjectsStore from 'sentry/stores/projectsStore';
-import {browserHistory} from 'sentry/utils/browserHistory';
 
 import AlertRuleDetails from './ruleDetails';
 
@@ -110,7 +109,7 @@ describe('AlertRuleDetails', () => {
     expect(await screen.findByLabelText('Next')).toBeEnabled();
     await userEvent.click(screen.getByLabelText('Next'));
 
-    expect(browserHistory.push).toHaveBeenCalledWith({
+    expect(context.router.push).toHaveBeenCalledWith({
       pathname: '/mock-pathname/',
       query: {
         cursor: '0:100:0',

+ 3 - 4
static/app/views/alerts/rules/issue/index.spec.tsx

@@ -27,7 +27,6 @@ import {updateOnboardingTask} from 'sentry/actionCreators/onboardingTasks';
 import ProjectsStore from 'sentry/stores/projectsStore';
 import type {PlainRoute} from 'sentry/types/legacyReactRouter';
 import {metric} from 'sentry/utils/analytics';
-import {browserHistory} from 'sentry/utils/browserHistory';
 import IssueRuleEditor from 'sentry/views/alerts/rules/issue';
 import {permissionAlertText} from 'sentry/views/settings/project/permissionAlert';
 import ProjectAlerts from 'sentry/views/settings/projectAlerts';
@@ -226,8 +225,8 @@ describe('IssueRuleEditor', function () {
         method: 'DELETE',
         body: {},
       });
-      createWrapper();
-      renderGlobalModal();
+      const {router} = createWrapper();
+      renderGlobalModal({router});
       await userEvent.click(screen.getByLabelText('Delete Rule'));
 
       expect(
@@ -236,7 +235,7 @@ describe('IssueRuleEditor', function () {
       await userEvent.click(screen.getByTestId('confirm-button'));
 
       await waitFor(() => expect(deleteMock).toHaveBeenCalled());
-      expect(browserHistory.replace).toHaveBeenCalledWith(
+      expect(router.replace).toHaveBeenCalledWith(
         '/settings/org-slug/projects/project-slug/alerts/'
       );
     });

+ 5 - 3
static/app/views/auth/loginForm.spec.tsx

@@ -1,7 +1,8 @@
+import {RouterFixture} from 'sentry-fixture/routerFixture';
+
 import {render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
 
 import ConfigStore from 'sentry/stores/configStore';
-import {browserHistory} from 'sentry/utils/browserHistory';
 import LoginForm from 'sentry/views/auth/loginForm';
 
 async function doLogin() {
@@ -38,6 +39,7 @@ describe('LoginForm', function () {
   });
 
   it('handles success', async function () {
+    const router = RouterFixture();
     const userObject = {
       id: 1,
       name: 'Joe',
@@ -53,7 +55,7 @@ describe('LoginForm', function () {
       },
     });
 
-    render(<LoginForm authConfig={emptyAuthConfig} />);
+    render(<LoginForm authConfig={emptyAuthConfig} />, {router});
     await doLogin();
 
     expect(mockRequest).toHaveBeenCalledWith(
@@ -64,7 +66,7 @@ describe('LoginForm', function () {
     );
 
     await waitFor(() => expect(ConfigStore.get('user')).toEqual(userObject));
-    expect(browserHistory.push).toHaveBeenCalledWith({pathname: '/next/'});
+    expect(router.push).toHaveBeenCalledWith({pathname: '/next/'});
   });
 
   it('renders login provider buttons', function () {

+ 5 - 3
static/app/views/auth/registerForm.spec.tsx

@@ -1,7 +1,8 @@
+import {RouterFixture} from 'sentry-fixture/routerFixture';
+
 import {render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
 
 import ConfigStore from 'sentry/stores/configStore';
-import {browserHistory} from 'sentry/utils/browserHistory';
 import RegisterForm from 'sentry/views/auth/registerForm';
 
 describe('Register', function () {
@@ -52,6 +53,7 @@ describe('Register', function () {
   });
 
   it('handles success', async function () {
+    const router = RouterFixture();
     const userObject = {
       id: 1,
       name: 'Joe',
@@ -67,10 +69,10 @@ describe('Register', function () {
       },
     });
 
-    render(<RegisterForm authConfig={emptyAuthConfig} />);
+    render(<RegisterForm authConfig={emptyAuthConfig} />, {router});
     await doLogin(mockRequest);
 
     await waitFor(() => expect(ConfigStore.get('user')).toEqual(userObject));
-    expect(browserHistory.push).toHaveBeenCalledWith({pathname: '/next/'});
+    expect(router.push).toHaveBeenCalledWith({pathname: '/next/'});
   });
 });

+ 5 - 5
static/app/views/auth/ssoForm.spec.tsx

@@ -1,6 +1,7 @@
+import {RouterFixture} from 'sentry-fixture/routerFixture';
+
 import {render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
 
-import {browserHistory} from 'sentry/utils/browserHistory';
 import SsoForm from 'sentry/views/auth/ssoForm';
 
 describe('SsoForm', function () {
@@ -54,6 +55,7 @@ describe('SsoForm', function () {
   });
 
   it('handles success', async function () {
+    const router = RouterFixture();
     const mockRequest = MockApiClient.addMockResponse({
       url: '/auth/sso-locate/',
       method: 'POST',
@@ -63,11 +65,9 @@ describe('SsoForm', function () {
       },
     });
 
-    render(<SsoForm authConfig={emptyAuthConfig} />);
+    render(<SsoForm authConfig={emptyAuthConfig} />, {router});
     await doSso(mockRequest);
 
-    await waitFor(() =>
-      expect(browserHistory.push).toHaveBeenCalledWith({pathname: '/next/'})
-    );
+    await waitFor(() => expect(router.push).toHaveBeenCalledWith({pathname: '/next/'}));
   });
 });

+ 4 - 5
static/app/views/dashboards/detail.spec.tsx

@@ -24,7 +24,6 @@ import ConfigStore from 'sentry/stores/configStore';
 import PageFiltersStore from 'sentry/stores/pageFiltersStore';
 import ProjectsStore from 'sentry/stores/projectsStore';
 import TeamStore from 'sentry/stores/teamStore';
-import {browserHistory} from 'sentry/utils/browserHistory';
 import CreateDashboard from 'sentry/views/dashboards/create';
 import DashboardDetail, {
   handleUpdateDashboardSplit,
@@ -1232,7 +1231,7 @@ describe('Dashboards > Detail', function () {
       await userEvent.click(document.body);
 
       await waitFor(() => {
-        expect(browserHistory.push).toHaveBeenCalledWith(
+        expect(testData.router.push).toHaveBeenCalledWith(
           expect.objectContaining({
             query: expect.objectContaining({
               release: '',
@@ -1340,7 +1339,7 @@ describe('Dashboards > Detail', function () {
       await userEvent.click(screen.getByText('Cancel'));
 
       screen.getByText('All Releases');
-      expect(browserHistory.replace).toHaveBeenCalledWith(
+      expect(testData.router.replace).toHaveBeenCalledWith(
         expect.objectContaining({
           query: expect.objectContaining({
             project: undefined,
@@ -1550,7 +1549,7 @@ describe('Dashboards > Detail', function () {
       await userEvent.click(screen.getByText('Cancel'));
 
       // release isn't used in the redirect
-      expect(browserHistory.replace).toHaveBeenCalledWith(
+      expect(testData.router.replace).toHaveBeenCalledWith(
         expect.objectContaining({
           query: {
             end: undefined,
@@ -1605,7 +1604,7 @@ describe('Dashboards > Detail', function () {
       await userEvent.click(document.body);
 
       await waitFor(() => {
-        expect(browserHistory.push).toHaveBeenCalledWith(
+        expect(testData.router.push).toHaveBeenCalledWith(
           expect.objectContaining({
             query: expect.objectContaining({
               release: ['sentry-android-shop@1.2.0'],

+ 5 - 6
static/app/views/dashboards/orgDashboards.spec.tsx

@@ -5,7 +5,6 @@ import {initializeOrg} from 'sentry-test/initializeOrg';
 import {render, screen, waitFor} from 'sentry-test/reactTestingLibrary';
 
 import ProjectsStore from 'sentry/stores/projectsStore';
-import {browserHistory} from 'sentry/utils/browserHistory';
 import DashboardDetail from 'sentry/views/dashboards/detail';
 import OrgDashboards from 'sentry/views/dashboards/orgDashboards';
 import {DashboardState} from 'sentry/views/dashboards/types';
@@ -97,7 +96,7 @@ describe('OrgDashboards', () => {
     );
 
     await waitFor(() =>
-      expect(browserHistory.replace).toHaveBeenCalledWith(
+      expect(initialData.router.replace).toHaveBeenCalledWith(
         expect.objectContaining({
           query: expect.objectContaining({
             project: [1, 2],
@@ -161,7 +160,7 @@ describe('OrgDashboards', () => {
     );
 
     await waitFor(() =>
-      expect(browserHistory.replace).toHaveBeenCalledWith(
+      expect(initialData.router.replace).toHaveBeenCalledWith(
         expect.objectContaining({
           query: expect.objectContaining({
             project: [1, 2],
@@ -260,7 +259,7 @@ describe('OrgDashboards', () => {
       {router: initialData.router}
     );
 
-    expect(browserHistory.replace).not.toHaveBeenCalled();
+    expect(initialData.router.replace).not.toHaveBeenCalled();
   });
 
   it('does not redirect to add query params if location is cleared manually', async () => {
@@ -305,7 +304,7 @@ describe('OrgDashboards', () => {
       {router: initialData.router}
     );
 
-    await waitFor(() => expect(browserHistory.replace).toHaveBeenCalledTimes(1));
+    await waitFor(() => expect(initialData.router.replace).toHaveBeenCalledTimes(1));
 
     rerender(
       <OrgDashboards
@@ -331,6 +330,6 @@ describe('OrgDashboards', () => {
     );
 
     expect(screen.queryByTestId('loading-indicator')).not.toBeInTheDocument();
-    expect(browserHistory.replace).toHaveBeenCalledTimes(1);
+    expect(initialData.router.replace).toHaveBeenCalledTimes(1);
   });
 });

Some files were not shown because too many files changed in this diff