Skip to content

Commit

Permalink
#113 Fixed regression introduced by relying on incorrect/incomplete t…
Browse files Browse the repository at this point in the history
…ype annotations of jsZip (zip.file() can return null)
  • Loading branch information
jjhbw committed Apr 23, 2020
1 parent 437c597 commit 83f523b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
Binary file modified src/__tests__/fixtures/imageHeader.docx
Binary file not shown.
31 changes: 30 additions & 1 deletion src/__tests__/images.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import path from 'path';
import fs from 'fs';
import { createReport } from '../index';
import { Image } from '../types';
import QR from 'qrcode';

it('001: Issue #61 Correctly renders an SVG image', async () => {
const template = await fs.promises.readFile(
Expand Down Expand Up @@ -144,3 +143,33 @@ it('003: can inject an svg without a thumbnail', async () => {
const result = await createReport(opts, 'JS');
expect(result).toMatchSnapshot();
});

it('004: can inject an image in the document header (regression test for #113)', async () => {
const template = await fs.promises.readFile(
path.join(__dirname, 'fixtures', 'imageHeader.docx')
);

const opts = {
template,
data: {},
additionalJsContext: {
image: async () => {
const data = await fs.promises.readFile(
path.join(__dirname, 'fixtures', 'sample.png')
);
return {
width: 6,
height: 6,
data,
extension: '.png',
};
},
},
};

// NOTE: bug does not happen when using debug probe arguments ('JS' or 'XML'),
// as these exit before the headers are parsed.
// TODO: build a snapshot test once _probe === 'XML' properly includes all document XMLs, not just
// the main document
return expect(createReport(opts)).resolves.toBeInstanceOf(Uint8Array);
});
8 changes: 6 additions & 2 deletions src/zip.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import JSZip from 'jszip';

const zipLoad = (inputFile: ArrayBuffer) => JSZip.loadAsync(inputFile);
const zipGetText = (zip: JSZip, filename: string) =>
zip.file(filename).async('text');
const zipGetText = (zip: JSZip, filename: string) => {
const file_in_zip = zip.file(filename);
if (!file_in_zip) return null;
return file_in_zip.async('text');
};

const zipSetText = (zip: JSZip, filename: string, data: string) =>
zip.file(filename, data);
const zipSetBinary = (zip: JSZip, filename: string, data: ArrayBuffer) =>
Expand Down

0 comments on commit 83f523b

Please sign in to comment.