Skip to content

Commit e00ca48

Browse files
committed
Complete linkFiber testing
1 parent be45418 commit e00ca48

File tree

3 files changed

+36
-49
lines changed

3 files changed

+36
-49
lines changed

src/backend/__tests__/linkFiber-testcases.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ export const classComponent: Fiber = {
5050
sibling: null,
5151
stateNode: {
5252
state: { count: 0 },
53-
setState: function (newState) {
54-
this.state = newState;
53+
setState: function (callback) {
54+
this.state = { ...callback() };
5555
},
5656
},
5757
child: null,
@@ -86,4 +86,9 @@ classPayload.addChild({ count: 0 }, 'IncrementClass', componentData, null);
8686

8787
export const updateClassPayload = new Tree('root', 'root');
8888
updateClassPayload.route = rootPayload.route;
89-
updateClassPayload.addChild({ count: 2 }, 'IncrementClass', { ...componentData, count: 2 }, null);
89+
updateClassPayload.addChild(
90+
{ count: 2 },
91+
'IncrementClass',
92+
{ ...componentData, state: { count: 2 } },
93+
null,
94+
);

src/backend/__tests__/linkFiber.test.tsx

Lines changed: 28 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,19 @@ import linkFiberInitialization from '../routers/linkFiber';
22
import { Snapshot, Status, FiberRoot } from '../types/backendTypes';
33
import Tree from '../models/tree';
44
import { DevTools } from '../types/linkFiberTypes';
5-
import updateAndSendSnapShotTree from '../routers/snapShot';
6-
import throttle from '../controllers/throttle';
7-
import createTree from '../controllers/createTree/createTree';
8-
import routes from '../models/routes';
5+
96
import { JSDOM } from 'jsdom';
107
import path from 'path';
118
import fs from 'fs';
129
import componentActionsRecord from '../models/masterState';
1310
import {
14-
functionalComponent,
1511
classComponent,
1612
root,
1713
rootPayload,
1814
classPayload,
1915
updateClassPayload,
2016
} from './linkFiber-testcases';
2117
import timeJumpInitialization from '../controllers/timeJump';
22-
// import Increment from './Increment'; // functional component
23-
// import React from 'react';
24-
// import { createRoot } from 'react-dom/client';
25-
// import { FunctionComponent } from '../types/backendTypes';
2618

2719
describe('linkFiber', () => {
2820
let snapshot: Snapshot;
@@ -42,15 +34,6 @@ describe('linkFiber', () => {
4234
global.window = dom.window;
4335
global.document = dom.window._document;
4436

45-
// Append functional component on browser:
46-
// const domNode = document.getElementById('root');
47-
// const root = await createRoot(domNode);
48-
// root.render(<Increment />);
49-
50-
// Get the fiber root object
51-
// fiberRoot = root._internalRoot;
52-
53-
// fiberRoot.current = { ...fiberRoot.current, elementType: Increment, tag: FunctionComponent };
5437
// Initialize Fiber Root:
5538
fiberRoot = { current: root };
5639
});
@@ -60,12 +43,6 @@ describe('linkFiber', () => {
6043
dom.window.close();
6144
});
6245

63-
beforeEach(() => {
64-
// routes = new Routes();
65-
// window.history.replaceState = jest.fn();
66-
// window.history.pushState = jest.fn();
67-
});
68-
6946
beforeEach(() => {
7047
// Create snapshot and mode objects
7148
snapshot = {
@@ -231,6 +208,21 @@ describe('linkFiber', () => {
231208
});
232209

233210
describe('mode unit tests', () => {
211+
it('should not send snapshot if mode is jumping & not navigating', async () => {
212+
// When first initialize linkFiber, should not have the update class payload
213+
fiberRoot = { current: classComponent };
214+
await new Promise(linkFiberDelayed);
215+
mockPostMessage.mockClear();
216+
217+
// Simulate jumping and navigating
218+
mode.jumping = true;
219+
await new Promise((resolve) =>
220+
setTimeout(async () => resolve(await devTools.onCommitFiberRoot(0, fiberRoot, 'high')), 75),
221+
);
222+
// During jumping &/or navigating, should not post any message/snapshot to front end
223+
expect(mockPostMessage).not.toHaveBeenCalled();
224+
});
225+
234226
it('should update react fiber tree based on the payload from frontend when mode is navigating', async () => {
235227
// When first initialize linkFiber, should not have the update class payload
236228
fiberRoot = { current: classComponent };
@@ -246,28 +238,26 @@ describe('linkFiber', () => {
246238

247239
// Simulate jumping and navigating
248240
mode.jumping = true;
249-
console.log(updateClassPayload.children[0]);
250241
mode.navigating = () => timeJump(updateClassPayload);
251242
await new Promise((resolve) =>
252243
setTimeout(async () => resolve(await devTools.onCommitFiberRoot(0, fiberRoot, 'high')), 75),
253244
);
254-
console.log('MODE UNIT TEST', componentActionsRecord.getAllComponents()[0].state.toString());
255245
// During jumping &/or navigating, should not post any message/snapshot to front end
256246
expect(mockPostMessage).not.toHaveBeenCalled();
257247

258248
// After navigate, react application should have updateClassPayload
259-
// mode.jumping = false;
260-
// await new Promise((resolve) =>
261-
// setTimeout(async () => resolve(await devTools.onCommitFiberRoot(0, fiberRoot, 'high')), 75),
262-
// );
263-
// expect(mockPostMessage).toHaveBeenCalledTimes(1);
264-
// expect(mockPostMessage).toHaveBeenCalledWith(
265-
// {
266-
// action: 'recordSnap',
267-
// payload: updateClassPayload,
268-
// },
269-
// '*',
270-
// );
249+
mode.jumping = false;
250+
await new Promise((resolve) =>
251+
setTimeout(async () => resolve(await devTools.onCommitFiberRoot(0, fiberRoot, 'high')), 75),
252+
);
253+
expect(mockPostMessage).toHaveBeenCalledTimes(1);
254+
expect(mockPostMessage).toHaveBeenCalledWith(
255+
{
256+
action: 'recordSnap',
257+
payload: updateClassPayload,
258+
},
259+
'*',
260+
);
271261
});
272262
});
273263
});

src/backend/controllers/timeJump.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ export default function timeJumpInitiation(mode: Status) {
2424
* @param targetSnapshot - The target snapshot to re-render. The payload from index.ts is assigned to targetSnapshot
2525
*/
2626
return async function timeJump(targetSnapshot: Tree): Promise<void> {
27-
console.log({ targetSnapshot: targetSnapshot.children[0] });
2827
mode.jumping = true;
2928
console.log('timeJump - START JUMPING');
3029
// Reset mode.navigating
@@ -70,11 +69,6 @@ async function updateReactFiberTree(
7069
// Check if it is a stateful class component
7170
// Index can be zero => falsy value => DO NOT REMOVE NULL
7271
if (index !== null) {
73-
console.log({
74-
componentActionsRecord: componentActionsRecord.getAllComponents()[0].state,
75-
state,
76-
name: targetSnapshot.name,
77-
});
7872
// Obtain the BOUND update method at the given index
7973
const classComponent = componentActionsRecord.getComponentByIndex(index);
8074
// Update component state
@@ -84,8 +78,6 @@ async function updateReactFiberTree(
8478
);
8579
// Iterate through new children after state has been set
8680
targetSnapshot.children.forEach((child) => updateReactFiberTree(child, circularComponentTable));
87-
console.log({ componentActionsRecord: componentActionsRecord.getAllComponents() });
88-
8981
return;
9082
}
9183

0 commit comments

Comments
 (0)