({
...jest.requireActual('react-router-dom'),
useNavigate: () => mockedUsedNavigate,
}));
+jest.mock('lib/hooks/api/schemas', () => ({
+ useGetSchemas: jest.fn(),
+}));
+
+jest.mock(
+ 'components/Schemas/List/GlobalSchemaSelector/GlobalSchemaSelector',
+ () => () => {GlobalSchemaSelectorText}
+);
+
const clusterName = 'testClusterName';
-const schemasAPIUrl = `/api/clusters/${clusterName}/schemas?page=1&perPage=25`;
-const schemasAPICompabilityUrl = `/api/clusters/${clusterName}/schemas/compatibility`;
-const renderComponent = (
- initialState: RootState['schemas'] = schemasInitialState,
- context: ContextProps = contextInitialValue
-) =>
+const renderComponent = (context: ContextProps = contextInitialValue) =>
render(
@@ -41,29 +44,17 @@ const renderComponent = (
,
{
initialEntries: [clusterSchemasPath(clusterName)],
- preloadedState: {
- schemas: initialState,
- },
}
);
describe('List', () => {
- afterEach(() => {
- fetchMock.reset();
- });
-
describe('fetch error', () => {
it('shows progressbar', async () => {
- const fetchSchemasMock = fetchMock.getOnce(schemasAPIUrl, 404);
- const fetchCompabilityMock = fetchMock.getOnce(
- schemasAPICompabilityUrl,
- 404
- );
- await act(() => {
- renderComponent();
- });
- expect(fetchSchemasMock.called()).toBeTruthy();
- expect(fetchCompabilityMock.called()).toBeTruthy();
+ (useGetSchemas as jest.Mock).mockImplementation(() => ({
+ data: {},
+ isError: true,
+ }));
+ renderComponent();
expect(screen.getByRole('progressbar')).toBeInTheDocument();
});
});
@@ -71,19 +62,12 @@ describe('List', () => {
describe('fetch success', () => {
describe('responded without schemas', () => {
beforeEach(async () => {
- const fetchSchemasMock = fetchMock.getOnce(
- schemasAPIUrl,
- schemasEmptyPayload
- );
- const fetchCompabilityMock = fetchMock.getOnce(
- schemasAPICompabilityUrl,
- 200
- );
- await act(() => {
- renderComponent();
- });
- expect(fetchSchemasMock.called()).toBeTruthy();
- expect(fetchCompabilityMock.called()).toBeTruthy();
+ (useGetSchemas as jest.Mock).mockImplementation(() => ({
+ data: schemasEmptyPayload,
+ isFetching: false,
+ isError: false,
+ }));
+ renderComponent();
});
it('renders empty table', () => {
expect(screen.getByText('No schemas found')).toBeInTheDocument();
@@ -91,19 +75,12 @@ describe('List', () => {
});
describe('responded with schemas', () => {
beforeEach(async () => {
- const fetchSchemasMock = fetchMock.getOnce(
- schemasAPIUrl,
- schemasPayload
- );
- const fetchCompabilityMock = fetchMock.getOnce(
- schemasAPICompabilityUrl,
- 200
- );
- await act(() => {
- renderComponent(schemasFulfilledState);
- });
- expect(fetchSchemasMock.called()).toBeTruthy();
- expect(fetchCompabilityMock.called()).toBeTruthy();
+ (useGetSchemas as jest.Mock).mockImplementation(() => ({
+ data: schemasPayload,
+ isFetching: false,
+ isError: false,
+ }));
+ renderComponent();
});
it('renders list', () => {
expect(screen.getByText(schemaVersion1.subject)).toBeInTheDocument();
@@ -125,22 +102,31 @@ describe('List', () => {
describe('responded with readonly cluster schemas', () => {
beforeEach(async () => {
- const fetchSchemasMock = fetchMock.getOnce(
- schemasAPIUrl,
- schemasPayload
- );
- fetchMock.getOnce(schemasAPICompabilityUrl, 200);
- await act(() => {
- renderComponent(schemasFulfilledState, {
- ...contextInitialValue,
- isReadOnly: true,
- });
+ (useGetSchemas as jest.Mock).mockImplementation(() => ({
+ data: schemasPayload,
+ isFetching: false,
+ isError: false,
+ }));
+ renderComponent({
+ ...contextInitialValue,
+ isReadOnly: true,
});
- expect(fetchSchemasMock.called()).toBeTruthy();
});
it('does not render Create Schema button', () => {
expect(screen.queryByText('Create Schema')).not.toBeInTheDocument();
});
});
});
+
+ describe('check the compatibility layer', () => {
+ it('should check if the compatibility layer component is being shown', () => {
+ (useGetSchemas as jest.Mock).mockImplementation(() => ({
+ data: {},
+ isError: false,
+ isFetching: false,
+ }));
+ renderComponent();
+ expect(screen.getByText(GlobalSchemaSelectorText)).toBeInTheDocument();
+ });
+ });
});
diff --git a/frontend/src/components/Schemas/List/__test__/fixtures.ts b/frontend/src/components/Schemas/List/__test__/fixtures.ts
index 992882d33..e9a357702 100644
--- a/frontend/src/components/Schemas/List/__test__/fixtures.ts
+++ b/frontend/src/components/Schemas/List/__test__/fixtures.ts
@@ -2,7 +2,7 @@ import {
schemaVersion1,
schemaVersion2,
schemaVersionWithNonAsciiChars,
-} from 'redux/reducers/schemas/__test__/fixtures';
+} from 'components/Schemas/Edit/__tests__/fixtures';
const schemas = [
schemaVersion1,
diff --git a/frontend/src/components/Schemas/__test__/Schemas.spec.tsx b/frontend/src/components/Schemas/__test__/Schemas.spec.tsx
index 604a21d0b..d9d97fcc8 100644
--- a/frontend/src/components/Schemas/__test__/Schemas.spec.tsx
+++ b/frontend/src/components/Schemas/__test__/Schemas.spec.tsx
@@ -10,7 +10,7 @@ import {
} from 'lib/paths';
import { screen, waitFor } from '@testing-library/dom';
import fetchMock from 'fetch-mock';
-import { schemaVersion } from 'redux/reducers/schemas/__test__/fixtures';
+import { schemaVersion } from 'components/Schemas/Edit/__tests__/fixtures';
const renderComponent = (pathname: string) =>
render(
diff --git a/frontend/src/redux/reducers/index.ts b/frontend/src/redux/reducers/index.ts
index 6385e0710..316ba00da 100644
--- a/frontend/src/redux/reducers/index.ts
+++ b/frontend/src/redux/reducers/index.ts
@@ -1,8 +1,6 @@
import { combineReducers } from '@reduxjs/toolkit';
import loader from 'redux/reducers/loader/loaderSlice';
-import schemas from 'redux/reducers/schemas/schemasSlice';
export default combineReducers({
loader,
- schemas,
});
diff --git a/frontend/src/redux/reducers/schemas/schemasSlice.ts b/frontend/src/redux/reducers/schemas/schemasSlice.ts
deleted file mode 100644
index 018775ced..000000000
--- a/frontend/src/redux/reducers/schemas/schemasSlice.ts
+++ /dev/null
@@ -1,147 +0,0 @@
-import {
- createAsyncThunk,
- createEntityAdapter,
- createSelector,
- createSlice,
-} from '@reduxjs/toolkit';
-import {
- SchemaSubject,
- SchemaSubjectsResponse,
- GetSchemasRequest,
- GetLatestSchemaRequest,
-} from 'generated-sources';
-import { schemasApiClient } from 'lib/api';
-import { AsyncRequestStatus } from 'lib/constants';
-import { getResponse, showServerError } from 'lib/errorHandling';
-import { ClusterName, RootState } from 'redux/interfaces';
-import { createFetchingSelector } from 'redux/reducers/loader/selectors';
-
-export const SCHEMA_LATEST_FETCH_ACTION = 'schemas/latest/fetch';
-export const fetchLatestSchema = createAsyncThunk<
- SchemaSubject,
- GetLatestSchemaRequest
->(SCHEMA_LATEST_FETCH_ACTION, async (schemaParams, { rejectWithValue }) => {
- try {
- return await schemasApiClient.getLatestSchema(schemaParams);
- } catch (error) {
- showServerError(error as Response);
- return rejectWithValue(await getResponse(error as Response));
- }
-});
-
-export const SCHEMAS_FETCH_ACTION = 'schemas/fetch';
-export const fetchSchemas = createAsyncThunk<
- SchemaSubjectsResponse,
- GetSchemasRequest
->(
- SCHEMAS_FETCH_ACTION,
- async ({ clusterName, page, perPage, search }, { rejectWithValue }) => {
- try {
- return await schemasApiClient.getSchemas({
- clusterName,
- page,
- perPage,
- search: search || undefined,
- });
- } catch (error) {
- showServerError(error as Response);
- return rejectWithValue(await getResponse(error as Response));
- }
- }
-);
-
-export const SCHEMAS_VERSIONS_FETCH_ACTION = 'schemas/versions/fetch';
-export const fetchSchemaVersions = createAsyncThunk<
- SchemaSubject[],
- { clusterName: ClusterName; subject: SchemaSubject['subject'] }
->(
- SCHEMAS_VERSIONS_FETCH_ACTION,
- async ({ clusterName, subject }, { rejectWithValue }) => {
- try {
- return await schemasApiClient.getAllVersionsBySubject({
- clusterName,
- subject,
- });
- } catch (error) {
- showServerError(error as Response);
- return rejectWithValue(await getResponse(error as Response));
- }
- }
-);
-
-const schemaVersionsAdapter = createEntityAdapter({
- selectId: ({ id }) => id,
- sortComparer: (a, b) => b.id - a.id,
-});
-const schemasAdapter = createEntityAdapter({
- selectId: ({ subject }) => subject,
-});
-
-const SCHEMAS_PAGE_COUNT = 1;
-
-const initialState = {
- totalPages: SCHEMAS_PAGE_COUNT,
- ...schemasAdapter.getInitialState(),
- versions: {
- ...schemaVersionsAdapter.getInitialState(),
- latest: null,
- },
-};
-
-const schemasSlice = createSlice({
- name: 'schemas',
- initialState,
- reducers: {
- schemaAdded: schemasAdapter.addOne,
- schemaUpdated: schemasAdapter.upsertOne,
- },
- extraReducers: (builder) => {
- builder.addCase(fetchSchemas.fulfilled, (state, { payload }) => {
- state.totalPages = payload.pageCount || SCHEMAS_PAGE_COUNT;
- schemasAdapter.setAll(state, payload.schemas || []);
- });
- builder.addCase(fetchLatestSchema.fulfilled, (state, { payload }) => {
- state.versions.latest = payload;
- });
- builder.addCase(fetchSchemaVersions.fulfilled, (state, { payload }) => {
- schemaVersionsAdapter.setAll(state.versions, payload);
- });
- },
-});
-
-export const { selectAll: selectAllSchemas } =
- schemasAdapter.getSelectors((state) => state.schemas);
-
-export const { selectAll: selectAllSchemaVersions } =
- schemaVersionsAdapter.getSelectors(
- (state) => state.schemas.versions
- );
-
-const getSchemaVersions = (state: RootState) => state.schemas.versions;
-export const getSchemaLatest = createSelector(
- getSchemaVersions,
- (state) => state.latest
-);
-
-export const { schemaAdded, schemaUpdated } = schemasSlice.actions;
-
-export const getAreSchemasFulfilled = createSelector(
- createFetchingSelector(SCHEMAS_FETCH_ACTION),
- (status) => status === AsyncRequestStatus.fulfilled
-);
-
-export const getAreSchemaLatestFulfilled = createSelector(
- createFetchingSelector(SCHEMA_LATEST_FETCH_ACTION),
- (status) => status === AsyncRequestStatus.fulfilled
-);
-export const getAreSchemaLatestRejected = createSelector(
- createFetchingSelector(SCHEMA_LATEST_FETCH_ACTION),
- (status) => status === AsyncRequestStatus.rejected
-);
-
-export const getAreSchemaVersionsFulfilled = createSelector(
- createFetchingSelector(SCHEMAS_VERSIONS_FETCH_ACTION),
- (status) => status === AsyncRequestStatus.fulfilled
-);
-
-export default schemasSlice.reducer;