forked from RReverser/estraverse-fb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
116 lines (101 loc) · 2.33 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
var assert = require('chai').assert;
var parse = require('esprima-fb').parse;
var originalKeys = require('./keys');
describe('works', function () {
var code = ['class MyClass{',
'x: number;',
'y: number;',
'constructor(x: number, y: number){',
'this.x = x;',
'this.y = y;',
'}',
'render(){',
'return <namespace:tag textAttr="value" exprAttr={expr} {...spreadAttr}><object.prop>!</object.prop>{}</namespace:tag>',
'}',
'}'].join('\n');
var ast = parse(code);
var expectedKeys = [
'ClassProperty',
'TypeAnnotation',
'NumberTypeAnnotation',
'ClassProperty',
'TypeAnnotation',
'NumberTypeAnnotation',
'XJSElement',
'XJSOpeningElement',
'XJSNamespacedName',
'XJSIdentifier',
'XJSIdentifier',
'XJSAttribute',
'XJSIdentifier',
'XJSAttribute',
'XJSIdentifier',
'XJSExpressionContainer',
'XJSSpreadAttribute',
'XJSClosingElement',
'XJSNamespacedName',
'XJSIdentifier',
'XJSIdentifier',
'XJSElement',
'XJSOpeningElement',
'XJSMemberExpression',
'XJSIdentifier',
'XJSIdentifier',
'XJSClosingElement',
'XJSMemberExpression',
'XJSIdentifier',
'XJSIdentifier',
'XJSExpressionContainer',
'XJSEmptyExpression'
];
beforeEach(function () {
for (var key in require.cache) {
delete require.cache[key];
}
});
it('directly from dependency', function () {
var traverse = require('./').traverse;
var actualKeys = [];
var actualTypeKeys = [];
traverse(ast, {
enter: function (node) {
if (originalKeys[node.type] != null) {
actualKeys.push(node.type);
}
}
});
assert.deepEqual(actualKeys, expectedKeys);
});
it('in injected mode', function () {
require('./');
var traverse = require('estraverse').traverse;
var actualKeys = [];
traverse(ast, {
enter: function (node) {
if (originalKeys[node.type] != null) {
actualKeys.push(node.type);
}
}
});
assert.deepEqual(actualKeys, expectedKeys);
});
it('in single-pass mode', function () {
var traverse = require('estraverse').traverse;
var keys = require('./keys');
var actualKeys = [];
traverse(ast, {
enter: function (node) {
if (originalKeys[node.type] != null) {
actualKeys.push(node.type);
}
},
keys: keys
});
assert.throws(function () {
traverse(ast, {
enter: function () {}
});
});
assert.deepEqual(actualKeys, expectedKeys);
});
});