From d13b607ec72af516172b08929beb07ade1b2deb8 Mon Sep 17 00:00:00 2001 From: schew2381 Date: Thu, 15 Aug 2024 01:52:38 -0700 Subject: [PATCH 1/2] test(alerts): Write test for deleting metric alert rule --- .../alerts/list/rules/alertRulesList.spec.tsx | 70 ++++++++++++++----- 1 file changed, 53 insertions(+), 17 deletions(-) diff --git a/static/app/views/alerts/list/rules/alertRulesList.spec.tsx b/static/app/views/alerts/list/rules/alertRulesList.spec.tsx index 598fe8e5986187..f022c88a60a3a1 100644 --- a/static/app/views/alerts/list/rules/alertRulesList.spec.tsx +++ b/static/app/views/alerts/list/rules/alertRulesList.spec.tsx @@ -179,35 +179,71 @@ describe('AlertRulesList', () => { expect(screen.getByText('Duplicate')).toBeInTheDocument(); }); - it('deletes a rule', async () => { - const {router, organization} = initializeOrg({ - organization: defaultOrg, + it('deletes an issue rule', async () => { + const deletedRuleName = 'Issue Rule'; + const issueRule = ProjectAlertRuleFixture({ + name: deletedRuleName, + projects: ['project-slug'], }); - const deletedRuleName = 'First Issue Alert'; + MockApiClient.addMockResponse({ url: '/organizations/org-slug/combined-rules/', headers: {Link: pageLinks}, - body: [ - { - ...ProjectAlertRuleFixture({ - id: '123', - name: deletedRuleName, - projects: ['earth'], - createdBy: {name: 'Samwise', id: 1, email: ''}, - }), - type: CombinedAlertType.ISSUE, - }, - ], + body: [{...issueRule, type: CombinedAlertType.ISSUE}], }); + + const {router, project, organization} = initializeOrg({organization: defaultOrg}); + render(, {router, organization}); + renderGlobalModal(); + const deleteMock = MockApiClient.addMockResponse({ - url: `/projects/${organization.slug}/earth/rules/123/`, + url: `/projects/${organization.slug}/${project.slug}/rules/${issueRule.id}/`, method: 'DELETE', body: {}, }); + const actions = (await screen.findAllByRole('button', {name: 'Actions'}))[0]; + + // Add a new response to the mock with no rules + const emptyListMock = MockApiClient.addMockResponse({ + url: '/organizations/org-slug/combined-rules/', + headers: {Link: pageLinks}, + body: [], + }); + + expect(screen.queryByText(deletedRuleName)).toBeInTheDocument(); + await userEvent.click(actions); + await userEvent.click(screen.getByRole('menuitemradio', {name: 'Delete'})); + await userEvent.click(screen.getByRole('button', {name: 'Delete Rule'})); + + expect(deleteMock).toHaveBeenCalledTimes(1); + expect(emptyListMock).toHaveBeenCalledTimes(1); + expect(screen.queryByText(deletedRuleName)).not.toBeInTheDocument(); + }); + + it('deletes a metric rule', async () => { + const deletedRuleName = 'Metric Rule'; + const metricRule = MetricRuleFixture({ + name: deletedRuleName, + query: 'is:unresolved', + }); + + MockApiClient.addMockResponse({ + url: '/organizations/org-slug/combined-rules/', + headers: {Link: pageLinks}, + body: [{...metricRule, type: CombinedAlertType.METRIC}], + }); + + const {router, organization} = initializeOrg({organization: defaultOrg}); render(, {router, organization}); renderGlobalModal(); + const deleteMock = MockApiClient.addMockResponse({ + url: `/organizations/${organization.slug}/alert-rules/${metricRule.id}/`, + method: 'DELETE', + body: {}, + }); + const actions = (await screen.findAllByRole('button', {name: 'Actions'}))[0]; // Add a new response to the mock with no rules @@ -219,7 +255,7 @@ describe('AlertRulesList', () => { expect(screen.queryByText(deletedRuleName)).toBeInTheDocument(); await userEvent.click(actions); - await userEvent.click(screen.getByText('Delete')); + await userEvent.click(screen.getByRole('menuitemradio', {name: 'Delete'})); await userEvent.click(screen.getByRole('button', {name: 'Delete Rule'})); expect(deleteMock).toHaveBeenCalledTimes(1); From 7064d7639a2905b719b470cbe5e06b31df1a6a05 Mon Sep 17 00:00:00 2001 From: schew2381 Date: Thu, 15 Aug 2024 01:56:03 -0700 Subject: [PATCH 2/2] remove fluff --- static/app/views/alerts/list/rules/alertRulesList.spec.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/static/app/views/alerts/list/rules/alertRulesList.spec.tsx b/static/app/views/alerts/list/rules/alertRulesList.spec.tsx index f022c88a60a3a1..07e2205885ed91 100644 --- a/static/app/views/alerts/list/rules/alertRulesList.spec.tsx +++ b/static/app/views/alerts/list/rules/alertRulesList.spec.tsx @@ -225,7 +225,6 @@ describe('AlertRulesList', () => { const deletedRuleName = 'Metric Rule'; const metricRule = MetricRuleFixture({ name: deletedRuleName, - query: 'is:unresolved', }); MockApiClient.addMockResponse({