The open and composable observability and data visualization platform. Visualize metrics, logs, and traces from multiple sources like Prometheus, Loki, Elasticsearch, InfluxDB, Postgres and many more.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
grafana/public/app/features/alerting/AlertRuleList.test.tsx

118 lines
3.2 KiB

import React from 'react';
7 years ago
import { shallow } from 'enzyme';
import { AlertRuleListUnconnected, Props } from './AlertRuleList';
import { AlertRule } from '../../types';
7 years ago
import appEvents from '../../core/app_events';
import { NavModel } from '@grafana/data';
import { CoreEvents } from 'app/types';
import { updateLocation } from '../../core/actions';
import { setSearchQuery } from './state/reducers';
import { mockToolkitActionCreator } from 'test/core/redux/mocks';
7 years ago
jest.mock('../../core/app_events', () => ({
emit: jest.fn(),
}));
const setup = (propOverrides?: object) => {
const props: Props = {
navModel: {} as NavModel,
alertRules: [] as AlertRule[],
updateLocation: mockToolkitActionCreator(updateLocation),
7 years ago
getAlertRulesAsync: jest.fn(),
setSearchQuery: mockToolkitActionCreator(setSearchQuery),
togglePauseAlertRule: jest.fn(),
7 years ago
stateFilter: '',
search: '',
isLoading: false,
ngAlertDefinitions: [],
7 years ago
};
Object.assign(props, propOverrides);
const wrapper = shallow(<AlertRuleListUnconnected {...props} />);
7 years ago
return {
wrapper,
instance: wrapper.instance() as AlertRuleListUnconnected,
7 years ago
};
};
describe('Life cycle', () => {
describe('component did mount', () => {
it('should call fetchrules', () => {
const { instance } = setup();
instance.fetchRules = jest.fn();
instance.componentDidMount();
expect(instance.fetchRules).toHaveBeenCalled();
});
});
7 years ago
describe('component did update', () => {
it('should call fetchrules if props differ', () => {
const { instance } = setup();
instance.fetchRules = jest.fn();
instance.componentDidUpdate({ stateFilter: 'ok' } as Props);
7 years ago
expect(instance.fetchRules).toHaveBeenCalled();
});
});
7 years ago
});
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',
});
7 years ago
const stateFilter = instance.getStateFilter();
expect(stateFilter).toEqual('ok');
});
});
describe('State filter changed', () => {
it('should update location', () => {
const { instance } = setup();
const mockEvent = { value: 'alerting' };
7 years ago
instance.onStateFilterChanged(mockEvent);
expect(instance.props.updateLocation).toHaveBeenCalledWith({ query: { state: 'alerting' } });
});
});
7 years ago
describe('Open how to', () => {
it('should emit show-modal event', () => {
const { instance } = setup();
instance.onOpenHowTo();
expect(appEvents.emit).toHaveBeenCalledWith(CoreEvents.showModal, {
7 years ago
src: 'public/app/features/alerting/partials/alert_howto.html',
modalClass: 'confirm-modal',
model: {},
});
});
});
7 years ago
describe('Search query change', () => {
it('should set search query', () => {
const { instance } = setup();
instance.onSearchQueryChange('dashboard');
7 years ago
expect(instance.props.setSearchQuery).toHaveBeenCalledWith('dashboard');
});
});
});