Skip to content

Commit

Permalink
[Uptime] Clear selectedFilters state when action payload is blank (e…
Browse files Browse the repository at this point in the history
…lastic#99851)

* Clear `selectedFilters` state when action payload is blank.

* Add tests.

* Fix import error.
  • Loading branch information
justinkambic authored May 12, 2021
1 parent ea75823 commit 5dec3db
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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<void>('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);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,12 @@ export function selectedFiltersReducer(
action: Action<any>
): 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;
}
Expand Down

0 comments on commit 5dec3db

Please sign in to comment.