/
file-handlers.spec.ts
157 lines (131 loc) · 4.64 KB
/
file-handlers.spec.ts
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/* global it, expect, describe */
import { renderFile, __express, templates, compile } from '../src/index'
import { buildRegEx } from './err.spec'
var path = require('path'),
filePath = path.join(__dirname, 'templates/simple.eta'),
errFilePath = path.join(__dirname, 'templates/badsyntax.eta'),
fakeFilePath = path.join(__dirname, 'templates/fake.eta')
describe('Simple renderFile tests', () => {
it('renders a simple template file', async () => {
var renderedFile = await renderFile(filePath, { name: 'Ben' })
expect(renderedFile).toEqual('Hi Ben')
})
it('renderFile is aliased as __express', async () => {
var renderedFile = await __express(filePath, { name: 'Ben' })
expect(renderedFile).toEqual('Hi Ben')
})
it('renders async template with callback', (done) => {
function cb(_err: Error | null, res?: string) {
try {
expect(res).toBe('Hi Ada Lovelace')
done()
} catch (error) {
done(error)
}
}
renderFile(filePath, { name: 'Ada Lovelace', async: true }, cb)
})
it('renders a simple template w/ a callback', async () => {
renderFile(filePath, { name: 'Ben' }, function (_err: Error | null, res?: string) {
expect(res).toEqual('Hi Ben')
})
})
it('renders a simple template w/ callback and explicit config', async () => {
// Note that rmWhitespace doesn't do anything specific
renderFile(filePath, { name: 'Ben' }, { rmWhitespace: true }, function (
_err: Error | null,
res?: string
) {
expect(res).toEqual('Hi Ben')
})
})
it('renders an async template using Promises', async () => {
var res = await renderFile(filePath, { name: 'Ada', async: true })
expect(res).toEqual('Hi Ada')
})
it('renders an async template with an explicit config using Promises', async () => {
var res = await renderFile(filePath, { name: 'Ada' }, { async: true })
expect(res).toEqual('Hi Ada')
})
it('uses cached version of a file', async () => {
templates.define(fakeFilePath, compile('This template does not exist'))
// renderFile should just look straight in the cache for the template
renderFile(fakeFilePath, { cache: true }, function (_err: Error | null, res?: string) {
expect(res).toEqual('This template does not exist')
})
})
it('parses a simple template w/ settings from Express', async () => {
renderFile(
filePath,
{
name: '<p>Ben</p>',
cache: true,
settings: {
views: [path.join(__dirname, 'templates'), path.join(__dirname, 'othertemplates')],
'view cache': true,
'view options': { autoEscape: false }
}
},
function (_err: Error | null, res?: string) {
expect(res).toEqual('Hi <p>Ben</p>')
}
)
})
})
describe('File location tests', () => {
it('locates a file with the views option', async () => {
let res = await renderFile(
'simple.eta',
{ name: 'Ada' },
{ views: path.join(__dirname, 'templates') }
)
expect(res).toEqual('Hi Ada')
})
})
describe('renderFile error tests', () => {
it('render file with callback works on error', (done) => {
function cb(err: Error, _res?: string) {
expect(err).toBeTruthy()
expect(err.message).toMatch(
buildRegEx(`
var tR='',__l,__lP,include=E.include.bind(E),includeFile=E.includeFile.bind(E)
function layout(p,d){__l=p;__lP=d}
tR+='Hi '
tR+=E.e(badSyntax(=!)
if(__l)tR=await includeFile(__l,Object.assign(it,{body:tR},__lP))
if(cb){cb(null,tR)} return tR
`)
)
done()
}
renderFile(errFilePath, { name: 'Ada Lovelace', async: true }, cb)
})
test('throws with bad inner JS syntax using Promises', async () => {
await expect(async () => {
await renderFile(errFilePath, {})
}).rejects.toThrow(
buildRegEx(`
var tR='',__l,__lP,include=E.include.bind(E),includeFile=E.includeFile.bind(E)
function layout(p,d){__l=p;__lP=d}
tR+='Hi '
tR+=E.e(badSyntax(=!)
if(__l)tR=includeFile(__l,Object.assign(it,{body:tR},__lP))
if(cb){cb(null,tR)} return tR
`)
)
})
})
// NOTE: the errors will really look like this:
/*
Loading file: /home/nebrelbug/Coding/eta/test/templates/badsyntax.eta failed:
Bad template syntax
Unexpected token '='
====================
var tR='',__l,__lP,include=E.include.bind(E),includeFile=E.includeFile.bind(E)
function layout(p,d){__l=p;__lP=d}
tR+='Hi '
tR+=E.e(badSyntax(=!)
if(__l)tR=includeFile(__l,Object.assign(it,{body:tR},__lP))
if(cb){cb(null,tR)} return tR
*/
// Unfortunately, Node throws different errors ("Unexpected token '='", "Unexpected token =", "Invalid or unexpected token") depending on the version so we can't test against the first part of the string.