|
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'; |
2 | 17 |
|
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 | + }); |
7 | 147 | }); |
8 | 148 | }); |
0 commit comments