@@ -9,6 +9,7 @@ describe('linkFiber', () => {
9
9
let snapShot : Snapshot ;
10
10
let mode : Status ;
11
11
let linkFiber ;
12
+ let fiberRoot : FiberRoot ;
12
13
const mockPostMessage = jest . fn ( ) ;
13
14
14
15
beforeEach ( ( ) => {
@@ -22,6 +23,23 @@ describe('linkFiber', () => {
22
23
navigating : undefined ,
23
24
} ;
24
25
26
+ fiberRoot = {
27
+ current : {
28
+ tag : 3 ,
29
+ key : null ,
30
+ elementType : 'div' ,
31
+ type : 'div' ,
32
+ stateNode : { } ,
33
+ child : null ,
34
+ sibling : null ,
35
+ index : 0 ,
36
+ memoizedState : null ,
37
+ memoizedProps : { } ,
38
+ dependencies : null ,
39
+ _debugHookTypes : [ ] ,
40
+ } ,
41
+ } ;
42
+
25
43
linkFiber = linkFiberInitialization ( snapShot , mode ) ;
26
44
// Set up mock postMessage function
27
45
window . postMessage = mockPostMessage ;
@@ -31,10 +49,12 @@ describe('linkFiber', () => {
31
49
renderers : new Map < 1 , { version : string } > ( [ [ 1 , { version : '16' } ] ] ) ,
32
50
inject : jest . fn ( ) ,
33
51
supportsFiber : true ,
34
- onCommitFiberRoot : jest . fn ( ) ,
52
+ onCommitFiberRoot : jest . fn ( ( ...args ) => {
53
+ console . log ( ...args ) ;
54
+ } ) ,
35
55
onCommitFiberUnmount : jest . fn ( ) ,
36
56
rendererInterfaces : { } ,
37
- getFiberRoots : jest . fn ( ( ) => [ { current : { tag : 0 } } ] ) ,
57
+ getFiberRoots : jest . fn ( ( ) => [ { current : { tag : 3 } } ] ) ,
38
58
} ;
39
59
} ) ;
40
60
@@ -43,53 +63,120 @@ describe('linkFiber', () => {
43
63
delete window . __REACT_DEVTOOLS_GLOBAL_HOOK__ ;
44
64
} ) ;
45
65
46
- xit ( 'link fiber should return a function' , ( ) => {
47
- expect ( typeof linkFiber ) . toBe ( 'function' ) ;
48
- } ) ;
66
+ describe ( 'link fiber initiliaztion' , ( ) => {
67
+ it ( 'link fiber should return a function' , ( ) => {
68
+ expect ( typeof linkFiber ) . toBe ( 'function' ) ;
69
+ } ) ;
49
70
50
- xit ( 'returned function should not throw an error' , ( ) => {
51
- expect ( ( ) => linkFiber ( ) ) . not . toThrowError ( ) ;
71
+ it ( 'returned function should not throw an error' , ( ) => {
72
+ expect ( ( ) => linkFiber ( ) ) . not . toThrowError ( ) ;
73
+ } ) ;
52
74
} ) ;
53
75
54
- xit ( 'should send message to front end that React DevTools is installed' , ( ) => {
55
- linkFiber ( ) ;
56
- expect ( mockPostMessage ) . toHaveBeenCalled ( ) ;
57
- expect ( mockPostMessage ) . toHaveBeenCalledWith (
58
- {
59
- action : 'devToolsInstalled' ,
60
- payload : 'devToolsInstalled' ,
61
- } ,
62
- '*' ,
63
- ) ;
76
+ describe ( 'React dev tools and react app check' , ( ) => {
77
+ it ( 'should send message to front end that React DevTools is installed' , ( ) => {
78
+ linkFiber ( ) ;
79
+ expect ( mockPostMessage ) . toHaveBeenCalled ( ) ;
80
+ expect ( mockPostMessage ) . toHaveBeenCalledWith (
81
+ {
82
+ action : 'devToolsInstalled' ,
83
+ payload : 'devToolsInstalled' ,
84
+ } ,
85
+ '*' ,
86
+ ) ;
87
+ } ) ;
88
+
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' , ( ) => {
96
+ linkFiber ( ) ;
97
+ // the third call is from the onCommitFiberRoot() function
98
+ expect ( mockPostMessage ) . toHaveBeenCalledTimes ( 3 ) ;
99
+ expect ( mockPostMessage ) . toHaveBeenCalledWith (
100
+ {
101
+ action : 'devToolsInstalled' ,
102
+ payload : 'devToolsInstalled' ,
103
+ } ,
104
+ '*' ,
105
+ ) ;
106
+ expect ( mockPostMessage ) . toHaveBeenCalledWith (
107
+ {
108
+ action : 'aReactApp' ,
109
+ payload : 'aReactApp' ,
110
+ } ,
111
+ '*' ,
112
+ ) ;
113
+ } ) ;
114
+
115
+ it ( 'should not do anything if the target application is not a React App' , ( ) => {
116
+ ( window as any ) . __REACT_DEVTOOLS_GLOBAL_HOOK__ . renderers = new Map ( ) ;
117
+ linkFiber ( ) ;
118
+ expect ( mockPostMessage ) . not . toHaveBeenCalledTimes ( 3 ) ;
119
+ } ) ;
64
120
} ) ;
65
121
66
- xit ( 'should not do anything if React Devtools is not installed' , ( ) => {
67
- delete window . __REACT_DEVTOOLS_GLOBAL_HOOK__ ;
68
- expect ( ( ) => linkFiber ( ) ) . not . toThrowError ( ) ;
69
- expect ( mockPostMessage ) . not . toHaveBeenCalled ( ) ;
122
+ describe ( 'document visibility' , ( ) => {
123
+ it ( 'should initiate an event listener for visibility change' , ( ) => {
124
+ const addEventListenerSpy = jest . spyOn ( document , 'addEventListener' ) ;
125
+ linkFiber ( ) ;
126
+ expect ( addEventListenerSpy ) . toHaveBeenCalledWith ( 'visibilitychange' , expect . any ( Function ) ) ;
127
+ } ) ;
70
128
} ) ;
71
129
72
- it ( 'should send a message to the front end if the target application is a React App' , ( ) => {
73
- linkFiber ( ) ;
74
- // the third call is from the onCommitFiberRoot() function
75
- expect ( mockPostMessage ) . toHaveBeenCalledTimes ( 3 ) ;
76
- expect ( mockPostMessage ) . toHaveBeenCalledWith (
77
- {
78
- action : 'devToolsInstalled' ,
79
- payload : 'devToolsInstalled' ,
80
- } ,
81
- '*' ,
82
- ) ;
83
- expect ( mockPostMessage ) . toHaveBeenCalledWith (
84
- {
85
- action : 'aReactApp' ,
86
- payload : 'aReactApp' ,
87
- } ,
88
- '*' ,
89
- ) ;
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 ) ;
160
+ } ) ;
161
+
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 ( ) ;
168
+ } ) ;
90
169
} ) ;
91
170
92
- xit ( 'should not do anything if the target application is not a React App' , ( ) => { } ) ;
171
+ describe ( 'addOneMoreStep' , ( ) => {
172
+ it ( 'should add a new step to the current path in the snapshot tree' , ( ) => { } ) ;
173
+ } ) ;
93
174
94
- xit ( 'should initiate an event listener for visibility change' , ( ) => { } ) ;
175
+ describe ( 'onCommitFiberRoot' , ( ) => {
176
+ it ( 'should call throttledUpdateSnapshot' , ( ) => {
177
+ linkFiber ( ) ;
178
+ const onCommitFiberRoot = window . __REACT_DEVTOOLS_GLOBAL_HOOK__ ?. onCommitFiberRoot ;
179
+ expect ( onCommitFiberRoot ) . toHaveBeenCalled ( ) ;
180
+ } ) ;
181
+ } ) ;
95
182
} ) ;
0 commit comments