MessagesTable.spec.tsx 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import React from 'react';
  2. import { screen } from '@testing-library/react';
  3. import { render } from 'lib/testHelpers';
  4. import MessagesTable from 'components/Topics/Topic/Messages/MessagesTable';
  5. import { SeekDirection, SeekType, TopicMessage } from 'generated-sources';
  6. import TopicMessagesContext, {
  7. ContextProps,
  8. } from 'components/contexts/TopicMessagesContext';
  9. import {
  10. topicMessagePayload,
  11. topicMessagesMetaPayload,
  12. } from 'redux/reducers/topicMessages/__test__/fixtures';
  13. const mockTopicsMessages: TopicMessage[] = [{ ...topicMessagePayload }];
  14. const mockNavigate = jest.fn();
  15. jest.mock('react-router-dom', () => ({
  16. ...jest.requireActual('react-router-dom'),
  17. useNavigate: () => mockNavigate,
  18. }));
  19. describe('MessagesTable', () => {
  20. const seekToResult = '&seekTo=0::9';
  21. const searchParamsValue = `?filterQueryType=STRING_CONTAINS&attempt=0&limit=100&seekDirection=${SeekDirection.FORWARD}&seekType=${SeekType.OFFSET}${seekToResult}`;
  22. const searchParams = new URLSearchParams(searchParamsValue);
  23. const contextValue: ContextProps = {
  24. isLive: false,
  25. seekDirection: SeekDirection.FORWARD,
  26. changeSeekDirection: jest.fn(),
  27. };
  28. const setUpComponent = (
  29. params: URLSearchParams = searchParams,
  30. ctx: ContextProps = contextValue,
  31. messages: TopicMessage[] = [],
  32. isFetching?: boolean,
  33. path?: string
  34. ) => {
  35. const customPath = path || params.toString();
  36. return render(
  37. <TopicMessagesContext.Provider value={ctx}>
  38. <MessagesTable />
  39. </TopicMessagesContext.Provider>,
  40. {
  41. initialEntries: [customPath],
  42. preloadedState: {
  43. topicMessages: {
  44. messages,
  45. meta: {
  46. ...topicMessagesMetaPayload,
  47. },
  48. isFetching: !!isFetching,
  49. },
  50. },
  51. }
  52. );
  53. };
  54. describe('Default props Setup for MessagesTable component', () => {
  55. beforeEach(() => {
  56. setUpComponent();
  57. });
  58. it('should check the render', () => {
  59. expect(screen.getByRole('table')).toBeInTheDocument();
  60. });
  61. it('should check the if no elements is rendered in the table', () => {
  62. expect(screen.getByText(/No messages found/i)).toBeInTheDocument();
  63. });
  64. });
  65. describe('Custom Setup with different props value', () => {
  66. it('should check if next click is gone during isLive Param', () => {
  67. setUpComponent(searchParams, { ...contextValue, isLive: true });
  68. expect(screen.queryByText(/next/i)).not.toBeInTheDocument();
  69. });
  70. it('should check the display of the loader element', () => {
  71. setUpComponent(searchParams, { ...contextValue, isLive: true }, [], true);
  72. expect(screen.getByRole('progressbar')).toBeInTheDocument();
  73. });
  74. });
  75. describe('should render Messages table with data', () => {
  76. beforeEach(() => {
  77. setUpComponent(searchParams, { ...contextValue }, mockTopicsMessages);
  78. });
  79. it('should check the rendering of the messages', () => {
  80. expect(screen.queryByText(/No messages found/i)).not.toBeInTheDocument();
  81. expect(
  82. screen.getByText(mockTopicsMessages[0].content as string)
  83. ).toBeInTheDocument();
  84. });
  85. });
  86. });