This repository has been archived by the owner on Dec 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.js
executable file
·167 lines (145 loc) · 4.12 KB
/
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
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
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/env node
const fs = require('fs')
const path = require('path')
const rimraf = require('rimraf')
const extend = require('xtend')
const test = require('tape')
const letterpress = require('.')
const letter = require('./test.md')
const data = JSON.parse(fs.readFileSync(path.join(__dirname, 'test.json')))
const date = new Date()
const dateString = date.getDate() + '/' + (date.getMonth() + 1) + '/' + date.getFullYear()
const testDir = path.join(__dirname, 'test')
if (!fs.existsSync(testDir)) {
try {
fs.mkdirSync(testDir)
} catch (e) {
throw new Error('Output directory does not exist and could not be created: ' + testDir)
}
}
const opts = {
puppeteer: {
args: [
'--no-sandbox',
'--no-default-browser-check',
'--no-first-run',
'--disable-default-apps'
]
}
}
test('print', function (t) {
const out = path.join(testDir, 'print')
const expected = [
'dapibus_foo.html',
'dapibus_foo.pdf'
]
t.plan(expected.length + 1)
const markdown = letter(dateString, data.from[0], data.to[0])
const id = data.from[0].id + '_' + data.to[0].id
t.comment(`removing dir ${out} ...`)
rimraf(out, function (err) {
if (err) throw err
letterpress.print(id, markdown, extend({
path: out
}, opts || {}))
.catch(e => {
console.error(e.toString())
t.fail('complete print')
})
.then(() => {
t.pass('complete print')
expected.forEach(p => t.ok(fs.existsSync(path.join(out, p)), 'exists: ' + p))
t.end()
})
})
})
test('print with launch & close', function (t) {
const out = path.join(testDir, 'print_with_launch_close')
const expected = [
'dapibus_foo.html',
'dapibus_foo.pdf'
]
t.plan(expected.length + 1)
const markdown = letter(dateString, data.from[0], data.to[0])
const id = data.from[0].id + '_' + data.to[0].id
t.comment(`removing dir ${out} ...`)
rimraf(out, async function (err) {
if (err) throw err
let instance
letterpress.launch(extend({
path: out
}, opts || {}))
.then(press => {
instance = press
return press
})
.then(press => press.print(id, markdown))
.then(press => press.close())
.then(() => t.pass('complete print with launch & close'))
.catch(e => {
console.error(e.toString())
t.fail('complete print with launch & close')
if (instance && instance.close) instance.close()
})
.then(() => {
expected.forEach(p => t.ok(fs.existsSync(path.join(out, p)), 'exists: ' + p))
t.end()
})
})
})
test('print multiple', function (t) {
const out = path.join(testDir, 'print_multiple')
const expected = [
'dapibus_foo.html',
'dapibus_bar.html',
'dapibus_fooo.html',
'dapibus_barr.html',
'parturient_foo.html',
'parturient_bar.html',
'parturient_fooo.html',
'parturient_barr.html',
'dapibus_foo.pdf',
'dapibus_bar.pdf',
'dapibus_fooo.pdf',
'dapibus_barr.pdf',
'parturient_foo.pdf',
'parturient_bar.pdf',
'parturient_fooo.pdf',
'parturient_barr.pdf'
]
t.plan(expected.length + (data.from.length * data.to.length) + 1)
t.comment(`removing dir ${out} ...`)
rimraf(out, async function (err) {
if (err) throw err
let press
try {
press = await letterpress.launch(extend({
path: out
}, opts || {}))
const ps = [] // promises
data.from.forEach(from => {
data.to.forEach(to => {
const markdown = letter(dateString, from, to)
const id = from.id + '_' + to.id
ps.push(
press.print(id, markdown)
.then(() => t.pass('print ' + id))
.catch(e => {
console.error(e.toString())
t.fail('print ' + id)
})
)
})
})
await Promise.all(ps)
press.close()
t.pass('complete print multiple')
} catch (e) {
console.error(e.toString())
t.fail('complete print multiple')
if (press && press.close) press.close()
}
expected.forEach(p => t.ok(fs.existsSync(path.join(out, p)), 'exists: ' + p))
t.end()
})
})