Skip to content

Commit d151bb3

Browse files
committed
Get helpers tests to 90% code coverage
1 parent 0fd444b commit d151bb3

File tree

1 file changed

+61
-42
lines changed

1 file changed

+61
-42
lines changed

src/backend/__tests__/helpers.test.ts

Lines changed: 61 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -13,46 +13,48 @@ import { throttle, getHooksNames } from '../helpers';
1313
// Newer Enzyme versions require an adapter to a particular version of React
1414
configure({ adapter: new Adapter() });
1515

16+
// Replace any setTimeout functions with jest timer
17+
jest.useFakeTimers();
18+
1619
describe('AST Unit Tests', () => {
1720

1821
describe('throttle', () => {
1922
let mockFunc;
20-
let throttleOutput;
23+
let throttledMockFunc;
2124

2225
beforeEach(() => {
23-
// Replace any setTimeout functions with jest timer
24-
jest.useFakeTimers();
2526
mockFunc = jest.fn();
26-
throttleOutput = throttle(mockFunc, 1000);
27+
throttledMockFunc = throttle(mockFunc, 1000);
2728
});
2829

2930
it('Should return a function', () => {
30-
expect(typeof throttleOutput).toBe('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);
3148
});
3249

3350
it('Should only invoke function', () => {
34-
/*
35-
How Throttle Works
36-
1. INIT isOnCooldown and isCallQueued to false
37-
38-
2. first time you call throttledFunc:
39-
1. invoke input function
40-
2. isOnCooldown set to true, isCallQueued set to false
41-
3. Invoke `runAfterTimeout` after X milliseconds (using setTimeout)
42-
- invoke input function
43-
- isOnCooldown set to false
44-
3. next time you call
45-
TO BE CONTINUED...
46-
*/
47-
48-
// Expect the mock function we pass in to only be called at most every X milliseconds
51+
// Because we haven't invoked returned function from throttle
52+
// mock func should not have been called yet
4953
expect(mockFunc).not.toHaveBeenCalled();
5054

51-
// Invoke returned timer function from throttle
52-
throttleOutput();
53-
// At this point, setTimeout has been invoked
54-
expect(setTimeout).toHaveBeenCalledTimes(1);
55-
expect(setTimeout).toHaveBeenLastCalledWith(expect.any(Function), 1000);
55+
throttledMockFunc();
56+
57+
expect(mockFunc).toHaveBeenCalledTimes(1);
5658
});
5759
});
5860

@@ -73,17 +75,22 @@ describe('AST Unit Tests', () => {
7375
expect(getHooksNames(elementType)).toEqual(['testCount', 'setTestCount']);
7476
});
7577

76-
it.skip('Should output the right number of properties when taking in multiple function definitions', () => {
77-
const useState = 'const singleUseStateTest = () => { const [testCount, setTestCount] = useState(0); const [age, setAge] = useState(20); return ( <div> <p> You clicked this {testCount} times </p> <button onClick={() => setTestCount(testCount + 1)}>+1</button> <button onClick={() => setTestCount(testCount - 1)}>-1</button> <p> You are {age} years old! </p> <button onClick={() => setAge(age + 1)}>Get Older</button> <hr /> </div>)';
78-
79-
const expectedObject = {
80-
_useState: 'testCount',
81-
_useState2: 'setTestCount',
82-
_useState3: 'age',
83-
_useState4: 'setAge',
84-
};
85-
expect(getHooksNames(useState)).toEqual(expectedObject);
86-
expect(Object.keys(getHooksNames(useState))).toHaveLength(4);
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);
8794
});
8895

8996
it('Should ignore any non-hook definitions', () => {
@@ -103,18 +110,30 @@ describe('AST Unit Tests', () => {
103110
expect(Object.keys(getHooksNames(elementType))).toHaveLength(expectedNumHookVariables);
104111
});
105112

106-
it.skip('Should return an empty object if no hooks found', () => {
107-
const useState = 'const singleUseStateTest = () => { const age = 20; return ( <div> <p> You are {age} years old! </p> <button onClick={age => age + 1}>Get Older</button> <hr /> </div>)';
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+
}`;
108124

109-
expect(getHooksNames(useState)).toBe({});
125+
expect(getHooksNames(elementType)).toEqual([]);
110126
});
111127

112-
it.skip('Should throw an error if input is invalid javascript', () => {
128+
it('Should throw an error if input returns invalid JSX', () => {
113129
const useState = `const singleUseStateTest = () => {
114130
age: 20;
115-
return ( <div> <p> You are {age} years old! </p> <button onClick={age + 1}>Get Older</button></div>) }`;
131+
return (<p> You are {age} years old! </p>
132+
<button onClick={age + 1}>Get Older</button>
133+
</div>)
134+
}`;
116135

117-
expect(getHooksNames(useState)).toThrow();
136+
expect(getHooksNames(useState)).toEqual(['unknown']);
118137
});
119138
});
120139
});

0 commit comments

Comments
 (0)