Skip to content

ref(test): Remove location from context #72028

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions static/app/components/sidebar/index.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
import type {UseQueryResult} from '@tanstack/react-query';
import {BroadcastFixture} from 'sentry-fixture/broadcast';
import {LocationFixture} from 'sentry-fixture/locationFixture';
import {OrganizationFixture} from 'sentry-fixture/organization';
import {ServiceIncidentFixture} from 'sentry-fixture/serviceIncident';
import {UserFixture} from 'sentry-fixture/user';

import {initializeOrg} from 'sentry-test/initializeOrg';
import {act, render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';

import {OnboardingContextProvider} from 'sentry/components/onboarding/onboardingContext';
import SidebarContainer from 'sentry/components/sidebar';
import ConfigStore from 'sentry/stores/configStore';
import type {Organization, StatuspageIncident} from 'sentry/types';
import {useLocation} from 'sentry/utils/useLocation';
import * as incidentsHook from 'sentry/utils/useServiceIncidents';

jest.mock('sentry/utils/useServiceIncidents');
jest.mock('sentry/utils/useLocation');

const mockUseLocation = jest.mocked(useLocation);

const sidebarAccordionFeatures = [
'performance-view',
Expand All @@ -23,7 +27,7 @@ const sidebarAccordionFeatures = [
];

describe('Sidebar', function () {
const {organization, routerContext} = initializeOrg();
const organization = OrganizationFixture();
const broadcast = BroadcastFixture();
const user = UserFixture();
const apiMocks = {
Expand All @@ -39,9 +43,10 @@ describe('Sidebar', function () {
);

const renderSidebar = ({organization: org}: {organization: Organization | null}) =>
render(getElement(), {context: routerContext, organization: org});
render(getElement(), {organization: org});

beforeEach(function () {
mockUseLocation.mockReset();
jest.spyOn(incidentsHook, 'useServiceIncidents').mockImplementation(
() =>
({
Expand Down Expand Up @@ -171,11 +176,9 @@ describe('Sidebar', function () {
expect(await screen.findByRole('dialog')).toBeInTheDocument();
expect(screen.getByText("What's new in Sentry")).toBeInTheDocument();

const oldPath = routerContext.context.location.pathname;
routerContext.context.location.pathname = '/other/path';
mockUseLocation.mockReturnValue({...LocationFixture(), pathname: '/other/path'});
rerender(getElement());
expect(screen.queryByText("What's new in Sentry")).not.toBeInTheDocument();
routerContext.context.location.pathname = oldPath;
});

it('can have onboarding feature', async function () {
Expand Down
21 changes: 3 additions & 18 deletions static/app/views/performance/vitalDetail/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -279,12 +279,7 @@ describe('Performance > VitalDetail', function () {
},
};

const context = RouterContextFixture([
{
router: newRouter,
location: newRouter.location,
},
]);
const context = RouterContextFixture([{router: newRouter}]);

render(<TestComponent router={newRouter} />, {
context,
Expand Down Expand Up @@ -331,12 +326,7 @@ describe('Performance > VitalDetail', function () {
},
};

const context = RouterContextFixture([
{
router: newRouter,
location: newRouter.location,
},
]);
const context = RouterContextFixture([{router: newRouter}]);

render(<TestComponent router={newRouter} />, {
context,
Expand Down Expand Up @@ -384,12 +374,7 @@ describe('Performance > VitalDetail', function () {
},
};

const context = RouterContextFixture([
{
router: newRouter,
location: newRouter.location,
},
]);
const context = RouterContextFixture([{router: newRouter}]);

render(<TestComponent router={newRouter} />, {
context,
Expand Down
3 changes: 0 additions & 3 deletions tests/js/fixtures/routerContextFixture.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import {LocationFixture} from 'sentry-fixture/locationFixture';
import {RouterFixture} from 'sentry-fixture/routerFixture';

import {SentryPropTypeValidators} from 'sentry/sentryPropTypeValidators';

export function RouterContextFixture([context, childContextTypes] = []) {
return {
context: {
location: LocationFixture(),
router: RouterFixture(),
...context,
},
childContextTypes: {
router: SentryPropTypeValidators.isObject,
location: SentryPropTypeValidators.isObject,
...childContextTypes,
},
};
Expand Down
11 changes: 3 additions & 8 deletions tests/js/sentry-test/initializeOrg.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ interface InitializeOrgOptions<RouterParams> {
* - a project or projects
* - organization owning above projects
* - router
* - context that contains org + projects + router
* - context that contains router
*/
export function initializeOrg<RouterParams = {orgId: string; projectId: string}>({
organization: additionalOrg,
Expand Down Expand Up @@ -65,12 +65,7 @@ export function initializeOrg<RouterParams = {orgId: string; projectId: string}>
},
});

const routerContext: any = RouterContextFixture([
{
router,
location: router.location,
},
]);
const routerContext: any = RouterContextFixture([{router}]);

/**
* A collection of router props that are passed to components by react-router
Expand All @@ -86,7 +81,7 @@ export function initializeOrg<RouterParams = {orgId: string; projectId: string}>
router,
route: router.routes[0],
routes: router.routes,
location: routerContext.context.location,
location: router.location,
};

return {
Expand Down
Loading