From 6152067ce1684002542bf32dbe75c82cd163d6bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Mierzwa?= Date: Sun, 14 Jul 2019 21:49:51 +0100 Subject: [PATCH] fix(ui): don't crash when removing last Alertmanager instance Removing last value sets the value that's expected to be a list to null, which breaks some logic, ensure we always have a list there. Fixes #826 --- .../SilenceModal/AlertManagerInput/index.js | 2 +- .../SilenceModal/AlertManagerInput/index.test.js | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/ui/src/Components/SilenceModal/AlertManagerInput/index.js b/ui/src/Components/SilenceModal/AlertManagerInput/index.js index c90a6cb6a..f3ead661e 100644 --- a/ui/src/Components/SilenceModal/AlertManagerInput/index.js +++ b/ui/src/Components/SilenceModal/AlertManagerInput/index.js @@ -36,7 +36,7 @@ const AlertManagerInput = observer( onChange = action((newValue, actionMeta) => { const { silenceFormStore } = this.props; - silenceFormStore.data.alertmanagers = newValue; + silenceFormStore.data.alertmanagers = newValue || []; }); componentDidUpdate() { diff --git a/ui/src/Components/SilenceModal/AlertManagerInput/index.test.js b/ui/src/Components/SilenceModal/AlertManagerInput/index.test.js index ebfd9b56c..1313dd7ec 100644 --- a/ui/src/Components/SilenceModal/AlertManagerInput/index.test.js +++ b/ui/src/Components/SilenceModal/AlertManagerInput/index.test.js @@ -174,4 +174,19 @@ describe("", () => { const select = tree.find("StateManager"); expect(select.props().isDisabled).toBe(true); }); + + it("removing last options sets silenceFormStore.data.alertmanagers to []", () => { + const tree = MountedAlertManagerInput(); + expect(silenceFormStore.data.alertmanagers).toHaveLength(2); + + tree + .find(".react-select__multi-value__remove") + .at(0) + .simulate("click"); + expect(silenceFormStore.data.alertmanagers).toHaveLength(1); + + tree.find(".react-select__multi-value__remove").simulate("click"); + expect(silenceFormStore.data.alertmanagers).toHaveLength(0); + expect(silenceFormStore.data.alertmanagers).toEqual([]); + }); });