From 5dec3db8c304b17238045a4cd5ed2124b736a11b Mon Sep 17 00:00:00 2001 From: Justin Kambic Date: Wed, 12 May 2021 15:38:43 -0400 Subject: [PATCH] [Uptime] Clear `selectedFilters` state when action payload is blank (#99851) * Clear `selectedFilters` state when action payload is blank. * Add tests. * Fix import error. --- .../state/reducers/selected_filters.test.ts | 76 +++++++++++++++++++ .../public/state/reducers/selected_filters.ts | 6 +- 2 files changed, 78 insertions(+), 4 deletions(-) create mode 100644 x-pack/plugins/uptime/public/state/reducers/selected_filters.test.ts diff --git a/x-pack/plugins/uptime/public/state/reducers/selected_filters.test.ts b/x-pack/plugins/uptime/public/state/reducers/selected_filters.test.ts new file mode 100644 index 0000000000000..f69af7bd1b5a3 --- /dev/null +++ b/x-pack/plugins/uptime/public/state/reducers/selected_filters.test.ts @@ -0,0 +1,76 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import { selectedFiltersReducer } from './selected_filters'; +import { + getSelectedFilters, + setSelectedFilters, + SelectedFilters, +} from '../actions/selected_filters'; +import { createAction } from 'redux-actions'; + +describe('selectedFiltersReducer', () => { + let state: SelectedFilters; + + beforeEach(() => { + state = { + locations: [], + ports: [], + schemes: ['http'], + tags: [], + }; + }); + + it('returns state by default', () => { + expect(selectedFiltersReducer(state, createAction('fake action')())).toEqual(state); + }); + + describe('setSelectedFilters', () => { + it('returns null for null payload', () => { + expect(selectedFiltersReducer(state, setSelectedFilters(null))).toBeNull(); + }); + + it('sets state to the action payload if state is null', () => { + expect( + selectedFiltersReducer( + null, + setSelectedFilters({ + locations: [], + ports: [5601], + schemes: [], + tags: [], + }) + ) + ).toEqual({ locations: [], ports: [5601], schemes: [], tags: [] }); + }); + + it('merges state and action payload', () => { + expect( + selectedFiltersReducer( + { + locations: [], + ports: [], + schemes: [], + tags: [], + }, + setSelectedFilters({ + locations: [], + ports: [5601], + schemes: [], + tags: ['prod'], + }) + ) + ).toEqual({ locations: [], ports: [5601], schemes: [], tags: ['prod'] }); + }); + }); + + describe('getSelectedFilters', () => { + it('returns the state', () => { + expect(selectedFiltersReducer(state, getSelectedFilters())).toEqual(state); + }); + }); +}); diff --git a/x-pack/plugins/uptime/public/state/reducers/selected_filters.ts b/x-pack/plugins/uptime/public/state/reducers/selected_filters.ts index fc24241331169..b5e3544b66156 100644 --- a/x-pack/plugins/uptime/public/state/reducers/selected_filters.ts +++ b/x-pack/plugins/uptime/public/state/reducers/selected_filters.ts @@ -19,14 +19,12 @@ export function selectedFiltersReducer( action: Action ): SelectedFilters | null { switch (action.type) { - case String(getSelectedFilters): - return state; case String(setSelectedFilters): - if (state === null) return { ...action.payload }; + if (action.payload === null) return null; return { - ...(state || {}), ...action.payload, }; + case String(getSelectedFilters): default: return state; }