@@ -4,6 +4,8 @@ import Tree from '../models/tree';
4
4
import { DevTools } from '../types/linkFiberTypes' ;
5
5
import updateAndSendSnapShotTree from '../routers/snapShot' ;
6
6
import throttle from '../controllers/throttle' ;
7
+ import createTree from '../controllers/createTree/createTree' ;
8
+ import routes from '../models/routes' ;
7
9
8
10
describe ( 'linkFiber' , ( ) => {
9
11
let snapShot : Snapshot ;
@@ -49,9 +51,7 @@ describe('linkFiber', () => {
49
51
renderers : new Map < 1 , { version : string } > ( [ [ 1 , { version : '16' } ] ] ) ,
50
52
inject : jest . fn ( ) ,
51
53
supportsFiber : true ,
52
- onCommitFiberRoot : jest . fn ( ( ...args ) => {
53
- console . log ( ...args ) ;
54
- } ) ,
54
+ onCommitFiberRoot : ( ) => console . log ( 'test' ) ,
55
55
onCommitFiberUnmount : jest . fn ( ) ,
56
56
rendererInterfaces : { } ,
57
57
getFiberRoots : jest . fn ( ( ) => [ { current : { tag : 3 } } ] ) ,
@@ -74,7 +74,13 @@ describe('linkFiber', () => {
74
74
} ) ;
75
75
76
76
describe ( 'React dev tools and react app check' , ( ) => {
77
- it ( 'should send message to front end that React DevTools is installed' , ( ) => {
77
+ it ( 'should not do anything if React Devtools is not installed' , ( ) => {
78
+ delete window . __REACT_DEVTOOLS_GLOBAL_HOOK__ ;
79
+ expect ( ( ) => linkFiber ( ) ) . not . toThrowError ( ) ;
80
+ expect ( mockPostMessage ) . not . toHaveBeenCalled ( ) ;
81
+ } ) ;
82
+
83
+ it ( 'should post a message to front end that React DevTools is installed' , ( ) => {
78
84
linkFiber ( ) ;
79
85
expect ( mockPostMessage ) . toHaveBeenCalled ( ) ;
80
86
expect ( mockPostMessage ) . toHaveBeenCalledWith (
@@ -86,15 +92,9 @@ describe('linkFiber', () => {
86
92
) ;
87
93
} ) ;
88
94
89
- it ( 'should not do anything if React Devtools is not installed' , ( ) => {
90
- delete window . __REACT_DEVTOOLS_GLOBAL_HOOK__ ;
91
- expect ( ( ) => linkFiber ( ) ) . not . toThrowError ( ) ;
92
- expect ( mockPostMessage ) . not . toHaveBeenCalled ( ) ;
93
- } ) ;
94
-
95
- it ( 'should send a message to the front end if the target application is a React App' , ( ) => {
95
+ it ( 'should post a message to the front end if the target application is a React App' , ( ) => {
96
96
linkFiber ( ) ;
97
- // the third call is from the onCommitFiberRoot() function
97
+ // the third call is from the onCommitFiberRoot() function to send snapshot to front end
98
98
expect ( mockPostMessage ) . toHaveBeenCalledTimes ( 3 ) ;
99
99
expect ( mockPostMessage ) . toHaveBeenCalledWith (
100
100
{
@@ -115,6 +115,7 @@ describe('linkFiber', () => {
115
115
it ( 'should not do anything if the target application is not a React App' , ( ) => {
116
116
( window as any ) . __REACT_DEVTOOLS_GLOBAL_HOOK__ . renderers = new Map ( ) ;
117
117
linkFiber ( ) ;
118
+ expect ( mockPostMessage ) . toHaveBeenCalledTimes ( 1 ) ;
118
119
expect ( mockPostMessage ) . not . toHaveBeenCalledTimes ( 3 ) ;
119
120
} ) ;
120
121
} ) ;
@@ -127,56 +128,56 @@ describe('linkFiber', () => {
127
128
} ) ;
128
129
} ) ;
129
130
130
- describe ( 'throttledUpdateSnapshot' , ( ) => {
131
- const mockUpdateAndSendSnapShotTree = jest . fn ( ) ;
132
-
133
- beforeEach ( ( ) => {
134
- jest . useFakeTimers ( ) ;
135
- mockUpdateAndSendSnapShotTree . mockClear ( ) ;
136
- } ) ;
137
-
138
- afterEach ( ( ) => {
139
- jest . runOnlyPendingTimers ( ) ;
140
- jest . useRealTimers ( ) ;
141
- } ) ;
142
-
143
- it ( 'throttled function should be called with the correct arguments' , ( ) => {
144
- const throttledUpdateSnapshot = throttle ( mockUpdateAndSendSnapShotTree , 1000 ) ;
145
- const onCoolDown = true ;
146
- const isCallQueued = true ;
147
- throttledUpdateSnapshot ( onCoolDown , isCallQueued ) ;
148
-
149
- expect ( mockUpdateAndSendSnapShotTree ) . toHaveBeenCalledWith ( onCoolDown , isCallQueued ) ;
150
- } ) ;
151
-
152
- it ( 'should call updateAndSendSnapShotTree only once per 100ms' , ( ) => {
153
- const throttledUpdateSnapshot = throttle ( mockUpdateAndSendSnapShotTree , 100 ) ;
154
- throttledUpdateSnapshot ( ) ;
155
- expect ( mockUpdateAndSendSnapShotTree ) . toHaveBeenCalledTimes ( 1 ) ;
156
- throttledUpdateSnapshot ( ) ;
157
- expect ( mockUpdateAndSendSnapShotTree ) . toHaveBeenCalledTimes ( 1 ) ;
158
- jest . advanceTimersByTime ( 100 ) ;
159
- expect ( mockUpdateAndSendSnapShotTree ) . toHaveBeenCalledTimes ( 2 ) ;
131
+ describe ( 'addOneMoreStep' , ( ) => {
132
+ it ( 'should monkey patch on onCommitFiberRoot' , ( ) => {
133
+ const mockOnCommitFiberRoot =
134
+ window . __REACT_DEVTOOLS_GLOBAL_HOOK__ ?. onCommitFiberRoot . toString ( ) ;
135
+ linkFiber ( ) ;
136
+ const onCommitFiberRoot = window . __REACT_DEVTOOLS_GLOBAL_HOOK__ ?. onCommitFiberRoot . toString ( ) ;
137
+ expect ( mockOnCommitFiberRoot ) . not . toEqual ( onCommitFiberRoot ) ;
160
138
} ) ;
161
139
162
- it ( 'should call throttle after visibility change' , ( ) => {
163
- const throttledUpdateSnapshot = throttle ( mockUpdateAndSendSnapShotTree , 100 ) ;
164
- // Simulate visibility change
165
- const visibilityChangeEvent = new Event ( 'visibilitychange' ) ;
166
- document . dispatchEvent ( visibilityChangeEvent ) ;
167
- expect ( throttle ) . toHaveBeenCalled ( ) ;
140
+ it ( 'should send a snapshot when new fiberRoot is committed' , ( ) => {
141
+ linkFiber ( ) ;
142
+ const payload = createTree ( fiberRoot . current ) ;
143
+ payload . route = routes . addRoute ( window . location . href ) ;
144
+ expect ( mockPostMessage ) . toHaveBeenCalled ( ) ;
145
+ expect ( mockPostMessage ) . toHaveBeenCalledTimes ( 3 ) ;
146
+ expect ( mockPostMessage ) . toHaveBeenCalledWith (
147
+ {
148
+ action : 'recordSnap' ,
149
+ payload,
150
+ } ,
151
+ '*' ,
152
+ ) ;
168
153
} ) ;
169
154
} ) ;
170
155
171
- describe ( 'addOneMoreStep' , ( ) => {
172
- it ( 'should add a new step to the current path in the snapshot tree' , ( ) => { } ) ;
173
- } ) ;
174
-
175
- describe ( 'onCommitFiberRoot' , ( ) => {
176
- it ( 'should call throttledUpdateSnapshot' , ( ) => {
177
- linkFiber ( ) ;
178
- const onCommitFiberRoot = window . __REACT_DEVTOOLS_GLOBAL_HOOK__ ?. onCommitFiberRoot ;
179
- expect ( onCommitFiberRoot ) . toHaveBeenCalled ( ) ;
156
+ describe ( 'mode unit tests' , ( ) => {
157
+ it ( 'should update react fiber tree based on the payload from frontend when mode is navigating' , ( ) => {
158
+ const newRoot : FiberRoot = {
159
+ current : {
160
+ tag : 1 ,
161
+ key : null ,
162
+ elementType : 'div' ,
163
+ type : 'div' ,
164
+ stateNode : {
165
+ state : { count : 2 } ,
166
+ setState : function ( newState ) {
167
+ this . state = newState ;
168
+ } ,
169
+ } ,
170
+ child : null ,
171
+ sibling : null ,
172
+ index : 0 ,
173
+ memoizedState : null ,
174
+ memoizedProps : { } ,
175
+ dependencies : null ,
176
+ _debugHookTypes : [ ] ,
177
+ } ,
178
+ } ;
179
+ const payload = createTree ( newRoot . current ) ;
180
+ payload . route = routes . addRoute ( window . location . href ) ;
180
181
} ) ;
181
182
} ) ;
182
183
} ) ;
0 commit comments