From 45e410986191111041381d4f9bac43c731e71747 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Urba=C5=84czyk?= Date: Fri, 16 Sep 2022 15:01:13 +0200 Subject: [PATCH] fix: render default,const boolean values in Schema component (#638) --- library/src/components/Schema.tsx | 4 +- .../src/components/__tests__/Schema.test.tsx | 46 +++++++++++++++++++ library/src/helpers/__tests__/schema.test.ts | 14 +++++- library/src/helpers/schema.ts | 6 +-- 4 files changed, 64 insertions(+), 6 deletions(-) diff --git a/library/src/components/Schema.tsx b/library/src/components/Schema.tsx index 3a40a80e2..1994c5af9 100644 --- a/library/src/components/Schema.tsx +++ b/library/src/components/Schema.tsx @@ -168,7 +168,9 @@ export const Schema: React.FunctionComponent = ({ {rawValue ? (
-
{schema.const()}
+
+ {SchemaHelpers.prettifyValue(schema.const(), false)} +
) : (
diff --git a/library/src/components/__tests__/Schema.test.tsx b/library/src/components/__tests__/Schema.test.tsx index b270e424b..afe2f2988 100644 --- a/library/src/components/__tests__/Schema.test.tsx +++ b/library/src/components/__tests__/Schema.test.tsx @@ -63,4 +63,50 @@ describe('Schema component', () => { expect(screen.getAllByText('object [CIRCULAR]')).toBeDefined(); expect(screen.getAllByText('object [CIRCULAR]')).toHaveLength(2); }); + + describe('should render boolean values', () => { + test('defined as defaults', async () => { + const schema = { + type: 'object', + properties: { + trueValue: { + type: 'boolean', + default: true, + }, + falseValue: { + type: 'boolean', + default: false, + }, + }, + }; + const schemaModel = new SchemaModel(schema); + + render(); + + expect(screen.getByText('true')).toBeDefined(); + expect(screen.getByText('false')).toBeDefined(); + }); + + test('defined as const', async () => { + const schema = { + type: 'object', + properties: { + trueValue: { + type: 'boolean', + const: true, + }, + falseValue: { + type: 'boolean', + const: false, + }, + }, + }; + const schemaModel = new SchemaModel(schema); + + render(); + + expect(screen.getByText('true')).toBeDefined(); + expect(screen.getByText('false')).toBeDefined(); + }); + }); }); diff --git a/library/src/helpers/__tests__/schema.test.ts b/library/src/helpers/__tests__/schema.test.ts index 12208b8fc..98b09872a 100644 --- a/library/src/helpers/__tests__/schema.test.ts +++ b/library/src/helpers/__tests__/schema.test.ts @@ -176,14 +176,24 @@ describe('SchemaHelpers', () => { expect(result).toEqual(`"foobar"`); }); + test('should handle string in strict mode (default behaviour)', () => { + const result = SchemaHelpers.prettifyValue('foobar'); + expect(result).toEqual(`"foobar"`); + }); + + test('should handle string in non strict mode', () => { + const result = SchemaHelpers.prettifyValue('foobar', false); + expect(result).toEqual(`foobar`); + }); + test('should handle number', () => { const result = SchemaHelpers.prettifyValue(2137); - expect(result).toEqual(2137); + expect(result).toEqual('2137'); }); test('should handle boolean', () => { const result = SchemaHelpers.prettifyValue(false); - expect(result).toEqual(false); + expect(result).toEqual('false'); }); test('should handle array', () => { diff --git a/library/src/helpers/schema.ts b/library/src/helpers/schema.ts index bcf95ad82..d85dc763a 100644 --- a/library/src/helpers/schema.ts +++ b/library/src/helpers/schema.ts @@ -97,13 +97,13 @@ export class SchemaHelpers { return type; } - static prettifyValue(value: any) { + static prettifyValue(value: any, strict = true): string { const typeOf = typeof value; if (typeOf === 'string') { - return `"${value}"`; + return strict ? `"${value}"` : value; } if (typeOf === 'number' || typeOf === 'bigint' || typeOf === 'boolean') { - return value; + return `${value}`; } if (Array.isArray(value)) { return `[${value.toString()}]`;