Skip to content

Commit 39ff5e6

Browse files
committed
adding tree test
1 parent e35289f commit 39ff5e6

File tree

6 files changed

+94
-43
lines changed

6 files changed

+94
-43
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",
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: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
1-
const puppeteer = require('puppeteer');
1+
// const puppeteer = require('puppeteer');
2+
// const path = import('path');
23

3-
test('Adds two numbers', () => {
4-
const sum = 1+2;
5-
expect(sum).toEqual(3);
6-
})
4+
// test('Adds two numbers', () => {
5+
// const sum = 1+2;
6+
// expect(sum).toEqual(3);
7+
// })
78

8-
test('We can launch a browser', async () => {
9-
const browser = await puppeteer.launch({
10-
headless: false
11-
});
9+
// test('We can launch a browser', async () => {
10+
// const browser = await puppeteer.launch({
11+
// headless: false
12+
// });
1213

13-
const page = await browser.newPage();
14-
await page.goto('localhost:3000');
15-
})
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
});

src/backend/__tests__/tree.test.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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('its children can recognize the parent, () => {
65+
expect(returnChild.parent).toEqual(newTree);
66+
expect(returnSibling.parent).toEqual(newTree);
67+
})
68+
})
69+
})

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/tree.ts

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -110,26 +110,6 @@ class Tree {
110110
copyInstances--;
111111
return copy;
112112
}
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-
// }
133113
}
134114

135115
export default Tree;

0 commit comments

Comments
 (0)