Skip to content

Commit f9416d3

Browse files
committed
ast test
1 parent 4755f0b commit f9416d3

File tree

1 file changed

+37
-32
lines changed

1 file changed

+37
-32
lines changed

package/__tests__/astParser.test.js

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,43 @@
1-
// import { configure, shallow } from 'enzyme';
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';
211

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-
*/
12+
// Newer Enzyme versions require an adapter to a particular version of React
13+
configure({ adapter: new Adapter() });
1914

20-
// EXPECTED RESULT of astParser(input)
21-
/**
22-
* {
23-
* _useState: "testCount",
24-
* _useState2: "setTestCount"
25-
* }
26-
*/
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 singleUseState = '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> ) }';
2719

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
20+
const expectedObject = {
21+
_useState: 'testCount',
22+
_useState2: 'setTestCount',
23+
};
24+
expect(astParser(singleUseState)).toEqual(expectedObject);
3325

34-
describe('placeholder', () => {
35-
it.skip('placeholder for tests', () => {
36-
expect(1 + 1).toEqual(2);
26+
// TEST 2: Should take in multiple function definitions
27+
it.skip('Should return object with two getters/setters for a single useState instance', () => {
28+
const singleUseState = '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(singleUseState)).toEqual(expectedObject);
37+
// with hooks and return an object with all 4 properties
38+
// TEST 3: Should ignore any non-hook definitions
39+
// Test 4: Should return an empty object if no hooks found
40+
// Test 5: Should throw an error if input is invalid javascript
41+
});
3742
});
3843
});

0 commit comments

Comments
 (0)