AlertRuleList.test.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import React from 'react';
  2. import { shallow } from 'enzyme';
  3. import { AlertRuleList, Props } from './AlertRuleList';
  4. import { AlertRule } from '../../types';
  5. import appEvents from '../../core/app_events';
  6. import { mockActionCreator } from 'app/core/redux';
  7. import { updateLocation } from 'app/core/actions';
  8. import { NavModel } from '@grafana/data';
  9. jest.mock('../../core/app_events', () => ({
  10. emit: jest.fn(),
  11. }));
  12. const setup = (propOverrides?: object) => {
  13. const props: Props = {
  14. navModel: {} as NavModel,
  15. alertRules: [] as AlertRule[],
  16. updateLocation: mockActionCreator(updateLocation),
  17. getAlertRulesAsync: jest.fn(),
  18. setSearchQuery: jest.fn(),
  19. togglePauseAlertRule: jest.fn(),
  20. stateFilter: '',
  21. search: '',
  22. isLoading: false,
  23. };
  24. Object.assign(props, propOverrides);
  25. const wrapper = shallow(<AlertRuleList {...props} />);
  26. return {
  27. wrapper,
  28. instance: wrapper.instance() as AlertRuleList,
  29. };
  30. };
  31. describe('Render', () => {
  32. it('should render component', () => {
  33. const { wrapper } = setup();
  34. expect(wrapper).toMatchSnapshot();
  35. });
  36. it('should render alert rules', () => {
  37. const { wrapper } = setup({
  38. alertRules: [
  39. {
  40. id: 1,
  41. dashboardId: 7,
  42. dashboardUid: 'ggHbN42mk',
  43. dashboardSlug: 'alerting-with-testdata',
  44. panelId: 3,
  45. name: 'TestData - Always OK',
  46. state: 'ok',
  47. newStateDate: '2018-09-04T10:01:01+02:00',
  48. evalDate: '0001-01-01T00:00:00Z',
  49. evalData: {},
  50. executionError: '',
  51. url: '/d/ggHbN42mk/alerting-with-testdata',
  52. },
  53. {
  54. id: 3,
  55. dashboardId: 7,
  56. dashboardUid: 'ggHbN42mk',
  57. dashboardSlug: 'alerting-with-testdata',
  58. panelId: 3,
  59. name: 'TestData - ok',
  60. state: 'ok',
  61. newStateDate: '2018-09-04T10:01:01+02:00',
  62. evalDate: '0001-01-01T00:00:00Z',
  63. evalData: {},
  64. executionError: 'error',
  65. url: '/d/ggHbN42mk/alerting-with-testdata',
  66. },
  67. ],
  68. });
  69. expect(wrapper).toMatchSnapshot();
  70. });
  71. });
  72. describe('Life cycle', () => {
  73. describe('component did mount', () => {
  74. it('should call fetchrules', () => {
  75. const { instance } = setup();
  76. instance.fetchRules = jest.fn();
  77. instance.componentDidMount();
  78. expect(instance.fetchRules).toHaveBeenCalled();
  79. });
  80. });
  81. describe('component did update', () => {
  82. it('should call fetchrules if props differ', () => {
  83. const { instance } = setup();
  84. instance.fetchRules = jest.fn();
  85. instance.componentDidUpdate({ stateFilter: 'ok' } as Props);
  86. expect(instance.fetchRules).toHaveBeenCalled();
  87. });
  88. });
  89. });
  90. describe('Functions', () => {
  91. describe('Get state filter', () => {
  92. it('should get all if prop is not set', () => {
  93. const { instance } = setup();
  94. const stateFilter = instance.getStateFilter();
  95. expect(stateFilter).toEqual('all');
  96. });
  97. it('should return state filter if set', () => {
  98. const { instance } = setup({
  99. stateFilter: 'ok',
  100. });
  101. const stateFilter = instance.getStateFilter();
  102. expect(stateFilter).toEqual('ok');
  103. });
  104. });
  105. describe('State filter changed', () => {
  106. it('should update location', () => {
  107. const { instance } = setup();
  108. const mockEvent = { target: { value: 'alerting' } } as React.ChangeEvent<HTMLSelectElement>;
  109. instance.onStateFilterChanged(mockEvent);
  110. expect(instance.props.updateLocation).toHaveBeenCalledWith({ query: { state: 'alerting' } });
  111. });
  112. });
  113. describe('Open how to', () => {
  114. it('should emit show-modal event', () => {
  115. const { instance } = setup();
  116. instance.onOpenHowTo();
  117. expect(appEvents.emit).toHaveBeenCalledWith('show-modal', {
  118. src: 'public/app/features/alerting/partials/alert_howto.html',
  119. modalClass: 'confirm-modal',
  120. model: {},
  121. });
  122. });
  123. });
  124. describe('Search query change', () => {
  125. it('should set search query', () => {
  126. const { instance } = setup();
  127. instance.onSearchQueryChange('dashboard');
  128. expect(instance.props.setSearchQuery).toHaveBeenCalledWith('dashboard');
  129. });
  130. });
  131. });