Skip to content

Commit 1f853c7

Browse files
committed
add some tests
1 parent 89a466b commit 1f853c7

File tree

3 files changed

+327
-1
lines changed

3 files changed

+327
-1
lines changed

.babelrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
"presets": ["airbnb", "@babel/preset-typescript"],
3-
"plugins": ["@emotion"]
3+
// "plugins": ["@emotion"]
44
}

src/backend/routers/snapShot.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Snapshot, FiberRoot } from '../types/backendTypes';
22
import componentActionsRecord from '../models/masterState';
33
import routes from '../models/routes';
44
import createTree from '../controllers/createTree';
5+
const _ = require('lodash');
56

67
// -------------------------UPDATE & SEND TREE SNAP SHOT------------------------
78
/**
@@ -13,6 +14,42 @@ import createTree from '../controllers/createTree';
1314
*/
1415
// updating tree depending on current mode on the panel (pause, etc)
1516
export default function updateAndSendSnapShotTree(fiberRoot: FiberRoot): void {
17+
18+
// This function compares trees with one another
19+
function isIdentical(tree1, tree2) {
20+
// Check if both are null
21+
if (!tree1 && !tree2) return true;
22+
23+
// Check if either of them is null
24+
if (!tree1 || !tree2) return false;
25+
26+
// Get the keys
27+
const tree1Keys = Object.keys(tree1);
28+
const tree2Keys = Object.keys(tree2);
29+
30+
// Check if length of keys is different
31+
if (tree1Keys.length !== tree2Keys.length) return false;
32+
33+
// Check keys and values
34+
for (let key of tree1Keys) {
35+
const val1 = tree1[key];
36+
const val2 = tree2[key];
37+
38+
const areObjects = isObject(val1) && isObject(val2);
39+
if (
40+
(areObjects && !isIdentical(val1, val2)) ||
41+
(!areObjects && val1 !== val2)
42+
) {
43+
return false;
44+
}
45+
}
46+
return true;
47+
}
48+
49+
function isObject(object) {
50+
return object != null && typeof object === 'object';
51+
}
52+
1653
// This is the currently active root fiber(the mutable root of the tree)
1754
const { current } = fiberRoot;
1855
// Clear all of the legacy actions from old fiber tree because we are about to create a new one
@@ -29,7 +66,16 @@ export default function updateAndSendSnapShotTree(fiberRoot: FiberRoot): void {
2966
// the postMessage action will be received on the content script to later update the tabsObj
3067
// this will fire off everytime there is a change in test application
3168
// convert the payload from a fiber tree to an object to avoid a data clone error when postMessage processes the argument
69+
console.log('This is the payload, baby: ', payload);
70+
const clonedDeepPayload = _.cloneDeep(payload);
71+
console.log('cloneDeepPayload tree', clonedDeepPayload);
72+
// compare payload and clonedDeepPayload with isIdentical
73+
// console.log('are they identical?', isIdentical(payload, clonedDeepPayload));
74+
// console.log('typeof payload', typeof payload);
3275
const obj = JSON.parse(JSON.stringify(payload));
76+
// console.log('righton, this is the obj: ', obj)
77+
// console.log('typeof obj', typeof obj);
78+
console.log('passing in obj');
3379
window.postMessage(
3480
{
3581
action: 'recordSnap',

0 commit comments

Comments
 (0)