grafana/public/app/features/alerting/AlertRuleList.test.tsx

158 lines
4.1 KiB
TypeScript
Raw Normal View History

import React from 'react';
2018-09-04 08:00:04 -05:00
import { shallow } from 'enzyme';
import { AlertRuleList, Props } from './AlertRuleList';
2018-09-04 08:00:04 -05:00
import { AlertRule, NavModel } from '../../types';
import appEvents from '../../core/app_events';
jest.mock('../../core/app_events', () => ({
emit: jest.fn(),
}));
const setup = (propOverrides?: object) => {
const props: Props = {
navModel: {} as NavModel,
alertRules: [] as AlertRule[],
updateLocation: jest.fn(),
getAlertRulesAsync: jest.fn(),
setSearchQuery: jest.fn(),
togglePauseAlertRule: jest.fn(),
2018-09-04 08:00:04 -05:00
stateFilter: '',
search: '',
isLoading: false
2018-09-04 08:00:04 -05:00
};
Object.assign(props, propOverrides);
const wrapper = shallow(<AlertRuleList {...props} />);
return {
wrapper,
instance: wrapper.instance() as AlertRuleList,
};
};
describe('Render', () => {
it('should render component', () => {
const { wrapper } = setup();
expect(wrapper).toMatchSnapshot();
});
it('should render alert rules', () => {
const { wrapper } = setup({
alertRules: [
{
2018-09-04 08:00:04 -05:00
id: 1,
dashboardId: 7,
dashboardUid: 'ggHbN42mk',
dashboardSlug: 'alerting-with-testdata',
panelId: 3,
2018-09-04 08:00:04 -05:00
name: 'TestData - Always OK',
state: 'ok',
2018-09-04 08:00:04 -05:00
newStateDate: '2018-09-04T10:01:01+02:00',
evalDate: '0001-01-01T00:00:00Z',
evalData: {},
executionError: '',
2018-09-04 08:00:04 -05:00
url: '/d/ggHbN42mk/alerting-with-testdata',
},
{
id: 3,
dashboardId: 7,
dashboardUid: 'ggHbN42mk',
dashboardSlug: 'alerting-with-testdata',
panelId: 3,
name: 'TestData - ok',
state: 'ok',
newStateDate: '2018-09-04T10:01:01+02:00',
evalDate: '0001-01-01T00:00:00Z',
evalData: {},
executionError: 'error',
url: '/d/ggHbN42mk/alerting-with-testdata',
},
2018-09-04 08:00:04 -05:00
],
});
expect(wrapper).toMatchSnapshot();
});
});
describe('Life cycle', () => {
describe('component did mount', () => {
it('should call fetchrules', () => {
const { instance } = setup();
instance.fetchRules = jest.fn();
instance.componentDidMount();
expect(instance.fetchRules).toHaveBeenCalled();
});
});
2018-09-04 08:00:04 -05:00
describe('component did update', () => {
it('should call fetchrules if props differ', () => {
const { instance } = setup();
instance.fetchRules = jest.fn();
2018-09-04 15:15:45 -05:00
instance.componentDidUpdate({ stateFilter: 'ok' } as Props);
2018-09-04 08:00:04 -05:00
expect(instance.fetchRules).toHaveBeenCalled();
});
});
2018-09-04 08:00:04 -05:00
});
describe('Functions', () => {
describe('Get state filter', () => {
it('should get all if prop is not set', () => {
const { instance } = setup();
const stateFilter = instance.getStateFilter();
expect(stateFilter).toEqual('all');
});
it('should return state filter if set', () => {
const { instance } = setup({
stateFilter: 'ok',
});
2018-09-04 08:00:04 -05:00
const stateFilter = instance.getStateFilter();
expect(stateFilter).toEqual('ok');
});
});
describe('State filter changed', () => {
it('should update location', () => {
const { instance } = setup();
const mockEvent = { target: { value: 'alerting' } } as React.ChangeEvent<HTMLSelectElement>;
2018-09-04 08:00:04 -05:00
instance.onStateFilterChanged(mockEvent);
expect(instance.props.updateLocation).toHaveBeenCalledWith({ query: { state: 'alerting' } });
});
2018-01-01 11:54:23 -06:00
});
2018-09-04 08:00:04 -05:00
describe('Open how to', () => {
it('should emit show-modal event', () => {
const { instance } = setup();
instance.onOpenHowTo();
expect(appEvents.emit).toHaveBeenCalledWith('show-modal', {
src: 'public/app/features/alerting/partials/alert_howto.html',
modalClass: 'confirm-modal',
model: {},
});
});
});
2018-01-01 11:54:23 -06:00
2018-09-04 08:00:04 -05:00
describe('Search query change', () => {
it('should set search query', () => {
const { instance } = setup();
const mockEvent = { target: { value: 'dashboard' } } as React.ChangeEvent<HTMLInputElement>;
2018-01-01 11:54:23 -06:00
2018-09-04 08:00:04 -05:00
instance.onSearchQueryChange(mockEvent);
2018-01-01 11:54:23 -06:00
2018-09-04 08:00:04 -05:00
expect(instance.props.setSearchQuery).toHaveBeenCalledWith('dashboard');
});
});
});