This repository was archived by the owner on Dec 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathloadTemplate.test.js
More file actions
57 lines (50 loc) · 1.45 KB
/
loadTemplate.test.js
File metadata and controls
57 lines (50 loc) · 1.45 KB
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
/* eslint-disable */
jest.mock('./logger')
const fs = require('fs')
const path = require('path')
// require module
let m = null
beforeAll(() => {
m = require('./loadTemplate')
return m
})
test('loads html template', () => {
const filename = 'filename'
fs.readFile = jest.fn()
m(filename)
expect(fs.readFile.mock.calls[0][0])
.toBe(path.join(__dirname, `${filename}.html.mustache`))
})
test('loads plain template', () => {
const filename = 'filename'
fs.readFile = jest.fn()
m(filename)
expect(fs.readFile.mock.calls[1][0])
.toBe(path.join(__dirname, `${filename}.plain.mustache`))
})
test('returns a template renderer', () => {
fs.readFile = jest.fn((name, encoding, cb) => cb(null, 'data'))
expect.assertions(2)
return m('filename')
.then((renderer) => {
const rendered = renderer({})
expect(rendered.html).toBe('data')
expect(rendered.plain).toBe('data')
})
})
test('returned renderer substitutes variables', () => {
fs.readFile = jest.fn((name, encoding, cb) => cb(null, '{{data}}'))
expect.assertions(2)
return m('filename')
.then((renderer) => {
const rendered = renderer({data:'hello'})
expect(rendered.html).toBe('hello')
expect(rendered.plain).toBe('hello')
})
})
test('calls reject on template read fault', () => {
fs.readFile = jest.fn((name, encoding, cb) => cb('error', null))
expect.assertions(1)
return m('filename')
.catch(e => expect(e).toBe('error'))
})