AlertRuleList.test.tsx 4.0 KB

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