fix: test
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Vicent Llopis 2022-12-29 11:47:12 +01:00
parent dc93454ac3
commit 2b62e16164
2 changed files with 111 additions and 118 deletions

View File

@ -105,8 +105,6 @@ module.exports = Self => {
serialLetter, serialLetter,
args.invoiceDate args.invoiceDate
], myOptions); ], myOptions);
if (client.id == 1101 && args.addressId == 121)
throw new Error('No se ha podido facturar');
const [newInvoice] = await Self.rawSql(`SELECT @invoiceId id`, null, myOptions); const [newInvoice] = await Self.rawSql(`SELECT @invoiceId id`, null, myOptions);
if (newInvoice.id) { if (newInvoice.id) {

View File

@ -1,126 +1,121 @@
import './index.js'; import './index';
import popover from 'core/mocks/popover';
import crudModel from 'core/mocks/crud-model';
describe('Zone Component vnZoneDeliveryDays', () => { describe('InvoiceOut', () => {
let $httpBackend; describe('Component vnInvoiceOutGlobalInvoicing', () => {
let controller; let controller;
let $element; let $httpBackend;
let $httpParamSerializer;
beforeEach(ngModule('zone')); beforeEach(ngModule('invoiceOut'));
beforeEach(inject(($componentController, _$httpBackend_) => { beforeEach(inject(($componentController, $rootScope, _$httpBackend_, _$httpParamSerializer_) => {
$httpBackend = _$httpBackend_; $httpBackend = _$httpBackend_;
$element = angular.element('<vn-zone-delivery-days></vn-zone-delivery-days'); $httpParamSerializer = _$httpParamSerializer_;
controller = $componentController('vnZoneDeliveryDays', {$element}); let $scope = $rootScope.$new();
controller.$.zoneEvents = popover; const $element = angular.element('<vn-invoice-out-global-invoicing></vn-invoice-out-global-invoicing>');
controller.$.params = {}; const $transclude = {
controller.$.zoneModel = crudModel; $$boundTransclude: {
})); $$slots: []
describe('deliveryMethodFk() setter', () => {
it('should set the deliveryMethodFk property as pickup and then perform a query that sets the filter', () => {
$httpBackend.expect('GET', 'DeliveryMethods').respond([{id: 999}]);
controller.deliveryMethodFk = 'pickUp';
$httpBackend.flush();
expect(controller.agencyFilter).toEqual({deliveryMethodFk: {inq: [999]}});
});
});
describe('setParams()', () => {
it('should do nothing when no params are received', () => {
controller.setParams();
expect(controller.deliveryMethodFk).toBeUndefined();
expect(controller.geoFk).toBeUndefined();
expect(controller.agencyModeFk).toBeUndefined();
});
it('should set the controller properties when the params are provided', () => {
controller.$params = {
deliveryMethodFk: 3,
geoFk: 2,
agencyModeFk: 1
};
controller.setParams();
expect(controller.deliveryMethodFk).toEqual(controller.$params.deliveryMethodFk);
expect(controller.geoFk).toEqual(controller.$params.geoFk);
expect(controller.agencyModeFk).toEqual(controller.$params.agencyModeFk);
});
});
describe('fetchData()', () => {
it('should make an HTTP GET query and then call the showMessage() method', () => {
jest.spyOn(controller.vnApp, 'showMessage');
jest.spyOn(controller.$state, 'go');
controller.agencyModeFk = 1;
controller.deliveryMethodFk = 2;
controller.geoFk = 3;
controller.$state.current.name = 'myState';
const expectedData = {events: []};
const url = 'Zones/getEvents?agencyModeFk=1&deliveryMethodFk=2&geoFk=3';
$httpBackend.when('GET', 'DeliveryMethods').respond([]);
$httpBackend.expect('GET', url).respond({events: []});
controller.fetchData();
$httpBackend.flush();
expect(controller.$.data).toEqual(expectedData);
expect(controller.vnApp.showMessage).toHaveBeenCalledWith('No service for the specified zone');
expect(controller.$state.go).toHaveBeenCalledWith(
controller.$state.current.name,
{
agencyModeFk: 1,
deliveryMethodFk: 2,
geoFk: 3
} }
);
});
});
describe('onSelection()', () => {
it('should not call the show popover method if events array is empty', () => {
jest.spyOn(controller.$.zoneEvents, 'show');
const event = new Event('click');
const target = document.createElement('div');
target.dispatchEvent(event);
const events = [];
controller.onSelection(event, events);
expect(controller.$.zoneEvents.show).not.toHaveBeenCalled();
});
it('should call the show() method and call getZoneClosing() with the expected ids', () => {
jest.spyOn(controller.$.zoneEvents, 'show');
const event = new Event('click');
const target = document.createElement('div');
target.dispatchEvent(event);
const day = new Date();
const events = [
{zoneFk: 1},
{zoneFk: 2},
{zoneFk: 8}
];
const params = {
zoneIds: [1, 2, 8],
date: [day][0]
}; };
const response = [{id: 1, hour: ''}]; controller = $componentController('vnInvoiceOutGlobalInvoicing', {$element, $scope, $transclude});
controller.$.invoiceButton = {disabled: false};
}));
$httpBackend.when('POST', 'Zones/getZoneClosing', params).respond({response}); describe('getMinClientId()', () => {
controller.onSelection(event, events, [day]); it('should set the invoice fromClientId property', () => {
$httpBackend.flush(); const filter = {
order: 'id ASC',
limit: 1
};
expect(controller.$.zoneEvents.show).toHaveBeenCalledWith(target); const serializedParams = $httpParamSerializer({filter});
expect(controller.zoneClosing.id).toEqual(response.id); $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('makeInvoice()', () => {
it('should throw an error when invoiceDate or maxShipped properties are not filled in', () => {
jest.spyOn(controller.vnApp, 'showError');
controller.invoice = {
fromClientId: 1101,
toClientId: 1101
};
controller.makeInvoice();
const expectedError = 'Invoice date and the max date should be filled';
expect(controller.vnApp.showError).toHaveBeenCalledWith(expectedError);
});
it('should throw an error when fromClientId or toClientId properties are not filled in', () => {
jest.spyOn(controller.vnApp, 'showError');
controller.invoice = {
invoiceDate: new Date(),
maxShipped: new Date()
};
controller.makeInvoice();
expect(controller.vnApp.showError).toHaveBeenCalledWith(`Choose a valid clients range`);
});
it('should make an http POST query and then call to the showSuccess() method', () => {
jest.spyOn(controller.vnApp, 'showSuccess');
const minShipped = new Date();
minShipped.setFullYear(minShipped.getFullYear() - 1);
minShipped.setMonth(1);
minShipped.setDate(1);
minShipped.setHours(0, 0, 0, 0);
controller.invoice = {
invoiceDate: new Date(),
maxShipped: new Date(),
fromClientId: 1101,
toClientId: 1101,
companyFk: 442,
minShipped: minShipped
};
const response = {
clientsAndAddresses: [{clientId: 1101, addressId: 121}],
invoice: controller.invoice
};
const address = {id: 121};
$httpBackend.expect('POST', `InvoiceOuts/clientsToInvoice`).respond(response);
$httpBackend.expect('GET', `Addresses/${response.clientsAndAddresses[0].addressId}`).respond(address);
$httpBackend.expect('POST', `InvoiceOuts/invoiceClient`).respond({id: 1});
controller.makeInvoice();
$httpBackend.flush();
expect(controller.vnApp.showSuccess).toHaveBeenCalled();
});
}); });
}); });
}); });