7
7
import { configure } from 'enzyme' ;
8
8
import Adapter from 'enzyme-adapter-react-16' ;
9
9
// import toJson from 'enzyme-to-json';
10
- import astParser from '../astParser ' ;
10
+ import { getHooksNames } from '../helpers ' ;
11
11
12
12
// Newer Enzyme versions require an adapter to a particular version of React
13
13
configure ( { adapter : new Adapter ( ) } ) ;
14
14
15
15
describe ( 'AST Unit Tests' , ( ) => {
16
- describe ( 'astParser ' , ( ) => {
16
+ describe ( 'getHooksNames ' , ( ) => {
17
17
it . skip ( 'Should return object with one getter/setter for a single useState instance' , ( ) => {
18
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
19
20
20
const expectedObject = {
21
21
_useState : 'testCount' ,
22
22
_useState2 : 'setTestCount' ,
23
23
} ;
24
- expect ( astParser ( useState ) ) . toEqual ( expectedObject ) ;
24
+ expect ( getHooksNames ( useState ) ) . toEqual ( expectedObject ) ;
25
25
} ) ;
26
26
27
27
it . skip ( 'Should output the right number of properties when taking in multiple function definitions' , ( ) => {
@@ -33,26 +33,94 @@ describe('AST Unit Tests', () => {
33
33
_useState3 : 'age' ,
34
34
_useState4 : 'setAge' ,
35
35
} ;
36
- expect ( astParser ( useState ) ) . toEqual ( expectedObject ) ;
37
- expect ( Object . keys ( astParser ( useState ) ) ) . toHaveLength ( 4 ) ;
36
+ expect ( getHooksNames ( useState ) ) . toEqual ( expectedObject ) ;
37
+ expect ( Object . keys ( getHooksNames ( useState ) ) ) . toHaveLength ( 4 ) ;
38
38
} ) ;
39
39
40
40
it . skip ( 'Should ignore any non-hook definitions' , ( ) => {
41
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
42
43
- expect ( Object . keys ( astParser ( useState ) ) ) . toHaveLength ( 2 ) ;
43
+ expect ( Object . keys ( getHooksNames ( useState ) ) ) . toHaveLength ( 2 ) ;
44
44
} ) ;
45
45
46
46
it . skip ( 'Should return an empty object if no hooks found' , ( ) => {
47
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
48
49
- expect ( astParser ( useState ) ) . toBe ( { } ) ;
49
+ expect ( getHooksNames ( useState ) ) . toBe ( { } ) ;
50
50
} ) ;
51
51
52
52
it . skip ( 'Should throw an error if input is invalid javascript' , ( ) => {
53
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
54
55
- expect ( astParser ( useState ) ) . toThrow ( ) ;
55
+ expect ( getHooksNames ( useState ) ) . toThrow ( ) ;
56
56
} ) ;
57
57
} ) ;
58
58
} ) ;
59
+
60
+
61
+
62
+ /* /*
63
+ console.log('getHooksNames: ', getHooksNames(`function LastSnapshot(props) {
64
+ var _useState = Object(react__WEBPACK_IMPORTED_MODULE_0__["useState"])(''),
65
+ _useState2 = _slicedToArray(_useState, 2),
66
+ currentSnapshot = _useState2[0],
67
+ setCurrentSnapshot = _useState2[1];
68
+
69
+ var _useState3 = Object(react__WEBPACK_IMPORTED_MODULE_0__["useState"])(25),
70
+ _useState4 = _slicedToArray(_useState3, 2),
71
+ testState = _useState4[0],
72
+ setTestState = _useState4[1];
73
+
74
+ var _useState5 = Object(react__WEBPACK_IMPORTED_MODULE_0__["useState"])(50),
75
+ _useState6 = _slicedToArray(_useState5, 2),
76
+ testState2 = _useState6[0],
77
+ setTestState2 = _useState6[1];
78
+
79
+ function replacer(name, val) {
80
+ // Ignore the key that is the name of the state variable
81
+ if (name === 'currentSnapshot') {
82
+ console.log('filtering currentSnapshot from display');
83
+ return undefined;
84
+ }
85
+
86
+ return val;
87
+ }
88
+
89
+ Object(react__WEBPACK_IMPORTED_MODULE_0__["useEffect"])(function () {
90
+ window.addEventListener('message', function (_ref) {
91
+ var _ref$data = _ref.data,
92
+ action = _ref$data.action,
93
+ payload = _ref$data.payload;
94
+
95
+ if (action === 'recordSnap') {
96
+ console.log('stringifying payload:', payload);
97
+ var payloadContent = JSON.stringify(payload, replacer, 1);
98
+ setCurrentSnapshot(payloadContent);
99
+ setTestState(function (state) {
100
+ return state * 2;
101
+ });
102
+ setTestState2(function (state) {
103
+ return state * 2;
104
+ });
105
+ console.log('current snapshot', currentSnapshot);
106
+ }
107
+ });
108
+ }, []);
109
+ /*
110
+ // This method is for testing. Setting state after the activeSandbox is changed modifies the overall behavior of the sandbox environment.
111
+ const { activeSandbox } = props;
112
+ useEffect(() => {
113
+ // Reset the current snapshot when a new sandbox is entered
114
+ setCurrentSnapshot('');
115
+ }, [activeSandbox]);
116
+ */
117
+ /*
118
+ return react__WEBPACK_IMPORTED_MODULE_0___default.a.createElement("div", null, react__WEBPACK_IMPORTED_MODULE_0___default.a.createElement("div", {
119
+ id: "lastSnapshot",
120
+ className: "ml-5 mt-2",
121
+ style: {
122
+ whiteSpace: 'pre'
123
+ }
124
+ }, testState, testState2, currentSnapshot));
125
+ };`));
126
+ */
0 commit comments