Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/__stories__/JsonSchemaViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { State, Store } from '@sambego/storybook-state';
import { action } from '@storybook/addon-actions';
import { boolean, number, object, text, withKnobs } from '@storybook/addon-knobs';
import { storiesOf } from '@storybook/react';
import { JsonSchemaViewer } from '../JsonSchemaViewer';
import { JsonSchemaViewer } from '../components/JsonSchemaViewer';

import * as schema from '../__fixtures__/default-schema.json';
import * as schemaWithRefs from '../__fixtures__/ref/original.json';
Expand Down
17 changes: 8 additions & 9 deletions src/JsonSchemaViewer.tsx → src/components/JsonSchemaViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ import { TreeStore } from '@stoplight/tree-list';
import { Omit } from '@stoplight/types';
import { runInAction } from 'mobx';
import * as React from 'react';
import { ErrorMessage } from './components/common/ErrorMessage';
import { MutedText } from './components/common/MutedText';
import { ISchemaView, SchemaView } from './SchemaView';
import { ThemeZone } from './theme';
import { isSchemaViewerEmpty } from './utils/isSchemaViewerEmpty';
import { renderSchema } from './utils/renderSchema';

export interface IJsonSchemaViewer extends Omit<ISchemaView, 'emptyText' | 'treeStore'> {
import { ThemeZone } from '../theme';
import { isSchemaViewerEmpty, renderSchema } from '../utils';
import { ErrorMessage } from './common/ErrorMessage';
import { MutedText } from './common/MutedText';
import { ISchemaTree, SchemaTree } from './SchemaTree';

export interface IJsonSchemaViewer extends Omit<ISchemaTree, 'emptyText' | 'treeStore'> {
emptyText?: string;
defaultExpandedDepth?: number;
}
Expand Down Expand Up @@ -90,7 +89,7 @@ export class JsonSchemaViewer extends React.PureComponent<IJsonSchemaViewer, IJs

return (
<ThemeZone name="json-schema-viewer">
<SchemaView expanded={expanded} name={name} schema={schema} treeStore={this.treeStore} {...props} />
<SchemaTree expanded={expanded} name={name} schema={schema} treeStore={this.treeStore} {...props} />
</ThemeZone>
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/MaskedSchema.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Dialog } from '@stoplight/ui-kit';
import * as React from 'react';
import { IJsonSchemaViewer, JsonSchemaViewer } from '../JsonSchemaViewer';
import { IJsonSchemaViewer, JsonSchemaViewer } from './JsonSchemaViewer';

export interface IMaskedSchema extends IJsonSchemaViewer {
onClose(): void;
Expand Down
9 changes: 3 additions & 6 deletions src/components/Property.tsx → src/components/SchemaRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ import { Omit } from '@stoplight/types';
import { Box, Button, Checkbox, Flex, IBox } from '@stoplight/ui-kit';
import * as React from 'react';
import { IMasking, SchemaNodeWithMeta } from '../types';
import { formatRef } from '../utils/formatRef';
import { isCombiner } from '../utils/isCombiner';
import { isRef } from '../utils/isRef';
import { pathToString } from '../utils/pathToString';
import { formatRef, isCombiner, isRef, pathToString } from '../utils';
import { Additional } from './Additional';
import { MutedText } from './common/MutedText';
import { Divider } from './Divider';
Expand All @@ -14,12 +11,12 @@ import { Type } from './Type';
import { Types } from './Types';
import { Validations } from './Validations';

export interface IProperty extends Omit<IBox, 'onClick'>, IMasking {
export interface ISchemaRow extends Omit<IBox, 'onClick'>, IMasking {
node: SchemaNodeWithMeta;
onMaskEdit(node: SchemaNodeWithMeta): void;
}

export const Property: React.FunctionComponent<IProperty> = ({
export const SchemaRow: React.FunctionComponent<ISchemaRow> = ({
node,
canSelect,
onSelect,
Expand Down
22 changes: 11 additions & 11 deletions src/SchemaView.tsx → src/components/SchemaTree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ import { Box, IBox, ThemeZone } from '@stoplight/ui-kit';
import { JSONSchema4 } from 'json-schema';
import _isEmpty = require('lodash/isEmpty');
import * as React from 'react';
import { MaskedSchema } from './components/MaskedSchema';
import { IProperty, Property } from './components/Property';
import { TopBar } from './components/TopBar';
import { useMetadata } from './hooks/useMetadata';
import { useMetadata } from '../hooks';
import { useTheme } from '../theme';
import { IMasking, SchemaNodeWithMeta } from '../types';
import { lookupRef } from '../utils';
import { IJsonSchemaViewer } from './JsonSchemaViewer';
import { useTheme } from './theme';
import { IMasking, SchemaNodeWithMeta } from './types';
import { lookupRef } from './utils/lookupRef';
import { MaskedSchema } from './MaskedSchema';
import { ISchemaRow, SchemaRow } from './SchemaRow';
import { TopBar } from './TopBar';

const canDrag = () => false;

export interface ISchemaView extends Omit<IBox, 'onSelect'>, IMasking {
export interface ISchemaTree extends Omit<IBox, 'onSelect'>, IMasking {
name?: string;
dereferencedSchema?: JSONSchema4;
schema: JSONSchema4;
Expand All @@ -24,7 +24,7 @@ export interface ISchemaView extends Omit<IBox, 'onSelect'>, IMasking {
treeStore: TreeStore;
}

export const SchemaView: React.FunctionComponent<ISchemaView> = props => {
export const SchemaTree: React.FunctionComponent<ISchemaTree> = props => {
const {
emptyText,
expanded = false,
Expand All @@ -44,7 +44,7 @@ export const SchemaView: React.FunctionComponent<ISchemaView> = props => {

const metadata = useMetadata(schema);

const handleMaskEdit = React.useCallback<IProperty['onMaskEdit']>(
const handleMaskEdit = React.useCallback<ISchemaRow['onMaskEdit']>(
node => {
setMaskedSchema(lookupRef(node.path, dereferencedSchema));
},
Expand Down Expand Up @@ -84,7 +84,7 @@ export const SchemaView: React.FunctionComponent<ISchemaView> = props => {
canDrag={canDrag}
store={treeStore}
onNodeClick={handleNodeClick}
rowRenderer={node => <Property node={node.metadata as SchemaNodeWithMeta} {...itemData} />}
rowRenderer={node => <SchemaRow node={node.metadata as SchemaNodeWithMeta} {...itemData} />}
/>
</ThemeZone>
</Box>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { shallow } from 'enzyme';
import 'jest-enzyme';
import * as React from 'react';
import { MutedText } from '../components/common/MutedText';
import { isSchemaViewerEmpty } from '../../utils';
import { MutedText } from '../common/MutedText';
import { JsonSchemaViewer } from '../JsonSchemaViewer';
import { SchemaView } from '../SchemaView';
import { isSchemaViewerEmpty } from '../utils/isSchemaViewerEmpty';
import { SchemaTree } from '../SchemaTree';

jest.mock('../theme');
jest.mock('../utils/isSchemaViewerEmpty');
jest.mock('../../theme');
jest.mock('../../utils/isSchemaViewerEmpty');

const schema = {
properties: {
Expand All @@ -32,14 +32,14 @@ describe('JSON Schema Viewer component', () => {
const wrapper = shallow(<JsonSchemaViewer schema={{}} />);
expect(isSchemaViewerEmpty).toHaveBeenCalledWith({});
expect(wrapper.find(MutedText)).toExist();
expect(wrapper.find(SchemaView)).not.toExist();
expect(wrapper.find(SchemaTree)).not.toExist();
});

test('should render SchemaView if schema is provided', () => {
(isSchemaViewerEmpty as jest.Mock).mockReturnValue(false);
const wrapper = shallow(<JsonSchemaViewer schema={schema} />);
expect(isSchemaViewerEmpty).toHaveBeenCalledWith(schema);
expect(wrapper.find(MutedText)).not.toExist();
expect(wrapper.find(SchemaView)).toExist();
expect(wrapper.find(SchemaTree)).toExist();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ import 'jest-enzyme';
import { JSONSchema4 } from 'json-schema';
import * as React from 'react';
import { ReactElement } from 'react';
import { Property } from '../components/Property';
import { TopBar } from '../components/TopBar';
import { useMetadata } from '../hooks/useMetadata';
import { SchemaView } from '../SchemaView';
import { ITreeNodeMeta } from '../types';
import { SchemaRow, SchemaTree, TopBar } from '../../components';
import { useMetadata } from '../../hooks';
import { ITreeNodeMeta } from '../../types';

jest.mock('../theme');
jest.mock('../hooks/useMetadata');
jest.mock('../../theme');
jest.mock('../../hooks');

const schema: JSONSchema4 = {
type: 'object',
Expand All @@ -31,7 +29,7 @@ const schema: JSONSchema4 = {
},
};

describe('SchemaView component', () => {
describe('SchemaTree component', () => {
let useStateSpy: jest.SpyInstance;
let setStateActionSpy: jest.Mock;
let store: TreeStore;
Expand All @@ -52,12 +50,12 @@ describe('SchemaView component', () => {

describe('top bar', () => {
test('should not be rendered if hideTopBar is truthy', () => {
const wrapper = shallow(<SchemaView schema={schema} treeStore={store} hideTopBar />);
const wrapper = shallow(<SchemaTree schema={schema} treeStore={store} hideTopBar />);
expect(wrapper.find(TopBar)).not.toExist();
});

test('should be rendered if name is given', () => {
const wrapper = shallow(<SchemaView schema={schema} treeStore={store} name="test" />);
const wrapper = shallow(<SchemaTree schema={schema} treeStore={store} name="test" />);
expect(wrapper.find(TopBar)).toExist();
expect(wrapper.find(TopBar)).toHaveProp('name', 'test');
});
Expand All @@ -68,7 +66,7 @@ describe('SchemaView component', () => {
$schema: 'schema',
});

const wrapper = shallow(<SchemaView schema={schema} treeStore={store} name="test" />);
const wrapper = shallow(<SchemaTree schema={schema} treeStore={store} name="test" />);
expect(wrapper.find(TopBar)).toExist();
expect(wrapper.find(TopBar)).toHaveProp('name', 'test');
});
Expand All @@ -79,14 +77,14 @@ describe('SchemaView component', () => {
$schema: 'schema',
});

const wrapper = shallow(<SchemaView schema={schema} treeStore={store} name="test" hideTopBar />);
const wrapper = shallow(<SchemaTree schema={schema} treeStore={store} name="test" hideTopBar />);
expect(wrapper.find(TopBar)).not.toExist();
});
});

describe('tree-list', () => {
test('should be rendered', () => {
const wrapper = shallow(<SchemaView schema={schema} treeStore={store} />);
const wrapper = shallow(<SchemaTree schema={schema} treeStore={store} />);

expect(wrapper.find(TreeList)).toExist();
expect(wrapper.find(TreeList)).toHaveProp({
Expand All @@ -97,20 +95,20 @@ describe('SchemaView component', () => {
});

test('should be not draggable', () => {
const treeList = shallow(<SchemaView schema={schema} treeStore={store} />).find(TreeList);
const treeList = shallow(<SchemaTree schema={schema} treeStore={store} />).find(TreeList);

expect(treeList.prop('canDrag')).toHaveLength(0);
expect(treeList.prop('canDrag')!({} as any)).toBe(false);
});

test('should have extra padding if top bar is rendered', () => {
const treeList = shallow(<SchemaView schema={schema} treeStore={store} name="my-schema" />).find(TreeList);
const treeList = shallow(<SchemaTree schema={schema} treeStore={store} name="my-schema" />).find(TreeList);

expect(treeList).toHaveProp('top', '40px');
});

test('should render property for each row', () => {
const treeList = shallow(<SchemaView schema={schema} treeStore={store} />).find(TreeList);
const treeList = shallow(<SchemaTree schema={schema} treeStore={store} />).find(TreeList);
const node: TreeListNode<ITreeNodeMeta> = {
id: 'random-id',
level: 0,
Expand All @@ -123,7 +121,7 @@ describe('SchemaView component', () => {
const rendered = treeList.prop('rowRenderer')!(node) as ReactElement<any>;

expect(rendered).toMatchObject({
type: Property,
type: SchemaRow,
props: expect.objectContaining({
node: node.metadata,
}),
Expand Down
8 changes: 8 additions & 0 deletions src/components/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export * from './Divider';
export * from './JsonSchemaViewer';
export * from './MaskedSchema';
export * from './SchemaRow';
export * from './SchemaTree';
export * from './TopBar';
export * from './Type';
export * from './Types';
2 changes: 0 additions & 2 deletions src/consts.ts

This file was deleted.

1 change: 1 addition & 0 deletions src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './useMetadata';
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './JsonSchemaViewer';
export * from './components/JsonSchemaViewer';
export { ThemeProvider } from './theme';
12 changes: 12 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export * from './assignId';
export * from './formatRef';
export * from './getAnnotations';
export * from './getMetadata';
export * from './getValidations';
export * from './isCombiner';
export * from './isRef';
export * from './isSchemaViewerEmpty';
export * from './lookupRef';
export * from './pathToString';
export * from './renderSchema';
export * from './walk';