New.spec.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import React from 'react';
  2. import New from 'components/Topics/New/New';
  3. import { Route, Routes } from 'react-router-dom';
  4. import configureStore from 'redux-mock-store';
  5. import { RootState } from 'redux/interfaces';
  6. import * as redux from 'react-redux';
  7. import { act, screen, waitFor } from '@testing-library/react';
  8. import fetchMock from 'fetch-mock-jest';
  9. import {
  10. clusterTopicCopyPath,
  11. clusterTopicNewPath,
  12. clusterTopicPath,
  13. } from 'lib/paths';
  14. import userEvent from '@testing-library/user-event';
  15. import { render } from 'lib/testHelpers';
  16. const { Provider } = redux;
  17. const mockStore = configureStore();
  18. const clusterName = 'local';
  19. const topicName = 'test-topic';
  20. const initialState: Partial<RootState> = {};
  21. const storeMock = mockStore(initialState);
  22. const mockNavigate = jest.fn();
  23. jest.mock('react-router-dom', () => ({
  24. ...jest.requireActual('react-router-dom'),
  25. useNavigate: () => mockNavigate,
  26. }));
  27. const renderComponent = (path: string, store = storeMock) =>
  28. render(
  29. <Routes>
  30. <Route
  31. path={clusterTopicNewPath()}
  32. element={
  33. <Provider store={store}>
  34. <New />
  35. </Provider>
  36. }
  37. />
  38. <Route
  39. path={clusterTopicCopyPath()}
  40. element={
  41. <Provider store={store}>
  42. <New />
  43. </Provider>
  44. }
  45. />
  46. <Route path={clusterTopicPath()} element="New topic path" />
  47. </Routes>,
  48. { initialEntries: [path] }
  49. );
  50. describe('New', () => {
  51. beforeEach(() => {
  52. fetchMock.reset();
  53. });
  54. afterEach(() => {
  55. mockNavigate.mockClear();
  56. });
  57. it('checks header for create new', async () => {
  58. renderComponent(clusterTopicNewPath(clusterName));
  59. expect(
  60. screen.getByRole('heading', { name: 'Create new Topic' })
  61. ).toHaveTextContent('Create new Topic');
  62. });
  63. it('checks header for copy', async () => {
  64. renderComponent(`${clusterTopicCopyPath(clusterName)}?name=test`);
  65. expect(
  66. screen.getByRole('heading', { name: 'Copy Topic' })
  67. ).toHaveTextContent('Copy Topic');
  68. });
  69. it('validates form', async () => {
  70. renderComponent(clusterTopicNewPath(clusterName));
  71. await waitFor(() => {
  72. userEvent.click(screen.getByText(/submit/i));
  73. });
  74. await waitFor(() => {
  75. expect(screen.getByText('name is a required field')).toBeInTheDocument();
  76. });
  77. await waitFor(() => {
  78. expect(mockNavigate).not.toHaveBeenCalled();
  79. });
  80. });
  81. it('submits valid form', async () => {
  82. const useDispatchSpy = jest.spyOn(redux, 'useDispatch');
  83. const useDispatchMock = jest.fn(() => ({
  84. meta: { requestStatus: 'fulfilled' },
  85. })) as jest.Mock;
  86. useDispatchSpy.mockReturnValue(useDispatchMock);
  87. await act(() => {
  88. renderComponent(clusterTopicNewPath(clusterName));
  89. });
  90. await waitFor(() => {
  91. userEvent.type(screen.getByPlaceholderText('Topic Name'), topicName);
  92. userEvent.click(screen.getByText(/submit/i));
  93. });
  94. await waitFor(() => {
  95. expect(mockNavigate).toBeCalledTimes(1);
  96. expect(mockNavigate).toHaveBeenLastCalledWith(`../${topicName}`);
  97. });
  98. expect(useDispatchMock).toHaveBeenCalledTimes(1);
  99. });
  100. it('does not redirect page when request is not fulfilled', async () => {
  101. const useDispatchSpy = jest.spyOn(redux, 'useDispatch');
  102. const useDispatchMock = jest.fn(() => ({
  103. meta: { requestStatus: 'pending' },
  104. })) as jest.Mock;
  105. useDispatchSpy.mockReturnValue(useDispatchMock);
  106. await act(() => {
  107. renderComponent(clusterTopicNewPath(clusterName));
  108. });
  109. await waitFor(() => {
  110. userEvent.type(screen.getByPlaceholderText('Topic Name'), topicName);
  111. userEvent.click(screen.getByText(/submit/i));
  112. });
  113. await waitFor(() => {
  114. expect(mockNavigate).not.toHaveBeenCalled();
  115. });
  116. });
  117. it('submits valid form that result in an error', async () => {
  118. const useDispatchSpy = jest.spyOn(redux, 'useDispatch');
  119. const useDispatchMock = jest.fn();
  120. useDispatchSpy.mockReturnValue(useDispatchMock);
  121. renderComponent(clusterTopicNewPath(clusterName));
  122. await act(() => {
  123. userEvent.type(screen.getByPlaceholderText('Topic Name'), topicName);
  124. userEvent.click(screen.getByText(/submit/i));
  125. });
  126. expect(useDispatchMock).toHaveBeenCalledTimes(1);
  127. expect(mockNavigate).not.toHaveBeenCalled();
  128. });
  129. });