@@ -64,4 +64,51 @@ describe('update', () => {
64
64
65
65
expect ( ( ) => wrapper . unmount ( ) ) . not . toThrow ( )
66
66
} )
67
+
68
+ it ( 'components should be treacked' , ( ) => {
69
+ let renderCount = 0
70
+ const { withContext, useTracked, useUpdates } = Backset . create ( {
71
+ test : 'value1' ,
72
+ other : 0 ,
73
+ } )
74
+ const MockComponent1 = ( ) => {
75
+ const { test, other } = useUpdates ( )
76
+ return (
77
+ < div >
78
+ < button onClick = { e => test ( ( e . target as any ) . value ) } id = "btn" />
79
+ < button onClick = { ( ) => other ( last => last + 1 ) } id = "other" />
80
+ </ div >
81
+ )
82
+ }
83
+ const MockComponent2 = ( ) => {
84
+ const { test } = useTracked ( [ 'test' ] )
85
+ useEffect ( ( ) => {
86
+ renderCount ++
87
+ } )
88
+ return < span id = "value" > { test } </ span >
89
+ }
90
+ const MockWrapper = withContext ( ( ) => {
91
+ return (
92
+ < div >
93
+ < MockComponent1 />
94
+ < MockComponent2 />
95
+ </ div >
96
+ )
97
+ } )
98
+ const wrapper = mount ( < MockWrapper /> )
99
+ expect ( wrapper . find ( '#value' ) . text ( ) ) . toEqual ( 'value1' )
100
+ expect ( renderCount ) . toBe ( 1 )
101
+
102
+ const btn = wrapper . find ( '#btn' )
103
+ simulateClick ( btn , 'value2' )
104
+ expect ( wrapper . find ( '#value' ) . text ( ) ) . toEqual ( 'value2' )
105
+ expect ( renderCount ) . toBe ( 2 )
106
+
107
+ const otherButton = wrapper . find ( '#other' )
108
+ simulateClick ( otherButton , '' )
109
+ simulateClick ( otherButton , '' )
110
+ simulateClick ( otherButton , '' )
111
+ expect ( wrapper . find ( '#value' ) . text ( ) ) . toEqual ( 'value2' )
112
+ expect ( renderCount ) . toBe ( 2 )
113
+ } )
67
114
} )
0 commit comments