|
1 | | -// import { configure, shallow } from 'enzyme'; |
2 | | - |
3 | | -// Test 1: Should take in a function definition with 1 hook |
4 | | -// and return an object with the getter / setter |
5 | | -// EXAMPLE INPUT FOR TEST |
6 | | -/** |
7 | | - * function UseStateHookTest() { |
8 | | - * const [testCount, setTestCount] = useState(0); |
9 | | - * return ( |
10 | | - * <div> |
11 | | - * <p>You clicked this {useStateCount} times</p> |
12 | | - * <button onClick={() => setTestCount(testCount + 1)}>+1</button> |
13 | | - * <button onClick={() => setTestCount(testCount - 1)}>-1</button> |
14 | | - * <hr /> |
15 | | - * </div> |
16 | | - * ); |
17 | | - * } |
18 | | -*/ |
19 | | - |
20 | | -// EXPECTED RESULT of astParser(input) |
21 | | -/** |
22 | | - * { |
23 | | - * _useState: "testCount", |
24 | | - * _useState2: "setTestCount" |
25 | | - * } |
26 | | - */ |
27 | | - |
28 | | -// TEST 2: Should take in multiple function definitions |
29 | | -// with hooks and return an object with all 4 properties |
30 | | -// TEST 3: Should ignore any non-hook definitions |
31 | | -// Test 4: Should return an empty object if no hooks found |
32 | | -// Test 5: Should throw an error if input is invalid javascript |
33 | | - |
34 | | -describe('placeholder', () => { |
35 | | - it.skip('placeholder for tests', () => { |
36 | | - expect(1 + 1).toEqual(2); |
| 1 | +/* eslint-disable jest/no-disabled-tests */ |
| 2 | +/* eslint-disable import/no-extraneous-dependencies */ |
| 3 | +/* eslint-disable react/button-has-type */ |
| 4 | +/* eslint-disable react/jsx-filename-extension */ |
| 5 | +/* eslint-disable jest/valid-describe */ |
| 6 | +/* eslint-disable react/react-in-jsx-scope */ |
| 7 | +import { configure } from 'enzyme'; |
| 8 | +import Adapter from 'enzyme-adapter-react-16'; |
| 9 | +// import toJson from 'enzyme-to-json'; |
| 10 | +import astParser from '../astParser'; |
| 11 | + |
| 12 | +// Newer Enzyme versions require an adapter to a particular version of React |
| 13 | +configure({ adapter: new Adapter() }); |
| 14 | + |
| 15 | +describe('AST Unit Tests', () => { |
| 16 | + describe('astParser', () => { |
| 17 | + it.skip('Should return object with one getter/setter for a single useState instance', () => { |
| 18 | + const useState = 'const singleUseStateTest = () => { const [testCount, setTestCount] = useState(0); return ( <div> <p> You clicked this {testCount} times </p> <button onClick={() => setTestCount(testCount + 1)}>+1</button> <button onClick={() => setTestCount(testCount - 1)}>-1</button> <hr /> </div> )'; |
| 19 | + |
| 20 | + const expectedObject = { |
| 21 | + _useState: 'testCount', |
| 22 | + _useState2: 'setTestCount', |
| 23 | + }; |
| 24 | + expect(astParser(useState)).toEqual(expectedObject); |
| 25 | + }); |
| 26 | + |
| 27 | + it.skip('Should output the right number of properties when taking in multiple function definitions', () => { |
| 28 | + 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>)'; |
| 29 | + |
| 30 | + const expectedObject = { |
| 31 | + _useState: 'testCount', |
| 32 | + _useState2: 'setTestCount', |
| 33 | + _useState3: 'age', |
| 34 | + _useState4: 'setAge', |
| 35 | + }; |
| 36 | + expect(astParser(useState)).toEqual(expectedObject); |
| 37 | + expect(Object.keys(astParser(useState))).toHaveLength(4); |
| 38 | + }); |
| 39 | + |
| 40 | + it.skip('Should ignore any non-hook definitions', () => { |
| 41 | + const useState = 'const singleUseStateTest = () => { const [testCount, setTestCount] = useState(0); const age = 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={age => age + 1}>Get Older</button> <hr /> </div>)'; |
| 42 | + |
| 43 | + expect(Object.keys(astParser(useState))).toHaveLength(2); |
| 44 | + }); |
| 45 | + |
| 46 | + it.skip('Should return an empty object if no hooks found', () => { |
| 47 | + 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>)'; |
| 48 | + |
| 49 | + expect(astParser(useState)).toBe({}); |
| 50 | + }); |
| 51 | + |
| 52 | + it.skip('Should throw an error if input is invalid javascript', () => { |
| 53 | + const useState = 'const singleUseStateTest = () => { age: 20; return ( <div> <p> You are {age} years old! </p> <button onClick={age + 1}>Get Older</button></div>) }'; |
| 54 | + |
| 55 | + expect(astParser(useState)).toThrow(); |
| 56 | + }); |
37 | 57 | }); |
38 | 58 | }); |
0 commit comments