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