AlertRuleList.test.tsx 4.1 KB

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