diff --git a/web/ui/react-app/src/pages/alerts/AlertContents.test.tsx b/web/ui/react-app/src/pages/alerts/AlertContents.test.tsx new file mode 100644 index 0000000000..78b5a05513 --- /dev/null +++ b/web/ui/react-app/src/pages/alerts/AlertContents.test.tsx @@ -0,0 +1,39 @@ +import React from 'react'; +import { shallow } from 'enzyme'; +import AlertsContent from './AlertContents'; + +describe('AlertsContent', () => { + const defaultProps = { + groups: [], + statsCount: { + inactive: 0, + pending: 0, + firing: 0, + }, + }; + const wrapper = shallow(); + + it('matches a snapshot', () => { + expect(wrapper).toMatchSnapshot(); + }); + + [ + { selector: '#inactive-toggler', propName: 'inactive' }, + { selector: '#pending-toggler', propName: 'pending' }, + { selector: '#firing-toggler', propName: 'firing' }, + ].forEach(testCase => { + it(`toggles the ${testCase.propName} checkbox from true to false when clicked and back to true when clicked again`, () => { + wrapper.find(testCase.selector).invoke('onClick')(testCase.propName); + expect(wrapper.find(testCase.selector).prop('checked')).toBe(false); + wrapper.find(testCase.selector).invoke('onClick')(testCase.propName); + expect(wrapper.find(testCase.selector).prop('checked')).toBe(true); + }); + }); + + it('toggles the "annotations" checkbox from false to true when clicked and back to true when clicked again', () => { + wrapper.find('#show-annotations-toggler').invoke('onClick')(); + expect(wrapper.find('#show-annotations-toggler').prop('checked')).toBe(true); + wrapper.find('#show-annotations-toggler').invoke('onClick')(); + expect(wrapper.find('#show-annotations-toggler').prop('checked')).toBe(false); + }); +}); diff --git a/web/ui/react-app/src/pages/alerts/AlertContents.tsx b/web/ui/react-app/src/pages/alerts/AlertContents.tsx index 8f09e5f455..2cbfa71762 100644 --- a/web/ui/react-app/src/pages/alerts/AlertContents.tsx +++ b/web/ui/react-app/src/pages/alerts/AlertContents.tsx @@ -1,9 +1,10 @@ -import React, { FC, useState, Fragment } from 'react'; +import React, { FC, Fragment } from 'react'; import { Badge } from 'reactstrap'; import CollapsibleAlertPanel from './CollapsibleAlertPanel'; import Checkbox from '../../components/Checkbox'; import { isPresent } from '../../utils'; import { Rule } from '../../types/types'; +import { useLocalStorage } from '../../hooks/useLocalStorage'; export type RuleState = keyof RuleStatus; @@ -40,12 +41,12 @@ const stateColorTuples: Array<[RuleState, 'success' | 'warning' | 'danger']> = [ ]; const AlertsContent: FC = ({ groups = [], statsCount }) => { - const [filter, setFilter] = useState>({ + const [filter, setFilter] = useLocalStorage('alerts-status-filter', { firing: true, pending: true, inactive: true, }); - const [showAnnotations, setShowAnnotations] = useState(false); + const [showAnnotations, setShowAnnotations] = useLocalStorage('alerts-annotations-status', { checked: false }); const toggleFilter = (ruleState: RuleState) => () => { setFilter({ @@ -54,6 +55,10 @@ const AlertsContent: FC = ({ groups = [], statsCount }) => { }); }; + const toggleAnnotations = () => { + setShowAnnotations({ checked: !showAnnotations.checked }); + }; + return ( <>
@@ -62,7 +67,7 @@ const AlertsContent: FC = ({ groups = [], statsCount }) => { @@ -74,8 +79,9 @@ const AlertsContent: FC = ({ groups = [], statsCount }) => { })} setShowAnnotations(!showAnnotations)} + onClick={() => toggleAnnotations()} > Show annotations @@ -90,7 +96,7 @@ const AlertsContent: FC = ({ groups = [], statsCount }) => { {group.rules.map((rule, j) => { return ( filter[rule.state] && ( - + ) ); })} diff --git a/web/ui/react-app/src/pages/alerts/__snapshots__/AlertContents.test.tsx.snap b/web/ui/react-app/src/pages/alerts/__snapshots__/AlertContents.test.tsx.snap new file mode 100644 index 0000000000..fdbba2c9f8 --- /dev/null +++ b/web/ui/react-app/src/pages/alerts/__snapshots__/AlertContents.test.tsx.snap @@ -0,0 +1,100 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`AlertsContent matches a snapshot 1`] = ` + +
+ + + inactive + ( + 0 + ) + + + + + pending + ( + 0 + ) + + + + + firing + ( + 0 + ) + + + + + Show annotations + + +
+
+`;