/
compile_options_test.js
97 lines (80 loc) · 2.75 KB
/
compile_options_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
import { compile, compileOptions, defaultPlugins, registerPlugin, unregisterPlugin } from '../../index';
import { moduleFor, AbstractTestCase, RenderingTestCase } from 'internal-test-helpers';
moduleFor('ember-template-compiler: default compile options', class extends AbstractTestCase {
['@test default options are a new copy'](assert) {
assert.notEqual(compileOptions(), compileOptions());
}
['@test has default AST plugins'](assert) {
assert.expect(defaultPlugins.length);
let plugins = compileOptions().plugins.ast;
for (let i = 0; i < defaultPlugins.length; i++) {
let plugin = defaultPlugins[i];
assert.ok(plugins.indexOf(plugin) > -1, `includes ${plugin}`);
}
}
});
let customTransformCounter = 0;
class CustomTransform {
constructor(options) {
customTransformCounter++;
this.options = options;
this.syntax = null;
}
transform(ast) {
let walker = new this.syntax.Walker();
walker.visit(ast, node => {
if (node.type !== 'ElementNode') {
return;
}
for (var i = 0; i < node.attributes.length; i++) {
let attribute = node.attributes[i];
if (attribute.name === 'data-test') {
node.attributes.splice(i, 1);
}
}
});
return ast;
}
}
class CustomPluginsTests extends RenderingTestCase {
afterEach() {
customTransformCounter = 0;
return super.afterEach();
}
['@test custom plugins can be used']() {
this.render('<div data-test="foo" data-blah="derp" class="hahaha"></div>');
this.assertElement(this.firstChild, {
tagName: 'div',
attrs: { class: 'hahaha', 'data-blah': 'derp' },
content: ''
});
}
['@test wrapped plugins are only invoked once per template'](assert) {
this.render('<div>{{#if falsey}}nope{{/if}}</div>');
assert.equal(customTransformCounter, 1, 'transform should only be instantiated once');
}
}
moduleFor('ember-template-compiler: registerPlugin with a custom plugins', class extends CustomPluginsTests {
beforeEach() {
registerPlugin('ast', CustomTransform);
}
afterEach() {
unregisterPlugin('ast', CustomTransform);
return super.afterEach();
}
['@test custom registered plugins are deduplicated'](assert) {
registerPlugin('ast', CustomTransform);
this.registerTemplate('application', '<div data-test="foo" data-blah="derp" class="hahaha"></div>');
assert.equal(customTransformCounter, 1, 'transform should only be instantiated once');
}
});
moduleFor('ember-template-compiler: custom plugins passed to compile', class extends RenderingTestCase {
// override so that we can provide custom AST plugins to compile
compile(templateString) {
return compile(templateString, {
plugins: {
ast: [CustomTransform]
}
});
}
});