Scaffolder: Fix for a step with no properties

Signed-off-by: Bogdan Nechyporenko <bnechyporenko@bol.com>
Signed-off-by: bnechyporenko <bnechyporenko@bol.com>
This commit is contained in:
bnechyporenko
2024-01-04 15:18:03 +01:00
parent 7896e7daf9
commit ce30118cf0
2 changed files with 114 additions and 2 deletions
@@ -263,6 +263,118 @@ describe('useTemplateSchema', () => {
expect(first.schema).toEqual({
type: 'object',
properties: {},
});
});
it('should deal with dependencies and oneOf options', () => {
const firstStepDependencies = {
preconditions: {
oneOf: [
{
title: 'About',
description: 'you have chosen option A',
properties: {
preconditions: {
enum: ['optionA'],
},
},
},
{
title: 'About',
description: 'you have chosen option B',
properties: {
preconditions: {
enum: ['optionB'],
},
},
},
],
},
};
const secondStepDependencies = {
preconditions: {
oneOf: [
{
required: ['inputA'],
properties: {
preconditions: {
enum: ['optionA'],
},
inputA: {
title: 'Input A',
type: 'string',
},
},
},
{
required: ['inputB'],
properties: {
preconditions: {
enum: ['optionB'],
},
inputA: {
title: 'Input B',
type: 'string',
},
},
},
],
},
};
const manifest: TemplateParameterSchema = {
title: 'Test Template',
description: 'Test Template Description',
steps: [
{
title: 'First step',
schema: {
type: 'object',
properties: {
preconditions: {
title: 'Preconditions',
type: 'string',
description: 'Choose an option',
enum: ['optionA', 'optionB'],
enumNames: ['Option A', 'Option B'],
},
},
dependencies: firstStepDependencies,
},
},
{
title: 'Second step',
schema: {
dependencies: secondStepDependencies,
},
},
],
};
const { result } = renderHook(() => useTemplateSchema(manifest), {
wrapper: ({ children }: React.PropsWithChildren<{}>) => (
<TestApiProvider
apis={[[featureFlagsApiRef, { isActive: () => false }]]}
>
{children}
</TestApiProvider>
),
});
const [first, second] = result.current.steps;
expect(first.schema).toEqual({
dependencies: firstStepDependencies,
properties: expect.anything(),
title: undefined,
type: 'object',
});
expect(second.schema).toEqual({
dependencies: secondStepDependencies,
title: undefined,
});
});
});
@@ -69,9 +69,9 @@ export const useTemplateSchema = (
},
} as ParsedTemplateSchema;
if (step.schema?.properties) {
if (step.schema?.properties || !step.schema?.dependencies) {
strippedSchema.schema.properties = Object.fromEntries(
Object.entries((step.schema?.properties ?? []) as JsonObject).filter(
Object.entries((step.schema?.properties ?? {}) as JsonObject).filter(
([key]) => {
const stepFeatureFlag =
step.uiSchema[key]?.['ui:backstage']?.featureFlag;