Skip to content

Commit

Permalink
fix: request devfile JSON schema (#424)
Browse files Browse the repository at this point in the history
  • Loading branch information
akurinnoy authored Dec 13, 2021
1 parent d1462c8 commit 6dcde16
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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...',
Expand All @@ -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 () => {
Expand All @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<JSONSchema7>,
export default async function fetchAndUpdateDevfileSchema(
workspaceClient: CheWorkspaceClient,
schemaVersion: string,
): Promise<JSONSchema7> {
const schema = await apiCallback(schemaVersion);
const schema = await workspaceClient.restApiClient.getDevfileSchema<JSONSchema7>(schemaVersion);

if (
typeof schema?.properties?.schemaVersion === 'object' &&
Expand Down
10 changes: 4 additions & 6 deletions packages/dashboard-frontend/src/store/DevfileRegistries/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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],
Expand Down

0 comments on commit 6dcde16

Please sign in to comment.