Skip to content

Commit

Permalink
Remove Schema code reducers + Fix all the tests for Schema + add TODO…
Browse files Browse the repository at this point in the history
… comments for missing functionalities
  • Loading branch information
Mgrdich committed Apr 14, 2024
1 parent cb8d190 commit c4496ae
Show file tree
Hide file tree
Showing 12 changed files with 262 additions and 420 deletions.
132 changes: 67 additions & 65 deletions frontend/src/components/Schemas/Details/__test__/Details.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,36 @@ import { render, WithRoute } from 'lib/testHelpers';
import { clusterSchemaPath } from 'lib/paths';
import { screen } from '@testing-library/dom';
import {
schemasInitialState,
schemaVersion,
schemaVersionWithNonAsciiChars,
} from 'redux/reducers/schemas/__test__/fixtures';
import fetchMock from 'fetch-mock';
} from 'components/Schemas/Edit/__tests__/fixtures';
import ClusterContext, {
ContextProps,
initialValue as contextInitialValue,
} from 'components/contexts/ClusterContext';
import { RootState } from 'redux/interfaces';
import { act } from '@testing-library/react';
import {
useDeleteSchema,
useGetLatestSchema,
useGetSchemasVersions,
} from 'lib/hooks/api/schemas';

import { versionPayload, versionEmptyPayload } from './fixtures';

const clusterName = 'testClusterName';
const schemasAPILatestUrl = `/api/clusters/${clusterName}/schemas/${schemaVersion.subject}/latest`;
const schemasAPIVersionsUrl = `/api/clusters/${clusterName}/schemas/${schemaVersion.subject}/versions`;

const mockHistoryPush = jest.fn();
jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useNavigate: () => mockHistoryPush,
}));

const renderComponent = (
initialState: RootState['schemas'] = schemasInitialState,
context: ContextProps = contextInitialValue
) =>
jest.mock('lib/hooks/api/schemas', () => ({
useGetSchemasVersions: jest.fn(),
useGetLatestSchema: jest.fn(),
useDeleteSchema: jest.fn(),
}));

const renderComponent = (context: ContextProps = contextInitialValue) =>
render(
<WithRoute path={clusterSchemaPath()}>
<ClusterContext.Provider value={context}>
Expand All @@ -40,27 +42,33 @@ const renderComponent = (
</WithRoute>,
{
initialEntries: [clusterSchemaPath(clusterName, schemaVersion.subject)],
preloadedState: {
schemas: initialState,
},
}
);

describe('Details', () => {
afterEach(() => fetchMock.reset());
const deleteMockfn = jest.fn();
beforeEach(() => {
deleteMockfn.mockClear();

// TODO test case should be added for this
(useDeleteSchema as jest.Mock).mockImplementation(() => ({
mutateAsync: deleteMockfn,
}));
});

describe('fetch failed', () => {
it('renders pageloader', async () => {
const schemasAPILatestMock = fetchMock.getOnce(schemasAPILatestUrl, 404);
const schemasAPIVersionsMock = fetchMock.getOnce(
schemasAPIVersionsUrl,
404
);
await act(() => {
renderComponent();
});
expect(schemasAPILatestMock.called(schemasAPILatestUrl)).toBeTruthy();
expect(schemasAPIVersionsMock.called(schemasAPIVersionsUrl)).toBeTruthy();
it('renders page loader', async () => {
(useGetSchemasVersions as jest.Mock).mockImplementation(() => ({
data: undefined,
isFetching: false,
isError: false,
}));
(useGetLatestSchema as jest.Mock).mockImplementation(() => ({
data: undefined,
isFetching: false,
isError: true,
}));
renderComponent();
expect(screen.getByRole('progressbar')).toBeInTheDocument();
expect(screen.queryByText(schemaVersion.subject)).not.toBeInTheDocument();
expect(screen.queryByText('Edit Schema')).not.toBeInTheDocument();
Expand All @@ -71,19 +79,17 @@ describe('Details', () => {
describe('fetch success', () => {
describe('has schema versions', () => {
it('renders component with schema info', async () => {
const schemasAPILatestMock = fetchMock.getOnce(
schemasAPILatestUrl,
schemaVersion
);
const schemasAPIVersionsMock = fetchMock.getOnce(
schemasAPIVersionsUrl,
versionPayload
);
await act(() => {
renderComponent();
});
expect(schemasAPILatestMock.called()).toBeTruthy();
expect(schemasAPIVersionsMock.called()).toBeTruthy();
(useGetSchemasVersions as jest.Mock).mockImplementation(() => ({
data: versionPayload,
isFetching: false,
isError: false,
}));
(useGetLatestSchema as jest.Mock).mockImplementation(() => ({
data: useGetSchemasVersions,
isFetching: false,
isError: false,
}));
renderComponent();
expect(screen.getByText('Edit Schema')).toBeInTheDocument();
expect(screen.queryByRole('progressbar')).not.toBeInTheDocument();
expect(screen.getByRole('table')).toBeInTheDocument();
Expand All @@ -93,19 +99,17 @@ describe('Details', () => {
describe('fetch success schema with non ascii characters', () => {
describe('has schema versions', () => {
it('renders component with schema info', async () => {
const schemasAPILatestMock = fetchMock.getOnce(
schemasAPILatestUrl,
schemaVersionWithNonAsciiChars
);
const schemasAPIVersionsMock = fetchMock.getOnce(
schemasAPIVersionsUrl,
versionPayload
);
await act(() => {
renderComponent();
});
expect(schemasAPILatestMock.called()).toBeTruthy();
expect(schemasAPIVersionsMock.called()).toBeTruthy();
(useGetSchemasVersions as jest.Mock).mockImplementation(() => ({
data: versionPayload,
isFetching: false,
isError: false,
}));
(useGetLatestSchema as jest.Mock).mockImplementation(() => ({
data: schemaVersionWithNonAsciiChars,
isFetching: false,
isError: false,
}));
renderComponent();
expect(screen.getByText('Edit Schema')).toBeInTheDocument();
expect(screen.queryByRole('progressbar')).not.toBeInTheDocument();
expect(screen.getByRole('table')).toBeInTheDocument();
Expand All @@ -115,19 +119,17 @@ describe('Details', () => {

describe('empty schema versions', () => {
beforeEach(async () => {
const schemasAPILatestMock = fetchMock.getOnce(
schemasAPILatestUrl,
schemaVersion
);
const schemasAPIVersionsMock = fetchMock.getOnce(
schemasAPIVersionsUrl,
versionEmptyPayload
);
await act(() => {
renderComponent();
});
expect(schemasAPILatestMock.called()).toBeTruthy();
expect(schemasAPIVersionsMock.called()).toBeTruthy();
(useGetSchemasVersions as jest.Mock).mockImplementation(() => ({
data: versionEmptyPayload,
isFetching: false,
isError: false,
}));
(useGetLatestSchema as jest.Mock).mockImplementation(() => ({
data: schemaVersionWithNonAsciiChars,
isFetching: false,
isError: false,
}));
renderComponent();
});

// seems like incorrect behaviour
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
schemaVersion1,
schemaVersion2,
schemaVersionWithNonAsciiChars,
} from 'redux/reducers/schemas/__test__/fixtures';
} from 'components/Schemas/Edit/__tests__/fixtures';

export const versionPayload = [
schemaVersion1,
Expand Down
93 changes: 48 additions & 45 deletions frontend/src/components/Schemas/Diff/__test__/Diff.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import React from 'react';
import Diff, { DiffProps } from 'components/Schemas/Diff/Diff';
import Diff from 'components/Schemas/Diff/Diff';
import { render, WithRoute } from 'lib/testHelpers';
import { screen } from '@testing-library/react';
import { clusterSchemaComparePath } from 'lib/paths';
import userEvent from '@testing-library/user-event';
import { useGetSchemasVersions } from 'lib/hooks/api/schemas';

import { versions } from './fixtures';

Expand All @@ -14,9 +15,12 @@ const defaultPathName = clusterSchemaComparePath(
defaultSubject
);

jest.mock('lib/hooks/api/schemas', () => ({
useGetSchemasVersions: jest.fn(),
}));

describe('Diff', () => {
const setupComponent = (
props: DiffProps,
searchQuery: { rightVersion?: string; leftVersion?: string } = {}
) => {
let pathname = defaultPathName;
Expand All @@ -32,10 +36,7 @@ describe('Diff', () => {

return render(
<WithRoute path={clusterSchemaComparePath()}>
<Diff
versions={props.versions}
areVersionsFetched={props.areVersionsFetched}
/>
<Diff />
</WithRoute>,
{
initialEntries: [pathname],
Expand All @@ -45,26 +46,25 @@ describe('Diff', () => {

describe('Container', () => {
it('renders view', () => {
setupComponent({
areVersionsFetched: true,
versions,
});
expect(screen.getAllByText('Version 3').length).toEqual(4);
(useGetSchemasVersions as jest.Mock).mockImplementation(() => ({
data: versions,
isFetching: false,
isError: false,
}));
setupComponent();
// TODO make sure this case it correct
expect(screen.getAllByText('Version 3').length).toEqual(2);
});
});

describe('View', () => {
setupComponent({
areVersionsFetched: true,
versions,
});
});
describe('when page with schema versions is loading', () => {
beforeAll(() => {
setupComponent({
areVersionsFetched: false,
versions: [],
});
(useGetSchemasVersions as jest.Mock).mockImplementation(() => ({
data: undefined,
isFetching: true,
isError: false,
}));
setupComponent();
});
it('renders PageLoader', () => {
expect(screen.getByRole('progressbar')).toBeInTheDocument();
Expand All @@ -73,10 +73,12 @@ describe('Diff', () => {

describe('when schema versions are loaded and no specified versions in path', () => {
beforeEach(() => {
setupComponent({
areVersionsFetched: true,
versions,
});
(useGetSchemasVersions as jest.Mock).mockImplementation(() => ({
data: versions,
isFetching: false,
isError: false,
}));
setupComponent();
});

it('renders all options', () => {
Expand All @@ -94,15 +96,15 @@ describe('Diff', () => {
expect(select).toHaveTextContent(versions[0].version);
});
});

describe('when schema versions are loaded and two versions in path', () => {
beforeEach(() => {
setupComponent(
{
areVersionsFetched: true,
versions,
},
{ leftVersion: '1', rightVersion: '2' }
);
(useGetSchemasVersions as jest.Mock).mockImplementation(() => ({
data: versions,
isFetching: false,
isError: false,
}));
setupComponent({ leftVersion: '1', rightVersion: '2' });
});

it('renders left select with version 1', () => {
Expand All @@ -120,15 +122,14 @@ describe('Diff', () => {

describe('when schema versions are loaded and only one versions in path', () => {
beforeEach(() => {
setupComponent(
{
areVersionsFetched: true,
versions,
},
{
leftVersion: '1',
}
);
(useGetSchemasVersions as jest.Mock).mockImplementation(() => ({
data: versions,
isFetching: false,
isError: false,
}));
setupComponent({
leftVersion: '1',
});
});

it('renders left select with version 1', () => {
Expand All @@ -146,10 +147,12 @@ describe('Diff', () => {

describe('Back button', () => {
beforeEach(() => {
setupComponent({
areVersionsFetched: true,
versions,
});
(useGetSchemasVersions as jest.Mock).mockImplementation(() => ({
data: versions,
isFetching: false,
isError: false,
}));
setupComponent();
});

it('back button is appear', () => {
Expand Down
Loading

0 comments on commit c4496ae

Please sign in to comment.