Messages.spec.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import React from 'react';
  2. import { mount, shallow } from 'enzyme';
  3. import JSONTree from 'react-json-tree';
  4. import * as useDebounce from 'use-debounce';
  5. import DatePicker from 'react-datepicker';
  6. import Messages, { Props } from 'components/Topics/Details/Messages/Messages';
  7. import PageLoader from 'components/common/PageLoader/PageLoader';
  8. describe('Messages component', () => {
  9. beforeEach(() => {
  10. jest.restoreAllMocks();
  11. });
  12. const setupWrapper = (props: Partial<Props> = {}) => (
  13. <Messages
  14. clusterName="Test cluster"
  15. topicName="Cluster topic"
  16. isFetched
  17. fetchTopicMessages={jest.fn()}
  18. messages={[]}
  19. partitions={[]}
  20. {...props}
  21. />
  22. );
  23. describe('Initial state', () => {
  24. it('renders PageLoader', () => {
  25. expect(
  26. shallow(setupWrapper({ isFetched: false })).exists(PageLoader)
  27. ).toBeTruthy();
  28. });
  29. });
  30. describe('Messages table', () => {
  31. describe('With messages', () => {
  32. const messagesWrapper = mount(
  33. setupWrapper({
  34. messages: [
  35. {
  36. partition: 1,
  37. offset: 2,
  38. timestamp: new Date('05-05-1994'),
  39. content: [1, 2, 3],
  40. },
  41. ],
  42. })
  43. );
  44. it('renders table', () => {
  45. expect(
  46. messagesWrapper.exists('[className="table is-striped is-fullwidth"]')
  47. ).toBeTruthy();
  48. });
  49. it('renders JSONTree', () => {
  50. expect(messagesWrapper.find(JSONTree).length).toEqual(1);
  51. });
  52. it('parses message content correctly', () => {
  53. const messages = [
  54. {
  55. partition: 1,
  56. offset: 2,
  57. timestamp: new Date('05-05-1994'),
  58. content: [1, 2, 3],
  59. },
  60. ];
  61. const content = JSON.stringify(messages[0].content);
  62. expect(JSON.parse(content)).toEqual(messages[0].content);
  63. });
  64. });
  65. describe('Without messages', () => {
  66. it('renders string', () => {
  67. const wrapper = mount(setupWrapper());
  68. expect(wrapper.text()).toContain('No messages at selected topic');
  69. });
  70. });
  71. });
  72. describe('Offset field', () => {
  73. describe('Seek Type dependency', () => {
  74. const wrapper = mount(setupWrapper());
  75. it('renders DatePicker', () => {
  76. wrapper
  77. .find('[id="selectSeekType"]')
  78. .simulate('change', { target: { value: 'TIMESTAMP' } });
  79. expect(
  80. wrapper.find('[id="selectSeekType"]').first().props().value
  81. ).toEqual('TIMESTAMP');
  82. expect(wrapper.exists(DatePicker)).toBeTruthy();
  83. });
  84. });
  85. describe('With defined offset value', () => {
  86. const wrapper = shallow(setupWrapper());
  87. it('shows offset value in input', () => {
  88. const offset = '10';
  89. wrapper
  90. .find('#searchOffset')
  91. .simulate('change', { target: { value: offset } });
  92. expect(wrapper.find('#searchOffset').first().props().value).toEqual(
  93. offset
  94. );
  95. });
  96. });
  97. describe('With invalid offset value', () => {
  98. const wrapper = shallow(setupWrapper());
  99. it('shows 0 in input', () => {
  100. wrapper
  101. .find('#searchOffset')
  102. .simulate('change', { target: { value: null } });
  103. expect(wrapper.find('#searchOffset').first().props().value).toBe('0');
  104. });
  105. });
  106. });
  107. describe('Search field', () => {
  108. it('renders input correctly', () => {
  109. const query = 20;
  110. const mockedUseDebouncedCallback = jest.fn();
  111. jest
  112. .spyOn(useDebounce, 'useDebouncedCallback')
  113. .mockImplementationOnce(() => [
  114. mockedUseDebouncedCallback,
  115. jest.fn(),
  116. jest.fn(),
  117. ]);
  118. const wrapper = shallow(setupWrapper());
  119. wrapper
  120. .find('#searchText')
  121. .simulate('change', { target: { value: query } });
  122. expect(wrapper.find('#searchText').first().props().value).toEqual(query);
  123. expect(mockedUseDebouncedCallback).toHaveBeenCalledWith({ q: query });
  124. });
  125. });
  126. describe('Submit button', () => {
  127. it('fetches topic messages', () => {
  128. const mockedfetchTopicMessages = jest.fn();
  129. const wrapper = mount(
  130. setupWrapper({ fetchTopicMessages: mockedfetchTopicMessages })
  131. );
  132. wrapper.find('[type="submit"]').simulate('click');
  133. expect(mockedfetchTopicMessages).toHaveBeenCalled();
  134. });
  135. });
  136. });