pdfkit/tests/unit/document.spec.js
Jake Holland 36549b3946
Add support for dynamic sizing (#1576)
* Add support for dynamic sizing

- Enable defining sizes using any units (defaulting to Points)
- This also allows us to define sizes based on the current font context i.e. em's
- The new public `sizeToPoint` method allows users to also interact with these sizes to generate the correct point sizes

* Optimise side normalization

* Added test case for one margin being undefined
2025-01-12 18:17:13 -03:00

72 lines
1.7 KiB
JavaScript

import PDFDocument from '../../lib/document';
import { logData } from './helpers';
describe('PDFDocument', () => {
describe('font option', () => {
let fontSpy;
beforeEach(() => {
fontSpy = jest.spyOn(PDFDocument.prototype, 'font').mockReturnThis();
});
afterEach(() => {
fontSpy.mockRestore();
});
test('not defined', () => {
new PDFDocument();
expect(fontSpy).toBeCalledWith('Helvetica', null);
});
test('a string value', () => {
new PDFDocument({ font: 'Roboto' });
expect(fontSpy).toBeCalledWith('Roboto', null);
});
test('a falsy value', () => {
new PDFDocument({ font: null });
new PDFDocument({ font: false });
new PDFDocument({ font: '' });
expect(fontSpy).not.toBeCalled();
});
});
describe('document info', () => {
test('accepts properties with value undefined', () => {
expect(() => new PDFDocument({ info: { Title: undefined } })).not.toThrow(
new TypeError("Cannot read property 'toString' of undefined")
);
});
test('accepts properties with value null', () => {
expect(() => new PDFDocument({ info: { Title: null } })).not.toThrow(
new TypeError("Cannot read property 'toString' of null")
);
});
});
test('metadata is present for PDF 1.4', () => {
let doc = new PDFDocument({pdfVersion: '1.4'});
const data = logData(doc);
doc.end();
let catalog = data[data.length-28];
expect(catalog).toContain('/Metadata');
});
test('metadata is NOT present for PDF 1.3', () => {
let doc = new PDFDocument({pdfVersion: '1.3'});
const data = logData(doc);
doc.end();
let catalog = data[data.length-27];
expect(catalog).not.toContain('/Metadata');
});
});