|
| 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 | +describe('AST Unit Tests', () => { |
| 17 | + |
| 18 | + describe('throttle', () => { |
| 19 | + let mockFunc; |
| 20 | + let throttleOutput; |
| 21 | + |
| 22 | + beforeEach(() => { |
| 23 | + // Replace any setTimeout functions with jest timer |
| 24 | + jest.useFakeTimers(); |
| 25 | + mockFunc = jest.fn(); |
| 26 | + throttleOutput = throttle(mockFunc, 1000); |
| 27 | + }); |
| 28 | + |
| 29 | + it('Should return a function', () => { |
| 30 | + expect(typeof throttleOutput).toBe('function'); |
| 31 | + }); |
| 32 | + |
| 33 | + 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 |
| 49 | + expect(mockFunc).not.toHaveBeenCalled(); |
| 50 | + |
| 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); |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + describe('getHooksNames', () => { |
| 60 | + it('Should return object with one getter/setter for a single useState instance', () => { |
| 61 | + const elementType = `function SingleUseFakeComponent() { |
| 62 | + const [testCount, setTestCount] = useState(0); |
| 63 | + const age = 20; |
| 64 | + return (<div> <p> You clicked this {testCount} times </p> |
| 65 | + <button onClick={() => setTestCount(testCount + 1)}>+1</button> |
| 66 | + <button onClick={() => setTestCount(testCount - 1)}>-1</button> <p> |
| 67 | + You are {age} years old! </p> |
| 68 | + <button onClick={age => age + 1}>Get Older</button> |
| 69 | + <hr /> |
| 70 | + </div>); |
| 71 | + }`; |
| 72 | + |
| 73 | + expect(getHooksNames(elementType)).toEqual(['testCount', 'setTestCount']); |
| 74 | + }); |
| 75 | + |
| 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); |
| 87 | + }); |
| 88 | + |
| 89 | + it('Should ignore any non-hook definitions', () => { |
| 90 | + const elementType = `function SingleUseFakeComponent() { |
| 91 | + const [testCount, setTestCount] = useState(0); |
| 92 | + const age = 20; |
| 93 | + return (<div> <p> You clicked this {testCount} times </p> |
| 94 | + <button onClick={() => setTestCount(testCount + 1)}>+1</button> |
| 95 | + <button onClick={() => setTestCount(testCount - 1)}>-1</button> <p> |
| 96 | + You are {age} years old! </p> |
| 97 | + <button onClick={age => age + 1}>Get Older</button> |
| 98 | + <hr /> |
| 99 | + </div>); |
| 100 | + }`; |
| 101 | + |
| 102 | + const expectedNumHookVariables = 2; |
| 103 | + expect(Object.keys(getHooksNames(elementType))).toHaveLength(expectedNumHookVariables); |
| 104 | + }); |
| 105 | + |
| 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>)'; |
| 108 | + |
| 109 | + expect(getHooksNames(useState)).toBe({}); |
| 110 | + }); |
| 111 | + |
| 112 | + it.skip('Should throw an error if input is invalid javascript', () => { |
| 113 | + const useState = `const singleUseStateTest = () => { |
| 114 | + age: 20; |
| 115 | + return ( <div> <p> You are {age} years old! </p> <button onClick={age + 1}>Get Older</button></div>) }`; |
| 116 | + |
| 117 | + expect(getHooksNames(useState)).toThrow(); |
| 118 | + }); |
| 119 | + }); |
| 120 | +}); |
0 commit comments