|
| 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 | +}); |
0 commit comments