New.spec.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 } 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 minValue = '1';
  16. const mockNavigate = jest.fn();
  17. jest.mock('react-router-dom', () => ({
  18. ...jest.requireActual('react-router-dom'),
  19. useNavigate: () => mockNavigate,
  20. }));
  21. jest.mock('lib/hooks/api/topics', () => ({
  22. useCreateTopic: jest.fn(),
  23. }));
  24. const renderComponent = (path: string) => {
  25. render(
  26. <Routes>
  27. <Route path={clusterTopicNewPath()} element={<New />} />
  28. <Route path={clusterTopicCopyPath()} element={<New />} />
  29. <Route path={clusterTopicPath()} element="New topic path" />
  30. </Routes>,
  31. { initialEntries: [path] }
  32. );
  33. };
  34. const createTopicMock = jest.fn();
  35. describe('New', () => {
  36. beforeEach(() => {
  37. (useCreateTopic as jest.Mock).mockImplementation(() => ({
  38. createResource: createTopicMock,
  39. }));
  40. });
  41. it('checks header for create new', async () => {
  42. await act(() => {
  43. renderComponent(clusterTopicNewPath(clusterName));
  44. });
  45. expect(screen.getByRole('heading', { name: 'Create' })).toBeInTheDocument();
  46. });
  47. it('checks header for copy', async () => {
  48. await act(() => {
  49. renderComponent(`${clusterTopicCopyPath(clusterName)}?name=test`);
  50. });
  51. expect(screen.getByRole('heading', { name: 'Copy' })).toBeInTheDocument();
  52. });
  53. it('validates form', async () => {
  54. renderComponent(clusterTopicNewPath(clusterName));
  55. await userEvent.type(screen.getByPlaceholderText('Topic Name'), topicName);
  56. await userEvent.clear(screen.getByPlaceholderText('Topic Name'));
  57. await userEvent.tab();
  58. await expect(
  59. screen.getByText('name is a required field')
  60. ).toBeInTheDocument();
  61. await userEvent.type(
  62. screen.getByLabelText('Number of partitions *'),
  63. minValue
  64. );
  65. await userEvent.clear(screen.getByLabelText('Number of partitions *'));
  66. await userEvent.tab();
  67. await expect(
  68. screen.getByText('Number of partitions is required and must be a number')
  69. ).toBeInTheDocument();
  70. expect(createTopicMock).not.toHaveBeenCalled();
  71. expect(mockNavigate).not.toHaveBeenCalled();
  72. });
  73. it('validates form invalid name', async () => {
  74. renderComponent(clusterTopicNewPath(clusterName));
  75. await userEvent.type(
  76. screen.getByPlaceholderText('Topic Name'),
  77. 'Invalid,Name'
  78. );
  79. expect(
  80. screen.getByText('Only alphanumeric, _, -, and . allowed')
  81. ).toBeInTheDocument();
  82. });
  83. it('submits valid form', async () => {
  84. renderComponent(clusterTopicNewPath(clusterName));
  85. await userEvent.type(screen.getByPlaceholderText('Topic Name'), topicName);
  86. await userEvent.type(
  87. screen.getByLabelText('Number of partitions *'),
  88. minValue
  89. );
  90. await userEvent.click(screen.getByText('Create topic'));
  91. expect(createTopicMock).toHaveBeenCalledTimes(1);
  92. expect(mockNavigate).toHaveBeenLastCalledWith(`../${topicName}`);
  93. });
  94. });