Skip to content

Commit 4400723

Browse files
authored
Merge pull request #6 from mbland/pluginimpl-compile-partials-tests
Final PluginImpl.compile() tests, custom matchers
2 parents 4ac4309 + b9ff0dd commit 4400723

File tree

2 files changed

+89
-4
lines changed

2 files changed

+89
-4
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

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

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

test/plugin-impl.test.js

Lines changed: 87 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,35 @@ describe('PluginImpl', () => {
9898
return expect.stringMatching(new RegExp(`^;{${numLines}}`))
9999
}
100100

101+
expect.extend({
102+
toStartWith(received, expected) {
103+
const actual = received.substring(0, expected.length)
104+
return {
105+
pass: actual === expected,
106+
message: () => 'expected string to begin with expected prefix',
107+
actual,
108+
expected
109+
}
110+
},
111+
toEndWith(received, expected) {
112+
const actual = received.substring(received.length - expected.length)
113+
return {
114+
pass: actual === expected,
115+
message: () => 'expected string to end with expected suffix',
116+
actual,
117+
expected
118+
}
119+
}
120+
})
121+
101122
test('emits precompiled template module and source map', () => {
102123
const impl = new PluginImpl()
103124

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

106127
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)
128+
expect(code).toStartWith(expectedPrefix)
129+
expect(code).toEndWith(SUFFIX)
109130
expect(map).toMatchObject({
110131
sources: [ 'foo.hbs' ],
111132
mappings: mappingSkipsPrefix(expectedPrefix)
@@ -132,5 +153,69 @@ describe('PluginImpl', () => {
132153
expect(map).toHaveProperty('sources', [ 'foo.hbs' ])
133154
expect(map).not.toHaveProperty('file')
134155
})
156+
157+
describe('imports partials using', () => {
158+
const templateStr = '{{>bar}}{{>baz}}{{>quux}}'
159+
160+
test('DEFAULT_PARTIAL_PATH', () => {
161+
const impl = new PluginImpl()
162+
163+
const { code, map } = impl.compile(templateStr, 'foo.hbs')
164+
165+
const expectedPrefix = [
166+
PREFIX,
167+
'import \'./_bar.hbs\'',
168+
'import \'./_baz.hbs\'',
169+
'import \'./_quux.hbs\'',
170+
BEGIN_TEMPLATE
171+
].join('\n')
172+
expect(code).toStartWith(expectedPrefix)
173+
expect(code).toEndWith(SUFFIX)
174+
expect(map).toMatchObject({
175+
sources: [ 'foo.hbs' ],
176+
mappings: mappingSkipsPrefix(expectedPrefix)
177+
})
178+
})
179+
180+
test('custom options.partialPath', () => {
181+
const impl = new PluginImpl({
182+
partialPath: (partialName) => `./${partialName}.partial.hbs`
183+
})
184+
185+
const { code } = impl.compile(templateStr, 'foo.hbs')
186+
187+
const expectedPrefix = [
188+
PREFIX,
189+
'import \'./bar.partial.hbs\'',
190+
'import \'./baz.partial.hbs\'',
191+
'import \'./quux.partial.hbs\'',
192+
BEGIN_TEMPLATE
193+
].join('\n')
194+
expect(code).toStartWith(expectedPrefix)
195+
})
196+
})
197+
198+
describe('registers partials using', () => {
199+
test('DEFAULT_PARTIALS filter and DEFAULT_PARTIAL_NAME', () => {
200+
const impl = new PluginImpl()
201+
202+
const { code } = impl.compile(templateStr, '_foo.hbs')
203+
204+
const expected = 'Handlebars.registerPartial(\'foo\', RawTemplate)'
205+
expect(code).toEndWith(`${SUFFIX}\n${expected}`)
206+
})
207+
208+
test('custom options.partials and options.partialName', () => {
209+
const impl = new PluginImpl({
210+
partials: '**/*.partial.hbs',
211+
partialName(id) { return id.replace(/\.partial\.hbs$/, '') }
212+
})
213+
214+
const { code } = impl.compile(templateStr, 'foo.partial.hbs')
215+
216+
const expected = 'Handlebars.registerPartial(\'foo\', RawTemplate)'
217+
expect(code).toEndWith(`${SUFFIX}\n${expected}`)
218+
})
219+
})
135220
})
136221
})

0 commit comments

Comments
 (0)