-
Notifications
You must be signed in to change notification settings - Fork 0
/
hooksFnEquality.test.js
37 lines (30 loc) · 1.12 KB
/
hooksFnEquality.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { act } from 'react-dom/test-utils'
import { useCorrectCallback } from './useCorrectCallback'
import { useMemoryHack } from './useMemoryHack'
import { mountObjectHook } from './util'
it('works with the memory hack', () => {
let hookResult = executeHook(useMemoryHack)
expect(hookResult.increment.name).toBe('increment')
validateFunctionEquality(hookResult)
})
it('works with useCallback', () => {
let hookResult = executeHook(useCorrectCallback)
expect(hookResult.increment.name).toBe('increment')
validateFunctionEquality(hookResult)
})
function executeHook(hook) {
let mountedHook = mountObjectHook(hook)
let hookResult = mountedHook.hookResult
return hookResult
}
function validateFunctionEquality(hookResult) {
// Store the last result of running the hook
let stored = { ...hookResult }
// Force a re-render to run the hook again
expect(hookResult.count).toEqual(0)
act(hookResult.increment)
expect(hookResult.count).toEqual(1)
// Compare the previous results to the current results
expect(hookResult.constant).toEqual(stored.constant)
expect(hookResult.increment).toEqual(stored.increment)
}