Skip to content

Commit c6b4b05

Browse files
authored
Merge pull request #4 from stoplightio/chore/SL-1223/tests
[SL-1223] tests
2 parents 0578da6 + 0adf5ac commit c6b4b05

File tree

15 files changed

+1012
-94
lines changed

15 files changed

+1012
-94
lines changed

__mocks__/react.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// https://github.com/airbnb/enzyme/issues/1875#issuecomment-451177239
2+
const r = require.requireActual('react');
3+
4+
module.exports = {
5+
...r,
6+
memo: jest.fn((x: Function) => x),
7+
useCallback: jest.fn((fn: Function) => fn),
8+
};

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,13 @@
4949
"@stoplight/storybook-config": "1.x.x",
5050
"@stoplight/types": "3.1.x",
5151
"@stoplight/ui-kit": "1.27.x",
52+
"@types/enzyme": "3.1.x",
5253
"@types/react": "16.x.x",
5354
"@types/react-dom": "16.x.x",
5455
"enzyme": "3.x.x",
5556
"enzyme-adapter-react-16": "1.x.x",
5657
"enzyme-to-json": "3.x.x",
58+
"jest-enzyme": "7.0.x",
5759
"react": "16.7.0-alpha.2",
5860
"react-dom": "16.7.0-alpha.2",
5961
"typescript": "3.2.2"

setupTests.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ const Enzyme = require('enzyme');
22
const Adapter = require('enzyme-adapter-react-16');
33

44
Enzyme.configure({ adapter: new Adapter() });
5+
6+
jest.mock('react');

src/__mocks__/theme.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
export const useTheme = () => ({
2+
canvas: {
3+
bg: '#111',
4+
fg: '#fff',
5+
error: 'red',
6+
muted: 'rgba(255, 255, 255, 0.4)'
7+
},
8+
9+
divider: {
10+
bg: '#bababa',
11+
},
12+
13+
row: {
14+
hoverBg: '#999',
15+
hoverFg: '#222',
16+
evenBg: '#333',
17+
},
18+
});
19+
20+
export const ThemeZone = jest.fn(({ children }) => children);
21+
// @ts-ignore
22+
ThemeZone.displayName = 'ThemeZone';
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/* @jsx jsx */
2+
import { jsx } from '@emotion/core';
3+
import { shallow } from 'enzyme';
4+
import 'jest-enzyme';
5+
import { MutedText } from '../common/MutedText';
6+
import { JsonSchemaViewer } from '../JsonSchemaViewer';
7+
import { SchemaView } from '../SchemaView';
8+
import { isSchemaViewerEmpty } from '../util/isSchemaViewerEmpty';
9+
10+
jest.mock('../theme');
11+
jest.mock('../util/isSchemaViewerEmpty');
12+
13+
const schema = {
14+
properties: {
15+
data: {
16+
items: {
17+
$ref: '#/definitions/Gif',
18+
},
19+
type: 'array',
20+
},
21+
meta: {
22+
$ref: '#/definitions/Meta',
23+
},
24+
pagination: {
25+
$ref: '#/definitions/Pagination',
26+
},
27+
},
28+
};
29+
30+
describe('JSON Schema Viewer component', () => {
31+
test('should render empty message if schema is empty', () => {
32+
// @ts-ignore storybook has some issues with (isSchemaViewer as jest.Mock)
33+
isSchemaViewerEmpty.mockReturnValue(true);
34+
const wrapper = shallow(<JsonSchemaViewer schemas={{}} schema={{}} />);
35+
expect(isSchemaViewerEmpty).toHaveBeenCalledWith({});
36+
expect(wrapper.find(MutedText)).toExist();
37+
expect(wrapper.find(SchemaView)).not.toExist();
38+
});
39+
40+
test('should render SchemaView if schema is provided', () => {
41+
// @ts-ignore storybook has some issues with (isSchemaViewer as jest.Mock)
42+
isSchemaViewerEmpty.mockReturnValue(false);
43+
const wrapper = shallow(<JsonSchemaViewer schemas={{}} schema={schema} />);
44+
expect(isSchemaViewerEmpty).toHaveBeenCalledWith(schema);
45+
expect(wrapper.find(MutedText)).not.toExist();
46+
expect(wrapper.find(SchemaView)).toExist();
47+
});
48+
});

src/__tests__/SchemaView.spec.tsx

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/* @jsx jsx */
2+
import { jsx } from '@emotion/core';
3+
import { Button } from '@stoplight/ui-kit';
4+
import { shallow } from 'enzyme';
5+
import 'jest-enzyme';
6+
import * as React from 'react';
7+
import { MutedText } from '../common/MutedText';
8+
import { SchemaView } from '../SchemaView';
9+
10+
jest.mock('../theme');
11+
12+
const schema = {
13+
properties: {
14+
data: {
15+
items: {
16+
$ref: '#/definitions/Gif',
17+
},
18+
type: 'array',
19+
},
20+
meta: {
21+
$ref: '#/definitions/Meta',
22+
},
23+
pagination: {
24+
$ref: '#/definitions/Pagination',
25+
},
26+
},
27+
};
28+
29+
describe('SchemaView', () => {
30+
let useStateSpy: jest.SpyInstance;
31+
let setStateActionSpy: jest.Mock;
32+
33+
beforeAll(() => {
34+
setStateActionSpy = jest.fn();
35+
useStateSpy = jest
36+
.spyOn(React, 'useState')
37+
.mockImplementation((initialValue: any) => [initialValue, setStateActionSpy]);
38+
});
39+
40+
afterAll(() => {
41+
useStateSpy.mockRestore();
42+
});
43+
44+
test('should render empty text if schema has no properties', () => {
45+
const emptyText = 'abc';
46+
const wrapper = shallow(<SchemaView emptyText={emptyText} schema={{}} schemas={{}} />);
47+
48+
expect(wrapper).toContainReact(<MutedText>{emptyText}</MutedText>);
49+
});
50+
51+
test('should limit display items to limitPropertyCount when limitPropertyCount > 0', () => {
52+
const wrapper = shallow(<SchemaView emptyText="" schema={schema} schemas={{}} limitPropertyCount={2} />);
53+
54+
expect(wrapper.children()).toHaveLength(3);
55+
});
56+
57+
test('should toggle showExtra on btn click', () => {
58+
const wrapper = shallow(<SchemaView emptyText="" schema={schema} schemas={{}} limitPropertyCount={2} />);
59+
60+
wrapper.find(Button).simulate('click');
61+
expect(setStateActionSpy).toHaveBeenLastCalledWith(true);
62+
});
63+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"allOf": [
3+
{
4+
"properties": {
5+
"id": {
6+
"description": "Group ID",
7+
"type": "string"
8+
}
9+
}
10+
},
11+
{
12+
"$ref": "#/definitions/Group"
13+
}
14+
]
15+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"properties": {
3+
"members": {
4+
"description": "List of Group members",
5+
"items": {
6+
"properties": {
7+
"email": {
8+
"description": "User email. If ID given, email is ignored.",
9+
"type": "string"
10+
},
11+
"id": {
12+
"description": "User ID.",
13+
"type": "string"
14+
}
15+
}
16+
},
17+
"maximum": 30,
18+
"type": "array"
19+
}
20+
}
21+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"properties": {
3+
"id": {
4+
"description": "Group ID",
5+
"type": "string"
6+
},
7+
"name": {
8+
"description": "Group name",
9+
"type": "string"
10+
},
11+
"total_members": {
12+
"description": "Group member count",
13+
"type": "integer"
14+
}
15+
}
16+
}

0 commit comments

Comments
 (0)