Skip to content

Commit 4ac4309

Browse files
authored
Merge pull request #5 from mbland/pluginimpl-compile-tests
Add first PluginImpl.compile() tests
2 parents 9b42511 + 5ca8e11 commit 4ac4309

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

test/plugin-impl.test.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,56 @@ describe('PluginImpl', () => {
8181
expect(impl.isTemplate('quux/xyzzy/plugh.mustache')).toBe(false)
8282
})
8383
})
84+
85+
describe('compile()', () => {
86+
const PREFIX = [
87+
'import Handlebars from \'handlebars/lib/handlebars.runtime\'',
88+
`import Render from '${PLUGIN_ID}'`
89+
].join('\n')
90+
const BEGIN_TEMPLATE = 'export const RawTemplate = Handlebars.template(\n'
91+
const SUFFIX = '\n)\nexport default Render(RawTemplate)'
92+
93+
const templateStr = '<p>Hello, {{ recipient }}</p>'
94+
95+
const mappingSkipsPrefix = (prefix) => {
96+
// Really? All the methods on String and Array, and no .count()?
97+
const numLines = prefix.length - prefix.replaceAll('\n', '').length
98+
return expect.stringMatching(new RegExp(`^;{${numLines}}`))
99+
}
100+
101+
test('emits precompiled template module and source map', () => {
102+
const impl = new PluginImpl()
103+
104+
const { code, map } = impl.compile(templateStr, 'foo.hbs')
105+
106+
const expectedPrefix = `${PREFIX}\n${BEGIN_TEMPLATE}`
107+
expect(code.substring(0, expectedPrefix.length)).toBe(expectedPrefix)
108+
expect(code.substring(code.length - SUFFIX.length)).toBe(SUFFIX)
109+
expect(map).toMatchObject({
110+
sources: [ 'foo.hbs' ],
111+
mappings: mappingSkipsPrefix(expectedPrefix)
112+
})
113+
})
114+
115+
describe('emits empty source map if', () => {
116+
test.each(['sourceMap', 'sourcemap'])('options.%s === false', key => {
117+
const impl = new PluginImpl({ [key]: false })
118+
119+
const { map } = impl.compile(templateStr, 'foo.hbs')
120+
121+
expect(map).toStrictEqual({ mappings: '' })
122+
})
123+
})
124+
125+
test('ignores options.compiler.{srcName,destName}', () => {
126+
const impl = new PluginImpl({
127+
compiler: { srcName: 'bar/baz.handlebars', destName: 'quux/xyzzy.js' }
128+
})
129+
130+
const { map } = impl.compile(templateStr, 'foo.hbs')
131+
132+
expect(map).toHaveProperty('sources', [ 'foo.hbs' ])
133+
expect(map).not.toHaveProperty('file')
134+
})
135+
})
84136
})

0 commit comments

Comments
 (0)