@@ -4,6 +4,8 @@ import Tree from '../models/tree';
44import { DevTools } from '../types/linkFiberTypes' ;
55import updateAndSendSnapShotTree from '../routers/snapShot' ;
66import throttle from '../controllers/throttle' ;
7+ import createTree from '../controllers/createTree/createTree' ;
8+ import routes from '../models/routes' ;
79
810describe ( 'linkFiber' , ( ) => {
911 let snapShot : Snapshot ;
@@ -49,9 +51,7 @@ describe('linkFiber', () => {
4951 renderers : new Map < 1 , { version : string } > ( [ [ 1 , { version : '16' } ] ] ) ,
5052 inject : jest . fn ( ) ,
5153 supportsFiber : true ,
52- onCommitFiberRoot : jest . fn ( ( ...args ) => {
53- console . log ( ...args ) ;
54- } ) ,
54+ onCommitFiberRoot : ( ) => console . log ( 'test' ) ,
5555 onCommitFiberUnmount : jest . fn ( ) ,
5656 rendererInterfaces : { } ,
5757 getFiberRoots : jest . fn ( ( ) => [ { current : { tag : 3 } } ] ) ,
@@ -74,7 +74,13 @@ describe('linkFiber', () => {
7474 } ) ;
7575
7676 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' , ( ) => {
7884 linkFiber ( ) ;
7985 expect ( mockPostMessage ) . toHaveBeenCalled ( ) ;
8086 expect ( mockPostMessage ) . toHaveBeenCalledWith (
@@ -86,15 +92,9 @@ describe('linkFiber', () => {
8692 ) ;
8793 } ) ;
8894
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' , ( ) => {
9696 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
9898 expect ( mockPostMessage ) . toHaveBeenCalledTimes ( 3 ) ;
9999 expect ( mockPostMessage ) . toHaveBeenCalledWith (
100100 {
@@ -115,6 +115,7 @@ describe('linkFiber', () => {
115115 it ( 'should not do anything if the target application is not a React App' , ( ) => {
116116 ( window as any ) . __REACT_DEVTOOLS_GLOBAL_HOOK__ . renderers = new Map ( ) ;
117117 linkFiber ( ) ;
118+ expect ( mockPostMessage ) . toHaveBeenCalledTimes ( 1 ) ;
118119 expect ( mockPostMessage ) . not . toHaveBeenCalledTimes ( 3 ) ;
119120 } ) ;
120121 } ) ;
@@ -127,56 +128,56 @@ describe('linkFiber', () => {
127128 } ) ;
128129 } ) ;
129130
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 ) ;
160138 } ) ;
161139
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+ ) ;
168153 } ) ;
169154 } ) ;
170155
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 ) ;
180181 } ) ;
181182 } ) ;
182183} ) ;
0 commit comments