@@ -3,6 +3,7 @@ import { ClassModel, action, snapshottedView, getSnapshot, register, types, onPa
3
3
import { Apple } from "./fixtures/FruitAisle" ;
4
4
import { create } from "./helpers" ;
5
5
import { getParent } from "mobx-state-tree" ;
6
+ import { setDefaultShouldEmitPatchOnChange } from "../src/class-model" ;
6
7
7
8
@register
8
9
class ViewExample extends ClassModel ( { key : types . identifier , name : types . string } ) {
@@ -285,4 +286,149 @@ describe("class model snapshotted views", () => {
285
286
expect ( onError ) . not . toHaveBeenCalled ( ) ;
286
287
} ) ;
287
288
} ) ;
289
+
290
+ describe ( "shouldEmitPatchOnChange" , ( ) => {
291
+ afterEach ( ( ) => {
292
+ // reset the default value
293
+ setDefaultShouldEmitPatchOnChange ( true ) ;
294
+ } ) ;
295
+
296
+ test ( "readonly instances don't use the shouldEmitPatchOnChange option" , ( ) => {
297
+ const fn = jest . fn ( ) ;
298
+ @register
299
+ class MyViewExample extends ClassModel ( { key : types . identifier , name : types . string } ) {
300
+ @snapshottedView ( { shouldEmitPatchOnChange : fn } )
301
+ get slug ( ) {
302
+ return this . name . toLowerCase ( ) . replace ( / / g, "-" ) ;
303
+ }
304
+ }
305
+
306
+ const instance = MyViewExample . createReadOnly ( { key : "1" , name : "Test" } ) ;
307
+ expect ( instance . slug ) . toEqual ( "test" ) ;
308
+ expect ( fn ) . not . toHaveBeenCalled ( ) ;
309
+ } ) ;
310
+
311
+ test ( "observable instances don't emit a patch when shouldEmitPatchOnChange returns false" , ( ) => {
312
+ const shouldEmitPatchOnChangeFn = jest . fn ( ( ) => false ) ;
313
+ const observableArray = observable . array < string > ( [ ] ) ;
314
+
315
+ @register
316
+ class MyViewExample extends ClassModel ( { key : types . identifier , name : types . string } ) {
317
+ @snapshottedView ( { shouldEmitPatchOnChange : shouldEmitPatchOnChangeFn } )
318
+ get arrayLength ( ) {
319
+ return observableArray . length ;
320
+ }
321
+ }
322
+
323
+ const instance = MyViewExample . create ( { key : "1" , name : "Test" } ) ;
324
+ expect ( shouldEmitPatchOnChangeFn ) . toHaveBeenCalled ( ) ;
325
+
326
+ const onPatchFn = jest . fn ( ) ;
327
+ onPatch ( instance , onPatchFn ) ;
328
+
329
+ runInAction ( ( ) => {
330
+ observableArray . push ( "a" ) ;
331
+ } ) ;
332
+
333
+ expect ( onPatchFn ) . not . toHaveBeenCalled ( ) ;
334
+ } ) ;
335
+
336
+ test ( "observable instances do emit a patch when shouldEmitPatchOnChange returns true" , ( ) => {
337
+ const shouldEmitPatchOnChangeFn = jest . fn ( ( ) => true ) ;
338
+ const observableArray = observable . array < string > ( [ ] ) ;
339
+
340
+ @register
341
+ class MyViewExample extends ClassModel ( { key : types . identifier , name : types . string } ) {
342
+ @snapshottedView ( { shouldEmitPatchOnChange : shouldEmitPatchOnChangeFn } )
343
+ get arrayLength ( ) {
344
+ return observableArray . length ;
345
+ }
346
+ }
347
+
348
+ const instance = MyViewExample . create ( { key : "1" , name : "Test" } ) ;
349
+ expect ( shouldEmitPatchOnChangeFn ) . toHaveBeenCalled ( ) ;
350
+
351
+ const onPatchFn = jest . fn ( ) ;
352
+ onPatch ( instance , onPatchFn ) ;
353
+
354
+ runInAction ( ( ) => {
355
+ observableArray . push ( "a" ) ;
356
+ } ) ;
357
+
358
+ expect ( onPatchFn ) . toHaveBeenCalled ( ) ;
359
+ } ) ;
360
+
361
+ test ( "observable instances do emit a patch when shouldEmitPatchOnChange is undefined and setDefaultShouldEmitPatchOnChange hasn't been called" , ( ) => {
362
+ const observableArray = observable . array < string > ( [ ] ) ;
363
+
364
+ @register
365
+ class MyViewExample extends ClassModel ( { key : types . identifier , name : types . string } ) {
366
+ @snapshottedView ( )
367
+ get arrayLength ( ) {
368
+ return observableArray . length ;
369
+ }
370
+ }
371
+
372
+ const instance = MyViewExample . create ( { key : "1" , name : "Test" } ) ;
373
+
374
+ const onPatchFn = jest . fn ( ) ;
375
+ onPatch ( instance , onPatchFn ) ;
376
+
377
+ runInAction ( ( ) => {
378
+ observableArray . push ( "a" ) ;
379
+ } ) ;
380
+
381
+ expect ( onPatchFn ) . toHaveBeenCalled ( ) ;
382
+ } ) ;
383
+
384
+ test ( "observable instances do emit a patch when shouldEmitPatchOnChange is undefined and setDefaultShouldEmitPatchOnChange was passed true" , ( ) => {
385
+ setDefaultShouldEmitPatchOnChange ( true ) ;
386
+
387
+ const observableArray = observable . array < string > ( [ ] ) ;
388
+
389
+ @register
390
+ class MyViewExample extends ClassModel ( { key : types . identifier , name : types . string } ) {
391
+ @snapshottedView ( )
392
+ get arrayLength ( ) {
393
+ return observableArray . length ;
394
+ }
395
+ }
396
+
397
+ const instance = MyViewExample . create ( { key : "1" , name : "Test" } ) ;
398
+
399
+ const onPatchFn = jest . fn ( ) ;
400
+ onPatch ( instance , onPatchFn ) ;
401
+
402
+ runInAction ( ( ) => {
403
+ observableArray . push ( "a" ) ;
404
+ } ) ;
405
+
406
+ expect ( onPatchFn ) . toHaveBeenCalled ( ) ;
407
+ } ) ;
408
+
409
+ test ( "observable instances don't emit a patch when shouldEmitPatchOnChange is undefined and setDefaultShouldEmitPatchOnChange was passed false" , ( ) => {
410
+ setDefaultShouldEmitPatchOnChange ( false ) ;
411
+
412
+ const observableArray = observable . array < string > ( [ ] ) ;
413
+
414
+ @register
415
+ class MyViewExample extends ClassModel ( { key : types . identifier , name : types . string } ) {
416
+ @snapshottedView ( )
417
+ get arrayLength ( ) {
418
+ return observableArray . length ;
419
+ }
420
+ }
421
+
422
+ const instance = MyViewExample . create ( { key : "1" , name : "Test" } ) ;
423
+
424
+ const onPatchFn = jest . fn ( ) ;
425
+ onPatch ( instance , onPatchFn ) ;
426
+
427
+ runInAction ( ( ) => {
428
+ observableArray . push ( "a" ) ;
429
+ } ) ;
430
+
431
+ expect ( onPatchFn ) . not . toHaveBeenCalled ( ) ;
432
+ } ) ;
433
+ } ) ;
288
434
} ) ;
0 commit comments