Skip to content

Commit

Permalink
fix: render default,const boolean values in Schema component (#638)
Browse files Browse the repository at this point in the history
  • Loading branch information
magicmatatjahu authored Sep 16, 2022
1 parent a72bec2 commit 45e4109
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 6 deletions.
4 changes: 3 additions & 1 deletion library/src/components/Schema.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,9 @@ export const Schema: React.FunctionComponent<Props> = ({
</div>
{rawValue ? (
<div>
<div className="text-sm">{schema.const()}</div>
<div className="text-sm">
{SchemaHelpers.prettifyValue(schema.const(), false)}
</div>
</div>
) : (
<div>
Expand Down
46 changes: 46 additions & 0 deletions library/src/components/__tests__/Schema.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(<Schema schema={schemaModel} />);

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(<Schema schema={schemaModel} />);

expect(screen.getByText('true')).toBeDefined();
expect(screen.getByText('false')).toBeDefined();
});
});
});
14 changes: 12 additions & 2 deletions library/src/helpers/__tests__/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
6 changes: 3 additions & 3 deletions library/src/helpers/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()}]`;
Expand Down

0 comments on commit 45e4109

Please sign in to comment.