forked from christian-fei/Simple-Jekyll-Search
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Templater.test.js
62 lines (49 loc) · 1.58 KB
/
Templater.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const { serial: test, beforeEach } = require('ava')
let templater
beforeEach(() => {
templater = require('../src/Templater.js')
templater.setOptions({
template: '{foo}',
pattern: /\{(.*?)\}/g
})
})
test('renders the template with the provided data', t => {
t.deepEqual(templater.compile({ foo: 'bar' }), 'bar')
templater.setOptions({
template: '<a href="{url}">url</a>'
})
t.deepEqual(templater.compile({ url: 'http://google.com' }), '<a href="http://google.com">url</a>')
})
test('renders the template with the provided data and query', t => {
t.deepEqual(templater.compile({ foo: 'bar' }), 'bar')
templater.setOptions({
template: '<a href="{url}?query={query}">url</a>'
})
t.deepEqual(templater.compile({ url: 'http://google.com', query: 'bar' }), '<a href="http://google.com?query=bar">url</a>')
})
test('replaces not found properties with the original pattern', t => {
const template = '{foo}'
templater.setOptions({
template
})
t.deepEqual(templater.compile({ x: 'bar' }), template)
})
test('allows custom patterns to be set', t => {
templater.setOptions({
template: '{{foo}}',
pattern: /\{\{(.*?)\}\}/g
})
t.deepEqual(templater.compile({ foo: 'bar' }), 'bar')
})
test('middleware gets parameter to return new replacement', t => {
templater.setOptions({
template: '{foo} - {bar}',
middleware (prop, value) {
if (prop === 'bar') {
return value.replace(/^\//, '')
}
}
})
const compiled = templater.compile({ foo: 'foo', bar: '/leading/slash' })
t.deepEqual(compiled, 'foo - leading/slash')
})