Skip to content

Commit

Permalink
Merge pull request #6 from mbland/pluginimpl-compile-partials-tests
Browse files Browse the repository at this point in the history
Final PluginImpl.compile() tests, custom matchers
  • Loading branch information
mbland authored Jan 6, 2024
2 parents 4ac4309 + b9ff0dd commit 4400723
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 4 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

_**Status**: I've still got a bit of work to do before publishing v1.0.0. I need
to add tests based on the mbland/tomcat-servlet-testing-example project from
whence this came, add more documentation, and refactor. I plan to finish this by
2024-01-06._
whence this came and add more documentation. I plan to finish this by
2024-01-08._

Source: <https://github.com/mbland/rollup-plugin-handlebars-precompiler>

Expand Down
89 changes: 87 additions & 2 deletions test/plugin-impl.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,35 @@ describe('PluginImpl', () => {
return expect.stringMatching(new RegExp(`^;{${numLines}}`))
}

expect.extend({
toStartWith(received, expected) {
const actual = received.substring(0, expected.length)
return {
pass: actual === expected,
message: () => 'expected string to begin with expected prefix',
actual,
expected
}
},
toEndWith(received, expected) {
const actual = received.substring(received.length - expected.length)
return {
pass: actual === expected,
message: () => 'expected string to end with expected suffix',
actual,
expected
}
}
})

test('emits precompiled template module and source map', () => {
const impl = new PluginImpl()

const { code, map } = impl.compile(templateStr, 'foo.hbs')

const expectedPrefix = `${PREFIX}\n${BEGIN_TEMPLATE}`
expect(code.substring(0, expectedPrefix.length)).toBe(expectedPrefix)
expect(code.substring(code.length - SUFFIX.length)).toBe(SUFFIX)
expect(code).toStartWith(expectedPrefix)
expect(code).toEndWith(SUFFIX)
expect(map).toMatchObject({
sources: [ 'foo.hbs' ],
mappings: mappingSkipsPrefix(expectedPrefix)
Expand All @@ -132,5 +153,69 @@ describe('PluginImpl', () => {
expect(map).toHaveProperty('sources', [ 'foo.hbs' ])
expect(map).not.toHaveProperty('file')
})

describe('imports partials using', () => {
const templateStr = '{{>bar}}{{>baz}}{{>quux}}'

test('DEFAULT_PARTIAL_PATH', () => {
const impl = new PluginImpl()

const { code, map } = impl.compile(templateStr, 'foo.hbs')

const expectedPrefix = [
PREFIX,
'import \'./_bar.hbs\'',
'import \'./_baz.hbs\'',
'import \'./_quux.hbs\'',
BEGIN_TEMPLATE
].join('\n')
expect(code).toStartWith(expectedPrefix)
expect(code).toEndWith(SUFFIX)
expect(map).toMatchObject({
sources: [ 'foo.hbs' ],
mappings: mappingSkipsPrefix(expectedPrefix)
})
})

test('custom options.partialPath', () => {
const impl = new PluginImpl({
partialPath: (partialName) => `./${partialName}.partial.hbs`
})

const { code } = impl.compile(templateStr, 'foo.hbs')

const expectedPrefix = [
PREFIX,
'import \'./bar.partial.hbs\'',
'import \'./baz.partial.hbs\'',
'import \'./quux.partial.hbs\'',
BEGIN_TEMPLATE
].join('\n')
expect(code).toStartWith(expectedPrefix)
})
})

describe('registers partials using', () => {
test('DEFAULT_PARTIALS filter and DEFAULT_PARTIAL_NAME', () => {
const impl = new PluginImpl()

const { code } = impl.compile(templateStr, '_foo.hbs')

const expected = 'Handlebars.registerPartial(\'foo\', RawTemplate)'
expect(code).toEndWith(`${SUFFIX}\n${expected}`)
})

test('custom options.partials and options.partialName', () => {
const impl = new PluginImpl({
partials: '**/*.partial.hbs',
partialName(id) { return id.replace(/\.partial\.hbs$/, '') }
})

const { code } = impl.compile(templateStr, 'foo.partial.hbs')

const expected = 'Handlebars.registerPartial(\'foo\', RawTemplate)'
expect(code).toEndWith(`${SUFFIX}\n${expected}`)
})
})
})
})

0 comments on commit 4400723

Please sign in to comment.