Skip to content

Commit 7096df5

Browse files
added a lot code to testing
1 parent 350be44 commit 7096df5

File tree

3 files changed

+133
-47
lines changed

3 files changed

+133
-47
lines changed

src/backend/__tests__/linkFiber.test.tsx

Lines changed: 128 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ describe('linkFiber', () => {
99
let snapShot: Snapshot;
1010
let mode: Status;
1111
let linkFiber;
12+
let fiberRoot: FiberRoot;
1213
const mockPostMessage = jest.fn();
1314

1415
beforeEach(() => {
@@ -22,6 +23,23 @@ describe('linkFiber', () => {
2223
navigating: undefined,
2324
};
2425

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+
2543
linkFiber = linkFiberInitialization(snapShot, mode);
2644
// Set up mock postMessage function
2745
window.postMessage = mockPostMessage;
@@ -31,10 +49,12 @@ describe('linkFiber', () => {
3149
renderers: new Map<1, { version: string }>([[1, { version: '16' }]]),
3250
inject: jest.fn(),
3351
supportsFiber: true,
34-
onCommitFiberRoot: jest.fn(),
52+
onCommitFiberRoot: jest.fn((...args) => {
53+
console.log(...args);
54+
}),
3555
onCommitFiberUnmount: jest.fn(),
3656
rendererInterfaces: {},
37-
getFiberRoots: jest.fn(() => [{ current: { tag: 0 } }]),
57+
getFiberRoots: jest.fn(() => [{ current: { tag: 3 } }]),
3858
};
3959
});
4060

@@ -43,53 +63,120 @@ describe('linkFiber', () => {
4363
delete window.__REACT_DEVTOOLS_GLOBAL_HOOK__;
4464
});
4565

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+
});
4970

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+
});
5274
});
5375

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+
});
64120
});
65121

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+
});
70128
});
71129

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+
});
90169
});
91170

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+
});
93174

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+
});
95182
});

src/backend/__tests__/throttle.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ describe('throttle unit tests', () => {
1818
expect(typeof result).toBe('function');
1919
});
2020

21-
test('throttled function should be called with the correct arguments', () => {
21+
it('throttled function should be called with the correct arguments', () => {
2222
const throttledFunc = throttle(mockCallback, 1000);
2323

2424
throttledFunc(1, 2, 3);

src/backend/routers/linkFiber.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,17 @@ export default function linkFiber(snapShot: Snapshot, mode: Status): () => void
4747
* @function throttledUpdateSnapshot - a function that will wait for at least MIN_TIME_BETWEEN_UPDATE ms, before updating the tree snapShot being displayed on the Chrome Extension.
4848
*/
4949
const throttledUpdateSnapshot = throttle(async (fiberRoot) => {
50-
console.log('linkFiber - RERENDER');
50+
// console.log('linkFiber - RERENDER');
5151
// If not jumping
5252
if (!mode.jumping) {
53-
console.log('linkFiber - SEND SNAPSHOT');
53+
// console.log('linkFiber - SEND SNAPSHOT');
5454
// Update and Send SnapShot tree to front end
5555
updateAndSendSnapShotTree(snapShot, fiberRoot);
5656
}
5757

5858
// If navigating to another route during jumping:
5959
else if (mode.navigating) {
60-
console.log('linkFiber - NAVIGATING');
60+
// console.log('linkFiber - NAVIGATING');
6161
// Reset the array containing update methods:
6262
componentActionsRecord.clear();
6363
// Obtain new update methods for the current route:
@@ -100,7 +100,6 @@ export default function linkFiber(snapShot: Snapshot, mode: Status): () => void
100100
const reactInstance = devTools.renderers.get(1);
101101
// If target application is not a React App, this will return undefined.
102102
if (!reactInstance) {
103-
console.log('not a react instance', reactInstance);
104103
return;
105104
}
106105
// If target application is a React App, send a message to front end.
@@ -124,7 +123,7 @@ export default function linkFiber(snapShot: Snapshot, mode: Status): () => void
124123
// ---------OBTAIN THE INITIAL FIBEROOTNODE FROM REACT DEV TOOL-------------
125124
// Obtain the FiberRootNode, which is the first value in the FiberRoot Set:
126125
fiberRoot = devTools.getFiberRoots(1).values().next().value;
127-
console.log('fiberRoot', fiberRoot);
126+
// console.log('fiberRoot', fiberRoot);
128127

129128
// ----------INITIALIZE THE TREE SNAP SHOT ON CHROME EXTENSION--------------
130129
throttledUpdateSnapshot(fiberRoot); // only runs on start up

0 commit comments

Comments
 (0)