Alerting: Fix being redirected to list view when clicking Save rule button (#75510)

* Fix: dont allow 'Save rule' button and let 'Save rule and exit' in case of new rule

* Fix tests
This commit is contained in:
Sonia Aguilar 2023-10-03 14:55:42 +02:00 committed by GitHub
parent e4c1a7a141
commit 1456b26075
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 18 additions and 15 deletions

View File

@ -152,7 +152,7 @@ describe('RuleEditor cloud', () => {
await user.type(getLabelInput(ui.inputs.labelValue(0).get()), 'warn{enter}');
// save and check what was sent to backend
await user.click(ui.buttons.save.get());
await user.click(ui.buttons.saveAndExit.get());
await waitFor(() => expect(mocks.api.setRulerRuleGroup).toHaveBeenCalled());
expect(mocks.api.setRulerRuleGroup).toHaveBeenCalledWith(
{ dataSourceName: 'Prom', apiVersion: 'config' },

View File

@ -145,7 +145,7 @@ describe('RuleEditor grafana managed rules', () => {
//8 segons
// save and check what was sent to backend
await userEvent.click(ui.buttons.save.get());
await userEvent.click(ui.buttons.saveAndExit.get());
// 9seg
await waitFor(() => expect(mocks.api.setRulerRuleGroup).toHaveBeenCalled());
// 9seg

View File

@ -1,4 +1,4 @@
import { waitFor, screen, within, waitForElementToBeRemoved } from '@testing-library/react';
import { screen, waitFor, waitForElementToBeRemoved, within } from '@testing-library/react';
import userEvent, { PointerEventsCheckLevel } from '@testing-library/user-event';
import React from 'react';
import { renderRuleEditor, ui } from 'test/helpers/alertingRuleEditor';
@ -151,7 +151,7 @@ describe('RuleEditor recording rules', () => {
await userEvent.type(getLabelInput(ui.inputs.labelValue(1).get()), 'the a-team{enter}');
// try to save, find out that recording rule name is invalid
await userEvent.click(ui.buttons.save.get());
await userEvent.click(ui.buttons.saveAndExit.get());
await waitFor(() =>
expect(
byText(
@ -166,7 +166,7 @@ describe('RuleEditor recording rules', () => {
await userEvent.type(await ui.inputs.name.find(), 'my:great:new:recording:rule');
// save and check what was sent to backend
await userEvent.click(ui.buttons.save.get());
await userEvent.click(ui.buttons.saveAndExit.get());
await waitFor(() => expect(mocks.api.setRulerRuleGroup).toHaveBeenCalled());
expect(mocks.api.setRulerRuleGroup).toHaveBeenCalledWith(
{ dataSourceName: 'Prom', apiVersion: 'legacy' },

View File

@ -166,16 +166,18 @@ export const AlertRuleForm = ({ existing, prefill }: Props) => {
const actionButtons = (
<HorizontalGroup height="auto" justify="flex-end">
<Button
variant="primary"
type="button"
size="sm"
onClick={handleSubmit((values) => submit(values, false), onInvalid)}
disabled={submitState.loading}
>
{submitState.loading && <Spinner className={styles.buttonSpinner} inline={true} />}
Save rule
</Button>
{existing && (
<Button
variant="primary"
type="button"
size="sm"
onClick={handleSubmit((values) => submit(values, false), onInvalid)}
disabled={submitState.loading}
>
{submitState.loading && <Spinner className={styles.buttonSpinner} inline={true} />}
Save rule
</Button>
)}
<Button
variant="primary"
type="button"

View File

@ -25,6 +25,7 @@ export const ui = {
expr: byTestId('expr'),
},
buttons: {
saveAndExit: byRole('button', { name: 'Save rule and exit' }),
save: byRole('button', { name: 'Save rule' }),
addAnnotation: byRole('button', { name: /Add info/ }),
addLabel: byRole('button', { name: /Add label/ }),