@@ -13,46 +13,48 @@ import { throttle, getHooksNames } from '../helpers';
13
13
// Newer Enzyme versions require an adapter to a particular version of React
14
14
configure ( { adapter : new Adapter ( ) } ) ;
15
15
16
+ // Replace any setTimeout functions with jest timer
17
+ jest . useFakeTimers ( ) ;
18
+
16
19
describe ( 'AST Unit Tests' , ( ) => {
17
20
18
21
describe ( 'throttle' , ( ) => {
19
22
let mockFunc ;
20
- let throttleOutput ;
23
+ let throttledMockFunc ;
21
24
22
25
beforeEach ( ( ) => {
23
- // Replace any setTimeout functions with jest timer
24
- jest . useFakeTimers ( ) ;
25
26
mockFunc = jest . fn ( ) ;
26
- throttleOutput = throttle ( mockFunc , 1000 ) ;
27
+ throttledMockFunc = throttle ( mockFunc , 1000 ) ;
27
28
} ) ;
28
29
29
30
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 ) ;
31
48
} ) ;
32
49
33
50
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
49
53
expect ( mockFunc ) . not . toHaveBeenCalled ( ) ;
50
54
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 ) ;
56
58
} ) ;
57
59
} ) ;
58
60
@@ -73,17 +75,22 @@ describe('AST Unit Tests', () => {
73
75
expect ( getHooksNames ( elementType ) ) . toEqual ( [ 'testCount' , 'setTestCount' ] ) ;
74
76
} ) ;
75
77
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 ) ;
87
94
} ) ;
88
95
89
96
it ( 'Should ignore any non-hook definitions' , ( ) => {
@@ -103,18 +110,30 @@ describe('AST Unit Tests', () => {
103
110
expect ( Object . keys ( getHooksNames ( elementType ) ) ) . toHaveLength ( expectedNumHookVariables ) ;
104
111
} ) ;
105
112
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
+ }` ;
108
124
109
- expect ( getHooksNames ( useState ) ) . toBe ( { } ) ;
125
+ expect ( getHooksNames ( elementType ) ) . toEqual ( [ ] ) ;
110
126
} ) ;
111
127
112
- it . skip ( 'Should throw an error if input is invalid javascript ' , ( ) => {
128
+ it ( 'Should throw an error if input returns invalid JSX ' , ( ) => {
113
129
const useState = `const singleUseStateTest = () => {
114
130
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
+ }` ;
116
135
117
- expect ( getHooksNames ( useState ) ) . toThrow ( ) ;
136
+ expect ( getHooksNames ( useState ) ) . toEqual ( [ 'unknown' ] ) ;
118
137
} ) ;
119
138
} ) ;
120
139
} ) ;
0 commit comments