salix/modules/invoiceOut/back/methods/invoiceOut/specs/createPdf.spec.js

43 lines
1.3 KiB
JavaScript

const models = require('vn-loopback/server/server').models;
const LoopBackContext = require('loopback-context');
const print = require('vn-print');
describe('InvoiceOut createPdf()', () => {
const userId = 1;
const activeCtx = {
accessToken: {userId: userId, id: 'DEFAULT_TOKEN'},
headers: {origin: 'http://localhost:5000'}
};
const ctx = {req: activeCtx};
it('should create a new PDF file and set true the hasPdf property', async() => {
const invoiceId = 1;
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
active: activeCtx
});
spyOn(print, 'Report').and.returnValue({
toPdfStream: () => {
return '';
}
});
spyOn(print.storage, 'write').and.returnValue(true);
const tx = await models.InvoiceOut.beginTransaction({});
const options = {transaction: tx};
try {
await models.InvoiceOut.createPdf(ctx, invoiceId, options);
const invoiceOut = await models.InvoiceOut.findById(invoiceId, null, options);
expect(invoiceOut.hasPdf).toBe(true);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
});