-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test collectPartials(), ignore @partial-block
While adding tests for PartialCollector, it became apparent that wrapping it in collectPartials() made both the tests and the production code more concise. As expected, the code already handled block partials and ignored dynamic and inline partials. But while reading the partials documentation, I discovered that the code didn't yet handle `@partial-block`: - https://handlebarsjs.com/guide/partials.html It proved easy enough to test and fix.
- Loading branch information
Showing
4 changed files
with
97 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
import collectPartials from '../lib/partials.js' | ||
import Handlebars from 'handlebars' | ||
import { describe, expect, test } from 'vitest' | ||
|
||
describe('collectPartials', () => { | ||
/** | ||
* Returns the partial names parsed from a Handlebars template | ||
* @param {string} s - Handlebars template strin to parse | ||
* @returns {string[]} - a list of partial names parsed from the template | ||
*/ | ||
const partials = (s) => collectPartials(Handlebars.parse(s, {})) | ||
|
||
describe('parses', () => { | ||
test('regular partials', () => { | ||
const s = '{{ foo }}{{> bar }}{{ baz }}{{> quux }}{{ xyzzy }}{{> plugh }}' | ||
|
||
expect(partials(s)) | ||
.toEqual(['bar', 'quux', 'plugh']) | ||
}) | ||
|
||
test('partials with contexts or parameters', () => { | ||
expect(partials('{{> foo bar}}{{> baz quux=xyzzy }}')) | ||
.toEqual(['foo', 'baz']) | ||
}) | ||
|
||
test('block partials', () => { | ||
expect(partials('{{#> foo }}bar{{>baz}}quux{{/foo}}')) | ||
.toEqual(['foo', 'baz']) | ||
}) | ||
}) | ||
|
||
describe('ignores', () => { | ||
test('dynamic partials', () => { | ||
expect(partials('{{> foo }}{{> (lookup . ignored) }}{{> bar }}')) | ||
.toEqual(['foo', 'bar']) | ||
}) | ||
|
||
test('@partial-block', () => { | ||
expect(partials('Some text {{> @partial-block }}')) | ||
.toEqual([]) | ||
}) | ||
|
||
test('inline partials', () => { | ||
// Examples from: | ||
// - https://handlebarsjs.com/guide/partials.html#inline-partials | ||
const s = [ | ||
'{{#*inline "myPartial"}}', | ||
' My Content', | ||
'{{/inline}}', | ||
'{{#each people}}', | ||
' {{> myPartial}}', | ||
'{{/each}}', | ||
'', | ||
'{{#> layout}}', | ||
' {{#*inline "nav"}}', | ||
' My Nav', | ||
' {{/inline}}', | ||
' {{#*inline "content"}}', | ||
' My Content', | ||
' {{/inline}}', | ||
'{{/layout}}' | ||
].join('\n') | ||
|
||
expect(partials(s)) | ||
.toEqual(['myPartial', 'layout']) | ||
}) | ||
}) | ||
}) |