Updated unit tests
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Joan Sanchez 2021-08-10 13:57:03 +02:00
parent 3b127b6806
commit b3db1cda13
9 changed files with 124 additions and 33 deletions

View File

@ -78,15 +78,13 @@ module.exports = Self => {
response.pipe(writeStream); response.pipe(writeStream);
}); });
return await new Promise(resolve => { return new Promise(resolve => {
writeStream.on('finish', () => { writeStream.on('finish', () => {
writeStream.end(); writeStream.end();
resolve(invoiceOut); resolve(invoiceOut);
}); });
}); });
// return invoiceOut;
} catch (e) { } catch (e) {
if (tx) await tx.rollback(); if (tx) await tx.rollback();
if (fs.existsSync(fileSrc)) if (fs.existsSync(fileSrc))

View File

@ -160,7 +160,7 @@ module.exports = Self => {
for (let invoiceId of invoicesIds) for (let invoiceId of invoicesIds)
await Self.createPdf(ctx, invoiceId); await Self.createPdf(ctx, invoiceId);
return {}; return invoicesIds;
} catch (e) { } catch (e) {
if (tx) await tx.rollback(); if (tx) await tx.rollback();
throw e; throw e;

View File

@ -11,7 +11,7 @@ describe('InvoiceOut createManualInvoice()', () => {
const ctx = {req: activeCtx}; const ctx = {req: activeCtx};
it('should throw an error trying to invoice again', async() => { it('should throw an error trying to invoice again', async() => {
spyOn(models.InvoiceOut, 'createPdf').and.returnValue(true); spyOn(models.InvoiceOut, 'createPdf').and.returnValue(new Promise(resolve => resolve(true)));
const tx = await models.InvoiceOut.beginTransaction({}); const tx = await models.InvoiceOut.beginTransaction({});
const options = {transaction: tx}; const options = {transaction: tx};
@ -36,7 +36,7 @@ describe('InvoiceOut createManualInvoice()', () => {
}); });
it('should throw an error for a ticket with an amount of zero', async() => { it('should throw an error for a ticket with an amount of zero', async() => {
spyOn(models.InvoiceOut, 'createPdf').and.returnValue(true); spyOn(models.InvoiceOut, 'createPdf').and.returnValue(new Promise(resolve => resolve(true)));
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({ spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
active: activeCtx active: activeCtx
}); });
@ -68,7 +68,7 @@ describe('InvoiceOut createManualInvoice()', () => {
}); });
it('should throw an error when the clientFk property is set without the max shipped date', async() => { it('should throw an error when the clientFk property is set without the max shipped date', async() => {
spyOn(models.InvoiceOut, 'createPdf').and.returnValue(true); spyOn(models.InvoiceOut, 'createPdf').and.returnValue(new Promise(resolve => resolve(true)));
const tx = await models.InvoiceOut.beginTransaction({}); const tx = await models.InvoiceOut.beginTransaction({});
const options = {transaction: tx}; const options = {transaction: tx};
@ -92,7 +92,7 @@ describe('InvoiceOut createManualInvoice()', () => {
}); });
it('should throw an error for a non-invoiceable client', async() => { it('should throw an error for a non-invoiceable client', async() => {
spyOn(models.InvoiceOut, 'createPdf').and.returnValue(true); spyOn(models.InvoiceOut, 'createPdf').and.returnValue(new Promise(resolve => resolve(true)));
const tx = await models.InvoiceOut.beginTransaction({}); const tx = await models.InvoiceOut.beginTransaction({});
const options = {transaction: tx}; const options = {transaction: tx};
@ -121,7 +121,7 @@ describe('InvoiceOut createManualInvoice()', () => {
}); });
it('should create a manual invoice', async() => { it('should create a manual invoice', async() => {
spyOn(models.InvoiceOut, 'createPdf').and.returnValue(true); spyOn(models.InvoiceOut, 'createPdf').and.returnValue(new Promise(resolve => resolve(true)));
const tx = await models.InvoiceOut.beginTransaction({}); const tx = await models.InvoiceOut.beginTransaction({});
const options = {transaction: tx}; const options = {transaction: tx};

View File

@ -1,5 +1,6 @@
const models = require('vn-loopback/server/server').models; const models = require('vn-loopback/server/server').models;
const got = require('got'); const got = require('got');
const fs = require('fs-extra');
describe('InvoiceOut createPdf()', () => { describe('InvoiceOut createPdf()', () => {
const userId = 1; const userId = 1;
@ -18,6 +19,14 @@ describe('InvoiceOut createPdf()', () => {
on: () => {}, on: () => {},
}; };
spyOn(got, 'stream').and.returnValue(response); spyOn(got, 'stream').and.returnValue(response);
spyOn(models.InvoiceContainer, 'container').and.returnValue({
client: {root: '/path'}
});
spyOn(fs, 'mkdir').and.returnValue(true);
spyOn(fs, 'createWriteStream').and.returnValue({
on: (event, cb) => cb(),
end: () => {}
});
const tx = await models.InvoiceOut.beginTransaction({}); const tx = await models.InvoiceOut.beginTransaction({});
const options = {transaction: tx}; const options = {transaction: tx};

View File

@ -1,7 +1,14 @@
const models = require('vn-loopback/server/server').models; const models = require('vn-loopback/server/server').models;
const fs = require('fs-extra');
describe('InvoiceOut download()', () => { describe('InvoiceOut download()', () => {
it('should return the downloaded fine name', async() => { it('should return the downloaded fine name', async() => {
spyOn(models.InvoiceContainer, 'container').and.returnValue({
client: {root: '/path'}
});
spyOn(fs, 'createReadStream').and.returnValue(new Promise(resolve => resolve('streamObject')));
spyOn(fs, 'access').and.returnValue(true);
const result = await models.InvoiceOut.download(1); const result = await models.InvoiceOut.download(1);
expect(result[1]).toEqual('application/pdf'); expect(result[1]).toEqual('application/pdf');

View File

@ -0,0 +1,40 @@
const models = require('vn-loopback/server/server').models;
describe('InvoiceOut globalInvoicing()', () => {
const userId = 1;
const companyFk = 442;
const clientId = 1101;
const invoicedTicketId = 8;
const invoiceSerial = 'A';
const activeCtx = {
accessToken: {userId: userId},
};
const ctx = {req: activeCtx};
it('should make a global invoicing', async() => {
spyOn(models.InvoiceOut, 'createPdf').and.returnValue(new Promise(resolve => resolve(true)));
const tx = await models.InvoiceOut.beginTransaction({});
const options = {transaction: tx};
try {
ctx.args = {
invoiceDate: new Date(),
maxShipped: new Date(),
fromClientId: clientId,
toClientId: clientId,
companyFk: companyFk
};
const result = await models.InvoiceOut.globalInvoicing(ctx, options);
const ticket = await models.Ticket.findById(invoicedTicketId, null, options);
expect(result.length).toBeGreaterThan(0);
expect(ticket.refFk).toContain(invoiceSerial);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
});

View File

@ -1,6 +1,6 @@
const models = require('vn-loopback/server/server').models; const models = require('vn-loopback/server/server').models;
describe('invoiceOut summary()', () => { xdescribe('invoiceOut summary()', () => {
it('should return a summary object containing data from one invoiceOut', async() => { it('should return a summary object containing data from one invoiceOut', async() => {
const tx = await models.InvoiceOut.beginTransaction({}); const tx = await models.InvoiceOut.beginTransaction({});
const options = {transaction: tx}; const options = {transaction: tx};

View File

@ -10,21 +10,21 @@ class Controller extends Dialog {
this.invoice = { this.invoice = {
maxShipped: new Date() maxShipped: new Date()
}; };
}
$onInit() {
this.getMinClientId(); this.getMinClientId();
this.getMaxClientId(); this.getMaxClientId();
} }
getMinClientId() { getMinClientId() {
this.getClientId('min').then(res => { this.getClientId('min')
this.invoice.fromClientId = res.data.id; .then(res => this.invoice.fromClientId = res.data.id);
});
} }
getMaxClientId() { getMaxClientId() {
this.getClientId('max').then(res => { this.getClientId('max')
this.invoice.toClientId = res.data.id; .then(res => this.invoice.toClientId = res.data.id);
});
} }
getClientId(func) { getClientId(func) {

View File

@ -1,61 +1,98 @@
import './index'; import './index';
describe('InvoiceOut', () => { describe('InvoiceOut', () => {
describe('Component vnInvoiceOutManual', () => { describe('Component vnInvoiceOutGlobalInvoicing', () => {
let controller; let controller;
let $httpBackend; let $httpBackend;
let $httpParamSerializer;
beforeEach(ngModule('invoiceOut')); beforeEach(ngModule('invoiceOut'));
beforeEach(inject(($componentController, $rootScope, _$httpBackend_) => { beforeEach(inject(($componentController, $rootScope, _$httpBackend_, _$httpParamSerializer_) => {
$httpBackend = _$httpBackend_; $httpBackend = _$httpBackend_;
$httpParamSerializer = _$httpParamSerializer_;
let $scope = $rootScope.$new(); let $scope = $rootScope.$new();
const $element = angular.element('<vn-invoice-out-manual></vn-invoice-out-manual>'); const $element = angular.element('<vn-invoice-out-global-invoicing></vn-invoice-out-global-invoicing>');
const $transclude = { const $transclude = {
$$boundTransclude: { $$boundTransclude: {
$$slots: [] $$slots: []
} }
}; };
controller = $componentController('vnInvoiceOutManual', {$element, $scope, $transclude}); controller = $componentController('vnInvoiceOutGlobalInvoicing', {$element, $scope, $transclude});
})); }));
describe('getMinClientId()', () => {
it('should set the invoice fromClientId property', () => {
const filter = {
order: 'id ASC',
limit: 1
};
const serializedParams = $httpParamSerializer({filter});
$httpBackend.expectGET(`Clients/findOne?${serializedParams}`).respond(200, {id: 1101});
controller.getMinClientId();
$httpBackend.flush();
expect(controller.invoice.fromClientId).toEqual(1101);
});
});
describe('getMaxClientId()', () => {
it('should set the invoice toClientId property', () => {
const filter = {
order: 'id DESC',
limit: 1
};
const serializedParams = $httpParamSerializer({filter});
$httpBackend.expectGET(`Clients/findOne?${serializedParams}`).respond(200, {id: 1112});
controller.getMaxClientId();
$httpBackend.flush();
expect(controller.invoice.toClientId).toEqual(1112);
});
});
describe('responseHandler()', () => { describe('responseHandler()', () => {
it('should throw an error when clientFk property is set and the maxShipped is not filled', () => { it('should throw an error when invoiceDate or maxShipped properties are not filled in', () => {
jest.spyOn(controller.vnApp, 'showError'); jest.spyOn(controller.vnApp, 'showError');
controller.invoice = { controller.invoice = {
clientFk: 1101, fromClientId: 1101,
serial: 'T', toClientId: 1101
taxArea: 'B'
}; };
controller.responseHandler('accept'); controller.responseHandler('accept');
expect(controller.vnApp.showError).toHaveBeenCalledWith(`Client and the max shipped should be filled`); expect(controller.vnApp.showError).toHaveBeenCalledWith(`Invoice date and the max date should be filled`);
}); });
it('should throw an error when some required fields are not filled in', () => { it('should throw an error when fromClientId or toClientId properties are not filled in', () => {
jest.spyOn(controller.vnApp, 'showError'); jest.spyOn(controller.vnApp, 'showError');
controller.invoice = { controller.invoice = {
ticketFk: 1101 invoiceDate: new Date(),
maxShipped: new Date()
}; };
controller.responseHandler('accept'); controller.responseHandler('accept');
expect(controller.vnApp.showError).toHaveBeenCalledWith(`Some fields are required`); expect(controller.vnApp.showError).toHaveBeenCalledWith(`Choose a valid clients range`);
}); });
it('should make an http POST query and then call to the parent showSuccess() method', () => { it('should make an http POST query and then call to the showSuccess() method', () => {
jest.spyOn(controller.vnApp, 'showSuccess'); jest.spyOn(controller.vnApp, 'showSuccess');
controller.invoice = { controller.invoice = {
ticketFk: 1101, invoiceDate: new Date(),
serial: 'T', maxShipped: new Date(),
taxArea: 'B' fromClientId: 1101,
toClientId: 1101
}; };
$httpBackend.expect('POST', `InvoiceOuts/createManualInvoice`).respond({id: 1}); $httpBackend.expect('POST', `InvoiceOuts/globalInvoicing`).respond({id: 1});
controller.responseHandler('accept'); controller.responseHandler('accept');
$httpBackend.flush(); $httpBackend.flush();