Messages.spec.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import React from 'react';
  2. import { screen, waitFor } from '@testing-library/react';
  3. import { render, EventSourceMock, WithRoute } from 'lib/testHelpers';
  4. import Messages, {
  5. SeekDirectionOptions,
  6. SeekDirectionOptionsObj,
  7. } from 'components/Topics/Topic/Messages/Messages';
  8. import { SeekDirection, SeekType } from 'generated-sources';
  9. import userEvent from '@testing-library/user-event';
  10. import { clusterTopicMessagesPath } from 'lib/paths';
  11. import { useSerdes } from 'lib/hooks/api/topicMessages';
  12. import { serdesPayload } from 'lib/fixtures/topicMessages';
  13. jest.mock('lib/hooks/api/topicMessages', () => ({
  14. useSerdes: jest.fn(),
  15. }));
  16. describe('Messages', () => {
  17. const searchParams = `?filterQueryType=STRING_CONTAINS&attempt=0&limit=100&seekDirection=${SeekDirection.FORWARD}&seekType=${SeekType.OFFSET}&seekTo=0::9`;
  18. const renderComponent = (param: string = searchParams) => {
  19. const query = new URLSearchParams(param).toString();
  20. const path = `${clusterTopicMessagesPath()}?${query}`;
  21. return render(
  22. <WithRoute path={clusterTopicMessagesPath()}>
  23. <Messages />
  24. </WithRoute>,
  25. {
  26. initialEntries: [path],
  27. }
  28. );
  29. };
  30. beforeEach(() => {
  31. Object.defineProperty(window, 'EventSource', {
  32. value: EventSourceMock,
  33. });
  34. (useSerdes as jest.Mock).mockImplementation(() => ({
  35. data: serdesPayload,
  36. }));
  37. });
  38. describe('component rendering default behavior with the search params', () => {
  39. beforeEach(() => {
  40. renderComponent();
  41. });
  42. it('should check default seekDirection if it actually take the value from the url', () => {
  43. expect(screen.getAllByRole('listbox')[3]).toHaveTextContent(
  44. SeekDirectionOptionsObj[SeekDirection.FORWARD].label
  45. );
  46. });
  47. it('should check the SeekDirection select changes with live option', async () => {
  48. const seekDirectionSelect = screen.getAllByRole('listbox')[3];
  49. const seekDirectionOption = screen.getAllByRole('option')[3];
  50. expect(seekDirectionOption).toHaveTextContent(
  51. SeekDirectionOptionsObj[SeekDirection.FORWARD].label
  52. );
  53. const labelValue1 = SeekDirectionOptions[1].label;
  54. await userEvent.click(seekDirectionSelect);
  55. await userEvent.selectOptions(seekDirectionSelect, [labelValue1]);
  56. expect(seekDirectionOption).toHaveTextContent(labelValue1);
  57. const labelValue0 = SeekDirectionOptions[0].label;
  58. await userEvent.click(seekDirectionSelect);
  59. await userEvent.selectOptions(seekDirectionSelect, [labelValue0]);
  60. expect(seekDirectionOption).toHaveTextContent(labelValue0);
  61. const liveOptionConf = SeekDirectionOptions[2];
  62. const labelValue2 = liveOptionConf.label;
  63. await userEvent.click(seekDirectionSelect);
  64. const liveModeLi = screen.getByRole(
  65. (role, element) =>
  66. role === 'option' &&
  67. element?.getAttribute('value') === liveOptionConf.value
  68. );
  69. await userEvent.selectOptions(seekDirectionSelect, [liveModeLi]);
  70. expect(seekDirectionOption).toHaveTextContent(labelValue2);
  71. await waitFor(() => {
  72. expect(screen.getByRole('contentLoader')).toBeInTheDocument();
  73. });
  74. });
  75. });
  76. describe('Component rendering with custom Url search params', () => {
  77. it('reacts to a change of seekDirection in the url which make the select pick up different value', () => {
  78. renderComponent(
  79. searchParams.replace(SeekDirection.FORWARD, SeekDirection.BACKWARD)
  80. );
  81. expect(screen.getAllByRole('listbox')[3]).toHaveTextContent(
  82. SeekDirectionOptionsObj[SeekDirection.BACKWARD].label
  83. );
  84. });
  85. });
  86. });