Messages.spec.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 Messages, {
  6. Props,
  7. } from '../../../../components/Topics/Details/Messages/Messages';
  8. import PageLoader from '../../../../components/common/PageLoader/PageLoader';
  9. describe('Messages component', () => {
  10. beforeEach(() => {
  11. jest.restoreAllMocks();
  12. });
  13. const setupWrapper = (props: Partial<Props> = {}) => (
  14. <Messages
  15. clusterName="Test cluster"
  16. topicName="Cluster topic"
  17. isFetched
  18. fetchTopicMessages={jest.fn()}
  19. messages={[]}
  20. partitions={[]}
  21. {...props}
  22. />
  23. );
  24. describe('Initial state', () => {
  25. it('renders PageLoader', () => {
  26. expect(
  27. shallow(setupWrapper({ isFetched: false })).find(PageLoader)
  28. ).toBeTruthy();
  29. });
  30. });
  31. describe('Messages table', () => {
  32. describe('With messages', () => {
  33. const messagesWrapper = shallow(
  34. setupWrapper({
  35. messages: [
  36. {
  37. partition: 1,
  38. offset: 2,
  39. timestamp: new Date('05-05-1994'),
  40. content: [1, 2, 3],
  41. },
  42. ],
  43. })
  44. );
  45. it('renders table', () => {
  46. expect(messagesWrapper.find('TimeStamp')).toBeTruthy();
  47. });
  48. it('renders JSONTree', () => {
  49. expect(messagesWrapper.find(JSONTree)).toBeTruthy();
  50. });
  51. it('parses message content correctly', () => {
  52. const messages = [
  53. {
  54. partition: 1,
  55. offset: 2,
  56. timestamp: new Date('05-05-1994'),
  57. content: [1, 2, 3],
  58. },
  59. ];
  60. const content = JSON.stringify(messages[0].content);
  61. expect(JSON.parse(content)).toEqual(messages[0].content);
  62. });
  63. });
  64. describe('Without messages', () => {
  65. it('renders string', () => {
  66. const wrapper = shallow(setupWrapper());
  67. expect(wrapper.text()).toContain('No messages at selected topic');
  68. });
  69. });
  70. });
  71. describe('Offset field', () => {
  72. const wrapper = shallow(setupWrapper());
  73. describe('With defined offset value', () => {
  74. it('shows offset value in input', () => {
  75. const offset = '10';
  76. wrapper
  77. .find('#searchOffset')
  78. .simulate('change', { target: { value: offset } });
  79. expect(wrapper.find('#searchOffset').first().props().value).toEqual(
  80. offset
  81. );
  82. });
  83. });
  84. describe('With invalid offset value', () => {
  85. it('shows 0 in input', () => {
  86. const offset = null;
  87. wrapper
  88. .find('#searchOffset')
  89. .simulate('change', { target: { value: offset } });
  90. expect(wrapper.find('#searchOffset').first().props().value).toBe('0');
  91. });
  92. });
  93. });
  94. describe('Search field', () => {
  95. it('renders input correctly', () => {
  96. const query = 20;
  97. const mockedUseDebouncedCallback = jest.fn();
  98. jest
  99. .spyOn(useDebounce, 'useDebouncedCallback')
  100. .mockImplementationOnce(() => [mockedUseDebouncedCallback]);
  101. const wrapper = shallow(setupWrapper());
  102. wrapper
  103. .find('#searchText')
  104. .simulate('change', { target: { value: query } });
  105. expect(wrapper.find('#searchText').first().props().value).toEqual(query);
  106. expect(mockedUseDebouncedCallback).toHaveBeenCalledWith({ q: query });
  107. });
  108. });
  109. });