Skip to content

Commit 71977ea

Browse files
committed
Update useCustomResourceDefinitionQuery.spec.ts
1 parent 59485ab commit 71977ea

File tree

1 file changed

+49
-31
lines changed

1 file changed

+49
-31
lines changed

src/hooks/useCustomResourceDefinitionQuery.spec.ts

Lines changed: 49 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,28 @@ import { describe, it, expect, vi, beforeEach, afterEach, Mock } from 'vitest';
33
import { useCustomResourceDefinitionQuery } from './useCustomResourceDefinitionQuery';
44
import { CustomResourceDefinition } from '../types/customResourceDefinition';
55
import * as useApiResourceModule from '../lib/api/useApiResource';
6+
import * as useResourcePluralNamesModule from './useResourcePluralNames';
67

78
vi.mock('../lib/api/useApiResource');
9+
vi.mock('./useResourcePluralNames');
810

911
describe('useCustomResourceDefinitionQuery', () => {
1012
let useApiResourceMock: Mock;
13+
let useResourcePluralNamesMock: Mock;
1114

1215
beforeEach(() => {
1316
useApiResourceMock = vi.fn();
1417
vi.spyOn(useApiResourceModule, 'useApiResource').mockImplementation(useApiResourceMock);
18+
19+
useResourcePluralNamesMock = vi.fn();
20+
vi.spyOn(useResourcePluralNamesModule, 'useResourcePluralNames').mockImplementation(useResourcePluralNamesMock);
1521
});
1622

1723
afterEach(() => {
1824
vi.clearAllMocks();
1925
});
2026

21-
it('should return schema and crdData when CRD is loaded successfully', async () => {
22-
// ARRANGE
27+
it('should return schema and CRD data on successful load', async () => {
2328
const mockCRD: CustomResourceDefinition = {
2429
kind: 'CustomResourceDefinition',
2530
apiVersion: 'apiextensions.k8s.io/v1',
@@ -73,7 +78,11 @@ describe('useCustomResourceDefinitionQuery', () => {
7378
error: undefined,
7479
});
7580

76-
// ACT
81+
useResourcePluralNamesMock.mockReturnValue({
82+
getPluralKind: (kind: string) => kind.toLowerCase() + 's',
83+
isLoading: false,
84+
});
85+
7786
const { result } = renderHook(() =>
7887
useCustomResourceDefinitionQuery({
7988
kind: 'Workspace',
@@ -82,7 +91,6 @@ describe('useCustomResourceDefinitionQuery', () => {
8291
}),
8392
);
8493

85-
// ASSERT
8694
await waitFor(() => {
8795
expect(result.current.crdData).toEqual(mockCRD);
8896
expect(result.current.isLoading).toBe(false);
@@ -92,15 +100,18 @@ describe('useCustomResourceDefinitionQuery', () => {
92100
});
93101
});
94102

95-
it('should call useApiResource with correct path and parameters', () => {
96-
// ARRANGE
103+
it('should construct the correct API path for the CRD', () => {
97104
useApiResourceMock.mockReturnValue({
98105
data: undefined,
99106
isLoading: true,
100107
error: undefined,
101108
});
102109

103-
// ACT
110+
useResourcePluralNamesMock.mockReturnValue({
111+
getPluralKind: (kind: string) => kind.toLowerCase() + 's',
112+
isLoading: false,
113+
});
114+
104115
renderHook(() =>
105116
useCustomResourceDefinitionQuery({
106117
kind: 'Workspace',
@@ -109,7 +120,6 @@ describe('useCustomResourceDefinitionQuery', () => {
109120
}),
110121
);
111122

112-
// ASSERT
113123
expect(useApiResourceMock).toHaveBeenCalledWith(
114124
{
115125
path: '/apis/apiextensions.k8s.io/v1/customresourcedefinitions/workspaces.core.openmcp.cloud',
@@ -120,15 +130,18 @@ describe('useCustomResourceDefinitionQuery', () => {
120130
);
121131
});
122132

123-
it('should disable API call when kind is undefined', () => {
124-
// ARRANGE
133+
it('should not fetch if the kind is undefined', () => {
125134
useApiResourceMock.mockReturnValue({
126135
data: undefined,
127136
isLoading: false,
128137
error: undefined,
129138
});
130139

131-
// ACT
140+
useResourcePluralNamesMock.mockReturnValue({
141+
getPluralKind: () => '',
142+
isLoading: false,
143+
});
144+
132145
renderHook(() =>
133146
useCustomResourceDefinitionQuery({
134147
kind: undefined,
@@ -137,26 +150,28 @@ describe('useCustomResourceDefinitionQuery', () => {
137150
}),
138151
);
139152

140-
// ASSERT
141153
expect(useApiResourceMock).toHaveBeenCalledWith(
142154
{
143-
path: '/apis/apiextensions.k8s.io/v1/customresourcedefinitions/undefined.core.openmcp.cloud',
155+
path: '/apis/apiextensions.k8s.io/v1/customresourcedefinitions/.core.openmcp.cloud',
144156
},
145157
undefined,
146158
undefined,
147159
true, // disabled
148160
);
149161
});
150162

151-
it('should return undefined schema when no CRD data is available', () => {
152-
// ARRANGE
163+
it('should return undefined schema and data when CRD is not found', () => {
153164
useApiResourceMock.mockReturnValue({
154165
data: undefined,
155166
isLoading: false,
156167
error: undefined,
157168
});
158169

159-
// ACT
170+
useResourcePluralNamesMock.mockReturnValue({
171+
getPluralKind: (kind: string) => kind.toLowerCase() + 's',
172+
isLoading: false,
173+
});
174+
160175
const { result } = renderHook(() =>
161176
useCustomResourceDefinitionQuery({
162177
kind: 'Workspace',
@@ -165,13 +180,11 @@ describe('useCustomResourceDefinitionQuery', () => {
165180
}),
166181
);
167182

168-
// ASSERT
169183
expect(result.current.schema).toBeUndefined();
170184
expect(result.current.crdData).toBeUndefined();
171185
});
172186

173-
it('should fall back to first version when specified apiVersion is not found', async () => {
174-
// ARRANGE
187+
it('should use the first available version if the specified one is not found', async () => {
175188
const mockCRD: CustomResourceDefinition = {
176189
kind: 'CustomResourceDefinition',
177190
apiVersion: 'apiextensions.k8s.io/v1',
@@ -225,7 +238,11 @@ describe('useCustomResourceDefinitionQuery', () => {
225238
error: undefined,
226239
});
227240

228-
// ACT
241+
useResourcePluralNamesMock.mockReturnValue({
242+
getPluralKind: (kind: string) => kind.toLowerCase() + 's',
243+
isLoading: false,
244+
});
245+
229246
const { result } = renderHook(() =>
230247
useCustomResourceDefinitionQuery({
231248
kind: 'Workspace',
@@ -234,24 +251,25 @@ describe('useCustomResourceDefinitionQuery', () => {
234251
}),
235252
);
236253

237-
// ASSERT
238254
await waitFor(() => {
239255
expect(result.current.schema).toBeDefined();
240-
// Should use the fallback (first version's) schema
241256
expect(result.current.schema?.properties?.spec?.properties).toHaveProperty('fallbackField');
242257
});
243258
});
244259

245-
it('should return error when API call fails', () => {
246-
// ARRANGE
260+
it('should propagate errors from the API call', () => {
247261
const mockError = { message: 'Failed to fetch CRD', status: 404 };
248262
useApiResourceMock.mockReturnValue({
249263
data: undefined,
250264
isLoading: false,
251265
error: mockError,
252266
});
253267

254-
// ACT
268+
useResourcePluralNamesMock.mockReturnValue({
269+
getPluralKind: (kind: string) => kind.toLowerCase() + 's',
270+
isLoading: false,
271+
});
272+
255273
const { result } = renderHook(() =>
256274
useCustomResourceDefinitionQuery({
257275
kind: 'Workspace',
@@ -260,13 +278,11 @@ describe('useCustomResourceDefinitionQuery', () => {
260278
}),
261279
);
262280

263-
// ASSERT
264281
expect(result.current.error).toEqual(mockError);
265282
expect(result.current.schema).toBeUndefined();
266283
});
267284

268-
it('should handle multiple versions and select correct one', async () => {
269-
// ARRANGE
285+
it('should select the correct schema for the specified API version', async () => {
270286
const mockCRD: CustomResourceDefinition = {
271287
kind: 'CustomResourceDefinition',
272288
apiVersion: 'apiextensions.k8s.io/v1',
@@ -340,7 +356,11 @@ describe('useCustomResourceDefinitionQuery', () => {
340356
error: undefined,
341357
});
342358

343-
// ACT
359+
useResourcePluralNamesMock.mockReturnValue({
360+
getPluralKind: (kind: string) => kind.toLowerCase() + 's',
361+
isLoading: false,
362+
});
363+
344364
const { result } = renderHook(() =>
345365
useCustomResourceDefinitionQuery({
346366
kind: 'Workspace',
@@ -349,10 +369,8 @@ describe('useCustomResourceDefinitionQuery', () => {
349369
}),
350370
);
351371

352-
// ASSERT
353372
await waitFor(() => {
354373
expect(result.current.schema).toBeDefined();
355-
// Should use v1beta1's schema, not v1alpha1
356374
expect(result.current.schema?.properties?.spec?.properties).toHaveProperty('betaField');
357375
expect(result.current.schema?.properties?.spec?.properties).not.toHaveProperty('alphaField');
358376
});

0 commit comments

Comments
 (0)