Skip to content

Commit 0c53948

Browse files
committed
Add hook tests suites for schemas
1 parent bd4d4fc commit 0c53948

File tree

4 files changed

+169
-18
lines changed

4 files changed

+169
-18
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ const Details: React.FC = () => {
4646
subject,
4747
});
4848

49-
const { mutateAsync: deleteSchema } = useDeleteSchema(clusterName);
49+
const { mutateAsync: deleteSchema } = useDeleteSchema({
50+
clusterName,
51+
subject,
52+
});
5053

5154
const columns = React.useMemo(
5255
() => [
@@ -58,9 +61,7 @@ const Details: React.FC = () => {
5861
);
5962

6063
const deleteHandler = async () => {
61-
await deleteSchema({
62-
subject,
63-
});
64+
await deleteSchema();
6465
navigate('../');
6566
};
6667

frontend/src/components/Schemas/Edit/Form.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const Form: React.FC<FormProps> = ({ schema }) => {
3838
const { clusterName, subject } = useAppParams<ClusterSubjectParam>();
3939
const { mutateAsync: createSchema } = useCreateSchema(clusterName);
4040
const { mutateAsync: updateCompatibilityLayer } =
41-
useUpdateSchemaCompatibilityLayer(clusterName);
41+
useUpdateSchemaCompatibilityLayer({ clusterName, subject });
4242

4343
const formatedSchema = React.useMemo(() => {
4444
return schema?.schemaType === SchemaType.PROTOBUF
@@ -75,7 +75,6 @@ const Form: React.FC<FormProps> = ({ schema }) => {
7575
if (dirtyFields.compatibilityLevel) {
7676
await updateCompatibilityLayer({
7777
...schema,
78-
subject,
7978
compatibilityLevel: {
8079
compatibility: props.compatibilityLevel,
8180
},
Lines changed: 145 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,148 @@
1-
import { expectQueryWorks, renderQueryHook } from 'lib/testHelpers';
1+
import {
2+
expectQueryWorks,
3+
renderQueryHook,
4+
TestQueryClientProvider,
5+
} from 'lib/testHelpers';
6+
import fetchMock from 'fetch-mock';
7+
import * as hooks from 'lib/hooks/api/schemas';
8+
import { act } from 'react-dom/test-utils';
9+
import { renderHook, waitFor } from '@testing-library/react';
10+
import { CompatibilityLevelCompatibilityEnum } from 'generated-sources';
11+
import { schemaVersion } from 'components/Schemas/Edit/__tests__/fixtures';
12+
import {
13+
jsonSchema,
14+
versionPayload,
15+
} from 'components/Schemas/Details/__test__/fixtures';
16+
import { schemasPayload } from 'components/Schemas/List/__test__/fixtures';
217

3-
// TODO should be written
4-
describe('schema hooks', () => {
5-
it('should ', () => {
6-
console.log(expectQueryWorks, renderQueryHook);
18+
const clusterName = 'test-cluster';
19+
const { subject } = schemaVersion;
20+
21+
const schemasAPIUrl = `/api/clusters/${clusterName}/schemas`;
22+
const schemasWithSubjectAPIUrl = `${schemasAPIUrl}/${subject}`;
23+
const schemasAPILatestUrl = `${schemasWithSubjectAPIUrl}/latest`;
24+
const schemasAPIVersionsUrl = `${schemasWithSubjectAPIUrl}/versions`;
25+
const schemaCompatibilityUrl = `${schemasAPIUrl}/compatibility`;
26+
const schemaCompatibilityWithSubjectUrl = `${schemasWithSubjectAPIUrl}/compatibility`;
27+
28+
describe('Schema hooks', () => {
29+
beforeEach(() => fetchMock.restore());
30+
31+
describe('Get Queries', () => {
32+
describe('useGetSchemas', () => {
33+
it('returns the correct data', async () => {
34+
const mock = fetchMock.getOnce(schemasAPIUrl, schemasPayload);
35+
const { result } = renderQueryHook(() =>
36+
hooks.useGetSchemas({ clusterName })
37+
);
38+
await expectQueryWorks(mock, result);
39+
});
40+
});
41+
42+
describe('useGetLatestSchema', () => {
43+
it('returns the correct data', async () => {
44+
const mock = fetchMock.getOnce(schemasAPILatestUrl, schemaVersion);
45+
const { result } = renderQueryHook(() =>
46+
hooks.useGetLatestSchema({ clusterName, subject })
47+
);
48+
await expectQueryWorks(mock, result);
49+
});
50+
});
51+
52+
describe('useGetSchemasVersions', () => {
53+
it('returns the correct data', async () => {
54+
const mock = fetchMock.getOnce(schemasAPIVersionsUrl, versionPayload);
55+
const { result } = renderQueryHook(() =>
56+
hooks.useGetSchemasVersions({ clusterName, subject })
57+
);
58+
await expectQueryWorks(mock, result);
59+
});
60+
});
61+
62+
describe('useGetGlobalCompatibilityLayer', () => {
63+
it('returns the correct data', async () => {
64+
const mock = fetchMock.getOnce(schemaCompatibilityUrl, {
65+
compatibility: CompatibilityLevelCompatibilityEnum.FULL,
66+
});
67+
const { result } = renderQueryHook(() =>
68+
hooks.useGetGlobalCompatibilityLayer(clusterName)
69+
);
70+
await expectQueryWorks(mock, result);
71+
});
72+
});
73+
});
74+
75+
describe('Mutations', () => {
76+
describe('useCreateSchema', () => {
77+
it('returns the correct data', async () => {
78+
const mock = fetchMock.postOnce(schemasAPIUrl, jsonSchema);
79+
const { result } = renderHook(
80+
() => hooks.useCreateSchema(clusterName),
81+
{ wrapper: TestQueryClientProvider }
82+
);
83+
await act(async () => {
84+
await result.current.mutateAsync(jsonSchema);
85+
});
86+
await waitFor(() => expect(result.current.isSuccess).toBeTruthy());
87+
expect(mock.calls()).toHaveLength(1);
88+
});
89+
});
90+
91+
describe('useUpdateSchemaCompatibilityLayer', () => {
92+
it('returns the correct data', async () => {
93+
const mock = fetchMock.putOnce(schemaCompatibilityWithSubjectUrl, 200);
94+
const { result } = renderHook(
95+
() =>
96+
hooks.useUpdateSchemaCompatibilityLayer({ clusterName, subject }),
97+
{ wrapper: TestQueryClientProvider }
98+
);
99+
await act(async () => {
100+
await result.current.mutateAsync({
101+
compatibilityLevel: {
102+
compatibility: CompatibilityLevelCompatibilityEnum.BACKWARD,
103+
},
104+
});
105+
});
106+
await waitFor(() => expect(result.current.isSuccess).toBeTruthy());
107+
expect(mock.calls()).toHaveLength(1);
108+
});
109+
});
110+
111+
describe('useUpdateGlobalSchemaCompatibilityLevel', () => {
112+
it('returns the correct data', async () => {
113+
const mock = fetchMock.putOnce(schemaCompatibilityUrl, {
114+
body: {
115+
compatibility: CompatibilityLevelCompatibilityEnum.BACKWARD,
116+
},
117+
});
118+
const { result } = renderHook(
119+
() => hooks.useUpdateGlobalSchemaCompatibilityLevel(clusterName),
120+
{ wrapper: TestQueryClientProvider }
121+
);
122+
123+
await act(() =>
124+
result.current.mutateAsync({
125+
compatibilityLevel: {
126+
compatibility: CompatibilityLevelCompatibilityEnum.BACKWARD,
127+
},
128+
})
129+
);
130+
await waitFor(() => expect(result.current.isSuccess).toBeTruthy());
131+
expect(mock.calls()).toHaveLength(1);
132+
});
133+
});
134+
135+
describe('useDeleteSchema', () => {
136+
it('returns the correct data', async () => {
137+
const mock = fetchMock.deleteOnce(schemasWithSubjectAPIUrl, 200);
138+
const { result } = renderHook(
139+
() => hooks.useDeleteSchema({ clusterName, subject }),
140+
{ wrapper: TestQueryClientProvider }
141+
);
142+
await act(() => result.current.mutateAsync());
143+
await waitFor(() => expect(result.current.isSuccess).toBeTruthy());
144+
expect(mock.calls()).toHaveLength(1);
145+
});
146+
});
7147
});
8148
});

frontend/src/lib/hooks/api/schemas.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
} from 'lib/queries';
88
import {
99
CompatibilityLevel,
10-
DeleteSchemaRequest,
1110
GetAllVersionsBySubjectRequest,
1211
GetLatestSchemaRequest,
1312
GetSchemasRequest,
@@ -92,14 +91,20 @@ export function useCreateSchema(clusterName: ClusterName) {
9291
});
9392
}
9493

95-
export function useUpdateSchemaCompatibilityLayer(clusterName: ClusterName) {
94+
export function useUpdateSchemaCompatibilityLayer({
95+
clusterName,
96+
subject,
97+
}: {
98+
clusterName: ClusterName;
99+
subject: string;
100+
}) {
96101
const queryClient = useQueryClient();
97102
return useMutation<
98103
void,
99104
void,
100-
Omit<UpdateSchemaCompatibilityLevelRequest, 'clusterName'>
105+
Omit<UpdateSchemaCompatibilityLevelRequest, 'clusterName' | 'subject'>
101106
>({
102-
mutationFn: ({ subject, compatibilityLevel }) =>
107+
mutationFn: ({ compatibilityLevel }) =>
103108
schemasApiClient.updateSchemaCompatibilityLevel({
104109
clusterName,
105110
subject,
@@ -146,11 +151,17 @@ export function useUpdateGlobalSchemaCompatibilityLevel(
146151
});
147152
}
148153

149-
export function useDeleteSchema(clusterName: ClusterName) {
154+
export function useDeleteSchema({
155+
clusterName,
156+
subject,
157+
}: {
158+
clusterName: ClusterName;
159+
subject: string;
160+
}) {
150161
const queryClient = useQueryClient();
151162

152-
return useMutation<void, unknown, Omit<DeleteSchemaRequest, 'clusterName'>>({
153-
mutationFn: ({ subject }) =>
163+
return useMutation<void, unknown>({
164+
mutationFn: () =>
154165
schemasApiClient.deleteSchema({
155166
clusterName,
156167
subject,

0 commit comments

Comments
 (0)