Skip to content

Commit 3985b3e

Browse files
authored
Merge pull request #13 from hienqn/addingtest
Adding test for tree and masterState
2 parents f00e38b + 3a73f2b commit 3985b3e

File tree

8 files changed

+164
-37
lines changed

8 files changed

+164
-37
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"scripts": {
1616
"build": "webpack --mode production",
1717
"dev": "webpack --mode development --watch",
18-
"test": "jest --verbose --coverage --watchAll --forceExit",
18+
"test": "jest --verbose --coverage --watchAll --runInBand --detectOpenHandles --forceExit",
1919
"docker-test-lint": "eslint --ext .js --ext .jsx src",
2020
"docs": "typedoc --json docs --inputFiles src/app --inputFiles src/backend --readme docs/readme.md"
2121
},
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// const puppeteer = require('puppeteer');
2+
// const path = import('path');
3+
4+
// test('Adds two numbers', () => {
5+
// const sum = 1+2;
6+
// expect(sum).toEqual(3);
7+
// })
8+
9+
// test('We can launch a browser', async () => {
10+
// const browser = await puppeteer.launch({
11+
// headless: false
12+
// });
13+
14+
// const page = await browser.newPage();
15+
// await page.goto('localhost:3000');
16+
// // console.log(await page.addScriptTag({path: 'src/extension/build/bundles/backend.bundle.js'}));
17+
// // console.log(page);
18+
// // const script = document.createElement('script');
19+
// // script.setAttribute('type', 'text/javascript');
20+
// // script.setAttribute('src', 'file');
21+
// })

src/backend/__tests__/helpers.test.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
/* eslint-disable react/jsx-filename-extension */
66
/* eslint-disable jest/valid-describe */
77
/* eslint-disable react/react-in-jsx-scope */
8-
import { configure } from 'enzyme';
9-
import Adapter from 'enzyme-adapter-react-16';
8+
// import { configure } from 'enzyme';
9+
// import Adapter from 'enzyme-adapter-react-16';
1010
// import toJson from 'enzyme-to-json';
1111
import { throttle, getHooksNames } from '../helpers';
1212

1313
// Newer Enzyme versions require an adapter to a particular version of React
14-
configure({ adapter: new Adapter() });
14+
// configure({ adapter: new Adapter() });
1515

1616
// Replace any setTimeout functions with jest timer
1717
jest.useFakeTimers();
@@ -41,19 +41,15 @@ describe('AST Unit Tests', () => {
4141
jest.advanceTimersByTime(20);
4242
throttledMockFunc();
4343
expect(mockFunc).toHaveBeenCalledTimes(1);
44-
4544
jest.advanceTimersByTime(941);
46-
4745
expect(mockFunc).toHaveBeenCalledTimes(2);
4846
});
4947

5048
it('Should only invoke function', () => {
5149
// Because we haven't invoked returned function from throttle
5250
// mock func should not have been called yet
5351
expect(mockFunc).not.toHaveBeenCalled();
54-
5552
throttledMockFunc();
56-
5753
expect(mockFunc).toHaveBeenCalledTimes(1);
5854
});
5955
});
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import masterState from '../masterState'
2+
import {
3+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
4+
HookStateItem,
5+
HookStates
6+
} from '../types/backendTypes';
7+
8+
describe('Testing masterState functionality', () => {
9+
let hookOne : HookStateItem = {state: 'A', component: 'counter1'};
10+
let hookTwo : HookStateItem = {state: 'B', component: 'counter2'};
11+
let allHooks : HookStates = [hookOne, hookTwo];
12+
masterState.saveNew(hookOne.state, hookOne.component);
13+
masterState.saveNew(hookTwo.state, hookTwo.component);
14+
15+
describe('Save new', () => {
16+
masterState.saveNew(hookOne.state, hookOne.component);
17+
masterState.saveNew(hookTwo.state, hookTwo.component);
18+
})
19+
20+
describe('getComponentByIndex', () => {
21+
it('should be able to get both hook states component', () => {
22+
expect(masterState.getComponentByIndex(0)).toEqual(hookOne.component);
23+
expect(masterState.getComponentByIndex(1)).toEqual(hookTwo.component);
24+
})
25+
})
26+
27+
describe('getRecordByIndex', () => {
28+
it('should be able to get both hook states', () => {
29+
expect(masterState.getRecordByIndex(0)).toEqual(hookOne);
30+
expect(masterState.getRecordByIndex(1)).toEqual(hookTwo);
31+
})
32+
})
33+
34+
35+
})

src/backend/__tests__/tree.test.ts

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import Tree from '../tree';
2+
import { networkInterfaces } from 'os';
3+
4+
describe('Tree unit test', () => {
5+
describe('Constructor', () => {
6+
let newTree = new Tree({});
7+
8+
it('should be able to create a newTree', () => {
9+
expect(newTree.state).toEqual({});
10+
})
11+
12+
it('should have 5 properties', () => {
13+
expect(newTree).toHaveProperty('state');
14+
expect(newTree).toHaveProperty('name');
15+
expect(newTree).toHaveProperty('componentData');
16+
expect(newTree).toHaveProperty('children');
17+
expect(newTree).toHaveProperty('parent');
18+
})
19+
20+
it('has name default value as stateless', () => {
21+
expect(newTree.name).toBe('nameless');
22+
})
23+
24+
it('has children as an empty array', () => {
25+
expect(newTree.children).toEqual([]);
26+
})
27+
})
28+
29+
30+
describe('Adding children', () => {
31+
let newTree = new Tree({});
32+
let returnChild = newTree.addChild('stateful', 'child', {});
33+
34+
it('should be able to add a child', () => {
35+
expect(typeof newTree.children).toEqual('object');
36+
expect(Array.isArray(newTree.children)).toBeTruthy;
37+
})
38+
39+
it(`its parent should be newTree`, () => {
40+
expect(returnChild.parent).toEqual(newTree);
41+
})
42+
43+
it('parent now contains an array of children and each children is a valid tree', () => {
44+
expect(newTree.children[0]).toHaveProperty('state');
45+
expect(newTree.children[0]).toHaveProperty('name');
46+
expect(newTree.children[0]).toHaveProperty('componentData');
47+
})
48+
})
49+
50+
describe('Adding sibling', () => {
51+
let newTree = new Tree({});
52+
let returnChild = newTree.addChild('stateful', 'child', {});
53+
let returnSibling = returnChild.addSibling('stateful', 'child', {});
54+
55+
it('the tree now has 2 children', () => {
56+
expect(newTree.children.length).toBe(2);
57+
})
58+
59+
it('both of the children has the parent as this tree', () => {
60+
expect(newTree.children[0]).toEqual(returnChild);
61+
expect(newTree.children[1]).toEqual(returnSibling);
62+
})
63+
64+
it('both of the children has the parent as this tree', () => {
65+
expect(returnChild.parent).toEqual(newTree);
66+
expect(returnSibling.parent).toEqual(newTree);
67+
})
68+
})
69+
70+
71+
describe('Adding sibling', () => {
72+
let newTree = new Tree({});
73+
let returnChild = newTree.addChild('stateful', 'child', {});
74+
let returnSibling = returnChild.addSibling('stateful', 'child', {});
75+
it('the tree now has 2 children', () => {
76+
expect(newTree.children.length).toBe(2);
77+
})
78+
79+
it('both of the children has the parent as this tree', () => {
80+
expect(newTree.children[0]).toEqual(returnChild);
81+
expect(newTree.children[1]).toEqual(returnSibling);
82+
})
83+
84+
it('both of the children has the parent as this tree', () => {
85+
expect(returnChild.parent).toEqual(newTree);
86+
expect(returnSibling.parent).toEqual(newTree);
87+
})
88+
})
89+
90+
91+
describe('Copy & clean tree', () => {
92+
let newTree = new Tree({});
93+
let returnChild = newTree.addChild('stateful', 'child', {});
94+
let returnSibling = returnChild.addSibling('stateful', 'child', {});
95+
let copy = newTree.cleanTreeCopy();
96+
it('its copy has 2 children', () => {
97+
expect(copy.children.length).toEqual(2);
98+
})
99+
})
100+
})

src/backend/linkFiber.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ export default (snap: Snapshot, mode: Mode): (() => void) => {
6666
if (!snap.tree) {
6767
snap.tree = new Tree('root', 'root');
6868
}
69+
6970
const payload = snap.tree.cleanTreeCopy(); // snap.tree.getCopy();
7071

7172
window.postMessage(
@@ -309,7 +310,7 @@ export default (snap: Snapshot, mode: Mode): (() => void) => {
309310
circularComponentTable.add(sibling);
310311
createTree(sibling, newNode, true);
311312
}
312-
313+
313314
return tree;
314315
}
315316

@@ -318,9 +319,8 @@ export default (snap: Snapshot, mode: Mode): (() => void) => {
318319
const { current } = fiberRoot;
319320
circularComponentTable.clear();
320321
snap.tree = createTree(current);
322+
console.log(snap.tree);
321323
}
322-
323-
324324
sendSnapshot();
325325
}
326326

src/backend/timeJump.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ export default (origin, mode) => {
6464
const hookState = Object.values(hook);
6565
console.log('target', target);
6666
if (hooksComponent && hooksComponent.dispatch) {
67-
6867
hooksComponent.dispatch(hookState[0]);
6968
}
7069
});
@@ -76,8 +75,6 @@ export default (origin, mode) => {
7675
jump(child);
7776
}
7877
});
79-
80-
// }
8178
}
8279

8380
return (target, firstCall = false) => {

src/backend/tree.ts

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -97,39 +97,17 @@ class Tree {
9797
// copy.children = this.children;
9898

9999
// creates copy of each child of the present node
100-
101100
copy.children = this.children.map((child: Tree): Tree | string => {
102101
if (!circularComponentTable.has(child)) {
103102
return child.cleanTreeCopy();
104103
}
105104
return 'circular';
106105
});
107-
108-
106+
109107
// returns copy
110108
copyInstances--;
111109
return copy;
112110
}
113-
114-
// print out the tree structure in the console
115-
// DEV: Process may be different for useState components
116-
// BUG FIX: Don't print the Router as a component
117-
// Change how the children are printed
118-
// print() {
119-
// const children = ['children: '];
120-
// // DEV: What should we push instead for components using hooks (it wouldn't be state)
121-
// // if this.children is always initialized to empty array, when would there ever be anything to iterate through here?
122-
// this.children.forEach((child: any) => {
123-
// children.push(child.state || child.component.state);
124-
// });
125-
// if (this.name) console.log('this.name if exists: ', this.name);
126-
// if (children.length === 1) {
127-
// console.log(`children length 1. ${this.state ? 'this.state: ' : 'this.component.state: '}`, this.state || this.component.state);
128-
// } else console.log(`children length !== 1. ${this.state ? 'this.state: ' : 'this.component.state, children: '}`, this.state || this.component.state, ...children);
129-
// this.children.forEach((child: any) => {
130-
// child.print();
131-
// });
132-
// }
133111
}
134112

135113
export default Tree;

0 commit comments

Comments
 (0)