|
| 1 | +import { act, renderHook } from '@testing-library/react'; |
| 2 | +import { useCreateManagedControlPlane } from './useCreateManagedControlPlane.tsx'; |
| 3 | +import { CreateManagedControlPlaneType } from '../lib/api/types/crate/createManagedControlPlane.ts'; |
| 4 | + |
| 5 | +import { describe, it, expect, vi, afterEach, Mock } from 'vitest'; |
| 6 | +import { assertNonNullish, assertString } from '../utils/test/vitest-utils.ts'; |
| 7 | + |
| 8 | +describe('useCreateManagedControlPlane', () => { |
| 9 | + afterEach(() => { |
| 10 | + vi.clearAllMocks(); |
| 11 | + }); |
| 12 | + |
| 13 | + it('should perform a valid request', async () => { |
| 14 | + // ARRANGE |
| 15 | + const mockData: CreateManagedControlPlaneType = { |
| 16 | + apiVersion: 'core.openmcp.cloud/v1alpha1', |
| 17 | + kind: 'ManagedControlPlane', |
| 18 | + metadata: { |
| 19 | + name: 'name', |
| 20 | + namespace: 'project-projectName--ws-workspaceName', |
| 21 | + annotations: { |
| 22 | + 'openmcp.cloud/display-name': 'display-name', |
| 23 | + }, |
| 24 | + labels: { |
| 25 | + 'openmcp.cloud.sap/charging-target-type': 'BTP', |
| 26 | + 'openmcp.cloud.sap/charging-target': 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa', |
| 27 | + }, |
| 28 | + }, |
| 29 | + spec: { |
| 30 | + authentication: { |
| 31 | + enableSystemIdentityProvider: true, |
| 32 | + }, |
| 33 | + components: { |
| 34 | + externalSecretsOperator: { |
| 35 | + version: '0.20.1', |
| 36 | + }, |
| 37 | + flux: { |
| 38 | + version: '2.16.2', |
| 39 | + }, |
| 40 | + kyverno: { |
| 41 | + version: '3.5.2', |
| 42 | + }, |
| 43 | + btpServiceOperator: { |
| 44 | + version: '0.9.2', |
| 45 | + }, |
| 46 | + apiServer: { |
| 47 | + type: 'GardenerDedicated', |
| 48 | + }, |
| 49 | + crossplane: { |
| 50 | + version: '1.19.0', |
| 51 | + providers: [ |
| 52 | + { |
| 53 | + name: 'provider-hana', |
| 54 | + version: '0.2.0', |
| 55 | + }, |
| 56 | + ], |
| 57 | + }, |
| 58 | + }, |
| 59 | + authorization: { |
| 60 | + roleBindings: [ |
| 61 | + { |
| 62 | + role: 'admin', |
| 63 | + subjects: [ |
| 64 | + { |
| 65 | + kind: 'User', |
| 66 | + name: 'openmcp:user@domain.com', |
| 67 | + }, |
| 68 | + ], |
| 69 | + }, |
| 70 | + ], |
| 71 | + }, |
| 72 | + }, |
| 73 | + }; |
| 74 | + |
| 75 | + const fetchMock: Mock<typeof fetch> = vi.fn(); |
| 76 | + fetchMock.mockResolvedValue({ |
| 77 | + ok: true, |
| 78 | + status: 200, |
| 79 | + json: vi.fn().mockResolvedValue({}), |
| 80 | + } as unknown as Response); |
| 81 | + global.fetch = fetchMock; |
| 82 | + |
| 83 | + // ACT |
| 84 | + const renderHookResult = renderHook(() => useCreateManagedControlPlane('projectName', 'workspaceName')); |
| 85 | + const { mutate: create } = renderHookResult.result.current; |
| 86 | + |
| 87 | + await act(async () => { |
| 88 | + await create(mockData); |
| 89 | + }); |
| 90 | + |
| 91 | + // ASSERT |
| 92 | + expect(fetchMock).toHaveBeenCalledTimes(1); |
| 93 | + const call = fetchMock.mock.calls[0]; |
| 94 | + const [url, init] = call; |
| 95 | + assertNonNullish(init); |
| 96 | + const { method, headers, body } = init; |
| 97 | + |
| 98 | + expect(url).toContain( |
| 99 | + '/api/onboarding/apis/core.openmcp.cloud/v1alpha1/namespaces/projectName--ws-workspaceName/managedcontrolplanes', |
| 100 | + ); |
| 101 | + |
| 102 | + expect(method).toBe('POST'); |
| 103 | + |
| 104 | + expect(headers).toEqual( |
| 105 | + expect.objectContaining({ |
| 106 | + 'Content-Type': 'application/json', |
| 107 | + 'X-use-crate': 'true', |
| 108 | + }), |
| 109 | + ); |
| 110 | + |
| 111 | + assertString(body); |
| 112 | + const parsedBody = JSON.parse(body); |
| 113 | + expect(parsedBody).toEqual(mockData); |
| 114 | + }); |
| 115 | +}); |
0 commit comments