-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathjsx-test.js
237 lines (199 loc) · 6.4 KB
/
jsx-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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/* eslint-env mocha, node */
'use strict';
var assert = require('assert');
var defaults = require('lodash/defaults');
var flatten = require('lodash/flatten');
var path = require('path');
var jsx = require('./jsx');
var tern = require('tern');
var ternDir = path.resolve(__dirname, 'node_modules/tern');
describe('jsx', function () {
var createServer = function () {
jsx.initialize(ternDir);
return new tern.Server({
plugins: {
jsx: {}
}
});
};
var joinLines = function () {
return flatten(Array.prototype.slice.call(arguments)).join('\n') + '\n';
};
var server;
beforeEach(function () {
server = createServer();
});
var fixtures = {
selfClosing: (function () {
var code = joinLines(
'var Element = {};',
'void <Element/>;'
);
return {
code: code,
definition: code.indexOf('var Element') + 'var '.length,
refs: [code.indexOf('<Element/>') + '<'.length]
};
}()),
closed: (function () {
var code = joinLines(
'var Element = {};',
'void <Element></Element>;'
);
return {
code: code,
definition: code.indexOf('var Element') + 'var '.length,
refs: [
code.indexOf('<Element>') + '<'.length,
code.indexOf('</Element>') + '</'.length
]
};
}()),
member: (function () {
var code = joinLines(
'var Element = {Property: 0};',
'void <Element.Property/>;'
);
return {
code: code,
definition: code.indexOf('{Property') + '{'.length,
refs: [code.indexOf('<Element.Property/>') + '<Element.'.length]
};
}()),
attribute: (function () {
var code = joinLines(
'var value;',
'void <Element attribute={value}/>;'
);
return {
code: code,
definition: code.indexOf('var value') + 'var '.length,
refs: [code.indexOf('{value}') + '{'.length]
};
}()),
spread: (function () {
var code = joinLines(
'var value;',
'void <Element {...value}/>;'
);
return {
code: code,
definition: code.indexOf('var value') + 'var '.length,
refs: [code.indexOf('{...value}') + '{...'.length]
};
}())
};
describe('definition', function () {
var assertDefinition = function (options) {
var code = options.code;
var definition = options.definition;
var refs = options.refs;
server.addFile('definition.js', code);
refs.forEach(function (ref) {
server.request({
query: {
type: 'definition',
file: 'definition.js',
end: ref
}
}, function (err, res) {
if (err) {
throw err;
}
assert.strictEqual(res.start, definition);
});
});
};
it('should find the definition of a self-closing JSXElement', function () {
assertDefinition(fixtures.selfClosing);
});
it('should find the definition of a closed JSXElement', function () {
assertDefinition(fixtures.closed);
});
it('should find the definition of a JSXMemberExpression property', function () {
assertDefinition(fixtures.member);
});
it('should find the definition of a JSXAttribute value', function () {
assertDefinition(fixtures.attribute);
});
it('should find the definition of a JSXSpreadAttribute value', function () {
assertDefinition(fixtures.spread);
});
});
describe('refs', function () {
var assertRefs = function (options) {
var code = options.code;
var definition = options.definition;
var refs = options.refs;
var from = options.from;
server.addFile('refs.js', code);
var fromWhere =
from === 'definition' ? [definition] :
from === 'refs' ? refs :
undefined;
fromWhere.forEach(function (currentRef) {
server.request({
query: {
type: 'refs',
file: 'refs.js',
end: currentRef
}
}, function (err, res) {
if (err) {
throw err;
}
assert.strictEqual(res.refs[0].start, definition);
refs.forEach(function (ref, index) {
assert.strictEqual(res.refs[1 + index].start, ref);
});
});
});
};
describe('from definition', function () {
var assertRefsFromDefinition = function (options) {
assertRefs(defaults({from: 'definition'}, options));
};
it('should find the refs of a self-closing JSXElement', function () {
assertRefsFromDefinition(fixtures.selfClosing);
});
it('should find the refs of a closed JSXElement', function () {
assertRefsFromDefinition(fixtures.closed);
});
// To make this pass we probably need to re-implement infer.findPropRefs
// to support JSXMemberExpression (or its walker needs to be made
// extensible, somehow).
it.skip('should find the refs of a JSXMemberExpression property', function () {
assertRefsFromDefinition(fixtures.member);
});
it('should find the refs of a JSXAttribute value', function () {
assertRefsFromDefinition(fixtures.attribute);
});
it('should find the refs of a JSXSpreadAttribute value', function () {
assertRefsFromDefinition(fixtures.spread);
});
});
describe('from refs', function () {
var assertRefsFromRefs = function (options) {
assertRefs(defaults({from: 'refs'}, options));
};
// To pass the next three tests we either need to patch Tern's refs query,
// or (probably preferable) refactor Tern's findRefs function to dispatch
// on expression node type instead.
it.skip('should find the refs of a self-closing JSXElement', function () {
assertRefsFromRefs(fixtures.selfClosing);
});
it.skip('should find the refs of a closed JSXElement', function () {
assertRefsFromRefs(fixtures.closed);
});
it.skip('should find the refs of a JSXMemberExpression property', function () {
assertRefsFromRefs(fixtures.member);
});
it('should find the refs of a JSXAttribute value', function () {
assertRefsFromRefs(fixtures.attribute);
});
it('should find the refs of a JSXSpreadAttribute value', function () {
assertRefsFromRefs(fixtures.spread);
});
});
});
});