Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(190)

Side by Side Diff: node_modules/vulcanize/test/test.js

Issue 800513006: Added vulcanize under third_party/npm_modules (Closed) Base URL: https://chromium.googlesource.com/infra/third_party/npm_modules.git@master
Patch Set: Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « node_modules/vulcanize/test/mocha.opts ('k') | node_modules/vulcanize/util/changelogs.sh » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /**
2 * @license
3 * Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
4 * This code may only be used under the BSD style license found at http://polyme r.github.io/LICENSE.txt
5 * The complete set of authors may be found at http://polymer.github.io/AUTHORS. txt
6 * The complete set of contributors may be found at http://polymer.github.io/CON TRIBUTORS.txt
7 * Code distributed by Google as part of the polymer project is also
8 * subject to an additional IP rights grant found at http://polymer.github.io/PA TENTS.txt
9 */
10 // jshint node: true
11 var assert = require('assert');
12 var path = require('path');
13
14 assert.AssertionError.prototype.showDiff = true;
15
16 suite('constants', function() {
17 var constants = require('../lib/constants.js');
18
19 suite('URLs', function() {
20
21 test('absolute urls', function() {
22 var abs = constants.ABS_URL;
23
24 assert(abs.test('data:charset=utf8,'), 'data urls');
25 assert(abs.test('http://foo.com'), 'http');
26 assert(abs.test('https://foo.com'), 'https');
27 assert(abs.test('mailto:foo@bar.com'), 'mailto');
28 assert(abs.test('tel:+49123123456'), 'phonecall');
29 assert(abs.test('sms:1-123-123456'), 'sms');
30 assert(abs.test('//foo.com'), 'protocol-free');
31 assert(abs.test('/components/'), '/');
32 assert(!abs.test('../foo/bar.html'), '../');
33 assert(!abs.test('bar.html'), 'sibling dependency');
34 });
35
36 test('remote absolute urls', function() {
37 var rabs = constants.REMOTE_ABS_URL;
38
39 assert(rabs.test('http://foo.com'), 'http');
40 assert(rabs.test('https://foo.com'), 'https');
41 assert(rabs.test('//foo.com'), 'protocol-free');
42 assert(!rabs.test('../foo/bar.html'), '../');
43 assert(!rabs.test('bar.html'), 'sibling dependency');
44 assert(!rabs.test('/components/'), '/');
45 });
46
47 test('CSS URLs', function() {
48 var url = constants.URL;
49
50 assert('url(foo.html)'.match(url), 'naked');
51 assert('url(\'foo.html\')'.match(url), 'single quote');
52 assert('url("foo.html")'.match(url), 'double quote');
53 });
54
55 });
56
57 test('Polymer Invocation', function() {
58 var polymer = constants.POLYMER_INVOCATION;
59
60 function test(invocation, msg) {
61 var matches = polymer.exec(invocation);
62 assert(matches, 'polymer invocation found', msg);
63 }
64
65 test('Polymer(\'core-input\', {})', 'full');
66 test('Polymer(\'core-input\')', 'name-only');
67 test('Polymer()', 'none');
68 test('Polymer({})', 'object-only');
69 test('Polymer(p)', 'indirect');
70 });
71
72 });
73
74 suite('Path Resolver', function() {
75 var pathresolver = require('../lib/pathresolver.js');
76 var inputPath = '/foo/bar/my-element';
77 var outputPath = '/foo/bar';
78
79 test('Rewrite URLs', function() {
80 var css = [
81 'x-element {',
82 ' background-image: url(foo.jpg);',
83 '}',
84 'x-bar {',
85 ' background-image: url(data:xxxxx);',
86 '}',
87 'x-quuz {',
88 ' background-image: url(\'https://foo.bar/baz.jpg\');',
89 '}',
90 ].join('\n');
91
92 var expected = [
93 'x-element {',
94 ' background-image: url("my-element/foo.jpg");',
95 '}',
96 'x-bar {',
97 ' background-image: url("data:xxxxx");',
98 '}',
99 'x-quuz {',
100 ' background-image: url("https://foo.bar/baz.jpg");',
101 '}',
102 ].join('\n');
103
104 var actual = pathresolver.rewriteURL(inputPath, outputPath, css);
105 assert.equal(actual, expected);
106 });
107
108 test('Rewrite Paths', function() {
109 function testPath(val, expected, abs, msg) {
110 var actual = pathresolver.rewriteRelPath(inputPath, outputPath, val, abs);
111 assert.equal(actual, expected, msg);
112 }
113
114 testPath('biz.jpg', 'my-element/biz.jpg', null, 'local');
115 testPath('http://foo/biz.jpg', 'http://foo/biz.jpg', null, 'remote');
116 testPath('biz.jpg', 'bar/my-element/biz.jpg', '/foo/', 'build path');
117 });
118
119 test('Resolve Paths', function() {
120 var html = [
121 '<link rel="import" href="../polymer/polymer.html">',
122 '<link rel="stylesheet" href="my-element.css">',
123 '<polymer-element name="my-element">',
124 '<template>',
125 '<style>:host { background-image: url(background.svg); }</style>',
126 '<script>Polymer()</script>',
127 '</template>',
128 '</polymer-element>'
129 ].join('\n');
130
131 var expected = [
132 '<html><head><link rel="import" href="polymer/polymer.html">',
133 '<link rel="stylesheet" href="my-element/my-element.css">',
134 '</head><body><polymer-element name="my-element" assetpath="my-element/">' ,
135 '<template>',
136 '<style>:host { background-image: url("my-element/background.svg"); }</sty le>',
137 '<script>Polymer()</script>',
138 '</template>',
139 '</polymer-element></body></html>'
140 ].join('\n');
141
142 var expected2 = [
143 '<html><head><link rel="import" href="/bar/polymer/polymer.html">',
144 '<link rel="stylesheet" href="/bar/my-element/my-element.css">',
145 '</head><body><polymer-element name="my-element" assetpath="/bar/my-elemen t/">',
146 '<template>',
147 '<style>:host { background-image: url("/bar/my-element/background.svg"); } </style>',
148 '<script>Polymer()</script>',
149 '</template>',
150 '</polymer-element></body></html>'
151 ].join('\n');
152
153 var actual;
154 var whacko = require('whacko');
155 var $ = whacko.load(html);
156
157 pathresolver.resolvePaths($, inputPath, outputPath);
158
159 actual = $.html();
160 assert.equal(actual, expected, 'relative');
161
162 $ = whacko.load(html);
163
164 pathresolver.resolvePaths($, inputPath, outputPath, '/foo');
165
166 actual = $.html();
167 assert.equal(actual, expected2, 'absolute');
168 });
169
170 });
171
172 suite('Utils', function() {
173 var constants = require('../lib/constants.js');
174 var utils = require('../lib/utils.js');
175
176 test('getTextContent', function() {
177 var whacko = require('whacko');
178 var divEl = whacko('<div>some text!</div>');
179 assert.equal(utils.getTextContent(divEl), 'some text!', 'a textnode child');
180 var blankEl = whacko('<div></div>');
181 assert.equal(utils.getTextContent(blankEl), '', 'no textnode children');
182 });
183
184 test('setTextContent', function() {
185 var whacko = require('whacko');
186 var divEl = whacko('<div></div>');
187 utils.setTextContent(divEl, 'some text!');
188 assert.equal(utils.getTextContent(divEl), 'some text!', 'create text node');
189 utils.setTextContent(divEl, 'some text 2!');
190 assert.equal(utils.getTextContent(divEl), 'some text 2!', 'override text nod e');
191 });
192
193 test('unixPath', function() {
194 var pp = ['foo', 'bar', 'baz'];
195 var p = pp.join('/');
196 var actual = utils.unixPath(p);
197 assert.equal(actual, p, 'started unix');
198 var p2 = pp.join('\\');
199 actual = utils.unixPath(p2, '\\');
200 assert.equal(actual, p, 'windows path');
201 });
202
203 test('escapeForRegExp', function() {
204 var actual = utils.escapeForRegExp('foo-bar');
205 assert.equal(actual, 'foo\\-bar', 'element name');
206 actual = utils.escapeForRegExp('foo/bar/baz');
207 assert.equal(actual, 'foo\\/bar\\/baz', 'absolute path');
208 });
209
210 test('Polymer Invocation', function() {
211 var polymer = constants.POLYMER_INVOCATION;
212
213 function test(invocation, expected, msg) {
214 var matches = polymer.exec(invocation);
215 assert(matches, 'polymer invocation found');
216 var replacement = utils.processPolymerInvocation('core-input', matches);
217 var actual = invocation.replace(matches[0], replacement);
218 assert.strictEqual(actual, expected, msg);
219 }
220
221 test('Polymer(\'core-input\', {})', 'Polymer(\'core-input\', {})', 'full');
222 test('Polymer(\'core-input\')', 'Polymer(\'core-input\')', 'name-only');
223 test('Polymer()', 'Polymer(\'core-input\')', 'none');
224 test('Polymer({})', 'Polymer(\'core-input\',{})', 'object-only');
225 test('Polymer(p)', 'Polymer(\'core-input\',p)', 'indirect');
226
227 });
228
229 test('#82', function() {
230 var whacko = require('whacko');
231 var $ = whacko.load('<polymer-element name="paper-button-base"><script>(func tion(){ Polymer(p);}()</script></polymer-element>');
232 $(constants.JS_INLINE).each(function() {
233 var el = $(this);
234 var content = utils.getTextContent(el);
235 assert(content);
236 var parentElement = el.closest('polymer-element').get(0);
237 if (parentElement) {
238 var match = constants.POLYMER_INVOCATION.exec(content);
239 var elementName = $(parentElement).attr('name');
240 if (match) {
241 var invocation = utils.processPolymerInvocation(elementName, match);
242 content = content.replace(match[0], invocation);
243 utils.setTextContent(el, content);
244 }
245 }
246 assert.equal(utils.getTextContent(el), '(function(){ Polymer(\'paper-butto n-base\',p);}()');
247 });
248 });
249
250 test('searchAll', function() {
251 var searchAll = utils.searchAll;
252 var whacko = require('whacko');
253 var $ = whacko.load('<body><template><span>foo</span><template><span></span> </template></template><span><template><span>bar</span></template>baz</span></bod y>');
254 var out = searchAll($, 'span');
255
256 assert.equal(out.length, 4, 'found all spans, nested in templates');
257 });
258
259 });
260
261 suite('Optparser', function() {
262 var path = require('path');
263 var optParser = require('../lib/optparser.js');
264 var constants = require('../lib/constants.js');
265 var ABS_URL = constants.ABS_URL;
266 var REMOTE_ABS_URL = constants.REMOTE_ABS_URL;
267
268 function optParserTest(fn, opts, skipFail) {
269 if (typeof opts === 'undefined') {
270 opts = {input: path.resolve('index.html')};
271 }
272 optParser.processOptions(opts, function(err, options) {
273 if (!skipFail) {
274 assert.equal(err, null);
275 }
276 fn(err, options);
277 });
278 }
279
280 test('Error on no input', function(done) {
281 optParserTest(function(err, options) {
282 assert.equal(err, 'No input file or source string given!');
283 done();
284 }, null, true);
285 });
286
287 test('Defaults', function(done) {
288 optParserTest(function(err, options) {
289 assert.equal(options.input, path.resolve('index.html'));
290 assert.equal(options.output, path.resolve('vulcanized.html'));
291 assert.equal(options.outputDir, path.dirname(path.resolve('vulcanized.html ')));
292 assert(!options.csp);
293 assert(!options.abspath);
294 assert.deepEqual(options.excludes, {imports:[ABS_URL], scripts:[ABS_URL], styles:[ABS_URL]});
295 done();
296 });
297 });
298
299 test('CSP', function(done) {
300 optParserTest(function(err, options) {
301 assert.equal(options.csp, path.resolve('vulcanized.js'));
302 done();
303 }, {input: 'index.html', csp: true});
304 });
305
306 test('output', function(done) {
307 optParserTest(function(err, options) {
308 assert.equal(options.output, path.resolve('build.html'));
309 assert.equal(options.csp, path.resolve('build.js'));
310 done();
311 }, {input: path.resolve('index.html'), output: path.resolve('build.html'), c sp: true});
312 });
313
314 test('abspath', function(done) {
315 optParserTest(function(err, options) {
316 assert.equal(options.abspath, path.resolve('../'));
317 assert.deepEqual(options.excludes, {imports:[REMOTE_ABS_URL], scripts:[REM OTE_ABS_URL], styles:[REMOTE_ABS_URL]});
318 done();
319 }, {input: path.resolve('index.html'), abspath: path.resolve('../')});
320 });
321
322 test('excludes', function(done) {
323 var excludes = {
324 imports: [
325 '.*'
326 ]
327 };
328 var expected = [/.*/, ABS_URL];
329
330 optParserTest(function(err, options) {
331 assert.deepEqual(options.excludes.imports, expected);
332 done();
333 }, {input: path.resolve('index.html'), excludes: excludes});
334
335 });
336
337 test('config file', function(done) {
338 optParserTest(function(err, options) {
339 assert.equal(options.input, path.resolve('index.html'));
340 assert.equal(options.output, path.resolve('build.html'));
341 assert.equal(options.csp, path.resolve('build.js'));
342 assert(!options.abspath);
343 assert.deepEqual(options.excludes, {imports:[/.*/, ABS_URL], scripts:[ABS_ URL], styles:[ABS_URL]});
344 done();
345 }, {config: path.resolve('test/config.json'), input: path.resolve('index.htm l'), output: path.resolve('build.html'), csp: true});
346 });
347
348 test('report broken config file', function(done) {
349 optParserTest(function(err, options) {
350 assert.equal(err, 'Malformed config JSON!');
351 done();
352 }, {config: path.resolve('test/broken_config.json')}, true);
353 });
354
355 });
356
357 suite('Vulcan', function() {
358 var vulcan = require('../lib/vulcan.js');
359 var outputPath = path.resolve('test/html/actual.html');
360 var inputPath = path.resolve('test/html/default.html');
361
362 var searchAll = require('../lib/utils.js').searchAll;
363
364 test('set options', function(done) {
365 var options = {
366 input: 'index.html'
367 };
368 vulcan.setOptions(options, done);
369 });
370
371 function process(options, fn) {
372 var outputs = Object.create(null);
373 options.outputHandler = function(name, data, eop) {
374 if (!data) {
375 throw new Error("Writing empty data");
376 }
377 outputs[name] = data;
378 };
379 vulcan.setOptions(options, function(err) {
380 assert(!err);
381 vulcan.processDocument();
382 Object.keys(outputs).forEach(function(o) {
383 assert.equal(typeof outputs[o], 'string', 'all buffers are closed');
384 });
385 fn(outputs);
386 });
387 }
388
389 test('defaults', function(done) {
390 var getTextContent = require('../lib/utils.js').getTextContent;
391
392 process({input: inputPath, output: outputPath}, function(outputs) {
393 assert.equal(Object.keys(outputs).length, 1);
394 var vulcanized = outputs[outputPath];
395 assert(vulcanized);
396 var $ = require('whacko').load(vulcanized);
397 assert.equal(searchAll($, 'body > div[hidden]').length, 1, 'only one div[h idden]');
398 assert.equal(searchAll($, 'head > link[rel="import"]:not([href^="http://"] )').length, 0, 'all relative imports removed');
399 assert.equal(searchAll($, 'polymer-element').length, 1, 'imports were dedu plicated');
400 assert.equal(searchAll($, 'polymer-element').attr('noscript'), null, 'nosc ript removed');
401 assert.equal(getTextContent(searchAll($, 'polymer-element > script')), 'Po lymer(\'my-element\');', 'polymer script included');
402 assert.equal($('link', $('polymer-element > template').get(0).children[0]) .length, 0, 'external styles removed');
403 assert.equal($('style', $('polymer-element > template').get(0).children[0] ).length, 1, 'styles inlined');
404 assert.equal($('svg > *', $('polymer-element > template').get(0).children[ 0]).length, 6, 'svg children propery nested');
405 assert.equal(searchAll($, 'polymer-element').attr('assetpath'), 'imports/' , 'assetpath set');
406 done();
407 });
408 });
409
410 test('CSP', function(done) {
411
412 process({input: inputPath, output: outputPath, csp: true}, function(outputs) {
413 assert.equal(Object.keys(outputs).length, 2);
414 var vulcanized = outputs[outputPath];
415 var vulcanizedJS = outputs[path.resolve(outputPath, '../actual.js')];
416 assert(vulcanized);
417 assert(vulcanizedJS);
418 var $ = require('whacko').load(vulcanized);
419 assert(searchAll($, 'body > script[src="actual.js"]'), 'vulcanized script in body');
420 assert.equal(searchAll($, 'body script:not([src])').length, 0, 'inline scr ipts removed');
421 assert.equal(vulcanizedJS, 'Polymer(\'my-element\');', 'csp element script ');
422 done();
423 });
424 });
425
426 test('exclude', function(done) {
427
428 var i = 3;
429 function reallyDone() {
430 if (--i === 0) {
431 done();
432 }
433 }
434
435 process({input: inputPath, output: outputPath, excludes: {imports: ['simple- import']}}, function(outputs) {
436 var vulcanized = outputs[outputPath];
437 assert(vulcanized);
438 var $ = require('whacko').load(vulcanized);
439 assert.equal(searchAll($, 'head > link[href="imports/simple-import.html"]' ).length, 0, 'import excluded');
440 assert.equal(searchAll($, 'head > link[rel="stylesheet"][href="imports/sim ple-style.css"]').length, 0, 'import content excluded');
441 assert.equal(searchAll($, 'head > link[href="http://example.com/foo/bar.ht ml"]').length, 1, 'external import is not excluded');
442 reallyDone();
443 });
444
445 process({input: inputPath, output: outputPath, excludes: {styles: ['simple-s tyle']}}, function(outputs) {
446 var vulcanized = outputs[outputPath];
447 assert(vulcanized);
448 var $ = require('whacko').load(vulcanized);
449 assert.equal(searchAll($, 'link[href="imports/simple-style.css"]').length, 1, 'style excluded');
450 reallyDone();
451 });
452
453 process({input: inputPath, output: outputPath, excludes: {imports: ['simple- import']}, 'strip-excludes': false}, function(outputs) {
454 var vulcanized = outputs[outputPath];
455 assert(vulcanized);
456 var $ = require('whacko').load(vulcanized);
457 assert.equal(searchAll($, 'link[href="imports/simple-import.html"]').lengt h, 1, 'excluded import not stripped');
458 reallyDone();
459 });
460 });
461
462 suite('Strip', function() {
463
464 test('script', function(done) {
465 var options = {input: 'test/html/broken-js.html', strip: true};
466 vulcan.setOptions(options, function(err) {
467 assert.ifError(err);
468 assert.throws(vulcan.processDocument, function(err) {
469 assert.equal(err.message, 'Compress JS Error');
470 assert.equal(err.content.trim(), 'var a = (');
471 assert.ok(err.detail);
472 return true;
473 }, 'throws useful error');
474 done();
475 });
476 });
477
478 suite('css', function() {
479
480 test('precsision', function(done) {
481 process({inputSrc: '<style>test{ -ms-flex: 0 0 0.0000000001px; }</style> ', output: outputPath, strip: true}, function(outputs) {
482 var vulcanized = outputs[outputPath];
483 assert(vulcanized);
484 assert(vulcanized.indexOf('.0000000001px') > -1, 'precision is correct ');
485 done();
486 });
487 });
488
489 test('polyfill-next-selector', function(done) {
490 process({inputSrc: '<style>polyfill-next-selector {content:\':host > *\' ;}\n::content > * {z-index:-1000;}\npolyfill-next-selector {content:\':host > .c ore-selected\';}\n::content > .core-selected{z-index: auto;}</style>', output: o utputPath, strip: true}, function(outputs) {
491 var vulcanized = outputs[outputPath];
492 assert(vulcanized);
493 assert(vulcanized.indexOf('<style>polyfill-next-selector') > -1, 'polf ill-next-selector is kept');
494 done();
495 });
496 });
497
498 test('fallback on parse fail', function(done) {
499 var input = '<style>div{\r\nwidth: {{ foo }};\n}\r\n</style>';
500 process({inputSrc: input, output: outputPath, strip: true}, function(out puts) {
501 var vulcanized = outputs[outputPath];
502 assert(vulcanized);
503 assert(vulcanized.indexOf(input.replace(/[\r\n]/g, '')) > -1, 'newline s removed at least');
504 done();
505 });
506 });
507 });
508
509 test('comment removal', function(done) {
510 var options = {input: 'test/html/comments.html', output: outputPath, strip : true};
511 process(options, function(outputs) {
512 var vulcanized = outputs[outputPath];
513 assert(vulcanized);
514 assert.equal(vulcanized.indexOf('@license'), -1, 'license comment at top removed');
515 assert.equal(vulcanized.indexOf('comment 1'), -1, 'comment in body remov ed');
516 assert.equal(vulcanized.indexOf('comment 2'), -1, 'comment in template r emoved');
517 assert.equal(vulcanized.indexOf('comment 3'), -1, 'comment in style in t emplate removed');
518 assert.equal(vulcanized.indexOf('comment 4'), -1, 'comment in polymer-el ement removed');
519 assert.equal(vulcanized.indexOf('comment 5'), -1, 'comment in script rem oved');
520 done();
521 });
522 });
523
524 });
525
526 test('Multiple Polymer Invocations', function(done) {
527 var options = {input: 'test/html/multiple.html', output: outputPath};
528 process(options, function(outputs) {
529 var vulcanized = outputs[outputPath];
530 assert(vulcanized);
531 var $ = require('whacko').load(vulcanized);
532 var getText = require('../lib/utils.js').getTextContent;
533 var xa = $('polymer-element[name="x-a"] > script');
534 var xb = $('polymer-element[name="x-b"] > script');
535 assert.equal(getText(xa), 'Polymer(\'x-a\',{})');
536 assert.equal(getText(xb), 'Polymer(\'x-b\',{})');
537 done();
538 });
539 });
540
541 });
OLDNEW
« no previous file with comments | « node_modules/vulcanize/test/mocha.opts ('k') | node_modules/vulcanize/util/changelogs.sh » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698