-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1542fdf
commit 3584fbd
Showing
1 changed file
with
94 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,99 @@ | ||
import * as binding from '..'; | ||
import {Template} from '..'; | ||
|
||
describe('Template', () => { | ||
test('constructor works', () => { | ||
expect(() => { | ||
new Template('test'); | ||
}).not.toThrow(); | ||
let template: Template; | ||
|
||
beforeEach(() => { | ||
template = new Template('test_template'); | ||
}); | ||
|
||
// TODO: clone | ||
|
||
test('#definedTemplates works', () => { | ||
expect(template.definedTemplates()).toBe(''); | ||
template.parse('{{define "foo"}}{{ end }}'); | ||
expect(template.definedTemplates()).toMatch(/^; defined templates are: ("foo", "test_template"|"test_template", "foo")$/); | ||
}); | ||
|
||
// TODO: delims | ||
|
||
test('#executeString works', () => { | ||
template.parse('{{ .foo }}, {{ .bar }}'); | ||
expect(template.executeString({foo: 'hello', bar: 'world'})).toBe('hello, world'); | ||
}); | ||
|
||
test('#executeTemplateString works', () => { | ||
template.parse('{{ define "inner" }}inner {{ .param }}{{ end }}outer'); | ||
expect(template.executeTemplateString('inner', {param: 'param'})).toBe('inner param'); | ||
}); | ||
|
||
describe('#funcs', () => { | ||
it('works', () => { | ||
const myFunc = jest.fn((value: string) => `pre-${value}-post`); | ||
template.funcs({ myFunc }); | ||
template.parse('{{ myFunc "hello" }}'); | ||
expect(template.executeString()).toBe('pre-hello-post'); | ||
expect(myFunc).toHaveBeenCalledWith('hello'); | ||
expect(myFunc).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('round-trips JS types', () => { | ||
const myFunc = jest.fn((value: unknown) => value); | ||
template.funcs({ myFunc }); | ||
template.parse('{{ myFunc .param }}'); | ||
// expect(template.executeString({param: undefined})).toBe('<no value>'); | ||
expect(template.executeString({param: null})).toBe('<no value>'); | ||
expect(myFunc).toHaveBeenLastCalledWith(null); | ||
expect(template.executeString({param: 123})).toBe('123'); | ||
expect(myFunc).toHaveBeenLastCalledWith(123); | ||
expect(template.executeString({param: 'param'})).toBe('param'); | ||
expect(myFunc).toHaveBeenLastCalledWith('param'); | ||
expect(template.executeString({param: {foo: 'bar'}})).toBe('map[foo:bar]'); | ||
expect(myFunc).toHaveBeenLastCalledWith({foo: 'bar'}); | ||
expect(template.executeString({param: ['foo', 'bar']})).toBe('[foo bar]'); | ||
expect(myFunc).toHaveBeenLastCalledWith(['foo', 'bar']); | ||
// TODO: BigInt support when supported | ||
}); | ||
}); | ||
|
||
test('#lookup works', () => { | ||
template.parse('{{ define "foo" }}{{ end }}'); | ||
const fooTemplate = template.lookup('foo'); | ||
expect(fooTemplate).toBeInstanceOf(Template); | ||
expect(fooTemplate?.name()).toBe('foo'); | ||
}); | ||
|
||
test('#name works', () => { | ||
expect(template.name()).toBe('test_template'); | ||
}); | ||
|
||
test('#new works', () => { | ||
const inner = template.new('inner').parse('new {{ .param }}'); | ||
expect(inner).toBeInstanceOf(Template); | ||
expect(inner.name()).toBe('inner'); | ||
expect(template.executeTemplateString('inner', {param: 'param'})).toBe('new param'); | ||
}); | ||
|
||
// TODO: option, parse, parseFiles, parseGlob, templates | ||
}); | ||
|
||
test('htmlEscapeString works', () => { | ||
expect(binding.htmlEscapeString('<br>')).toBe('<br>'); | ||
}); | ||
|
||
test('htmlEscaper works', () => { | ||
expect(binding.htmlEscaper('foo', '<bar>')).toBe('foo<bar>'); | ||
}); | ||
|
||
test('jsEscapeString works', () => { | ||
expect(binding.jsEscapeString('"foo"')).toBe('\\"foo\\"'); | ||
}); | ||
|
||
test('jsEscaper works', () => { | ||
expect(binding.jsEscaper('foo', '"bar"')).toBe('foo\\"bar\\"'); | ||
}); | ||
|
||
test('urlQueryEscaper works', () => { | ||
expect(binding.urlQueryEscaper('foo', '&bar')).toBe('foo%26bar'); | ||
}); |