From 6dcde161d0fbfec88370b3612a03e84e2484c71b Mon Sep 17 00:00:00 2001 From: Oleksii Kurinnyi Date: Mon, 13 Dec 2021 16:47:30 +0200 Subject: [PATCH] fix: request devfile JSON schema (#424) --- .../fetchAndUpdateDevfileSchema.spec.ts} | 44 ++++++++++++++----- ...hema.ts => fetchAndUpdateDevfileSchema.ts} | 7 +-- .../src/store/DevfileRegistries/index.ts | 10 ++--- 3 files changed, 40 insertions(+), 21 deletions(-) rename packages/dashboard-frontend/src/store/{__tests__/DevfileRegistries/getDevfileSchema.spec.ts => DevfileRegistries/__tests__/fetchAndUpdateDevfileSchema.spec.ts} (57%) rename packages/dashboard-frontend/src/store/DevfileRegistries/{getDevfileSchema.ts => fetchAndUpdateDevfileSchema.ts} (68%) diff --git a/packages/dashboard-frontend/src/store/__tests__/DevfileRegistries/getDevfileSchema.spec.ts b/packages/dashboard-frontend/src/store/DevfileRegistries/__tests__/fetchAndUpdateDevfileSchema.spec.ts similarity index 57% rename from packages/dashboard-frontend/src/store/__tests__/DevfileRegistries/getDevfileSchema.spec.ts rename to packages/dashboard-frontend/src/store/DevfileRegistries/__tests__/fetchAndUpdateDevfileSchema.spec.ts index 5e3ada40f..f983f6e46 100644 --- a/packages/dashboard-frontend/src/store/__tests__/DevfileRegistries/getDevfileSchema.spec.ts +++ b/packages/dashboard-frontend/src/store/DevfileRegistries/__tests__/fetchAndUpdateDevfileSchema.spec.ts @@ -10,11 +10,31 @@ * Red Hat, Inc. - initial API and implementation */ -import getDevfileSchema from '../../DevfileRegistries/getDevfileSchema'; +import fetchAndUpdateDevfileSchema from '../fetchAndUpdateDevfileSchema'; import { JSONSchema7 } from 'json-schema'; import { cloneDeep } from 'lodash'; +import { CheWorkspaceClient } from '../../../services/workspace-client/cheworkspace/cheWorkspaceClient'; +import { IRemoteAPI } from '@eclipse-che/workspace-client'; +import { container } from '../../../inversify.config'; describe('Get devfile schema', () => { + const mockGetDevfileSchema = jest.fn(); + + beforeEach(() => { + class MockCheWorkspaceClient extends CheWorkspaceClient { + get restApiClient() { + return { + getDevfileSchema: async () => mockGetDevfileSchema(), + } as IRemoteAPI; + } + } + container.rebind(CheWorkspaceClient).to(MockCheWorkspaceClient).inSingletonScope(); + }); + + afterEach(() => { + jest.clearAllMocks(); + }); + it('Should return unmodified schema if the schemaVersion property includes a constant', async () => { const schemaWithVersionConst = { description: 'Devfile describes the structure...', @@ -32,12 +52,12 @@ describe('Get devfile schema', () => { }, } as JSONSchema7; - const targetScheme = await getDevfileSchema(version => { - const cloneObj = cloneDeep(schemaWithVersionConst); - return Promise.resolve(cloneObj); - }, '2.0.0'); + const mockWorkspaceClient = container.get(CheWorkspaceClient); + mockGetDevfileSchema.mockResolvedValueOnce(cloneDeep(schemaWithVersionConst)); + + const targetSchema = await fetchAndUpdateDevfileSchema(mockWorkspaceClient, '2.0.0'); - expect(targetScheme).toEqual(schemaWithVersionConst); + expect(targetSchema).toEqual(schemaWithVersionConst); }); it('Should return modified schema if the schemaVersion property does not include a constant', async () => { @@ -56,14 +76,14 @@ describe('Get devfile schema', () => { }, } as JSONSchema7; - const targetScheme = await getDevfileSchema(version => { - const cloneObj = cloneDeep(schemaWithoutVersionConst); - return Promise.resolve(cloneObj); - }, '2.0.0'); + const mockWorkspaceClient = container.get(CheWorkspaceClient); + mockGetDevfileSchema.mockResolvedValueOnce(cloneDeep(schemaWithoutVersionConst)); + + const targetSchema = await fetchAndUpdateDevfileSchema(mockWorkspaceClient, '2.0.0'); - expect(targetScheme).not.toEqual(schemaWithoutVersionConst); + expect(targetSchema).not.toEqual(schemaWithoutVersionConst); - expect(targetScheme).toEqual({ + expect(targetSchema).toEqual({ description: 'Devfile describes the structure...', type: 'object', title: 'Devfile schema', diff --git a/packages/dashboard-frontend/src/store/DevfileRegistries/getDevfileSchema.ts b/packages/dashboard-frontend/src/store/DevfileRegistries/fetchAndUpdateDevfileSchema.ts similarity index 68% rename from packages/dashboard-frontend/src/store/DevfileRegistries/getDevfileSchema.ts rename to packages/dashboard-frontend/src/store/DevfileRegistries/fetchAndUpdateDevfileSchema.ts index 55d4e0d92..1aa5aff1c 100644 --- a/packages/dashboard-frontend/src/store/DevfileRegistries/getDevfileSchema.ts +++ b/packages/dashboard-frontend/src/store/DevfileRegistries/fetchAndUpdateDevfileSchema.ts @@ -11,12 +11,13 @@ */ import { JSONSchema7 } from 'json-schema'; +import { CheWorkspaceClient } from '../../services/workspace-client/cheworkspace/cheWorkspaceClient'; -export default async function getDevfileSchema( - apiCallback: (schemaVersion: string) => Promise, +export default async function fetchAndUpdateDevfileSchema( + workspaceClient: CheWorkspaceClient, schemaVersion: string, ): Promise { - const schema = await apiCallback(schemaVersion); + const schema = await workspaceClient.restApiClient.getDevfileSchema(schemaVersion); if ( typeof schema?.properties?.schemaVersion === 'object' && diff --git a/packages/dashboard-frontend/src/store/DevfileRegistries/index.ts b/packages/dashboard-frontend/src/store/DevfileRegistries/index.ts index 06c78749e..cdd014018 100644 --- a/packages/dashboard-frontend/src/store/DevfileRegistries/index.ts +++ b/packages/dashboard-frontend/src/store/DevfileRegistries/index.ts @@ -19,7 +19,7 @@ import { container } from '../../inversify.config'; import { CheWorkspaceClient } from '../../services/workspace-client/cheworkspace/cheWorkspaceClient'; import { selectPlugins } from '../Plugins/chePlugins/selectors'; import { isDevworkspacesEnabled } from '../../services/helpers/devworkspace'; -import getDevfileSchema from './getDevfileSchema'; +import fetchAndUpdateDevfileSchema from './fetchAndUpdateDevfileSchema'; const WorkspaceClient = container.get(CheWorkspaceClient); @@ -222,11 +222,9 @@ export const actionCreators: ActionCreators = { ); const parsedSchemaV1 = JSON.parse(patchedJSONString); - const apiCallback = WorkspaceClient.restApiClient.getDevfileSchema; - - const schemav200 = await getDevfileSchema(apiCallback, '2.0.0'); - const schemav210 = await getDevfileSchema(apiCallback, '2.1.0'); - const schemav220 = await getDevfileSchema(apiCallback, '2.2.0'); + const schemav200 = await fetchAndUpdateDevfileSchema(WorkspaceClient, '2.0.0'); + const schemav210 = await fetchAndUpdateDevfileSchema(WorkspaceClient, '2.1.0'); + const schemav220 = await fetchAndUpdateDevfileSchema(WorkspaceClient, '2.2.0'); schema = { oneOf: [parsedSchemaV1, schemav200, schemav210, schemav220],