@@ -261,7 +261,7 @@ describe('QuadTree', () => {
261
261
expect ( found ) . to . contain ( points [ 3 ] ) ;
262
262
expect ( found ) . to . contain ( points [ 7 ] ) ;
263
263
} ) ;
264
- it ( 'returns correct number of southhwest points' , ( ) => {
264
+ it ( 'returns correct number of southwest points' , ( ) => {
265
265
let found = quadtree . query ( new Rectangle ( - 25 , - 25 , 10 , 10 ) ) ;
266
266
expect ( found ) . to . have . length ( 2 ) ;
267
267
} ) ;
@@ -305,6 +305,48 @@ describe('QuadTree', () => {
305
305
} ) ;
306
306
} ) ;
307
307
} ) ;
308
+ describe ( 'json operations' , ( ) => {
309
+ let quadtree ;
310
+ beforeEach ( ( ) => {
311
+ quadtree = new QuadTree ( new Rectangle ( 0 , 0 , 40 , 40 ) , 2 ) ;
312
+ points = [
313
+ new Point ( - 20 , 20 , { index : 0 } ) ,
314
+ new Point ( - 20 , - 20 , { index : 1 } ) ,
315
+ new Point ( 20 , 20 , { index : 2 } ) ,
316
+ new Point ( 20 , - 20 , { index : 3 } )
317
+ ] ;
318
+ points . forEach ( point => quadtree . insert ( point ) ) ;
319
+ } ) ;
320
+ it ( 'throws exception when JSON has no position data' , ( ) => {
321
+ expect ( ( ) => { new QuadTree . fromJSON ( { points : [ ] } ) } ) . to . throw ( TypeError ) ;
322
+ } ) ;
323
+ it ( 'saves all data to a JSON object' , ( ) => {
324
+ const obj = quadtree . toJSON ( ) ;
325
+ expect ( obj . x ) . to . equal ( quadtree . boundary . x ) ;
326
+ expect ( obj . y ) . to . equal ( quadtree . boundary . y ) ;
327
+ expect ( obj . w ) . to . equal ( quadtree . boundary . w ) ;
328
+ expect ( obj . h ) . to . equal ( quadtree . boundary . h ) ;
329
+ expect ( obj . capacity ) . to . equal ( quadtree . capacity ) ;
330
+ expect ( obj . ne . points . length ) . to . equal ( quadtree . northeast . points . length ) ;
331
+ expect ( obj . ne . points [ 0 ] . userData . index ) . to . equal ( quadtree . northeast . points [ 0 ] . userData . index ) ;
332
+ expect ( obj . ne . divided ) . to . be . undefined ;
333
+ expect ( obj . nw ) . to . be . undefined ;
334
+ } ) ;
335
+ it ( 'loads properly from a JSON object' , ( ) => {
336
+ const obj = quadtree . toJSON ( ) ;
337
+ const test = QuadTree . fromJSON ( obj ) ;
338
+ expect ( test . boundary . x ) . to . equal ( quadtree . boundary . x ) ;
339
+ expect ( test . boundary . y ) . to . equal ( quadtree . boundary . y ) ;
340
+ expect ( test . boundary . w ) . to . equal ( quadtree . boundary . w ) ;
341
+ expect ( test . boundary . h ) . to . equal ( quadtree . boundary . h ) ;
342
+ expect ( test . capacity ) . to . equal ( quadtree . capacity ) ;
343
+ expect ( test . northeast . boundary . x ) . to . equal ( quadtree . northeast . boundary . x ) ;
344
+ expect ( test . northeast . points [ 0 ] . userData . index ) . to . equal ( quadtree . northeast . points [ 0 ] . userData . index ) ;
345
+ expect ( test . northwest . divided ) . to . be . equal ( quadtree . northwest . divided ) ;
346
+ expect ( test . southeast . x ) . to . be . equal ( quadtree . southeast . x ) ;
347
+ expect ( test . southwest . y ) . to . be . equal ( quadtree . southwest . y ) ;
348
+ } ) ;
349
+ } ) ;
308
350
describe ( 'closest' , ( ) => {
309
351
let quadtree ;
310
352
let points ;
@@ -344,8 +386,7 @@ describe('QuadTree', () => {
344
386
expect ( found ) . to . have . length ( 1 ) ;
345
387
expect ( found ) . to . contain ( points [ 0 ] ) ;
346
388
} ) ;
347
- // no total count of items
348
- it . skip ( 'returns all items when number requested exceeds QuadTree contents' , ( ) => {
389
+ it ( 'returns all items when number requested exceeds QuadTree contents' , ( ) => {
349
390
found = quadtree . closest ( new Point ( 0 , 0 ) , 10 ) ;
350
391
expect ( found ) . to . have . length ( 4 ) ;
351
392
} ) ;
@@ -388,7 +429,7 @@ describe('QuadTree', () => {
388
429
points = [ ] ;
389
430
for ( let idx = 0 ; idx < 10 ; ++ idx ) {
390
431
points . push ( new Point (
391
- bound . left + bound . w * Math . random ( ) ,
432
+ bound . left + bound . w * Math . random ( ) ,
392
433
bound . top + bound . h * Math . random ( ) ) ) ;
393
434
}
394
435
points . forEach ( point => quadtree . insert ( point ) ) ;
0 commit comments