AlertRuleList.test.tsx 4.0 KB

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