Skip to content

Commit 2ab1195

Browse files
authored
ref(tests) Replace usage of predicate in MockApiClient (#29997)
Replace predicate callables with the new match behavior. This helps reduce bloat in tests and makes tests more readable. Make request matching more efficient. Don't do comparisons we don't need to as we know the match won't work.
1 parent 4130174 commit 2ab1195

File tree

19 files changed

+677
-870
lines changed

19 files changed

+677
-870
lines changed

static/app/__mocks__/api.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,13 @@ class Client implements ApiNamespace.Client {
140140

141141
static findMockResponse(url: string, options: Readonly<ApiNamespace.RequestOptions>) {
142142
return Client.mockResponses.find(([response]) => {
143-
const matchesURL = url === response.url;
144-
const matchesMethod = (options.method || 'GET') === response.method;
145-
const matchersMatch = response.match.every(matcher => matcher(url, options));
146-
147-
return matchesURL && matchesMethod && matchersMatch;
143+
if (url !== response.url) {
144+
return false;
145+
}
146+
if ((options.method || 'GET') !== response.method) {
147+
return false;
148+
}
149+
return response.match.every(matcher => matcher(url, options));
148150
});
149151
}
150152

tests/js/spec/components/discover/transactionsList.spec.jsx

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -72,36 +72,28 @@ describe('TransactionsList', function () {
7272
}),
7373
};
7474

75-
MockApiClient.addMockResponse(
76-
{
77-
url: `/organizations/${organization.slug}/eventsv2/`,
78-
body: {
79-
meta: {transaction: 'string', count: 'number'},
80-
data: [
81-
{transaction: '/a', count: 100},
82-
{transaction: '/b', count: 1000},
83-
],
84-
},
75+
MockApiClient.addMockResponse({
76+
url: `/organizations/${organization.slug}/eventsv2/`,
77+
body: {
78+
meta: {transaction: 'string', count: 'number'},
79+
data: [
80+
{transaction: '/a', count: 100},
81+
{transaction: '/b', count: 1000},
82+
],
8583
},
86-
{
87-
predicate: (_, opts) => opts?.query?.sort === 'transaction',
88-
}
89-
);
90-
MockApiClient.addMockResponse(
91-
{
92-
url: `/organizations/${organization.slug}/eventsv2/`,
93-
body: {
94-
meta: {transaction: 'string', count: 'number'},
95-
data: [
96-
{transaction: '/b', count: 1000},
97-
{transaction: '/a', count: 100},
98-
],
99-
},
84+
match: [MockApiClient.matchQuery({sort: 'transaction'})],
85+
});
86+
MockApiClient.addMockResponse({
87+
url: `/organizations/${organization.slug}/eventsv2/`,
88+
body: {
89+
meta: {transaction: 'string', count: 'number'},
90+
data: [
91+
{transaction: '/b', count: 1000},
92+
{transaction: '/a', count: 100},
93+
],
10094
},
101-
{
102-
predicate: (_, opts) => opts?.query?.sort === '-count',
103-
}
104-
);
95+
match: [MockApiClient.matchQuery({sort: '-count'})],
96+
});
10597
MockApiClient.addMockResponse({
10698
url: `/organizations/${organization.slug}/events-trends/`,
10799
body: {

tests/js/spec/components/group/externalIssueForm.spec.jsx

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,11 @@ describe('ExternalIssueForm', () => {
3535
});
3636

3737
const generateWrapper = (action = 'create') => {
38-
MockApiClient.addMockResponse(
39-
{
40-
url: `/groups/${group.id}/integrations/${integration.id}/`,
41-
body: formConfig,
42-
},
43-
{
44-
predicate: (_, options) => options?.query?.action === 'create',
45-
}
46-
);
38+
MockApiClient.addMockResponse({
39+
url: `/groups/${group.id}/integrations/${integration.id}/`,
40+
body: formConfig,
41+
match: [MockApiClient.matchQuery({action: 'create'})],
42+
});
4743
const component = mountWithTheme(
4844
<ExternalIssueForm
4945
Body={p => p.children}
@@ -134,15 +130,11 @@ describe('ExternalIssueForm', () => {
134130
},
135131
id: '5',
136132
};
137-
getFormConfigRequest = MockApiClient.addMockResponse(
138-
{
139-
url: `/groups/${group.id}/integrations/${integration.id}/`,
140-
body: formConfig,
141-
},
142-
{
143-
predicate: (_, options) => options?.query?.action === 'link',
144-
}
145-
);
133+
getFormConfigRequest = MockApiClient.addMockResponse({
134+
url: `/groups/${group.id}/integrations/${integration.id}/`,
135+
body: formConfig,
136+
match: [MockApiClient.matchQuery({action: 'link'})],
137+
});
146138
});
147139
it('renders', () => {
148140
wrapper = generateWrapper('link');

tests/js/spec/components/group/sentryAppExternalIssueForm.spec.jsx

Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -233,44 +233,34 @@ describe('SentryAppExternalIssueForm Dependent fields', () => {
233233
describe('create', () => {
234234
it('load options for field that has dependencies when the dependent option is selected', async () => {
235235
const url = `/sentry-app-installations/${sentryAppInstallation.uuid}/external-requests/`;
236-
Client.addMockResponse(
237-
{
238-
method: 'GET',
239-
url,
240-
body: {
241-
choices: [
242-
['A', 'project A'],
243-
['B', 'project B'],
244-
],
245-
},
236+
Client.addMockResponse({
237+
method: 'GET',
238+
url,
239+
body: {
240+
choices: [
241+
['A', 'project A'],
242+
['B', 'project B'],
243+
],
246244
},
247-
{
248-
predicate: (_url, options) => {
249-
return options.query.uri === '/integrations/sentry/projects';
250-
},
251-
}
252-
);
245+
match: [MockApiClient.matchQuery({uri: '/integrations/sentry/projects'})],
246+
});
253247

254-
const boardMock = Client.addMockResponse(
255-
{
256-
method: 'GET',
257-
url,
258-
body: {
259-
choices: [
260-
['R', 'board R'],
261-
['S', 'board S'],
262-
],
263-
},
248+
const boardMock = Client.addMockResponse({
249+
method: 'GET',
250+
url,
251+
body: {
252+
choices: [
253+
['R', 'board R'],
254+
['S', 'board S'],
255+
],
264256
},
265-
{
266-
predicate: (_url, {query}) => {
267-
return (
268-
query.uri === '/integrations/sentry/boards' &&
269-
query.dependentData === JSON.stringify({project_id: 'A'})
270-
);
271-
},
272-
}
273-
);
257+
match: [
258+
MockApiClient.matchQuery({
259+
uri: '/integrations/sentry/boards',
260+
dependentData: JSON.stringify({project_id: 'A'}),
261+
}),
262+
],
263+
});
274264

275265
const projectInput = wrapper.find('[data-test-id="project_id"] input').at(0);
276266
changeInputValue(projectInput, 'p');

tests/js/spec/utils/discover/teamKeyTransactionField.spec.jsx

Lines changed: 42 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -188,22 +188,15 @@ describe('TeamKeyTransactionField', function () {
188188
})),
189189
});
190190

191-
const postTeamKeyTransactionsMock = MockApiClient.addMockResponse(
192-
{
193-
method: 'POST',
194-
url: '/organizations/org-slug/key-transactions/',
195-
body: [],
196-
},
197-
{
198-
predicate: (_, options) =>
199-
options.method === 'POST' &&
200-
options.query.project.length === 1 &&
201-
options.query.project[0] === project.id &&
202-
options.data.team.length === 1 &&
203-
options.data.team[0] === teams[0].id &&
204-
options.data.transaction === 'transaction',
205-
}
206-
);
191+
const postTeamKeyTransactionsMock = MockApiClient.addMockResponse({
192+
method: 'POST',
193+
url: '/organizations/org-slug/key-transactions/',
194+
body: [],
195+
match: [
196+
MockApiClient.matchQuery({project: [project.id]}),
197+
MockApiClient.matchData({team: [teams[0].id], transaction: 'transaction'}),
198+
],
199+
});
207200

208201
const wrapper = mountWithTheme(
209202
<TeamKeyTransactionManager.Provider
@@ -249,22 +242,15 @@ describe('TeamKeyTransactionField', function () {
249242
})),
250243
});
251244

252-
const deleteTeamKeyTransactionsMock = MockApiClient.addMockResponse(
253-
{
254-
method: 'DELETE',
255-
url: '/organizations/org-slug/key-transactions/',
256-
body: [],
257-
},
258-
{
259-
predicate: (_, options) =>
260-
options.method === 'DELETE' &&
261-
options.query.project.length === 1 &&
262-
options.query.project[0] === project.id &&
263-
options.data.team.length === 1 &&
264-
options.data.team[0] === teams[0].id &&
265-
options.data.transaction === 'transaction',
266-
}
267-
);
245+
const deleteTeamKeyTransactionsMock = MockApiClient.addMockResponse({
246+
method: 'DELETE',
247+
url: '/organizations/org-slug/key-transactions/',
248+
body: [],
249+
match: [
250+
MockApiClient.matchQuery({project: [project.id]}),
251+
MockApiClient.matchData({team: [teams[0].id], transaction: 'transaction'}),
252+
],
253+
});
268254

269255
const wrapper = mountWithTheme(
270256
<TeamKeyTransactionManager.Provider
@@ -310,23 +296,18 @@ describe('TeamKeyTransactionField', function () {
310296
})),
311297
});
312298

313-
const postTeamKeyTransactionsMock = MockApiClient.addMockResponse(
314-
{
315-
method: 'POST',
316-
url: '/organizations/org-slug/key-transactions/',
317-
body: [],
318-
},
319-
{
320-
predicate: (_, options) =>
321-
options.method === 'POST' &&
322-
options.query.project.length === 1 &&
323-
options.query.project[0] === project.id &&
324-
options.data.team.length === 2 &&
325-
options.data.team[0] === teams[0].id &&
326-
options.data.team[1] === teams[1].id &&
327-
options.data.transaction === 'transaction',
328-
}
329-
);
299+
const postTeamKeyTransactionsMock = MockApiClient.addMockResponse({
300+
method: 'POST',
301+
url: '/organizations/org-slug/key-transactions/',
302+
body: [],
303+
match: [
304+
MockApiClient.matchQuery({project: [project.id]}),
305+
MockApiClient.matchData({
306+
team: [teams[0].id, teams[1].id],
307+
transaction: 'transaction',
308+
}),
309+
],
310+
});
330311

331312
const wrapper = mountWithTheme(
332313
<TeamKeyTransactionManager.Provider
@@ -369,23 +350,18 @@ describe('TeamKeyTransactionField', function () {
369350
})),
370351
});
371352

372-
const deleteTeamKeyTransactionsMock = MockApiClient.addMockResponse(
373-
{
374-
method: 'DELETE',
375-
url: '/organizations/org-slug/key-transactions/',
376-
body: [],
377-
},
378-
{
379-
predicate: (_, options) =>
380-
options.method === 'DELETE' &&
381-
options.query.project.length === 1 &&
382-
options.query.project[0] === project.id &&
383-
options.data.team.length === 2 &&
384-
options.data.team[0] === teams[0].id &&
385-
options.data.team[1] === teams[1].id &&
386-
options.data.transaction === 'transaction',
387-
}
388-
);
353+
const deleteTeamKeyTransactionsMock = MockApiClient.addMockResponse({
354+
method: 'DELETE',
355+
url: '/organizations/org-slug/key-transactions/',
356+
body: [],
357+
match: [
358+
MockApiClient.matchQuery({project: [project.id]}),
359+
MockApiClient.matchData({
360+
team: [teams[0].id, teams[1].id],
361+
transaction: 'transaction',
362+
}),
363+
],
364+
});
389365

390366
const wrapper = mountWithTheme(
391367
<TeamKeyTransactionManager.Provider

tests/js/spec/utils/performance/quickTrace/quickTraceQuery.spec.jsx

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,11 @@ describe('TraceLiteQuery', function () {
4444
},
4545
type: 'transaction',
4646
};
47-
traceLiteMock = MockApiClient.addMockResponse(
48-
{
49-
url: `/organizations/test-org/events-trace-light/${traceId}/`,
50-
body: [],
51-
},
52-
{
53-
predicate: (_, {query}) => query.event_id === eventId,
54-
}
55-
);
47+
traceLiteMock = MockApiClient.addMockResponse({
48+
url: `/organizations/test-org/events-trace-light/${traceId}/`,
49+
body: [],
50+
match: [MockApiClient.matchQuery({event_id: eventId})],
51+
});
5652
traceFullMock = MockApiClient.addMockResponse({
5753
url: `/organizations/test-org/events-trace/${traceId}/`,
5854
body: [],
@@ -127,15 +123,11 @@ describe('TraceLiteQuery', function () {
127123
});
128124

129125
it('uses full results when it finds current event', async function () {
130-
traceLiteMock = MockApiClient.addMockResponse(
131-
{
132-
url: `/organizations/test-org/events-trace-light/0${traceId}/`,
133-
body: [],
134-
},
135-
{
136-
predicate: (_, {query}) => query.event_id === eventId,
137-
}
138-
);
126+
traceLiteMock = MockApiClient.addMockResponse({
127+
url: `/organizations/test-org/events-trace-light/0${traceId}/`,
128+
body: [],
129+
match: [MockApiClient.matchQuery({event_id: eventId})],
130+
});
139131
traceFullMock = MockApiClient.addMockResponse({
140132
url: `/organizations/test-org/events-trace/0${traceId}/`,
141133
body: [{event_id: eventId, children: []}],

tests/js/spec/utils/performance/quickTrace/traceFullQuery.spec.jsx

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,11 @@ describe('TraceFullQuery', function () {
6262
});
6363

6464
it('fetches data on mount with detailed param', async function () {
65-
const getMock = MockApiClient.addMockResponse(
66-
{
67-
url: `/organizations/test-org/events-trace/${traceId}/`,
68-
body: [],
69-
},
70-
{
71-
predicate: (_, {query}) => query.detailed === '1',
72-
}
73-
);
65+
const getMock = MockApiClient.addMockResponse({
66+
url: `/organizations/test-org/events-trace/${traceId}/`,
67+
body: [],
68+
match: [MockApiClient.matchQuery({detailed: '1'})],
69+
});
7470
const wrapper = mountWithTheme(
7571
<TraceFullDetailedQuery
7672
api={api}

0 commit comments

Comments
 (0)