forked from christian-fei/Simple-Jekyll-Search
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTemplater.test.js
73 lines (63 loc) · 1.57 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
63
64
65
66
67
68
69
70
71
72
73
'use strict'
describe('Templater', function() {
var templater
beforeEach(function() {
templater = require('./Templater.js')
templater.setOptions({
template: '{foo}',
pattern: /\{(.*?)\}/g
})
})
it('renders the template with the provided data', function() {
expect(
templater.compile({foo:'bar'})
).toEqual(
'bar'
)
templater.setOptions({
template: '<a href="{url}">url</a>'
})
expect(
templater.compile({url:'http://google.com'})
).toEqual(
'<a href="http://google.com">url</a>'
)
})
it('replaces not found properties with the original pattern', function() {
var template = '{foo}'
templater.setOptions({
template: template
})
expect(
templater.compile({x:'bar'})
).toEqual(
template
)
})
it('allows custom patterns to be set', function() {
templater.setOptions({
template: '{{foo}}',
pattern: /\{\{(.*?)\}\}/g
})
expect(
templater.compile({foo:'bar'})
).toEqual(
'bar'
)
})
describe('middleware', function () {
it('middleware gets parameter to return new replacement', function () {
var middlewareOverwrite = 'middleware!'
templater.setOptions({
template: '{foo} - {bar}',
middleware: function(prop, value, template){
if( prop === 'bar' ){
return value.replace(/^\//, '')
}
}
})
var compiled = templater.compile({foo:'foo', bar: '/leading/slash'})
expect( compiled ).toEqual('foo - leading/slash')
})
})
})