Skip to content

Commit c4496ae

Browse files
committed
Remove Schema code reducers + Fix all the tests for Schema + add TODO comments for missing functionalities
1 parent cb8d190 commit c4496ae

File tree

12 files changed

+262
-420
lines changed

12 files changed

+262
-420
lines changed

frontend/src/components/Schemas/Details/__test__/Details.spec.tsx

Lines changed: 67 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,36 @@ import { render, WithRoute } from 'lib/testHelpers';
44
import { clusterSchemaPath } from 'lib/paths';
55
import { screen } from '@testing-library/dom';
66
import {
7-
schemasInitialState,
87
schemaVersion,
98
schemaVersionWithNonAsciiChars,
10-
} from 'redux/reducers/schemas/__test__/fixtures';
11-
import fetchMock from 'fetch-mock';
9+
} from 'components/Schemas/Edit/__tests__/fixtures';
1210
import ClusterContext, {
1311
ContextProps,
1412
initialValue as contextInitialValue,
1513
} from 'components/contexts/ClusterContext';
16-
import { RootState } from 'redux/interfaces';
17-
import { act } from '@testing-library/react';
14+
import {
15+
useDeleteSchema,
16+
useGetLatestSchema,
17+
useGetSchemasVersions,
18+
} from 'lib/hooks/api/schemas';
1819

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

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

2524
const mockHistoryPush = jest.fn();
2625
jest.mock('react-router-dom', () => ({
2726
...jest.requireActual('react-router-dom'),
2827
useNavigate: () => mockHistoryPush,
2928
}));
3029

31-
const renderComponent = (
32-
initialState: RootState['schemas'] = schemasInitialState,
33-
context: ContextProps = contextInitialValue
34-
) =>
30+
jest.mock('lib/hooks/api/schemas', () => ({
31+
useGetSchemasVersions: jest.fn(),
32+
useGetLatestSchema: jest.fn(),
33+
useDeleteSchema: jest.fn(),
34+
}));
35+
36+
const renderComponent = (context: ContextProps = contextInitialValue) =>
3537
render(
3638
<WithRoute path={clusterSchemaPath()}>
3739
<ClusterContext.Provider value={context}>
@@ -40,27 +42,33 @@ const renderComponent = (
4042
</WithRoute>,
4143
{
4244
initialEntries: [clusterSchemaPath(clusterName, schemaVersion.subject)],
43-
preloadedState: {
44-
schemas: initialState,
45-
},
4645
}
4746
);
4847

4948
describe('Details', () => {
50-
afterEach(() => fetchMock.reset());
49+
const deleteMockfn = jest.fn();
50+
beforeEach(() => {
51+
deleteMockfn.mockClear();
52+
53+
// TODO test case should be added for this
54+
(useDeleteSchema as jest.Mock).mockImplementation(() => ({
55+
mutateAsync: deleteMockfn,
56+
}));
57+
});
5158

5259
describe('fetch failed', () => {
53-
it('renders pageloader', async () => {
54-
const schemasAPILatestMock = fetchMock.getOnce(schemasAPILatestUrl, 404);
55-
const schemasAPIVersionsMock = fetchMock.getOnce(
56-
schemasAPIVersionsUrl,
57-
404
58-
);
59-
await act(() => {
60-
renderComponent();
61-
});
62-
expect(schemasAPILatestMock.called(schemasAPILatestUrl)).toBeTruthy();
63-
expect(schemasAPIVersionsMock.called(schemasAPIVersionsUrl)).toBeTruthy();
60+
it('renders page loader', async () => {
61+
(useGetSchemasVersions as jest.Mock).mockImplementation(() => ({
62+
data: undefined,
63+
isFetching: false,
64+
isError: false,
65+
}));
66+
(useGetLatestSchema as jest.Mock).mockImplementation(() => ({
67+
data: undefined,
68+
isFetching: false,
69+
isError: true,
70+
}));
71+
renderComponent();
6472
expect(screen.getByRole('progressbar')).toBeInTheDocument();
6573
expect(screen.queryByText(schemaVersion.subject)).not.toBeInTheDocument();
6674
expect(screen.queryByText('Edit Schema')).not.toBeInTheDocument();
@@ -71,19 +79,17 @@ describe('Details', () => {
7179
describe('fetch success', () => {
7280
describe('has schema versions', () => {
7381
it('renders component with schema info', async () => {
74-
const schemasAPILatestMock = fetchMock.getOnce(
75-
schemasAPILatestUrl,
76-
schemaVersion
77-
);
78-
const schemasAPIVersionsMock = fetchMock.getOnce(
79-
schemasAPIVersionsUrl,
80-
versionPayload
81-
);
82-
await act(() => {
83-
renderComponent();
84-
});
85-
expect(schemasAPILatestMock.called()).toBeTruthy();
86-
expect(schemasAPIVersionsMock.called()).toBeTruthy();
82+
(useGetSchemasVersions as jest.Mock).mockImplementation(() => ({
83+
data: versionPayload,
84+
isFetching: false,
85+
isError: false,
86+
}));
87+
(useGetLatestSchema as jest.Mock).mockImplementation(() => ({
88+
data: useGetSchemasVersions,
89+
isFetching: false,
90+
isError: false,
91+
}));
92+
renderComponent();
8793
expect(screen.getByText('Edit Schema')).toBeInTheDocument();
8894
expect(screen.queryByRole('progressbar')).not.toBeInTheDocument();
8995
expect(screen.getByRole('table')).toBeInTheDocument();
@@ -93,19 +99,17 @@ describe('Details', () => {
9399
describe('fetch success schema with non ascii characters', () => {
94100
describe('has schema versions', () => {
95101
it('renders component with schema info', async () => {
96-
const schemasAPILatestMock = fetchMock.getOnce(
97-
schemasAPILatestUrl,
98-
schemaVersionWithNonAsciiChars
99-
);
100-
const schemasAPIVersionsMock = fetchMock.getOnce(
101-
schemasAPIVersionsUrl,
102-
versionPayload
103-
);
104-
await act(() => {
105-
renderComponent();
106-
});
107-
expect(schemasAPILatestMock.called()).toBeTruthy();
108-
expect(schemasAPIVersionsMock.called()).toBeTruthy();
102+
(useGetSchemasVersions as jest.Mock).mockImplementation(() => ({
103+
data: versionPayload,
104+
isFetching: false,
105+
isError: false,
106+
}));
107+
(useGetLatestSchema as jest.Mock).mockImplementation(() => ({
108+
data: schemaVersionWithNonAsciiChars,
109+
isFetching: false,
110+
isError: false,
111+
}));
112+
renderComponent();
109113
expect(screen.getByText('Edit Schema')).toBeInTheDocument();
110114
expect(screen.queryByRole('progressbar')).not.toBeInTheDocument();
111115
expect(screen.getByRole('table')).toBeInTheDocument();
@@ -115,19 +119,17 @@ describe('Details', () => {
115119

116120
describe('empty schema versions', () => {
117121
beforeEach(async () => {
118-
const schemasAPILatestMock = fetchMock.getOnce(
119-
schemasAPILatestUrl,
120-
schemaVersion
121-
);
122-
const schemasAPIVersionsMock = fetchMock.getOnce(
123-
schemasAPIVersionsUrl,
124-
versionEmptyPayload
125-
);
126-
await act(() => {
127-
renderComponent();
128-
});
129-
expect(schemasAPILatestMock.called()).toBeTruthy();
130-
expect(schemasAPIVersionsMock.called()).toBeTruthy();
122+
(useGetSchemasVersions as jest.Mock).mockImplementation(() => ({
123+
data: versionEmptyPayload,
124+
isFetching: false,
125+
isError: false,
126+
}));
127+
(useGetLatestSchema as jest.Mock).mockImplementation(() => ({
128+
data: schemaVersionWithNonAsciiChars,
129+
isFetching: false,
130+
isError: false,
131+
}));
132+
renderComponent();
131133
});
132134

133135
// seems like incorrect behaviour

frontend/src/components/Schemas/Details/__test__/fixtures.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
schemaVersion1,
44
schemaVersion2,
55
schemaVersionWithNonAsciiChars,
6-
} from 'redux/reducers/schemas/__test__/fixtures';
6+
} from 'components/Schemas/Edit/__tests__/fixtures';
77

88
export const versionPayload = [
99
schemaVersion1,

frontend/src/components/Schemas/Diff/__test__/Diff.spec.tsx

Lines changed: 48 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import React from 'react';
2-
import Diff, { DiffProps } from 'components/Schemas/Diff/Diff';
2+
import Diff from 'components/Schemas/Diff/Diff';
33
import { render, WithRoute } from 'lib/testHelpers';
44
import { screen } from '@testing-library/react';
55
import { clusterSchemaComparePath } from 'lib/paths';
66
import userEvent from '@testing-library/user-event';
7+
import { useGetSchemasVersions } from 'lib/hooks/api/schemas';
78

89
import { versions } from './fixtures';
910

@@ -14,9 +15,12 @@ const defaultPathName = clusterSchemaComparePath(
1415
defaultSubject
1516
);
1617

18+
jest.mock('lib/hooks/api/schemas', () => ({
19+
useGetSchemasVersions: jest.fn(),
20+
}));
21+
1722
describe('Diff', () => {
1823
const setupComponent = (
19-
props: DiffProps,
2024
searchQuery: { rightVersion?: string; leftVersion?: string } = {}
2125
) => {
2226
let pathname = defaultPathName;
@@ -32,10 +36,7 @@ describe('Diff', () => {
3236

3337
return render(
3438
<WithRoute path={clusterSchemaComparePath()}>
35-
<Diff
36-
versions={props.versions}
37-
areVersionsFetched={props.areVersionsFetched}
38-
/>
39+
<Diff />
3940
</WithRoute>,
4041
{
4142
initialEntries: [pathname],
@@ -45,26 +46,25 @@ describe('Diff', () => {
4546

4647
describe('Container', () => {
4748
it('renders view', () => {
48-
setupComponent({
49-
areVersionsFetched: true,
50-
versions,
51-
});
52-
expect(screen.getAllByText('Version 3').length).toEqual(4);
49+
(useGetSchemasVersions as jest.Mock).mockImplementation(() => ({
50+
data: versions,
51+
isFetching: false,
52+
isError: false,
53+
}));
54+
setupComponent();
55+
// TODO make sure this case it correct
56+
expect(screen.getAllByText('Version 3').length).toEqual(2);
5357
});
5458
});
5559

56-
describe('View', () => {
57-
setupComponent({
58-
areVersionsFetched: true,
59-
versions,
60-
});
61-
});
6260
describe('when page with schema versions is loading', () => {
6361
beforeAll(() => {
64-
setupComponent({
65-
areVersionsFetched: false,
66-
versions: [],
67-
});
62+
(useGetSchemasVersions as jest.Mock).mockImplementation(() => ({
63+
data: undefined,
64+
isFetching: true,
65+
isError: false,
66+
}));
67+
setupComponent();
6868
});
6969
it('renders PageLoader', () => {
7070
expect(screen.getByRole('progressbar')).toBeInTheDocument();
@@ -73,10 +73,12 @@ describe('Diff', () => {
7373

7474
describe('when schema versions are loaded and no specified versions in path', () => {
7575
beforeEach(() => {
76-
setupComponent({
77-
areVersionsFetched: true,
78-
versions,
79-
});
76+
(useGetSchemasVersions as jest.Mock).mockImplementation(() => ({
77+
data: versions,
78+
isFetching: false,
79+
isError: false,
80+
}));
81+
setupComponent();
8082
});
8183

8284
it('renders all options', () => {
@@ -94,15 +96,15 @@ describe('Diff', () => {
9496
expect(select).toHaveTextContent(versions[0].version);
9597
});
9698
});
99+
97100
describe('when schema versions are loaded and two versions in path', () => {
98101
beforeEach(() => {
99-
setupComponent(
100-
{
101-
areVersionsFetched: true,
102-
versions,
103-
},
104-
{ leftVersion: '1', rightVersion: '2' }
105-
);
102+
(useGetSchemasVersions as jest.Mock).mockImplementation(() => ({
103+
data: versions,
104+
isFetching: false,
105+
isError: false,
106+
}));
107+
setupComponent({ leftVersion: '1', rightVersion: '2' });
106108
});
107109

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

121123
describe('when schema versions are loaded and only one versions in path', () => {
122124
beforeEach(() => {
123-
setupComponent(
124-
{
125-
areVersionsFetched: true,
126-
versions,
127-
},
128-
{
129-
leftVersion: '1',
130-
}
131-
);
125+
(useGetSchemasVersions as jest.Mock).mockImplementation(() => ({
126+
data: versions,
127+
isFetching: false,
128+
isError: false,
129+
}));
130+
setupComponent({
131+
leftVersion: '1',
132+
});
132133
});
133134

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

147148
describe('Back button', () => {
148149
beforeEach(() => {
149-
setupComponent({
150-
areVersionsFetched: true,
151-
versions,
152-
});
150+
(useGetSchemasVersions as jest.Mock).mockImplementation(() => ({
151+
data: versions,
152+
isFetching: false,
153+
isError: false,
154+
}));
155+
setupComponent();
153156
});
154157

155158
it('back button is appear', () => {

0 commit comments

Comments
 (0)