New.spec.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import React from 'react';
  2. import New from 'components/Topics/New/New';
  3. import { Route, Routes } from 'react-router-dom';
  4. import { act, screen, waitFor } from '@testing-library/react';
  5. import {
  6. clusterTopicCopyPath,
  7. clusterTopicNewPath,
  8. clusterTopicPath,
  9. } from 'lib/paths';
  10. import userEvent from '@testing-library/user-event';
  11. import { render } from 'lib/testHelpers';
  12. import { useCreateTopic } from 'lib/hooks/api/topics';
  13. const clusterName = 'local';
  14. const topicName = 'test-topic';
  15. const mockNavigate = jest.fn();
  16. jest.mock('react-router-dom', () => ({
  17. ...jest.requireActual('react-router-dom'),
  18. useNavigate: () => mockNavigate,
  19. }));
  20. jest.mock('lib/hooks/api/topics', () => ({
  21. useCreateTopic: jest.fn(),
  22. }));
  23. const renderComponent = (path: string) => {
  24. render(
  25. <Routes>
  26. <Route path={clusterTopicNewPath()} element={<New />} />
  27. <Route path={clusterTopicCopyPath()} element={<New />} />
  28. <Route path={clusterTopicPath()} element="New topic path" />
  29. </Routes>,
  30. { initialEntries: [path] }
  31. );
  32. };
  33. const createTopicMock = jest.fn();
  34. describe('New', () => {
  35. beforeEach(() => {
  36. (useCreateTopic as jest.Mock).mockImplementation(() => ({
  37. mutateAsync: createTopicMock,
  38. }));
  39. });
  40. afterEach(() => {
  41. mockNavigate.mockClear();
  42. });
  43. it('checks header for create new', async () => {
  44. await act(() => renderComponent(clusterTopicNewPath(clusterName)));
  45. expect(
  46. screen.getByRole('heading', { name: 'Create new Topic' })
  47. ).toHaveTextContent('Create new Topic');
  48. });
  49. it('checks header for copy', async () => {
  50. await act(() =>
  51. renderComponent(`${clusterTopicCopyPath(clusterName)}?name=test`)
  52. );
  53. expect(
  54. screen.getByRole('heading', { name: 'Copy Topic' })
  55. ).toHaveTextContent('Copy Topic');
  56. });
  57. it('validates form', async () => {
  58. await act(() => renderComponent(clusterTopicNewPath(clusterName)));
  59. await waitFor(() => {
  60. userEvent.type(screen.getByPlaceholderText('Topic Name'), topicName);
  61. });
  62. await waitFor(() => {
  63. userEvent.clear(screen.getByPlaceholderText('Topic Name'));
  64. });
  65. await waitFor(() => {
  66. expect(screen.getByText('name is a required field')).toBeInTheDocument();
  67. });
  68. expect(createTopicMock).not.toHaveBeenCalled();
  69. expect(mockNavigate).not.toHaveBeenCalled();
  70. });
  71. it('validates form invalid name', async () => {
  72. await act(() => renderComponent(clusterTopicNewPath(clusterName)));
  73. await waitFor(() => {
  74. userEvent.type(screen.getByPlaceholderText('Topic Name'), 'Invalid,Name');
  75. });
  76. await waitFor(() => {
  77. expect(
  78. screen.getByText('Only alphanumeric, _, -, and . allowed')
  79. ).toBeInTheDocument();
  80. });
  81. });
  82. it('submits valid form', async () => {
  83. await act(() => renderComponent(clusterTopicNewPath(clusterName)));
  84. await act(() => {
  85. userEvent.type(screen.getByPlaceholderText('Topic Name'), topicName);
  86. });
  87. await act(() => {
  88. userEvent.click(screen.getByText('Create topic'));
  89. });
  90. await waitFor(() => expect(createTopicMock).toHaveBeenCalledTimes(1));
  91. await waitFor(() =>
  92. expect(mockNavigate).toHaveBeenLastCalledWith(`../${topicName}`)
  93. );
  94. });
  95. });