Skip to content

Commit ae7d27f

Browse files
authored
test(ui): Migrate page filter tests off deprecatedRouterMocks (#105516)
Migrate to in-memory test router. Required a few local mock routers still
1 parent 2077256 commit ae7d27f

File tree

4 files changed

+376
-451
lines changed

4 files changed

+376
-451
lines changed

static/app/components/organizations/datePageFilter.spec.tsx

Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,16 @@
1-
import {initializeOrg} from 'sentry-test/initializeOrg';
1+
import {OrganizationFixture} from 'sentry-fixture/organization';
2+
import {RouterFixture} from 'sentry-fixture/routerFixture';
3+
24
import {fireEvent, render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
35

46
import {initializeUrlState} from 'sentry/actionCreators/pageFilters';
57
import {DatePageFilter} from 'sentry/components/organizations/datePageFilter';
68
import OrganizationStore from 'sentry/stores/organizationStore';
79
import PageFiltersStore from 'sentry/stores/pageFiltersStore';
810

9-
const {organization, router} = initializeOrg({
10-
router: {
11-
location: {
12-
query: {},
13-
pathname: '/test',
14-
},
15-
params: {},
16-
},
17-
});
18-
1911
describe('DatePageFilter', () => {
12+
const organization = OrganizationFixture();
13+
2014
beforeEach(() => {
2115
PageFiltersStore.init();
2216
OrganizationStore.init();
@@ -35,10 +29,11 @@ describe('DatePageFilter', () => {
3529
});
3630

3731
it('can change period', async () => {
38-
render(<DatePageFilter />, {
39-
router,
32+
const {router} = render(<DatePageFilter />, {
4033
organization,
41-
deprecatedRouterMocks: true,
34+
initialRouterConfig: {
35+
location: {pathname: '/test', query: {}},
36+
},
4237
});
4338

4439
// Open time period dropdown
@@ -51,9 +46,7 @@ describe('DatePageFilter', () => {
5146
expect(
5247
screen.getByRole('button', {name: '30D', expanded: false})
5348
).toBeInTheDocument();
54-
expect(router.push).toHaveBeenCalledWith(
55-
expect.objectContaining({query: {statsPeriod: '30d'}})
56-
);
49+
expect(router.location.query).toEqual(expect.objectContaining({statsPeriod: '30d'}));
5750
expect(PageFiltersStore.getState()).toEqual({
5851
isReady: true,
5952
shouldPersist: true,
@@ -73,10 +66,11 @@ describe('DatePageFilter', () => {
7366
});
7467

7568
it('can change absolute range', async () => {
76-
render(<DatePageFilter />, {
77-
router,
69+
const {router} = render(<DatePageFilter />, {
7870
organization,
79-
deprecatedRouterMocks: true,
71+
initialRouterConfig: {
72+
location: {pathname: '/test', query: {}},
73+
},
8074
});
8175

8276
// Open time period dropdown
@@ -96,9 +90,10 @@ describe('DatePageFilter', () => {
9690
expect(
9791
await screen.findByRole('button', {name: 'Oct 3 – Oct 4', expanded: false})
9892
).toBeInTheDocument();
99-
expect(router.push).toHaveBeenCalledWith(
93+
expect(router.location.query).toEqual(
10094
expect.objectContaining({
101-
query: {start: '2017-10-03T00:00:00', end: '2017-10-04T23:59:59'},
95+
start: '2017-10-03T00:00:00',
96+
end: '2017-10-04T23:59:59',
10297
})
10398
);
10499
expect(PageFiltersStore.getState()).toEqual({
@@ -129,30 +124,26 @@ describe('DatePageFilter', () => {
129124
});
130125

131126
it('displays a desynced state message', async () => {
132-
const {organization: desyncOrganization, router: desyncRouter} = initializeOrg({
133-
router: {
134-
location: {
135-
// the datetime parameters need to be non-null for desync detection to work
136-
query: {statsPeriod: '7d'},
137-
pathname: '/test',
138-
},
139-
params: {},
140-
},
141-
});
127+
const desyncOrganization = OrganizationFixture();
128+
// the datetime parameters need to be non-null for desync detection to work
129+
const desyncLocation = {pathname: '/test', query: {statsPeriod: '7d'}};
142130

143131
PageFiltersStore.reset();
144132
initializeUrlState({
145133
memberProjects: [],
146134
nonMemberProjects: [],
147135
organization: desyncOrganization,
148136
queryParams: {statsPeriod: '14d'},
149-
router: desyncRouter,
137+
router: RouterFixture({
138+
location: desyncLocation,
139+
}),
150140
});
151141

152142
render(<DatePageFilter />, {
153-
router: desyncRouter,
154143
organization: desyncOrganization,
155-
deprecatedRouterMocks: true,
144+
initialRouterConfig: {
145+
location: desyncLocation,
146+
},
156147
});
157148

158149
// Open menu

static/app/components/organizations/environmentPageFilter/index.spec.tsx

Lines changed: 41 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import {initializeOrg} from 'sentry-test/initializeOrg';
1+
import {OrganizationFixture} from 'sentry-fixture/organization';
2+
import {ProjectFixture} from 'sentry-fixture/project';
3+
import {RouterFixture} from 'sentry-fixture/routerFixture';
4+
25
import {act, fireEvent, render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
36

47
import {initializeUrlState, updateEnvironments} from 'sentry/actionCreators/pageFilters';
@@ -7,20 +10,11 @@ import OrganizationStore from 'sentry/stores/organizationStore';
710
import PageFiltersStore from 'sentry/stores/pageFiltersStore';
811
import ProjectsStore from 'sentry/stores/projectsStore';
912

10-
const {organization, projects, router} = initializeOrg({
11-
organization: {features: ['open-membership']},
12-
projects: [
13-
{id: '1', slug: 'project-1', environments: ['prod', 'staging']},
14-
{id: '2', slug: 'project-2', environments: ['prod', 'stage']},
15-
],
16-
router: {
17-
location: {
18-
pathname: '/organizations/org-slug/issues/',
19-
query: {},
20-
},
21-
params: {},
22-
},
23-
});
13+
const organization = OrganizationFixture({features: ['open-membership']});
14+
const projects = [
15+
ProjectFixture({id: '1', slug: 'project-1', environments: ['prod', 'staging']}),
16+
ProjectFixture({id: '2', slug: 'project-2', environments: ['prod', 'stage']}),
17+
];
2418

2519
describe('EnvironmentPageFilter', () => {
2620
beforeEach(() => {
@@ -40,10 +34,11 @@ describe('EnvironmentPageFilter', () => {
4034
afterEach(() => PageFiltersStore.reset());
4135

4236
it('renders & handles single selection', async () => {
43-
render(<EnvironmentPageFilter />, {
44-
router,
37+
const {router} = render(<EnvironmentPageFilter />, {
4538
organization,
46-
deprecatedRouterMocks: true,
39+
initialRouterConfig: {
40+
location: {pathname: '/organizations/org-slug/issues/', query: {}},
41+
},
4742
});
4843

4944
// Open menu
@@ -59,16 +54,15 @@ describe('EnvironmentPageFilter', () => {
5954

6055
// Trigger label & router is updated
6156
expect(screen.getByRole('button', {name: 'prod'})).toBeInTheDocument();
62-
expect(router.push).toHaveBeenCalledWith(
63-
expect.objectContaining({query: {environment: ['prod']}})
64-
);
57+
expect(router.location.query).toEqual(expect.objectContaining({environment: 'prod'}));
6558
});
6659

6760
it('handles multiple selection', async () => {
68-
render(<EnvironmentPageFilter />, {
69-
router,
61+
const {router} = render(<EnvironmentPageFilter />, {
7062
organization,
71-
deprecatedRouterMocks: true,
63+
initialRouterConfig: {
64+
location: {pathname: '/organizations/org-slug/issues/', query: {}},
65+
},
7266
});
7367

7468
// Open menu
@@ -83,27 +77,24 @@ describe('EnvironmentPageFilter', () => {
8377

8478
// Trigger button & router are updated
8579
expect(screen.getByRole('button', {name: 'prod, stage'})).toBeInTheDocument();
86-
expect(router.push).toHaveBeenCalledWith(
87-
expect.objectContaining({query: {environment: ['prod', 'stage']}})
80+
expect(router.location.query).toEqual(
81+
expect.objectContaining({environment: ['prod', 'stage']})
8882
);
8983
});
9084

9185
it('handles reset', async () => {
9286
const onReset = jest.fn();
93-
render(<EnvironmentPageFilter onReset={onReset} />, {
94-
router,
87+
const {router} = render(<EnvironmentPageFilter onReset={onReset} />, {
9588
organization,
96-
deprecatedRouterMocks: true,
89+
initialRouterConfig: {
90+
location: {pathname: '/organizations/org-slug/issues/', query: {}},
91+
},
9792
});
9893

9994
// Open the menu, select project-1
10095
await userEvent.click(screen.getByRole('button', {name: 'All Envs'}));
10196
await userEvent.click(screen.getByRole('row', {name: 'prod'}));
102-
expect(router.push).toHaveBeenCalledWith(
103-
expect.objectContaining({
104-
query: {environment: ['prod']},
105-
})
106-
);
97+
expect(router.location.query).toEqual(expect.objectContaining({environment: 'prod'}));
10798

10899
// Open menu again & click "Reset"
109100
await userEvent.click(screen.getByRole('button', {name: 'prod'}));
@@ -115,52 +106,47 @@ describe('EnvironmentPageFilter', () => {
115106
});
116107

117108
it('responds to page filter changes, async e.g. from back button nav', async () => {
109+
const mockRouter = RouterFixture({
110+
location: {pathname: '/organizations/org-slug/issues/', query: {}},
111+
});
112+
118113
render(<EnvironmentPageFilter />, {
119-
router,
120114
organization,
121-
deprecatedRouterMocks: true,
115+
initialRouterConfig: {
116+
location: {pathname: '/organizations/org-slug/issues/', query: {}},
117+
},
122118
});
123119

124120
// Confirm initial selection
125121
expect(await screen.findByRole('button', {name: 'All Envs'})).toBeInTheDocument();
126122

127123
// Edit store value
128-
act(() => updateEnvironments(['prod'], router));
124+
act(() => updateEnvironments(['prod'], mockRouter));
129125

130126
// <EnvironmentPageFilter /> is updated
131127
expect(screen.getByRole('button', {name: 'prod'})).toBeInTheDocument();
132128
});
133129

134130
it('displays a desynced state message', async () => {
135-
const {organization: desyncOrganization, router: desyncRouter} = initializeOrg({
136-
organization: {features: ['open-membership']},
137-
projects: [
138-
{id: '1', slug: 'project-1', environments: ['prod', 'staging']},
139-
{id: '2', slug: 'project-2', environments: ['prod', 'stage']},
140-
],
141-
router: {
142-
location: {
143-
pathname: '/organizations/org-slug/issues/',
144-
// the environment parameter needs to be non-null for desync detection to work
145-
query: {environment: 'prod'},
146-
},
147-
params: {},
148-
},
149-
});
131+
const desyncOrganization = OrganizationFixture({features: ['open-membership']});
132+
// the environment parameter needs to be non-null for desync detection to work
133+
const desyncLocation = {
134+
pathname: '/organizations/org-slug/issues/',
135+
query: {environment: 'prod'},
136+
};
150137

151138
PageFiltersStore.reset();
152139
initializeUrlState({
153140
memberProjects: projects,
154141
nonMemberProjects: [],
155142
organization: desyncOrganization,
156143
queryParams: {project: ['1'], environment: 'staging'},
157-
router: desyncRouter,
144+
router: RouterFixture({location: desyncLocation}),
158145
});
159146

160147
render(<EnvironmentPageFilter />, {
161-
router: desyncRouter,
162148
organization: desyncOrganization,
163-
deprecatedRouterMocks: true,
149+
initialRouterConfig: {location: desyncLocation},
164150
});
165151

166152
// Open menu

0 commit comments

Comments
 (0)