1- import { shallow } from 'enzyme' ;
1+ import { mount , shallow } from 'enzyme' ;
22import 'jest-enzyme' ;
33import * as React from 'react' ;
44
55import { JSONSchema4 } from 'json-schema' ;
6- import { JsonSchemaViewerComponent , SchemaTree } from '../../components' ;
6+ import { JsonSchemaViewer , SchemaTree } from '../../components' ;
77import { isSchemaViewerEmpty } from '../../utils/isSchemaViewerEmpty' ;
88import { renderSchema } from '../../utils/renderSchema' ;
99
@@ -47,21 +47,25 @@ describe('JSON Schema Viewer component', () => {
4747
4848 test ( 'should render empty message if schema is empty' , ( ) => {
4949 ( isSchemaViewerEmpty as jest . Mock ) . mockReturnValue ( true ) ;
50- const wrapper = shallow ( < JsonSchemaViewerComponent schema = { { } } onError = { jest . fn ( ) } /> ) ;
50+ const wrapper = shallow ( < JsonSchemaViewer schema = { { } } onError = { jest . fn ( ) } /> )
51+ . dive ( )
52+ . dive ( ) ;
5153 expect ( isSchemaViewerEmpty ) . toHaveBeenCalledWith ( { } ) ;
5254 expect ( wrapper . find ( SchemaTree ) ) . not . toExist ( ) ;
5355 } ) ;
5456
5557 test ( 'should render SchemaView if schema is provided' , ( ) => {
56- const wrapper = shallow ( < JsonSchemaViewerComponent schema = { schema as JSONSchema4 } onError = { jest . fn ( ) } /> ) ;
58+ const wrapper = shallow ( < JsonSchemaViewer schema = { schema as JSONSchema4 } onError = { jest . fn ( ) } /> )
59+ . dive ( )
60+ . dive ( ) ;
5761 expect ( isSchemaViewerEmpty ) . toHaveBeenCalledWith ( schema ) ;
5862 expect ( wrapper . find ( SchemaTree ) ) . toExist ( ) ;
5963 } ) ;
6064
6165 test ( 'should not perform full processing in a worker if provided schema has fewer nodes than maxRows' , ( ) => {
62- const wrapper = shallow (
63- < JsonSchemaViewerComponent schema = { schema as JSONSchema4 } maxRows = { 10 } onError = { jest . fn ( ) } /> ,
64- ) ;
66+ const wrapper = shallow ( < JsonSchemaViewer schema = { schema as JSONSchema4 } maxRows = { 10 } onError = { jest . fn ( ) } /> )
67+ . dive ( )
68+ . dive ( ) ;
6569 expect ( SchemaWorker . prototype . postMessage ) . not . toHaveBeenCalled ( ) ;
6670 expect ( wrapper . instance ( ) ) . toHaveProperty ( 'treeStore.nodes.length' , 4 ) ;
6771 } ) ;
@@ -81,7 +85,9 @@ describe('JSON Schema Viewer component', () => {
8185 ] ,
8286 } ;
8387
84- shallow ( < JsonSchemaViewerComponent schema = { schemaAllOf } maxRows = { 10 } onError = { jest . fn ( ) } /> ) ;
88+ shallow ( < JsonSchemaViewer schema = { schemaAllOf } maxRows = { 10 } onError = { jest . fn ( ) } /> )
89+ . dive ( )
90+ . dive ( ) ;
8591
8692 expect ( SchemaWorker . prototype . postMessage ) . toHaveBeenCalledWith ( {
8793 instanceId : expect . any ( String ) ,
@@ -105,15 +111,17 @@ describe('JSON Schema Viewer component', () => {
105111 ] ,
106112 } ;
107113
108- shallow ( < JsonSchemaViewerComponent schema = { schemaAllOf } maxRows = { 10 } mergeAllOf = { false } onError = { jest . fn ( ) } /> ) ;
114+ shallow ( < JsonSchemaViewer schema = { schemaAllOf } maxRows = { 10 } mergeAllOf = { false } onError = { jest . fn ( ) } /> )
115+ . dive ( )
116+ . dive ( ) ;
109117
110118 expect ( SchemaWorker . prototype . postMessage ) . not . toHaveBeenCalledWith ( ) ;
111119 } ) ;
112120
113121 test ( 'should pre-render maxRows nodes and perform full processing in a worker if provided schema has more nodes than maxRows' , ( ) => {
114- const wrapper = shallow (
115- < JsonSchemaViewerComponent schema = { schema as JSONSchema4 } maxRows = { 1 } onError = { jest . fn ( ) } /> ,
116- ) ;
122+ const wrapper = shallow ( < JsonSchemaViewer schema = { schema as JSONSchema4 } maxRows = { 1 } onError = { jest . fn ( ) } /> )
123+ . dive ( )
124+ . dive ( ) ;
117125 expect ( SchemaWorker . prototype . postMessage ) . toHaveBeenCalledWith ( {
118126 instanceId : expect . any ( String ) ,
119127 mergeAllOf : true ,
@@ -166,17 +174,17 @@ describe('JSON Schema Viewer component', () => {
166174
167175 test ( 'should render all nodes on main thread when worker cannot be spawned regardless of maxRows or schema' , ( ) => {
168176 SchemaWorker . prototype . isShim = true ;
169- const wrapper = shallow (
170- < JsonSchemaViewerComponent schema = { schema as JSONSchema4 } maxRows = { 1 } onError = { jest . fn ( ) } /> ,
171- ) ;
177+ const wrapper = shallow ( < JsonSchemaViewer schema = { schema as JSONSchema4 } maxRows = { 1 } onError = { jest . fn ( ) } /> )
178+ . dive ( )
179+ . dive ( ) ;
172180
173181 expect ( SchemaWorker . prototype . postMessage ) . not . toHaveBeenCalled ( ) ;
174182 expect ( wrapper . instance ( ) ) . toHaveProperty ( 'treeStore.nodes.length' , 4 ) ;
175183 } ) ;
176184
177185 test ( 'should handle exceptions that may occur during full rendering' , ( ) => {
178186 const onError = jest . fn ( ) ;
179- shallow ( < JsonSchemaViewerComponent schema = { schema as JSONSchema4 } maxRows = { 1 } onError = { onError } /> ) ;
187+ const wrapper = mount ( < JsonSchemaViewer schema = { schema as JSONSchema4 } maxRows = { 1 } onError = { onError } /> ) ;
180188
181189 SchemaWorker . prototype . addEventListener . mock . calls [ 0 ] [ 1 ] ( {
182190 data : {
@@ -186,13 +194,14 @@ describe('JSON Schema Viewer component', () => {
186194 } ,
187195 } ) ;
188196
189- expect ( onError ) . toHaveBeenCalledWith ( new Error ( 'error occurred' ) ) ;
197+ expect ( onError ) . toHaveBeenCalledWith ( new Error ( 'error occurred' ) , null ) ;
198+ wrapper . unmount ( ) ;
190199 } ) ;
191200
192201 test ( 'should not apply result of full processing in a worker if instance ids do not match' , ( ) => {
193- const wrapper = shallow (
194- < JsonSchemaViewerComponent schema = { schema as JSONSchema4 } maxRows = { 0 } onError = { jest . fn ( ) } /> ,
195- ) ;
202+ const wrapper = shallow ( < JsonSchemaViewer schema = { schema as JSONSchema4 } maxRows = { 0 } onError = { jest . fn ( ) } /> )
203+ . dive ( )
204+ . dive ( ) ;
196205 expect ( SchemaWorker . prototype . postMessage ) . toHaveBeenCalledWith ( {
197206 instanceId : expect . any ( String ) ,
198207 mergeAllOf : true ,
@@ -212,15 +221,18 @@ describe('JSON Schema Viewer component', () => {
212221 } ) ;
213222
214223 test ( 'should create one shared instance of schema worker one mounted for a first time and keep it' , ( ) => {
215- const worker = ( shallow (
216- < JsonSchemaViewerComponent schema = { schema as JSONSchema4 } maxRows = { 0 } onError = { jest . fn ( ) } /> ,
217- ) . instance ( ) ! . constructor as any ) . schemaWorker ;
224+ const worker = ( shallow ( < JsonSchemaViewer schema = { schema as JSONSchema4 } maxRows = { 0 } onError = { jest . fn ( ) } /> )
225+ . dive ( )
226+ . dive ( )
227+ . instance ( ) ! . constructor as any ) . schemaWorker ;
218228
219229 expect ( SchemaWorker ) . toHaveBeenCalledTimes ( 1 ) ;
220230
221231 expect ( worker ) . toBe (
222- ( shallow ( < JsonSchemaViewerComponent schema = { schema as JSONSchema4 } maxRows = { 0 } onError = { jest . fn ( ) } /> ) . instance ( ) !
223- . constructor as any ) . schemaWorker ,
232+ ( shallow ( < JsonSchemaViewer schema = { schema as JSONSchema4 } maxRows = { 0 } onError = { jest . fn ( ) } /> )
233+ . dive ( )
234+ . dive ( )
235+ . instance ( ) ! . constructor as any ) . schemaWorker ,
224236 ) ;
225237
226238 expect ( SchemaWorker ) . toHaveBeenCalledTimes ( 1 ) ;
0 commit comments