Skip to content

Commit 122d0e9

Browse files
committed
Update useCustomResourceDefinitionQuery.spec.ts
1 parent 7631026 commit 122d0e9

File tree

1 file changed

+103
-206
lines changed

1 file changed

+103
-206
lines changed

src/hooks/useCustomResourceDefinitionQuery.spec.ts

Lines changed: 103 additions & 206 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,51 @@ import * as useResourcePluralNamesModule from './useResourcePluralNames';
88
vi.mock('../lib/api/useApiResource');
99
vi.mock('./useResourcePluralNames');
1010

11+
const createMockCrd = (versions: CustomResourceDefinition['spec']['versions']): CustomResourceDefinition => ({
12+
kind: 'CustomResourceDefinition',
13+
apiVersion: 'apiextensions.k8s.io/v1',
14+
metadata: {
15+
name: 'workspaces.core.openmcp.cloud',
16+
uid: 'test-uid',
17+
resourceVersion: '1',
18+
generation: 1,
19+
creationTimestamp: '2024-01-01T00:00:00Z',
20+
},
21+
spec: {
22+
group: 'core.openmcp.cloud',
23+
names: {
24+
plural: 'workspaces',
25+
singular: 'workspace',
26+
kind: 'Workspace',
27+
listKind: 'WorkspaceList',
28+
},
29+
scope: 'Namespaced',
30+
versions,
31+
conversion: {
32+
strategy: 'None',
33+
},
34+
},
35+
});
36+
1137
describe('useCustomResourceDefinitionQuery', () => {
1238
let useApiResourceMock: Mock;
1339
let useResourcePluralNamesMock: Mock;
1440

41+
const setupApiResourceMock = (
42+
data: CustomResourceDefinition | undefined,
43+
isLoading = false,
44+
error: unknown = undefined,
45+
) => {
46+
useApiResourceMock.mockReturnValue({ data, isLoading, error });
47+
};
48+
49+
const setupResourcePluralNamesMock = (isLoading = false) => {
50+
useResourcePluralNamesMock.mockReturnValue({
51+
getPluralKind: (kind: string) => (kind ? kind.toLowerCase() + 's' : ''),
52+
isLoading,
53+
});
54+
};
55+
1556
beforeEach(() => {
1657
useApiResourceMock = vi.fn();
1758
vi.spyOn(useApiResourceModule, 'useApiResource').mockImplementation(useApiResourceMock);
@@ -25,63 +66,27 @@ describe('useCustomResourceDefinitionQuery', () => {
2566
});
2667

2768
it('should return schema and CRD data on successful load', async () => {
28-
const mockCRD: CustomResourceDefinition = {
29-
kind: 'CustomResourceDefinition',
30-
apiVersion: 'apiextensions.k8s.io/v1',
31-
metadata: {
32-
name: 'workspaces.core.openmcp.cloud',
33-
uid: 'test-uid',
34-
resourceVersion: '1',
35-
generation: 1,
36-
creationTimestamp: '2024-01-01T00:00:00Z',
37-
},
38-
spec: {
39-
group: 'core.openmcp.cloud',
40-
names: {
41-
plural: 'workspaces',
42-
singular: 'workspace',
43-
kind: 'Workspace',
44-
listKind: 'WorkspaceList',
45-
},
46-
scope: 'Namespaced',
47-
versions: [
48-
{
49-
name: 'v1alpha1',
50-
served: true,
51-
storage: true,
52-
schema: {
53-
openAPIV3Schema: {
69+
const mockCRD = createMockCrd([
70+
{
71+
name: 'v1alpha1',
72+
served: true,
73+
storage: true,
74+
schema: {
75+
openAPIV3Schema: {
76+
type: 'object',
77+
properties: {
78+
spec: {
5479
type: 'object',
55-
properties: {
56-
spec: {
57-
type: 'object',
58-
properties: {
59-
name: {
60-
type: 'string',
61-
},
62-
},
63-
},
64-
},
80+
properties: { name: { type: 'string' } },
6581
},
6682
},
6783
},
68-
],
69-
conversion: {
70-
strategy: 'None',
7184
},
7285
},
73-
};
86+
]);
7487

75-
useApiResourceMock.mockReturnValue({
76-
data: mockCRD,
77-
isLoading: false,
78-
error: undefined,
79-
});
80-
81-
useResourcePluralNamesMock.mockReturnValue({
82-
getPluralKind: (kind: string) => kind.toLowerCase() + 's',
83-
isLoading: false,
84-
});
88+
setupApiResourceMock(mockCRD);
89+
setupResourcePluralNamesMock();
8590

8691
const { result } = renderHook(() =>
8792
useCustomResourceDefinitionQuery({
@@ -101,16 +106,8 @@ describe('useCustomResourceDefinitionQuery', () => {
101106
});
102107

103108
it('should construct the correct API path for the CRD', () => {
104-
useApiResourceMock.mockReturnValue({
105-
data: undefined,
106-
isLoading: true,
107-
error: undefined,
108-
});
109-
110-
useResourcePluralNamesMock.mockReturnValue({
111-
getPluralKind: (kind: string) => kind.toLowerCase() + 's',
112-
isLoading: false,
113-
});
109+
setupApiResourceMock(undefined, true);
110+
setupResourcePluralNamesMock();
114111

115112
renderHook(() =>
116113
useCustomResourceDefinitionQuery({
@@ -131,16 +128,8 @@ describe('useCustomResourceDefinitionQuery', () => {
131128
});
132129

133130
it('should not fetch if the kind is undefined', () => {
134-
useApiResourceMock.mockReturnValue({
135-
data: undefined,
136-
isLoading: false,
137-
error: undefined,
138-
});
139-
140-
useResourcePluralNamesMock.mockReturnValue({
141-
getPluralKind: () => '',
142-
isLoading: false,
143-
});
131+
setupApiResourceMock(undefined);
132+
setupResourcePluralNamesMock();
144133

145134
renderHook(() =>
146135
useCustomResourceDefinitionQuery({
@@ -161,16 +150,8 @@ describe('useCustomResourceDefinitionQuery', () => {
161150
});
162151

163152
it('should return undefined schema and data when CRD is not found', () => {
164-
useApiResourceMock.mockReturnValue({
165-
data: undefined,
166-
isLoading: false,
167-
error: undefined,
168-
});
169-
170-
useResourcePluralNamesMock.mockReturnValue({
171-
getPluralKind: (kind: string) => kind.toLowerCase() + 's',
172-
isLoading: false,
173-
});
153+
setupApiResourceMock(undefined);
154+
setupResourcePluralNamesMock();
174155

175156
const { result } = renderHook(() =>
176157
useCustomResourceDefinitionQuery({
@@ -185,63 +166,27 @@ describe('useCustomResourceDefinitionQuery', () => {
185166
});
186167

187168
it('should use the first available version if the specified one is not found', async () => {
188-
const mockCRD: CustomResourceDefinition = {
189-
kind: 'CustomResourceDefinition',
190-
apiVersion: 'apiextensions.k8s.io/v1',
191-
metadata: {
192-
name: 'workspaces.core.openmcp.cloud',
193-
uid: 'test-uid',
194-
resourceVersion: '1',
195-
generation: 1,
196-
creationTimestamp: '2024-01-01T00:00:00Z',
197-
},
198-
spec: {
199-
group: 'core.openmcp.cloud',
200-
names: {
201-
plural: 'workspaces',
202-
singular: 'workspace',
203-
kind: 'Workspace',
204-
listKind: 'WorkspaceList',
205-
},
206-
scope: 'Namespaced',
207-
versions: [
208-
{
209-
name: 'v1beta1',
210-
served: true,
211-
storage: true,
212-
schema: {
213-
openAPIV3Schema: {
169+
const mockCRD = createMockCrd([
170+
{
171+
name: 'v1beta1',
172+
served: true,
173+
storage: true,
174+
schema: {
175+
openAPIV3Schema: {
176+
type: 'object',
177+
properties: {
178+
spec: {
214179
type: 'object',
215-
properties: {
216-
spec: {
217-
type: 'object',
218-
properties: {
219-
fallbackField: {
220-
type: 'string',
221-
},
222-
},
223-
},
224-
},
180+
properties: { fallbackField: { type: 'string' } },
225181
},
226182
},
227183
},
228-
],
229-
conversion: {
230-
strategy: 'None',
231184
},
232185
},
233-
};
234-
235-
useApiResourceMock.mockReturnValue({
236-
data: mockCRD,
237-
isLoading: false,
238-
error: undefined,
239-
});
186+
]);
240187

241-
useResourcePluralNamesMock.mockReturnValue({
242-
getPluralKind: (kind: string) => kind.toLowerCase() + 's',
243-
isLoading: false,
244-
});
188+
setupApiResourceMock(mockCRD);
189+
setupResourcePluralNamesMock();
245190

246191
const { result } = renderHook(() =>
247192
useCustomResourceDefinitionQuery({
@@ -259,16 +204,8 @@ describe('useCustomResourceDefinitionQuery', () => {
259204

260205
it('should propagate errors from the API call', () => {
261206
const mockError = { message: 'Failed to fetch CRD', status: 404 };
262-
useApiResourceMock.mockReturnValue({
263-
data: undefined,
264-
isLoading: false,
265-
error: mockError,
266-
});
267-
268-
useResourcePluralNamesMock.mockReturnValue({
269-
getPluralKind: (kind: string) => kind.toLowerCase() + 's',
270-
isLoading: false,
271-
});
207+
setupApiResourceMock(undefined, false, mockError);
208+
setupResourcePluralNamesMock();
272209

273210
const { result } = renderHook(() =>
274211
useCustomResourceDefinitionQuery({
@@ -283,83 +220,43 @@ describe('useCustomResourceDefinitionQuery', () => {
283220
});
284221

285222
it('should select the correct schema for the specified API version', async () => {
286-
const mockCRD: CustomResourceDefinition = {
287-
kind: 'CustomResourceDefinition',
288-
apiVersion: 'apiextensions.k8s.io/v1',
289-
metadata: {
290-
name: 'workspaces.core.openmcp.cloud',
291-
uid: 'test-uid',
292-
resourceVersion: '1',
293-
generation: 1,
294-
creationTimestamp: '2024-01-01T00:00:00Z',
295-
},
296-
spec: {
297-
group: 'core.openmcp.cloud',
298-
names: {
299-
plural: 'workspaces',
300-
singular: 'workspace',
301-
kind: 'Workspace',
302-
listKind: 'WorkspaceList',
303-
},
304-
scope: 'Namespaced',
305-
versions: [
306-
{
307-
name: 'v1alpha1',
308-
served: true,
309-
storage: false,
310-
schema: {
311-
openAPIV3Schema: {
223+
const mockCRD = createMockCrd([
224+
{
225+
name: 'v1alpha1',
226+
served: true,
227+
storage: false,
228+
schema: {
229+
openAPIV3Schema: {
230+
type: 'object',
231+
properties: {
232+
spec: {
312233
type: 'object',
313-
properties: {
314-
spec: {
315-
type: 'object',
316-
properties: {
317-
alphaField: {
318-
type: 'string',
319-
},
320-
},
321-
},
322-
},
234+
properties: { alphaField: { type: 'string' } },
323235
},
324236
},
325237
},
326-
{
327-
name: 'v1beta1',
328-
served: true,
329-
storage: true,
330-
schema: {
331-
openAPIV3Schema: {
238+
},
239+
},
240+
{
241+
name: 'v1beta1',
242+
served: true,
243+
storage: true,
244+
schema: {
245+
openAPIV3Schema: {
246+
type: 'object',
247+
properties: {
248+
spec: {
332249
type: 'object',
333-
properties: {
334-
spec: {
335-
type: 'object',
336-
properties: {
337-
betaField: {
338-
type: 'string',
339-
},
340-
},
341-
},
342-
},
250+
properties: { betaField: { type: 'string' } },
343251
},
344252
},
345253
},
346-
],
347-
conversion: {
348-
strategy: 'None',
349254
},
350255
},
351-
};
352-
353-
useApiResourceMock.mockReturnValue({
354-
data: mockCRD,
355-
isLoading: false,
356-
error: undefined,
357-
});
256+
]);
358257

359-
useResourcePluralNamesMock.mockReturnValue({
360-
getPluralKind: (kind: string) => kind.toLowerCase() + 's',
361-
isLoading: false,
362-
});
258+
setupApiResourceMock(mockCRD);
259+
setupResourcePluralNamesMock();
363260

364261
const { result } = renderHook(() =>
365262
useCustomResourceDefinitionQuery({

0 commit comments

Comments
 (0)