Skip to content

Commit

Permalink
Fix set-pageSize behaviour (#455)
Browse files Browse the repository at this point in the history
* Add unit test for broken pageSize setter

* Reorder settings check to capture pageSize
  • Loading branch information
eKoopmans committed Jul 10, 2021
1 parent 3b2d8cf commit e2bcb98
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
30 changes: 15 additions & 15 deletions src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,22 +297,22 @@ Worker.prototype.set = function set(opt) {

// Build an array of setter functions to queue.
var fns = Object.keys(opt || {}).map(function (key) {
if (key in Worker.template.prop) {
// Set pre-defined properties.
return function set_prop() { this.prop[key] = opt[key]; }
} else {
switch (key) {
case 'margin':
return this.setMargin.bind(this, opt.margin);
case 'jsPDF':
return function set_jsPDF() { this.opt.jsPDF = opt.jsPDF; return this.setPageSize(); }
case 'pageSize':
return this.setPageSize.bind(this, opt.pageSize);
default:
// Set any other properties in opt.
return function set_opt() { this.opt[key] = opt[key] };
switch (key) {
case 'margin':
return this.setMargin.bind(this, opt.margin);
case 'jsPDF':
return function set_jsPDF() { this.opt.jsPDF = opt.jsPDF; return this.setPageSize(); }
case 'pageSize':
return this.setPageSize.bind(this, opt.pageSize);
default:
if (key in Worker.template.prop) {
// Set pre-defined properties in prop.
return function set_prop() { this.prop[key] = opt[key]; }
} else {
// Set any other properties in opt.
return function set_opt() { this.opt[key] = opt[key] };
}
}
}
}, this);

// Set properties within the promise chain.
Expand Down
8 changes: 8 additions & 0 deletions test/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ describe('settings', function () {
return Math.floor(val * k / 72 * 96);
}

it('set({ pageSize }) should call setPageSize', function () {
var worker = html2pdf();
chai.spy.on(worker, 'setPageSize', function () { return this.then(function () {}); });
return worker.set({ pageSize: 'test' }).then(function () {
expect(worker.setPageSize).to.have.been.called.with('test');
chai.spy.restore();
});
});
it('setPageSize() with no argument should use jsPDF default settings', function () {
var worker = html2pdf();
return worker.setPageSize().get('pageSize').then(function (val) {
Expand Down

0 comments on commit e2bcb98

Please sign in to comment.