Skip to content

Commit 524ba49

Browse files
committed
Merge branch 'master' into fixHooksNamesBug
2 parents 510a6bc + 8d74c6d commit 524ba49

File tree

2 files changed

+139
-59
lines changed

2 files changed

+139
-59
lines changed

src/backend/__tests__/astParser.test.js

Lines changed: 0 additions & 59 deletions
This file was deleted.

src/backend/__tests__/helpers.test.ts

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
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+
45+
jest.advanceTimersByTime(941);
46+
47+
expect(mockFunc).toHaveBeenCalledTimes(2);
48+
});
49+
50+
it('Should only invoke function', () => {
51+
// Because we haven't invoked returned function from throttle
52+
// mock func should not have been called yet
53+
expect(mockFunc).not.toHaveBeenCalled();
54+
55+
throttledMockFunc();
56+
57+
expect(mockFunc).toHaveBeenCalledTimes(1);
58+
});
59+
});
60+
61+
describe('getHooksNames', () => {
62+
it('Should return object with one getter/setter for a single useState instance', () => {
63+
const elementType = `function SingleUseFakeComponent() {
64+
const [testCount, setTestCount] = useState(0);
65+
const age = 20;
66+
return (<div> <p> You clicked this {testCount} times </p>
67+
<button onClick={() => setTestCount(testCount + 1)}>+1</button>
68+
<button onClick={() => setTestCount(testCount - 1)}>-1</button> <p>
69+
You are {age} years old! </p>
70+
<button onClick={age => age + 1}>Get Older</button>
71+
<hr />
72+
</div>);
73+
}`;
74+
75+
expect(getHooksNames(elementType)).toEqual(['testCount', 'setTestCount']);
76+
});
77+
78+
it('Should output the right number of properties when taking in multiple function definitions', () => {
79+
const elementType = `function SingleUseFakeComponent() {
80+
const [testCount, setTestCount] = useState(0);
81+
const [biscuitCount, setBiscuitCount] = useState(0);
82+
const age = 20;
83+
return (<div> <p> You clicked this {testCount} times </p>
84+
<button onClick={() => setTestCount(testCount + 1)}>+1</button>
85+
<button onClick={() => setTestCount(testCount - 1)}>-1</button> <p>
86+
You are {age} years old! </p>
87+
<button onClick={age => age + 1}>Get Older</button>
88+
<hr />
89+
</div>);
90+
}`;
91+
92+
expect(getHooksNames(elementType)).toEqual(['testCount', 'setTestCount', 'biscuitCount', 'setBiscuitCount']);
93+
expect(Object.keys(getHooksNames(elementType))).toHaveLength(4);
94+
});
95+
96+
it('Should ignore any non-hook definitions', () => {
97+
const elementType = `function SingleUseFakeComponent() {
98+
const [testCount, setTestCount] = useState(0);
99+
const age = 20;
100+
return (<div> <p> You clicked this {testCount} times </p>
101+
<button onClick={() => setTestCount(testCount + 1)}>+1</button>
102+
<button onClick={() => setTestCount(testCount - 1)}>-1</button> <p>
103+
You are {age} years old! </p>
104+
<button onClick={age => age + 1}>Get Older</button>
105+
<hr />
106+
</div>);
107+
}`;
108+
109+
const expectedNumHookVariables = 2;
110+
expect(Object.keys(getHooksNames(elementType))).toHaveLength(expectedNumHookVariables);
111+
});
112+
113+
it('Should return an empty object if no hooks found', () => {
114+
const elementType = `function SingleUseFakeComponent() {
115+
const age = 20;
116+
return (<div> <p> You clicked this {testCount} times </p>
117+
<button onClick={() => setTestCount(testCount + 1)}>+1</button>
118+
<button onClick={() => setTestCount(testCount - 1)}>-1</button> <p>
119+
You are {age} years old! </p>
120+
<button onClick={age => age + 1}>Get Older</button>
121+
<hr />
122+
</div>);
123+
}`;
124+
125+
expect(getHooksNames(elementType)).toEqual([]);
126+
});
127+
128+
it('Should throw an error if input returns invalid JSX', () => {
129+
const useState = `const singleUseStateTest = () => {
130+
age: 20;
131+
return (<p> You are {age} years old! </p>
132+
<button onClick={age + 1}>Get Older</button>
133+
</div>)
134+
}`;
135+
136+
expect(getHooksNames(useState)).toEqual(['unknown']);
137+
});
138+
});
139+
});

0 commit comments

Comments
 (0)