AlertRuleList.test.tsx 4.1 KB

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