1
1
import React , { Component } from 'react' ;
2
2
import { render , cleanup , act } from '@testing-library/react/pure' ;
3
- // eslint-disable-next-line import/no-unresolved
4
- import { view , store , batch } from 'react-easy-state' ;
3
+ import {
4
+ view ,
5
+ store ,
6
+ autoEffect ,
7
+ clearEffect ,
8
+ batch ,
9
+ // eslint-disable-next-line import/no-unresolved
10
+ } from 'react-easy-state' ;
5
11
6
12
describe ( 'batching' , ( ) => {
7
13
afterEach ( cleanup ) ;
8
14
9
- test ( 'should batch state changes inside a batch() wrapper' , ( ) => {
15
+ test ( 'should batch state changes inside a batch() wrapper for views ' , ( ) => {
10
16
let renderCount = 0 ;
11
17
const person = store ( { name : 'Bob' } ) ;
12
18
const MyComp = view ( ( ) => {
@@ -27,6 +33,62 @@ describe('batching', () => {
27
33
expect ( renderCount ) . toBe ( 2 ) ;
28
34
} ) ;
29
35
36
+ test ( 'should batch state changes inside a batch() wrapper for global autoEffects' , ( ) => {
37
+ let numOfRuns = 0 ;
38
+ let name = '' ;
39
+
40
+ const person = store ( { name : 'Bob' } ) ;
41
+ const effect = autoEffect ( ( ) => {
42
+ numOfRuns += 1 ;
43
+ name = person . name ;
44
+ } ) ;
45
+
46
+ expect ( name ) . toBe ( 'Bob' ) ;
47
+ expect ( numOfRuns ) . toBe ( 1 ) ;
48
+
49
+ batch ( ( ) => {
50
+ person . name = 'Ann' ;
51
+ person . name = 'Rick' ;
52
+ } ) ;
53
+
54
+ expect ( name ) . toBe ( 'Rick' ) ;
55
+ expect ( numOfRuns ) . toBe ( 2 ) ;
56
+
57
+ clearEffect ( effect ) ;
58
+ } ) ;
59
+
60
+ test ( 'should batch state changes inside a batch() wrapper for local autoEffects' , ( ) => {
61
+ let effectCount = 0 ;
62
+ let renderCount = 0 ;
63
+ let name = '' ;
64
+
65
+ const person = store ( { name : 'Bob' } ) ;
66
+ const MyComp = view ( ( ) => {
67
+ renderCount += 1 ;
68
+ autoEffect ( ( ) => {
69
+ effectCount += 1 ;
70
+ name = person . name ;
71
+ } ) ;
72
+ return < div > { person . name } </ div > ;
73
+ } ) ;
74
+
75
+ const { container } = render ( < MyComp /> ) ;
76
+ expect ( container ) . toHaveTextContent ( 'Bob' ) ;
77
+ expect ( name ) . toBe ( 'Bob' ) ;
78
+ expect ( effectCount ) . toBe ( 1 ) ;
79
+ expect ( renderCount ) . toBe ( 1 ) ;
80
+
81
+ batch ( ( ) => {
82
+ person . name = 'Ann' ;
83
+ person . name = 'Rick' ;
84
+ } ) ;
85
+
86
+ expect ( name ) . toBe ( 'Rick' ) ;
87
+ expect ( container ) . toHaveTextContent ( 'Rick' ) ;
88
+ expect ( effectCount ) . toBe ( 2 ) ;
89
+ expect ( renderCount ) . toBe ( 2 ) ;
90
+ } ) ;
91
+
30
92
test ( 'should batch state changes inside a batch() wrapper in a class component' , ( ) => {
31
93
let renderCount = 0 ;
32
94
const person = store ( { name : 'Bob' } ) ;
0 commit comments