This repository has been archived by the owner on May 12, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjsxtransform.js
280 lines (277 loc) · 10.2 KB
/
jsxtransform.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/* global describe,it */
var express = require('express');
var jsxtransform = require('../lib/jsxtransform');
var app = express()
.use(jsxtransform())
.use(express['static']('/data'));
var expect = require('unexpected')
.clone()
.installPlugin(require('unexpected-fs'))
.installPlugin(require('unexpected-express'))
.addAssertion('to yield response', function (expect, subject, value) {
return expect(app, 'to yield exchange', {
request: subject,
response: value
});
});
describe('jsxtransform', function () {
it('should not mess with request for txt file', function () {
return expect('/something.txt', 'with fs mocked out', {
'/data': {
'something.txt': 'foo\n'
}
}, 'to yield response', {
headers: {
'Content-Type': 'text/plain; charset=UTF-8'
},
body: 'foo\n'
});
});
it('should not mess with request for js file that contains no jsx syntax', function () {
return expect('/something.js', 'with fs mocked out', {
'/data': {
'something.js': 'foo();\n'
}
}, 'to yield response', {
headers: {
'Content-Type': 'text/javascript; charset=UTF-8'
},
body: 'foo();\n'
});
});
it('should set a ETag with a suffix of -jsxtransform when requesting a .jsx file', function () {
return expect('/something.jsx', 'with fs mocked out', {
'/data': {
'something.jsx': ''
}
}, 'to yield response', {
headers: {
'ETag': /-jsxtransform"$/
}
});
});
it('should set a ETag with a suffix of -jsxtransform when requesting a .js file with a jsx annotation', function () {
return expect('/something.js', 'with fs mocked out', {
'/data': {
'something.js': '/** @jsx React.DOM */\nfoo();\n'
}
}, 'to yield response', {
headers: {
'ETag': /-jsxtransform"$/
}
});
});
it('should not set a ETag with a suffix of -jsxtransform when requesting a .js file without a jsx annotation', function () {
return expect('/something.js', 'with fs mocked out', {
'/data': {
'something.js': 'foo();\n'
}
}, 'to yield response', {
headers: {
'ETag': /-[0-9]+"$/
}
});
});
it('should return 304 for a request that has a matching ETag in if-none-match', function () {
var mockFs = {
'/data': {
'foobar.jsx': {
_isFile: true,
mtime: new Date(1),
content: 'foo'
}
}
};
return expect('/foobar.jsx', 'with fs mocked out', mockFs, 'to yield response', {
statusCode: 200,
headers: {
ETag: /^W\/".*-jsxtransform"$/
},
body: 'foo'
}).then(function (context) {
var etag = context.httpResponse.headers.get('ETag');
return expect({
url: '/foobar.jsx',
headers: {
'If-None-Match': etag
}
}, 'with fs mocked out', mockFs, 'to yield response', {
statusCode: 304,
headers: {
ETag: etag
}
});
});
});
it('should return 200 for a request that has an invalid ETag in if-none-match', function () {
return expect('/foobar.jsx', 'with fs mocked out', {
'/data': {
'foobar.jsx': {
_isFile: true,
mtime: new Date(1),
content: 'foo'
}
}
}, 'to yield response', {
statusCode: 200,
headers: {
ETag: /^W\/".*-jsxtransform"$/
},
body: 'foo'
}).then(function (context) {
var etag = context.httpResponse.headers.get('ETag');
return expect({
url: '/foobar.jsx',
headers: {
'If-None-Match': etag
}
}, 'with fs mocked out', {
'/data': {
'foobar.jsx': {
_isFile: true,
mtime: new Date(10),
content: 'foobar'
}
}
}, 'to yield response', {
statusCode: 200,
body: 'foobar'
});
});
});
it('should return 200 for a request that has an ETag in if-none-match with no tag from jsxtransformer', function () {
var mockFs = {
'/data': {
'foobar.jsx': {
_isFile: true,
mtime: new Date(1),
content: 'foo'
}
}
};
return expect('/foobar.jsx', 'with fs mocked out', mockFs, 'to yield response', {
statusCode: 200,
body: 'foo'
}).then(function (context) {
var etag = context.httpResponse.headers.get('ETag');
etag = etag.replace(/-jsxtransform"$/, '"');
return expect({
url: '/foobar.jsx',
headers: {
'If-None-Match': etag
}
}, 'with fs mocked out', mockFs, 'to yield response', {
statusCode: 200,
body: 'foo'
});
});
});
it('should transform a js file with jsx content and jsx annotation', function () {
return expect('/helloWorldJsx.js', 'with fs mocked out', {
'/data': {
'helloWorldJsx.js': [
'/** @jsx React.DOM */',
'React.renderComponent(',
' <h1>Hello, world!</h1>,',
' document.getElementById(\'example\')',
');'
].join('\n')
}
}, 'to yield response', [
'/** @jsx React.DOM */',
'React.renderComponent(',
' React.createElement("h1", null, "Hello, world!"),',
' document.getElementById(\'example\')',
');'
].join('\n'));
});
it('should not transform a js file with jsx content and no jsx annotation', function () {
return expect('/helloWorldJsxNoAnnotation.js', 'with fs mocked out', {
'/data': {
'helloWorldJsxNoAnnotation.js': [
"React.renderComponent(",
" <h1>Hello, world!</h1>,",
" document.getElementById('example')",
");"
].join('\n')
}
}, 'to yield response', [
'React.renderComponent(',
' <h1>Hello, world!</h1>,',
' document.getElementById(\'example\')',
');'
].join('\n'));
});
it('should transform a jsx file with jsx content and no jsx annotation', function () {
return expect('/helloWorldJsx.jsx', 'with fs mocked out', {
'/data': {
'helloWorldJsx.jsx': [
"React.renderComponent(",
" <h1>Hello, world!</h1>,",
" document.getElementById('example')",
");"
].join('\n')
}
}, 'to yield response', [
'React.renderComponent(',
' React.createElement("h1", null, "Hello, world!"),',
' document.getElementById(\'example\')',
');'
].join('\n'));
});
it('should transform a jsx file with jsx content and jsx annotation', function () {
return expect('/helloWorldJsxWithAnnotation.jsx', 'with fs mocked out', {
'/data': {
'helloWorldJsxWithAnnotation.jsx': [
"/** @jsx React.DOM */",
"React.renderComponent(",
" <h1>Hello, world!</h1>,",
" document.getElementById('example')",
");"
].join('\n')
}
}, 'to yield response', [
'/** @jsx React.DOM */',
'React.renderComponent(',
' React.createElement("h1", null, "Hello, world!"),',
' document.getElementById(\'example\')',
');'
].join('\n'));
});
it('should behave when it gets an error', function () {
return expect('/helloWorldJsxSyntaxError.jsx', 'with fs mocked out', {
'/data': {
'helloWorldJsxSyntaxError.jsx' : [
"React.renderComponent(",
" <h1>Hello, world!</h1,",
" document.getElementById('example')",
");"
].join('\n')
}
}, 'to yield response', [
'/**********************************************************',
' * Parse Error: Line 3: Unexpected token ,',
' * In file: /helloWorldJsxSyntaxError.jsx',
' *********************************************************/',
';(function () {',
' var element = document.createElement("DIV");',
' element.style.position = "absolute";',
' element.style.top = 0;',
' element.style.left = 0;',
' element.style.right = 0;',
' element.style.bottom = 0;',
' element.style.backgroundColor = "white";',
' element.style.zIndex = 999999;',
' element.style.fontSize = "24px";',
' element.style.color = "red";',
' element.style.padding = "20px";',
' element.style.textAlign = "left";',
' element.style.whiteSpace = "pre";',
' element.style.fontFamily = "monospace";',
' element.innerText = "Parse Error: Line 3: Unexpected token ,\\nIn file: /helloWorldJsxSyntaxError.jsx";',
' document.body.appendChild(element);',
'}());',
''
].join('\n'));
});
});