-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
153 lines (119 loc) · 4.26 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
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
'use strict';
/* globals QUnit */
const lint = require('qunit-eslint');
lint(['index.js', 'ember-addon-main.js', 'test.js']);
const babel = require('babel-core');
// work around https://github.com/qunitjs/qunit/issues/1182
const root = process.cwd();
for (let key in require.cache) {
if (key.startsWith(root) && !key.startsWith(`${root}/node_modules`)) {
delete require.cache[key];
}
}
const GlimmerInlinePrecompile = require('./index');
const GlimmerInlinePrecompileAddon = require('./ember-addon-main');
function transform(source, _plugins) {
let plugins = _plugins || [
[GlimmerInlinePrecompile, { precompile: input => JSON.stringify(input.toUpperCase()) }],
];
let result = babel.transform(source, {
plugins,
});
return result.code;
}
function matches(source, expected) {
QUnit.test(`${source}`, assert => {
let actual = transform(source);
assert.equal(actual, expected);
});
}
QUnit.module('glimmer-inline-precompile', () => {
QUnit.module('only processes with correct import', () => {
// should not replace (no import)
matches(
`let template = hbs\`{{hello}}\`;`,
`let template = hbs\`{{hello}}\`;`
);
matches(
`import hbs from 'glimmer-inline-precompile'; let template = hbs\`{{hello}}\`;`,
`let template = "{{HELLO}}";`
);
});
QUnit.module('does not allow interpolation', () => {
QUnit.test('throws an error', assert => {
assert.throws(() => {
let input = `import hbs from 'glimmer-inline-precompile'; let template = hbs\`{{hello}} $\{name}\`;`;
transform(input);
}, /placeholders inside a tagged template string are not supported/);
});
});
QUnit.test('allows configuring import path to replace', assert => {
let actual = transform(`import hbs from 'herpy/derpy/doo'; let template = hbs\`{{hello}}\`;`, [
[GlimmerInlinePrecompile, { importPath: 'herpy/derpy/doo', precompile: input => JSON.stringify(input.toUpperCase()) }],
]);
assert.equal(actual, `let template = "{{HELLO}}";`);
});
});
QUnit.module('ember-addon-main', hooks => {
let fakeGlimmerAppInstance;
hooks.before(() => {
GlimmerInlinePrecompileAddon.parent = { };
GlimmerInlinePrecompileAddon._super = {
included() { },
};
});
hooks.beforeEach(assert => {
fakeGlimmerAppInstance = { options: { } };
assert.hasBabelPlugin = () => {
let plugins = fakeGlimmerAppInstance.options.babel.plugins;
let hasPlugin = false;
for (let i = 0; i < plugins.length; i++) {
if (plugins[i][0] === GlimmerInlinePrecompile) {
hasPlugin = true;
break;
}
}
assert.pushResult({
result: hasPlugin,
actual: plugins,
message: 'glimmer-inline-precompile should be present',
});
};
});
hooks.after(() => {
delete GlimmerInlinePrecompileAddon._super;
});
QUnit.test('adds plugin to options', assert => {
GlimmerInlinePrecompileAddon.included(fakeGlimmerAppInstance);
assert.hasBabelPlugin();
});
QUnit.test('merges with existing options', assert => {
fakeGlimmerAppInstance.options.foo = true;
GlimmerInlinePrecompileAddon.included(fakeGlimmerAppInstance);
assert.hasBabelPlugin();
assert.ok(fakeGlimmerAppInstance.options.foo);
});
QUnit.test('merges with existing babel options', assert => {
fakeGlimmerAppInstance.options = { babel: { foo: true } };
GlimmerInlinePrecompileAddon.included(fakeGlimmerAppInstance);
assert.hasBabelPlugin();
assert.ok(fakeGlimmerAppInstance.options.babel.foo);
});
QUnit.test('merges with existing babel plugins', assert => {
fakeGlimmerAppInstance.options = { babel: { plugins: [['a']] } };
GlimmerInlinePrecompileAddon.included(fakeGlimmerAppInstance);
assert.hasBabelPlugin();
assert.deepEqual(fakeGlimmerAppInstance.options.babel.plugins[0], ['a']);
});
QUnit.test('does not add if plugin is already present', assert => {
fakeGlimmerAppInstance.options = {
babel: {
plugins: [[{ name: 'glimmer-inline-precompile', fakeEntry: true }]],
},
};
GlimmerInlinePrecompileAddon.included(fakeGlimmerAppInstance);
assert.deepEqual(fakeGlimmerAppInstance.options.babel.plugins, [
[{ name: 'glimmer-inline-precompile', fakeEntry: true }],
]);
});
});