Skip to content

Commit b76c137

Browse files
committed
Merge branch 'master' into map
2 parents 6ea11ff + 7561c47 commit b76c137

File tree

10 files changed

+324
-111
lines changed

10 files changed

+324
-111
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
},

src/backend/__tests__/astParser.test.js

Lines changed: 0 additions & 59 deletions
This file was deleted.
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: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/* eslint-disable max-len */
2+
/* eslint-disable jest/no-disabled-tests */
3+
/* eslint-disable import/no-extraneous-dependencies */
4+
/* eslint-disable react/button-has-type */
5+
/* eslint-disable react/jsx-filename-extension */
6+
/* eslint-disable jest/valid-describe */
7+
/* eslint-disable react/react-in-jsx-scope */
8+
// import { configure } from 'enzyme';
9+
// import Adapter from 'enzyme-adapter-react-16';
10+
// import toJson from 'enzyme-to-json';
11+
import { throttle, getHooksNames } from '../helpers';
12+
13+
// Newer Enzyme versions require an adapter to a particular version of React
14+
// configure({ adapter: new Adapter() });
15+
16+
// Replace any setTimeout functions with jest timer
17+
jest.useFakeTimers();
18+
19+
describe('AST Unit Tests', () => {
20+
21+
describe('throttle', () => {
22+
let mockFunc;
23+
let throttledMockFunc;
24+
25+
beforeEach(() => {
26+
mockFunc = jest.fn();
27+
throttledMockFunc = throttle(mockFunc, 1000);
28+
});
29+
30+
it('Should return a function', () => {
31+
expect(typeof throttledMockFunc).toBe('function');
32+
});
33+
34+
it('throttles subsequent fire attempts into one shot after cooldown', () => {
35+
throttledMockFunc();
36+
expect(mockFunc).toHaveBeenCalledTimes(1);
37+
jest.advanceTimersByTime(20);
38+
throttledMockFunc();
39+
jest.advanceTimersByTime(20);
40+
throttledMockFunc();
41+
jest.advanceTimersByTime(20);
42+
throttledMockFunc();
43+
expect(mockFunc).toHaveBeenCalledTimes(1);
44+
jest.advanceTimersByTime(941);
45+
expect(mockFunc).toHaveBeenCalledTimes(2);
46+
});
47+
48+
it('Should only invoke function', () => {
49+
// Because we haven't invoked returned function from throttle
50+
// mock func should not have been called yet
51+
expect(mockFunc).not.toHaveBeenCalled();
52+
throttledMockFunc();
53+
expect(mockFunc).toHaveBeenCalledTimes(1);
54+
});
55+
});
56+
57+
describe('getHooksNames', () => {
58+
it('Should return object with one getter/setter for a single useState instance', () => {
59+
const elementType = `function SingleUseFakeComponent() {
60+
const [testCount, setTestCount] = useState(0);
61+
const age = 20;
62+
return (<div> <p> You clicked this {testCount} times </p>
63+
<button onClick={() => setTestCount(testCount + 1)}>+1</button>
64+
<button onClick={() => setTestCount(testCount - 1)}>-1</button> <p>
65+
You are {age} years old! </p>
66+
<button onClick={age => age + 1}>Get Older</button>
67+
<hr />
68+
</div>);
69+
}`;
70+
71+
expect(getHooksNames(elementType)).toEqual(['testCount', 'setTestCount']);
72+
});
73+
74+
it('Should output the right number of properties when given multiple hooks', () => {
75+
const elementType = `function SingleUseFakeComponent() {
76+
const [testCount, setTestCount] = useState(0);
77+
const [biscuitCount, setBiscuitCount] = useState(0);
78+
const age = 20;
79+
return (<div> <p> You clicked this {testCount} times </p>
80+
<button onClick={() => setTestCount(testCount + 1)}>+1</button>
81+
<button onClick={() => setTestCount(testCount - 1)}>-1</button> <p>
82+
You are {age} years old! </p>
83+
<button onClick={age => age + 1}>Get Older</button>
84+
<hr />
85+
</div>);
86+
}`;
87+
88+
expect(getHooksNames(elementType)).toEqual(['testCount', 'setTestCount', 'biscuitCount', 'setBiscuitCount']);
89+
expect(Object.keys(getHooksNames(elementType))).toHaveLength(4);
90+
});
91+
92+
it('Should ignore any non-hook definitions', () => {
93+
const elementType = `function SingleUseFakeComponent() {
94+
const [testCount, setTestCount] = useState(0);
95+
const age = 20;
96+
return (<div> <p> You clicked this {testCount} times </p>
97+
<button onClick={() => setTestCount(testCount + 1)}>+1</button>
98+
<button onClick={() => setTestCount(testCount - 1)}>-1</button> <p>
99+
You are {age} years old! </p>
100+
<button onClick={age => age + 1}>Get Older</button>
101+
<hr />
102+
</div>);
103+
}`;
104+
105+
const expectedNumHookVariables = 2;
106+
expect(Object.keys(getHooksNames(elementType))).toHaveLength(expectedNumHookVariables);
107+
});
108+
109+
it('Should return an empty object if no hooks found', () => {
110+
const elementType = `function SingleUseFakeComponent() {
111+
const age = 20;
112+
return (<div> <p> You clicked this {testCount} times </p>
113+
<button onClick={() => setTestCount(testCount + 1)}>+1</button>
114+
<button onClick={() => setTestCount(testCount - 1)}>-1</button> <p>
115+
You are {age} years old! </p>
116+
<button onClick={age => age + 1}>Get Older</button>
117+
<hr />
118+
</div>);
119+
}`;
120+
121+
expect(getHooksNames(elementType)).toEqual([]);
122+
});
123+
124+
it('Should throw an error if input returns invalid JSX', () => {
125+
const useState = `const singleUseStateTest = () => {
126+
age: 20;
127+
return (<p> You are {age} years old! </p>
128+
<button onClick={age + 1}>Get Older</button>
129+
</div>)
130+
}`;
131+
132+
expect(getHooksNames(useState)).toEqual(['unknown']);
133+
});
134+
});
135+
});
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+
})

0 commit comments

Comments
 (0)