|
@@ -1,7 +1,7 @@
|
|
import React from 'react';
|
|
import React from 'react';
|
|
import New from 'components/Topics/New/New';
|
|
import New from 'components/Topics/New/New';
|
|
import { Route, Routes } from 'react-router-dom';
|
|
import { Route, Routes } from 'react-router-dom';
|
|
-import { act, screen, waitFor } from '@testing-library/react';
|
|
|
|
|
|
+import { act, screen } from '@testing-library/react';
|
|
import {
|
|
import {
|
|
clusterTopicCopyPath,
|
|
clusterTopicCopyPath,
|
|
clusterTopicNewPath,
|
|
clusterTopicNewPath,
|
|
@@ -13,18 +13,17 @@ import { useCreateTopic } from 'lib/hooks/api/topics';
|
|
|
|
|
|
const clusterName = 'local';
|
|
const clusterName = 'local';
|
|
const topicName = 'test-topic';
|
|
const topicName = 'test-topic';
|
|
|
|
+const minValue = '1';
|
|
|
|
|
|
const mockNavigate = jest.fn();
|
|
const mockNavigate = jest.fn();
|
|
jest.mock('react-router-dom', () => ({
|
|
jest.mock('react-router-dom', () => ({
|
|
...jest.requireActual('react-router-dom'),
|
|
...jest.requireActual('react-router-dom'),
|
|
useNavigate: () => mockNavigate,
|
|
useNavigate: () => mockNavigate,
|
|
}));
|
|
}));
|
|
-
|
|
|
|
jest.mock('lib/hooks/api/topics', () => ({
|
|
jest.mock('lib/hooks/api/topics', () => ({
|
|
useCreateTopic: jest.fn(),
|
|
useCreateTopic: jest.fn(),
|
|
}));
|
|
}));
|
|
-
|
|
|
|
-const renderComponent = (path: string) =>
|
|
|
|
|
|
+const renderComponent = (path: string) => {
|
|
render(
|
|
render(
|
|
<Routes>
|
|
<Routes>
|
|
<Route path={clusterTopicNewPath()} element={<New />} />
|
|
<Route path={clusterTopicNewPath()} element={<New />} />
|
|
@@ -33,9 +32,9 @@ const renderComponent = (path: string) =>
|
|
</Routes>,
|
|
</Routes>,
|
|
{ initialEntries: [path] }
|
|
{ initialEntries: [path] }
|
|
);
|
|
);
|
|
|
|
+};
|
|
|
|
|
|
const createTopicMock = jest.fn();
|
|
const createTopicMock = jest.fn();
|
|
-
|
|
|
|
describe('New', () => {
|
|
describe('New', () => {
|
|
beforeEach(() => {
|
|
beforeEach(() => {
|
|
(useCreateTopic as jest.Mock).mockImplementation(() => ({
|
|
(useCreateTopic as jest.Mock).mockImplementation(() => ({
|
|
@@ -45,7 +44,6 @@ describe('New', () => {
|
|
afterEach(() => {
|
|
afterEach(() => {
|
|
mockNavigate.mockClear();
|
|
mockNavigate.mockClear();
|
|
});
|
|
});
|
|
-
|
|
|
|
it('checks header for create new', async () => {
|
|
it('checks header for create new', async () => {
|
|
await act(() => {
|
|
await act(() => {
|
|
renderComponent(clusterTopicNewPath(clusterName));
|
|
renderComponent(clusterTopicNewPath(clusterName));
|
|
@@ -57,14 +55,25 @@ describe('New', () => {
|
|
renderComponent(`${clusterTopicCopyPath(clusterName)}?name=test`);
|
|
renderComponent(`${clusterTopicCopyPath(clusterName)}?name=test`);
|
|
expect(screen.getByRole('heading', { name: 'Copy' })).toBeInTheDocument();
|
|
expect(screen.getByRole('heading', { name: 'Copy' })).toBeInTheDocument();
|
|
});
|
|
});
|
|
-
|
|
|
|
it('validates form', async () => {
|
|
it('validates form', async () => {
|
|
- renderComponent(clusterTopicNewPath(clusterName));
|
|
|
|
|
|
+ await renderComponent(clusterTopicNewPath(clusterName));
|
|
await userEvent.type(screen.getByPlaceholderText('Topic Name'), topicName);
|
|
await userEvent.type(screen.getByPlaceholderText('Topic Name'), topicName);
|
|
await userEvent.clear(screen.getByPlaceholderText('Topic Name'));
|
|
await userEvent.clear(screen.getByPlaceholderText('Topic Name'));
|
|
- await waitFor(() => {
|
|
|
|
- expect(screen.getByText('name is a required field')).toBeInTheDocument();
|
|
|
|
- });
|
|
|
|
|
|
+ await userEvent.tab();
|
|
|
|
+ await expect(
|
|
|
|
+ screen.getByText('name is a required field')
|
|
|
|
+ ).toBeInTheDocument();
|
|
|
|
+
|
|
|
|
+ await userEvent.type(
|
|
|
|
+ screen.getByLabelText('Number of partitions *'),
|
|
|
|
+ minValue
|
|
|
|
+ );
|
|
|
|
+ await userEvent.clear(screen.getByLabelText('Number of partitions *'));
|
|
|
|
+ await userEvent.tab();
|
|
|
|
+ await expect(
|
|
|
|
+ screen.getByText('Number of partitions is required and must be a number')
|
|
|
|
+ ).toBeInTheDocument();
|
|
|
|
+
|
|
expect(createTopicMock).not.toHaveBeenCalled();
|
|
expect(createTopicMock).not.toHaveBeenCalled();
|
|
expect(mockNavigate).not.toHaveBeenCalled();
|
|
expect(mockNavigate).not.toHaveBeenCalled();
|
|
});
|
|
});
|
|
@@ -83,6 +92,10 @@ describe('New', () => {
|
|
it('submits valid form', async () => {
|
|
it('submits valid form', async () => {
|
|
renderComponent(clusterTopicNewPath(clusterName));
|
|
renderComponent(clusterTopicNewPath(clusterName));
|
|
await userEvent.type(screen.getByPlaceholderText('Topic Name'), topicName);
|
|
await userEvent.type(screen.getByPlaceholderText('Topic Name'), topicName);
|
|
|
|
+ await userEvent.type(
|
|
|
|
+ screen.getByLabelText('Number of partitions *'),
|
|
|
|
+ minValue
|
|
|
|
+ );
|
|
await userEvent.click(screen.getByText('Create topic'));
|
|
await userEvent.click(screen.getByText('Create topic'));
|
|
expect(createTopicMock).toHaveBeenCalledTimes(1);
|
|
expect(createTopicMock).toHaveBeenCalledTimes(1);
|
|
expect(mockNavigate).toHaveBeenLastCalledWith(`../${topicName}`);
|
|
expect(mockNavigate).toHaveBeenLastCalledWith(`../${topicName}`);
|