Skip to content

Commit

Permalink
Add hook tests suites for schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
Mgrdich committed Apr 15, 2024
1 parent bd4d4fc commit 0c53948
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 18 deletions.
9 changes: 5 additions & 4 deletions frontend/src/components/Schemas/Details/Details.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ const Details: React.FC = () => {
subject,
});

const { mutateAsync: deleteSchema } = useDeleteSchema(clusterName);
const { mutateAsync: deleteSchema } = useDeleteSchema({
clusterName,
subject,
});

const columns = React.useMemo(
() => [
Expand All @@ -58,9 +61,7 @@ const Details: React.FC = () => {
);

const deleteHandler = async () => {
await deleteSchema({
subject,
});
await deleteSchema();
navigate('../');
};

Expand Down
3 changes: 1 addition & 2 deletions frontend/src/components/Schemas/Edit/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const Form: React.FC<FormProps> = ({ schema }) => {
const { clusterName, subject } = useAppParams<ClusterSubjectParam>();
const { mutateAsync: createSchema } = useCreateSchema(clusterName);
const { mutateAsync: updateCompatibilityLayer } =
useUpdateSchemaCompatibilityLayer(clusterName);
useUpdateSchemaCompatibilityLayer({ clusterName, subject });

const formatedSchema = React.useMemo(() => {
return schema?.schemaType === SchemaType.PROTOBUF
Expand Down Expand Up @@ -75,7 +75,6 @@ const Form: React.FC<FormProps> = ({ schema }) => {
if (dirtyFields.compatibilityLevel) {
await updateCompatibilityLayer({
...schema,
subject,
compatibilityLevel: {
compatibility: props.compatibilityLevel,
},
Expand Down
150 changes: 145 additions & 5 deletions frontend/src/lib/hooks/api/__tests__/schema.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,148 @@
import { expectQueryWorks, renderQueryHook } from 'lib/testHelpers';
import {
expectQueryWorks,
renderQueryHook,
TestQueryClientProvider,
} from 'lib/testHelpers';
import fetchMock from 'fetch-mock';
import * as hooks from 'lib/hooks/api/schemas';
import { act } from 'react-dom/test-utils';
import { renderHook, waitFor } from '@testing-library/react';
import { CompatibilityLevelCompatibilityEnum } from 'generated-sources';
import { schemaVersion } from 'components/Schemas/Edit/__tests__/fixtures';
import {
jsonSchema,
versionPayload,
} from 'components/Schemas/Details/__test__/fixtures';
import { schemasPayload } from 'components/Schemas/List/__test__/fixtures';

// TODO should be written
describe('schema hooks', () => {
it('should ', () => {
console.log(expectQueryWorks, renderQueryHook);
const clusterName = 'test-cluster';
const { subject } = schemaVersion;

const schemasAPIUrl = `/api/clusters/${clusterName}/schemas`;
const schemasWithSubjectAPIUrl = `${schemasAPIUrl}/${subject}`;
const schemasAPILatestUrl = `${schemasWithSubjectAPIUrl}/latest`;
const schemasAPIVersionsUrl = `${schemasWithSubjectAPIUrl}/versions`;
const schemaCompatibilityUrl = `${schemasAPIUrl}/compatibility`;
const schemaCompatibilityWithSubjectUrl = `${schemasWithSubjectAPIUrl}/compatibility`;

describe('Schema hooks', () => {
beforeEach(() => fetchMock.restore());

describe('Get Queries', () => {
describe('useGetSchemas', () => {
it('returns the correct data', async () => {
const mock = fetchMock.getOnce(schemasAPIUrl, schemasPayload);
const { result } = renderQueryHook(() =>
hooks.useGetSchemas({ clusterName })
);
await expectQueryWorks(mock, result);
});
});

describe('useGetLatestSchema', () => {
it('returns the correct data', async () => {
const mock = fetchMock.getOnce(schemasAPILatestUrl, schemaVersion);
const { result } = renderQueryHook(() =>
hooks.useGetLatestSchema({ clusterName, subject })
);
await expectQueryWorks(mock, result);
});
});

describe('useGetSchemasVersions', () => {
it('returns the correct data', async () => {
const mock = fetchMock.getOnce(schemasAPIVersionsUrl, versionPayload);
const { result } = renderQueryHook(() =>
hooks.useGetSchemasVersions({ clusterName, subject })
);
await expectQueryWorks(mock, result);
});
});

describe('useGetGlobalCompatibilityLayer', () => {
it('returns the correct data', async () => {
const mock = fetchMock.getOnce(schemaCompatibilityUrl, {
compatibility: CompatibilityLevelCompatibilityEnum.FULL,
});
const { result } = renderQueryHook(() =>
hooks.useGetGlobalCompatibilityLayer(clusterName)
);
await expectQueryWorks(mock, result);
});
});
});

describe('Mutations', () => {
describe('useCreateSchema', () => {
it('returns the correct data', async () => {
const mock = fetchMock.postOnce(schemasAPIUrl, jsonSchema);
const { result } = renderHook(
() => hooks.useCreateSchema(clusterName),
{ wrapper: TestQueryClientProvider }
);
await act(async () => {
await result.current.mutateAsync(jsonSchema);
});
await waitFor(() => expect(result.current.isSuccess).toBeTruthy());
expect(mock.calls()).toHaveLength(1);
});
});

describe('useUpdateSchemaCompatibilityLayer', () => {
it('returns the correct data', async () => {
const mock = fetchMock.putOnce(schemaCompatibilityWithSubjectUrl, 200);
const { result } = renderHook(
() =>
hooks.useUpdateSchemaCompatibilityLayer({ clusterName, subject }),
{ wrapper: TestQueryClientProvider }
);
await act(async () => {
await result.current.mutateAsync({
compatibilityLevel: {
compatibility: CompatibilityLevelCompatibilityEnum.BACKWARD,
},
});
});
await waitFor(() => expect(result.current.isSuccess).toBeTruthy());
expect(mock.calls()).toHaveLength(1);
});
});

describe('useUpdateGlobalSchemaCompatibilityLevel', () => {
it('returns the correct data', async () => {
const mock = fetchMock.putOnce(schemaCompatibilityUrl, {
body: {
compatibility: CompatibilityLevelCompatibilityEnum.BACKWARD,
},
});
const { result } = renderHook(
() => hooks.useUpdateGlobalSchemaCompatibilityLevel(clusterName),
{ wrapper: TestQueryClientProvider }
);

await act(() =>
result.current.mutateAsync({
compatibilityLevel: {
compatibility: CompatibilityLevelCompatibilityEnum.BACKWARD,
},
})
);
await waitFor(() => expect(result.current.isSuccess).toBeTruthy());
expect(mock.calls()).toHaveLength(1);
});
});

describe('useDeleteSchema', () => {
it('returns the correct data', async () => {
const mock = fetchMock.deleteOnce(schemasWithSubjectAPIUrl, 200);
const { result } = renderHook(
() => hooks.useDeleteSchema({ clusterName, subject }),
{ wrapper: TestQueryClientProvider }
);
await act(() => result.current.mutateAsync());
await waitFor(() => expect(result.current.isSuccess).toBeTruthy());
expect(mock.calls()).toHaveLength(1);
});
});
});
});
25 changes: 18 additions & 7 deletions frontend/src/lib/hooks/api/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
} from 'lib/queries';
import {
CompatibilityLevel,
DeleteSchemaRequest,
GetAllVersionsBySubjectRequest,
GetLatestSchemaRequest,
GetSchemasRequest,
Expand Down Expand Up @@ -92,14 +91,20 @@ export function useCreateSchema(clusterName: ClusterName) {
});
}

export function useUpdateSchemaCompatibilityLayer(clusterName: ClusterName) {
export function useUpdateSchemaCompatibilityLayer({
clusterName,
subject,
}: {
clusterName: ClusterName;
subject: string;
}) {
const queryClient = useQueryClient();
return useMutation<
void,
void,
Omit<UpdateSchemaCompatibilityLevelRequest, 'clusterName'>
Omit<UpdateSchemaCompatibilityLevelRequest, 'clusterName' | 'subject'>
>({
mutationFn: ({ subject, compatibilityLevel }) =>
mutationFn: ({ compatibilityLevel }) =>
schemasApiClient.updateSchemaCompatibilityLevel({
clusterName,
subject,
Expand Down Expand Up @@ -146,11 +151,17 @@ export function useUpdateGlobalSchemaCompatibilityLevel(
});
}

export function useDeleteSchema(clusterName: ClusterName) {
export function useDeleteSchema({
clusterName,
subject,
}: {
clusterName: ClusterName;
subject: string;
}) {
const queryClient = useQueryClient();

return useMutation<void, unknown, Omit<DeleteSchemaRequest, 'clusterName'>>({
mutationFn: ({ subject }) =>
return useMutation<void, unknown>({
mutationFn: () =>
schemasApiClient.deleteSchema({
clusterName,
subject,
Expand Down

0 comments on commit 0c53948

Please sign in to comment.