-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.js
151 lines (141 loc) · 4.17 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
'use strict'
const test = require('mvt')
const docxmarks = require('./index')
const b64 = require('./docx.json')
const docxDefault = Buffer.from(b64.default, 'base64')
const docxNested = Buffer.from(b64.nested, 'base64')
const docxIllegal = Buffer.from(b64.illegal, 'base64')
test(`test a bunch of bookmarks`, async (assert) => {
let old = {
FIRST: 'John',
LAST: 'Doe',
DATE: '12/01/2000',
FIRST_AGAIN: 'John',
MIDDLE: '',
SPACES_3: ' ',
LAST_AGAIN: 'Doe',
MASSIVE_TEXT: 'This is a whole bunch of freaking text, even a new paragraph.And here’s that new paragraph. All inside of a bookmark.'
}
let replacements = {
FIRST: 'Andrew',
LAST: 'Carpenter',
DATE: '12/01/2017',
FIRST_AGAIN: 'Jerry',
MIDDLE: 'R',
SPACES_3: ' ',
LAST_AGAIN: 'Smith',
MASSIVE_TEXT: 'This is some textage, not so massive.'
}
let updated = {}
let updaters = {}
Object.keys(old).forEach((k) => {
updaters[k] = (v) => {
updated[k] = v
return replacements[k]
}
})
let newDx = await docxmarks(docxDefault, updaters)
Object.keys(old).forEach((k) => assert.is(updated[k], old[k]))
await docxmarks(newDx, updaters)
Object.keys(old).forEach((k) => assert.is(updated[k], replacements[k]))
})
test(`bookmark replace works with Buffer`, async (assert) => {
let oldLast
let newLast = 'Jones'
let setOld = (last) => {
oldLast = last
return newLast
}
let newDx = await docxmarks(docxDefault, { LAST: setOld })
assert.is(oldLast, 'Doe')
let finalDx = await docxmarks(newDx, { LAST: setOld })
assert.is(oldLast, newLast)
assert.true(finalDx instanceof Buffer)
})
test(`bookmark replace works with ArrayBuffer`, async (assert) => {
let oldLast
let newLast = 'Jones'
let setOld = (last) => {
oldLast = last
return newLast
}
let newDx = await docxmarks(docxDefault.buffer, { LAST: setOld })
assert.is(oldLast, 'Doe')
let finalDx = await docxmarks(newDx, { LAST: setOld })
assert.is(oldLast, newLast)
assert.true(finalDx instanceof ArrayBuffer)
})
test(`bookmark replace works with Uint8Array`, async (assert) => {
let oldLast
let newLast = 'Jones'
let setOld = (last) => {
oldLast = last
return newLast
}
let newDx = await docxmarks(new Uint8Array(docxDefault.buffer), { LAST: setOld })
assert.is(oldLast, 'Doe')
let finalDx = await docxmarks(newDx, { LAST: setOld })
assert.is(oldLast, newLast)
assert.true(finalDx instanceof Uint8Array)
})
test(`bookmark replace works with base64`, async (assert) => {
let oldLast
let newLast = 'Jones'
let setOld = (last) => {
oldLast = last
return newLast
}
let newDx = await docxmarks(docxDefault.toString('base64'), { LAST: setOld })
assert.is(oldLast, 'Doe')
let finalDx = await docxmarks(newDx, { LAST: setOld })
assert.is(oldLast, newLast)
assert.true(typeof finalDx === 'string')
})
test(`appends bookmark if not found and append enabled`, async (assert) => {
let oldLast
let newLast = `It's there now`
let setOld = (last) => {
oldLast = last
return newLast
}
let newDx = await docxmarks(docxDefault, { MIA: { setter: newLast, append: true } })
await docxmarks(newDx, { MIA: setOld })
assert.is(oldLast, newLast)
})
test(`handles nested bookmarks`, async (assert) => {
let oldInner
let newInner = 'newinner'
let setOld = (last) => {
oldInner = last
return newInner
}
let newDx = await docxmarks(docxNested, { inner: setOld })
assert.is(oldInner, 'test')
await docxmarks(newDx, { inner: setOld, outer: 'replace outer and inner' })
assert.is(oldInner, newInner)
})
test(`gets list of bookmarks with no replacements`, async (assert) => {
let bookmarks = await docxmarks(docxDefault)
assert.is(bookmarks.FIRST, 'John')
})
test(`handles illegal XML characters`, async (assert) => {
let expected = {
_GoBack: '',
amp: ' & ',
gt: ' > ',
lt: ' < ',
pos: ' ‘ ',
quot: ' “ '
}
assert.deepEqual(await docxmarks(docxIllegal), expected)
let replacers = {
_GoBack: '',
amp: 'amp',
gt: 'gt',
lt: 'lt',
pos: 'pos',
quot: 'quot'
}
let updated = await docxmarks(docxIllegal, replacers)
assert.deepEqual(await docxmarks(updated), replacers)
})