forked from eKoopmans/html2pdf.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnapshot.js
82 lines (75 loc) · 3.24 KB
/
snapshot.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
describe('snapshot', () => {
before(() => {
return pdftest.api.connect('http://localhost:3000');
});
function loadElement({ document, tagName, src }) {
const element = document.createElement(tagName);
const loaded = new Promise(resolve => element.addEventListener('load', () => resolve(element)));
element.src = src;
document.body.appendChild(element);
return loaded;
}
const defaultSettings = { html2canvas: { logging: false } };
const pageBreakSettings = pagebreak => Object.assign({}, defaultSettings, { pagebreak, jsPDF: { orientation: 'portrait', unit: 'in', format: 'letter' } });
const defaultCondition = (window, customSettings, src) => {
const settings = Object.assign({}, defaultSettings, customSettings);
return window.html2pdf().set(settings).from(src || window.document.body).outputPdf('arraybuffer');
};
const conditions = {
default: {
runner: defaultCondition,
name: file => `${file}.pdf`,
},
legacy: {
runner: window => window.html2pdf(window.document.body, defaultSettings).outputPdf('arraybuffer'),
name: file => `${file}.pdf`,
},
margin: {
runner: window => defaultCondition(window, { margin: 1, jsPDF: { unit: 'in' } }),
name: file => `${file}_margin.pdf`,
},
selectMainId: {
runner: window => defaultCondition(window, {}, window.document.getElementById('main')),
name: file => `${file}.pdf`,
},
pagebreakLegacy: {
runner: window => defaultCondition(window, pageBreakSettings({ mode: 'legacy' })),
name: file => `${file}_legacy.pdf`,
},
pagebreakCss: {
runner: window => defaultCondition(window, pageBreakSettings({ mode: 'css' })),
name: file => `${file}_css.pdf`,
},
pagebreakAvoidAll: {
runner: window => defaultCondition(window, pageBreakSettings({ mode: 'avoid-all' })),
name: file => `${file}_avoid-all.pdf`,
},
pagebreakSpecify: {
runner: window => defaultCondition(window, pageBreakSettings({ before: '.before', after: '.after', avoid: '.avoid' })),
name: file => `${file}_specify.pdf`,
},
};
const filesToTest = {
'blank': [ 'default' ],
'lorem-ipsum': [ 'default', 'legacy', 'margin' ],
'all-tags': [ 'default' ],
'css-selectors': [ 'selectMainId' ],
'pagebreaks': [ 'pagebreakLegacy', 'pagebreakCss', 'pagebreakAvoidAll', 'pagebreakSpecify' ],
};
Object.keys(filesToTest).forEach(file => describe(file, () => {
let iframe;
before(async () => {
iframe = await loadElement({ document, tagName: 'iframe', src: `/base/test/reference/${file}.html` });
await loadElement({ document: iframe.contentDocument, tagName: 'script', src: '/base/src/index.js' });
chai.spy.on(iframe.contentWindow.html2pdf.Worker.prototype, 'save', function () { return this.then(function save() {}); });
});
after(() => {
chai.spy.restore();
document.body.removeChild(iframe);
});
filesToTest[file].forEach(condition => it(`should match snapshot for ${condition} settings`, async () => {
const pdf = await conditions[condition].runner(iframe.contentWindow);
await expect(pdf).to.matchPdfSnapshot({ interactive: true, customSnapshotIdentifier: conditions[condition].name(file) });
}));
}));
});