Details.spec.tsx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. import React from 'react';
  2. import Details from 'components/Schemas/Details/Details';
  3. import { render, WithRoute } from 'lib/testHelpers';
  4. import { clusterSchemaPath } from 'lib/paths';
  5. import { screen, waitFor } from '@testing-library/dom';
  6. import {
  7. schemasInitialState,
  8. schemaVersion,
  9. schemaVersionWithNonAsciiChars,
  10. } from 'redux/reducers/schemas/__test__/fixtures';
  11. import fetchMock from 'fetch-mock';
  12. import ClusterContext, {
  13. ContextProps,
  14. initialValue as contextInitialValue,
  15. } from 'components/contexts/ClusterContext';
  16. import { RootState } from 'redux/interfaces';
  17. import { act } from '@testing-library/react';
  18. import { versionPayload, versionEmptyPayload } from './fixtures';
  19. const clusterName = 'testClusterName';
  20. const schemasAPILatestUrl = `/api/clusters/${clusterName}/schemas/${schemaVersion.subject}/latest`;
  21. const schemasAPIVersionsUrl = `/api/clusters/${clusterName}/schemas/${schemaVersion.subject}/versions`;
  22. const mockHistoryPush = jest.fn();
  23. jest.mock('react-router-dom', () => ({
  24. ...jest.requireActual('react-router-dom'),
  25. useNavigate: () => mockHistoryPush,
  26. }));
  27. const renderComponent = (
  28. initialState: RootState['schemas'] = schemasInitialState,
  29. context: ContextProps = contextInitialValue
  30. ) =>
  31. render(
  32. <WithRoute path={clusterSchemaPath()}>
  33. <ClusterContext.Provider value={context}>
  34. <Details />
  35. </ClusterContext.Provider>
  36. </WithRoute>,
  37. {
  38. initialEntries: [clusterSchemaPath(clusterName, schemaVersion.subject)],
  39. preloadedState: {
  40. schemas: initialState,
  41. },
  42. }
  43. );
  44. describe('Details', () => {
  45. afterEach(() => fetchMock.reset());
  46. describe('fetch failed', () => {
  47. beforeEach(async () => {
  48. const schemasAPILatestMock = fetchMock.getOnce(schemasAPILatestUrl, 404);
  49. const schemasAPIVersionsMock = fetchMock.getOnce(
  50. schemasAPIVersionsUrl,
  51. 404
  52. );
  53. await act(() => {
  54. renderComponent();
  55. });
  56. await waitFor(() => {
  57. expect(schemasAPILatestMock.called()).toBeTruthy();
  58. });
  59. await waitFor(() => {
  60. expect(schemasAPIVersionsMock.called()).toBeTruthy();
  61. });
  62. });
  63. it('handles [Delete schema] click', async () => {
  64. const deleteSchemaMock = fetchMock.deleteOnce(
  65. `/api/clusters/${clusterName}/schemas/${schemaVersion.subject}`,
  66. 200
  67. );
  68. await act(() => {
  69. renderComponent();
  70. });
  71. try {
  72. expect(deleteSchemaMock.called()).toBeTruthy();
  73. expect(mockHistoryPush).toHaveBeenCalledTimes(1);
  74. expect(mockHistoryPush).toHaveBeenCalledWith(
  75. clusterSchemaPath(clusterName)
  76. );
  77. } catch (e) {
  78. expect(deleteSchemaMock.called()).toBeTruthy();
  79. }
  80. });
  81. it('renders pageloader', () => {
  82. expect(screen.getByRole('progressbar')).toBeInTheDocument();
  83. expect(screen.queryByText(schemaVersion.subject)).not.toBeInTheDocument();
  84. expect(screen.queryByText('Edit Schema')).not.toBeInTheDocument();
  85. expect(screen.queryByText('Remove Schema')).not.toBeInTheDocument();
  86. });
  87. });
  88. describe('fetch success', () => {
  89. describe('has schema versions', () => {
  90. beforeEach(async () => {
  91. const schemasAPILatestMock = fetchMock.getOnce(
  92. schemasAPILatestUrl,
  93. schemaVersion
  94. );
  95. const schemasAPIVersionsMock = fetchMock.getOnce(
  96. schemasAPIVersionsUrl,
  97. versionPayload
  98. );
  99. await act(() => {
  100. renderComponent();
  101. });
  102. await waitFor(() => {
  103. expect(schemasAPILatestMock.called()).toBeTruthy();
  104. });
  105. await waitFor(() => {
  106. expect(schemasAPIVersionsMock.called()).toBeTruthy();
  107. });
  108. });
  109. it('renders component with schema info', () => {
  110. expect(screen.getByText('Edit Schema')).toBeInTheDocument();
  111. expect(screen.queryByRole('progressbar')).not.toBeInTheDocument();
  112. expect(screen.getByRole('table')).toBeInTheDocument();
  113. });
  114. });
  115. describe('fetch success schema with non ascii characters', () => {
  116. describe('has schema versions', () => {
  117. beforeEach(async () => {
  118. const schemasAPILatestMock = fetchMock.getOnce(
  119. schemasAPILatestUrl,
  120. schemaVersionWithNonAsciiChars
  121. );
  122. const schemasAPIVersionsMock = fetchMock.getOnce(
  123. schemasAPIVersionsUrl,
  124. versionPayload
  125. );
  126. await act(() => {
  127. renderComponent();
  128. });
  129. await waitFor(() => {
  130. expect(schemasAPILatestMock.called()).toBeTruthy();
  131. });
  132. await waitFor(() => {
  133. expect(schemasAPIVersionsMock.called()).toBeTruthy();
  134. });
  135. });
  136. it('renders component with schema info', () => {
  137. expect(screen.getByText('Edit Schema')).toBeInTheDocument();
  138. expect(screen.queryByRole('progressbar')).not.toBeInTheDocument();
  139. expect(screen.getByRole('table')).toBeInTheDocument();
  140. });
  141. });
  142. });
  143. describe('empty schema versions', () => {
  144. beforeEach(async () => {
  145. const schemasAPILatestMock = fetchMock.getOnce(
  146. schemasAPILatestUrl,
  147. schemaVersion
  148. );
  149. const schemasAPIVersionsMock = fetchMock.getOnce(
  150. schemasAPIVersionsUrl,
  151. versionEmptyPayload
  152. );
  153. await act(() => {
  154. renderComponent();
  155. });
  156. await waitFor(() => {
  157. expect(schemasAPILatestMock.called()).toBeTruthy();
  158. });
  159. await waitFor(() => {
  160. expect(schemasAPIVersionsMock.called()).toBeTruthy();
  161. });
  162. });
  163. // seems like incorrect behaviour
  164. it('renders versions table with 0 items', () => {
  165. expect(screen.getByRole('table')).toBeInTheDocument();
  166. });
  167. });
  168. });
  169. });