AlertRuleList.test.tsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import { shallow } from 'enzyme';
  2. import React from 'react';
  3. import { mockToolkitActionCreator } from 'test/core/redux/mocks';
  4. import { NavModel } from '@grafana/data';
  5. import { locationService } from '@grafana/runtime';
  6. import { getRouteComponentProps } from 'app/core/navigation/__mocks__/routeProps';
  7. import appEvents from '../../core/app_events';
  8. import { AlertRule } from '../../types';
  9. import { ShowModalReactEvent } from '../../types/events';
  10. import { AlertHowToModal } from './AlertHowToModal';
  11. import { AlertRuleListUnconnected, Props } from './AlertRuleList';
  12. import { setSearchQuery } from './state/reducers';
  13. jest.mock('../../core/app_events', () => ({
  14. publish: jest.fn(),
  15. }));
  16. const setup = (propOverrides?: object) => {
  17. const props: Props = {
  18. ...getRouteComponentProps({}),
  19. navModel: {} as NavModel,
  20. alertRules: [] as AlertRule[],
  21. getAlertRulesAsync: jest.fn(),
  22. setSearchQuery: mockToolkitActionCreator(setSearchQuery),
  23. togglePauseAlertRule: jest.fn(),
  24. search: '',
  25. isLoading: false,
  26. };
  27. Object.assign(props, propOverrides);
  28. const wrapper = shallow(<AlertRuleListUnconnected {...props} />);
  29. return {
  30. wrapper,
  31. instance: wrapper.instance() as AlertRuleListUnconnected,
  32. };
  33. };
  34. describe('Life cycle', () => {
  35. describe('component did mount', () => {
  36. it('should call fetchrules', () => {
  37. const { instance } = setup();
  38. instance.fetchRules = jest.fn();
  39. instance.componentDidMount();
  40. expect(instance.fetchRules).toHaveBeenCalled();
  41. });
  42. });
  43. describe('component did update', () => {
  44. it('should call fetchrules if props differ', () => {
  45. const { instance } = setup();
  46. instance.fetchRules = jest.fn();
  47. instance.componentDidUpdate({ queryParams: { state: 'ok' } } as any);
  48. expect(instance.fetchRules).toHaveBeenCalled();
  49. });
  50. });
  51. });
  52. describe('Functions', () => {
  53. describe('Get state filter', () => {
  54. it('should get all if prop is not set', () => {
  55. const { instance } = setup();
  56. const stateFilter = instance.getStateFilter();
  57. expect(stateFilter).toEqual('all');
  58. });
  59. it('should return state filter if set', () => {
  60. const { instance } = setup({
  61. queryParams: { state: 'ok' },
  62. });
  63. const stateFilter = instance.getStateFilter();
  64. expect(stateFilter).toEqual('ok');
  65. });
  66. });
  67. describe('State filter changed', () => {
  68. it('should update location', () => {
  69. const { instance } = setup();
  70. const mockEvent = { value: 'alerting' };
  71. instance.onStateFilterChanged(mockEvent);
  72. expect(locationService.getSearchObject().state).toBe('alerting');
  73. });
  74. });
  75. describe('Open how to', () => {
  76. it('should emit show-modal event', () => {
  77. const { instance } = setup();
  78. instance.onOpenHowTo();
  79. expect(appEvents.publish).toHaveBeenCalledWith(new ShowModalReactEvent({ component: AlertHowToModal }));
  80. });
  81. });
  82. describe('Search query change', () => {
  83. it('should set search query', () => {
  84. const { instance } = setup();
  85. instance.onSearchQueryChange('dashboard');
  86. expect(instance.props.setSearchQuery).toHaveBeenCalledWith('dashboard');
  87. });
  88. });
  89. });