salix/modules/ticket/front/sale/specs/index.spec.js

410 lines
17 KiB
JavaScript
Raw Normal View History

2019-06-06 07:34:36 +00:00
import '../index.js';
import watcher from 'core/mocks/watcher';
import crudModel from 'core/mocks/crud-model';
2020-03-13 07:47:41 +00:00
describe('Ticket', () => {
describe('Component vnTicketSale', () => {
2018-11-06 13:27:16 +00:00
let controller;
let $scope;
2019-09-13 14:09:14 +00:00
let $state;
2018-10-22 06:23:10 +00:00
let $httpBackend;
2019-09-13 14:09:14 +00:00
const ticket = {
2018-10-22 06:23:10 +00:00
id: 1,
2019-08-09 06:04:44 +00:00
clientFk: 101,
2018-10-22 06:23:10 +00:00
shipped: 1,
created: new Date(),
client: {salesPersonFk: 1},
address: {mobile: 111111111}
2018-10-22 06:23:10 +00:00
};
2019-09-13 14:09:14 +00:00
const sales = [
2018-10-22 06:23:10 +00:00
{
id: 1,
concept: 'Item 1',
2018-10-22 06:23:10 +00:00
quantity: 5,
price: 23.5,
2019-09-13 14:09:14 +00:00
discount: 0,
2018-10-22 06:23:10 +00:00
}, {
id: 4,
concept: 'Item 2',
2018-10-22 06:23:10 +00:00
quantity: 20,
price: 5.5,
2019-09-13 14:09:14 +00:00
discount: 0,
2018-10-22 06:23:10 +00:00
}
];
beforeEach(ngModule('ticket'));
2018-10-22 06:23:10 +00:00
2019-09-13 14:09:14 +00:00
beforeEach(angular.mock.inject(($componentController, $rootScope, _$state_, _$httpBackend_) => {
$state = _$state_;
$scope = $rootScope.$new();
$scope.watcher = watcher;
2019-09-13 14:09:14 +00:00
$scope.sms = {open: () => {}};
$scope.ticket = ticket;
$scope.model = crudModel;
2018-10-22 06:23:10 +00:00
$httpBackend = _$httpBackend_;
2019-09-13 14:09:14 +00:00
Object.defineProperties($state.params, {
id: {
value: ticket.id,
writable: true
},
go: {
value: () => {},
writable: false
}
});
2020-03-18 07:35:59 +00:00
const $element = angular.element('<vn-ticket-sale></vn-ticket-sale>');
controller = $componentController('vnTicketSale', {$element, $scope});
2018-11-06 13:27:16 +00:00
controller.card = {reload: () => {}};
2019-09-13 14:09:14 +00:00
controller.ticket = ticket;
controller.sales = sales;
}));
2018-09-11 10:49:31 +00:00
describe('createClaim()', () => {
2018-10-22 06:23:10 +00:00
it('should perform a query and call windows open', () => {
2020-02-26 12:22:52 +00:00
jest.spyOn(controller.$state, 'go');
2018-10-22 06:23:10 +00:00
const claim = {id: 1};
const sales = [{id: 1}, {id: 2}];
2019-09-13 14:09:14 +00:00
$httpBackend.whenGET(`Tickets/1/getVAT`).respond(200, 10.5);
$httpBackend.whenGET(`Tickets/1/subtotal`).respond(200, 227.5);
$httpBackend.whenGET(`Tickets/1/isEditable`).respond();
2020-02-27 11:31:38 +00:00
$httpBackend.whenGET(`Tickets/1/isLocked`).respond();
$httpBackend.when('POST', `Claims/createFromSales`, {claim: claim, sales: sales}).respond(claim);
$httpBackend.expect('POST', `Claims/createFromSales`).respond(claim);
2018-11-06 13:27:16 +00:00
controller.createClaim();
2018-09-11 10:49:31 +00:00
$httpBackend.flush();
expect(controller.subtotal).toEqual(227.5);
2018-11-06 13:27:16 +00:00
expect(controller.VAT).toEqual(10.5);
expect(controller.total).toEqual(238);
2019-09-13 14:09:14 +00:00
expect(controller.$state.go).toHaveBeenCalledWith('claim.card.basicData', {id: 1});
2018-07-13 13:57:19 +00:00
});
});
2018-10-22 06:23:10 +00:00
describe('isChecked() getter', () => {
it('should set isChecked value to true when one of the instances has the value checked to true', () => {
2018-11-06 13:27:16 +00:00
controller.sales[0].checked = true;
2018-11-06 13:27:16 +00:00
expect(controller.isChecked).toBeTruthy();
});
});
2019-08-09 06:04:44 +00:00
describe('checkedLines()', () => {
2018-07-13 13:57:19 +00:00
it('should make an array of the instances with the property checked true()', () => {
2019-09-13 14:09:14 +00:00
let sale = controller.sales[0];
2018-10-22 06:23:10 +00:00
sale.checked = true;
let expectedResult = [sale];
2018-07-13 13:57:19 +00:00
$httpBackend.whenGET(`Tickets/1/subtotal`).respond(200, 227.5);
$httpBackend.whenGET(`Tickets/1/getVAT`).respond(200, 10.5);
$httpBackend.whenGET(`Tickets/1/isEditable`).respond();
2020-02-27 11:31:38 +00:00
$httpBackend.whenGET(`Tickets/1/isLocked`).respond();
2019-09-13 14:09:14 +00:00
let result = controller.checkedLines();
$httpBackend.flush();
expect(result).toEqual(expectedResult);
2018-07-13 13:57:19 +00:00
});
});
describe('onStateOkClick()', () => {
it('should perform a get and then call a function', () => {
2018-11-06 13:27:16 +00:00
let filter = {where: {code: 'OK'}, fields: ['id']};
filter = encodeURIComponent(JSON.stringify(filter));
let res = [{id: 3}];
2020-02-27 06:19:42 +00:00
jest.spyOn(controller, 'onStateChange').mockReturnThis();
$httpBackend.whenGET(`States?filter=${filter}`).respond(res);
$httpBackend.expectGET(`Tickets/1/subtotal`).respond(200, 227.5);
$httpBackend.expectGET(`Tickets/1/getVAT`).respond(200, 10.5);
$httpBackend.whenGET(`Tickets/1/isEditable`).respond();
2020-02-27 11:31:38 +00:00
$httpBackend.whenGET(`Tickets/1/isLocked`).respond();
2018-11-06 13:27:16 +00:00
controller.onStateOkClick();
$httpBackend.flush();
2018-11-06 13:27:16 +00:00
expect(controller.onStateChange).toHaveBeenCalledWith(3);
});
});
describe('onStateChange()', () => {
it('should perform a post and then call a function', () => {
$httpBackend.expectPOST(`TicketTrackings/changeState`).respond();
$httpBackend.whenGET(`Tickets/1/subtotal`).respond(200, 227.5);
$httpBackend.whenGET(`Tickets/1/getVAT`).respond(200, 10.5);
$httpBackend.whenGET(`Tickets/1/isEditable`).respond();
2020-02-27 11:31:38 +00:00
$httpBackend.whenGET(`Tickets/1/isLocked`).respond();
2018-11-06 13:27:16 +00:00
controller.onStateChange(3);
$httpBackend.flush();
});
});
2018-09-18 07:52:50 +00:00
describe('onRemoveLinesClick()', () => {
2018-07-03 13:23:10 +00:00
it('should call getCheckedLines, call removeInstances, and make a query', () => {
2019-09-13 14:09:14 +00:00
controller.sales[0].checked = true;
2018-07-03 13:23:10 +00:00
$httpBackend.whenPOST(`Sales/removes`).respond();
$httpBackend.whenGET(`Tickets/1/subtotal`).respond(200, 227.5);
$httpBackend.whenGET(`Tickets/1/getVAT`).respond(200, 10.5);
$httpBackend.whenGET(`Tickets/1/isEditable`).respond();
2020-02-27 11:31:38 +00:00
$httpBackend.whenGET(`Tickets/1/isLocked`).respond();
2019-10-30 15:57:14 +00:00
controller.onRemoveLinesClick('accept');
2018-07-03 13:23:10 +00:00
$httpBackend.flush();
2018-11-06 13:27:16 +00:00
expect(controller.sales.length).toEqual(1);
2018-07-03 13:23:10 +00:00
});
});
describe('unmarkAsReserved()', () => {
it('should call setReserved with false', () => {
2020-02-26 12:22:52 +00:00
jest.spyOn(controller, 'setReserved');
2018-07-03 13:23:10 +00:00
2018-11-06 13:27:16 +00:00
controller.unmarkAsReserved(false);
2018-07-03 13:23:10 +00:00
2018-11-06 13:27:16 +00:00
expect(controller.setReserved).toHaveBeenCalledWith(false);
2018-07-03 13:23:10 +00:00
});
});
describe('markAsReserved()', () => {
it('should call setReserved with true', () => {
2020-02-26 12:22:52 +00:00
jest.spyOn(controller, 'setReserved');
2018-11-06 13:27:16 +00:00
controller.markAsReserved(true);
2018-07-03 13:23:10 +00:00
2018-11-06 13:27:16 +00:00
expect(controller.setReserved).toHaveBeenCalledWith(true);
});
2018-07-03 13:23:10 +00:00
});
2018-09-18 07:52:50 +00:00
describe('setReserved()', () => {
2018-07-03 13:23:10 +00:00
it('should call getCheckedLines, $.index.accept and make a query ', () => {
2019-09-13 14:09:14 +00:00
const sale = controller.sales[0];
sale.checked = true;
2018-10-22 06:23:10 +00:00
let expectedRequest = {
sales: [sale],
2018-10-22 06:23:10 +00:00
ticketFk: ticket.id,
reserved: false
};
$httpBackend.expectPOST(`Sales/reserve`, expectedRequest).respond();
$httpBackend.whenGET(`Tickets/1/subtotal`).respond(200, 227.5);
$httpBackend.whenGET(`Tickets/1/getVAT`).respond(200, 10.5);
$httpBackend.whenGET(`Tickets/1/isEditable`).respond();
2020-02-27 11:31:38 +00:00
$httpBackend.whenGET(`Tickets/1/isLocked`).respond();
2018-11-06 13:27:16 +00:00
controller.unmarkAsReserved(false);
$httpBackend.flush();
});
});
describe('showSMSDialog()', () => {
it('should open an SMS dialog with specified data', () => {
2020-03-18 07:35:59 +00:00
jest.spyOn(controller.$.sms, 'open');
2019-09-13 14:09:14 +00:00
controller.sales[0].checked = true;
controller.showSMSDialog();
2020-03-18 07:35:59 +00:00
expect(controller.$.sms.open).toHaveBeenCalledWith();
2019-04-16 06:21:20 +00:00
expect(controller.newSMS.destination).toEqual(111111111);
expect(controller.newSMS.message).not.toEqual('');
});
});
2020-03-13 07:44:01 +00:00
describe('onChangeQuantity()', () => {
it('should not call addSale() or updateQuantity() methods', () => {
jest.spyOn(controller, 'addSale');
jest.spyOn(controller, 'updateQuantity');
const sale = {itemFk: 4};
controller.onChangeQuantity(sale);
expect(controller.addSale).not.toHaveBeenCalled();
expect(controller.updateQuantity).not.toHaveBeenCalled();
});
it('should call addSale() method', () => {
jest.spyOn(controller, 'addSale');
const sale = {itemFk: 4, quantity: 5};
controller.onChangeQuantity(sale);
expect(controller.addSale).toHaveBeenCalledWith(sale);
});
it('should call updateQuantity() method', () => {
jest.spyOn(controller, 'updateQuantity');
jest.spyOn(controller, 'addSale');
const sale = {id: 1, itemFk: 4, quantity: 5};
controller.onChangeQuantity(sale);
expect(controller.addSale).not.toHaveBeenCalled();
expect(controller.updateQuantity).toHaveBeenCalledWith(sale);
});
});
describe('updateQuantity()', () => {
it('should make a POST query saving sale quantity', () => {
2020-03-18 07:35:59 +00:00
jest.spyOn(controller.$.watcher, 'updateOriginalData');
const data = {quantity: 10};
const sale = sales[0];
sale.quantity = 10;
$httpBackend.when('POST', `Sales/4/updateQuantity`, data).respond();
$httpBackend.expect('POST', `Sales/4/updateQuantity`, data).respond();
$httpBackend.whenGET(`Tickets/1/subtotal`).respond(200, 227.5);
$httpBackend.whenGET(`Tickets/1/getVAT`).respond(200, 10.5);
$httpBackend.whenGET(`Tickets/1/isEditable`).respond();
2020-02-27 11:31:38 +00:00
$httpBackend.whenGET(`Tickets/1/isLocked`).respond();
controller.updateQuantity(sale);
$httpBackend.flush();
2020-03-18 07:35:59 +00:00
expect(controller.$.watcher.updateOriginalData).toHaveBeenCalledWith();
});
});
describe('updateConcept()', () => {
it('should make a POST query saving sale concept', () => {
2020-03-18 07:35:59 +00:00
jest.spyOn(controller.$.watcher, 'updateOriginalData');
2019-09-06 12:56:56 +00:00
const data = {newConcept: 'My new weapon'};
const sale = sales[0];
sale.concept = 'My new weapon';
$httpBackend.when('POST', `Sales/4/updateConcept`, data).respond();
$httpBackend.expect('POST', `Sales/4/updateConcept`, data).respond();
$httpBackend.whenGET(`Tickets/1/subtotal`).respond(200, 227.5);
$httpBackend.whenGET(`Tickets/1/getVAT`).respond(200, 10.5);
$httpBackend.whenGET(`Tickets/1/isEditable`).respond();
2020-02-27 11:31:38 +00:00
$httpBackend.whenGET(`Tickets/1/isLocked`).respond();
controller.updateConcept(sale);
$httpBackend.flush();
2020-03-18 07:35:59 +00:00
expect(controller.$.watcher.updateOriginalData).toHaveBeenCalledWith();
});
});
describe('addSale()', () => {
it('should make a POST query adding a new sale', () => {
2020-03-18 07:35:59 +00:00
jest.spyOn(controller.$.watcher, 'updateOriginalData');
const newSale = {itemFk: 4, quantity: 10};
const params = {itemId: 4, quantity: 10};
const expectedResult = {
id: 30,
quantity: 10,
discount: 0,
price: 0,
itemFk: 4,
item: {
subName: 'Item subName',
image: '30.png'
}
};
$httpBackend.when('POST', `tickets/1/addSale`, params).respond(expectedResult);
$httpBackend.expect('POST', `tickets/1/addSale`, params).respond(expectedResult);
$httpBackend.whenGET(`Tickets/1/subtotal`).respond(200, 227.5);
$httpBackend.whenGET(`Tickets/1/getVAT`).respond(200, 10.5);
$httpBackend.whenGET(`Tickets/1/isEditable`).respond();
2020-02-27 11:31:38 +00:00
$httpBackend.whenGET(`Tickets/1/isLocked`).respond();
controller.addSale(newSale);
$httpBackend.flush();
2020-03-18 07:35:59 +00:00
expect(controller.$.watcher.updateOriginalData).toHaveBeenCalledWith();
});
});
2019-08-09 06:04:44 +00:00
describe('transferSales()', () => {
it('should transfer sales to a ticket', () => {
2020-02-26 12:22:52 +00:00
jest.spyOn(controller, 'goToTicket');
2019-08-09 06:04:44 +00:00
controller.transfer = {
sales: [{id: 1, itemFk: 1}, {id: 2, itemFk: 4}]
};
const expectedResponse = {id: 13};
const params = {
ticketId: 13,
sales: controller.transfer.sales
};
$httpBackend.when('POST', `tickets/1/transferSales`, params).respond(expectedResponse);
$httpBackend.expect('POST', `tickets/1/transferSales`, params).respond(expectedResponse);
$httpBackend.whenGET(`Tickets/1/subtotal`).respond(200, 227.5);
$httpBackend.whenGET(`Tickets/1/getVAT`).respond(200, 10.5);
$httpBackend.whenGET(`Tickets/1/isEditable`).respond();
2020-02-27 11:31:38 +00:00
$httpBackend.whenGET(`Tickets/1/isLocked`).respond();
2019-08-09 06:04:44 +00:00
controller.transferSales(13);
$httpBackend.flush();
expect(controller.goToTicket).toHaveBeenCalledWith(13);
});
});
describe('setTransferParams()', () => {
it('should define the transfer object on the controller and its default properties', () => {
2019-09-13 14:09:14 +00:00
let sale = controller.sales[0];
2019-08-09 06:04:44 +00:00
sale.checked = true;
const expectedResponse = [sale];
$httpBackend.when('GET', `clients/101/lastActiveTickets?ticketId=1`).respond(expectedResponse);
$httpBackend.expect('GET', `clients/101/lastActiveTickets?ticketId=1`).respond(expectedResponse);
$httpBackend.whenGET(`Tickets/1/subtotal`).respond(200, 227.5);
$httpBackend.whenGET(`Tickets/1/getVAT`).respond(200, 10.5);
$httpBackend.whenGET(`Tickets/1/isEditable`).respond();
2020-02-27 11:31:38 +00:00
$httpBackend.whenGET(`Tickets/1/isLocked`).respond();
2019-08-09 06:04:44 +00:00
controller.setTransferParams();
$httpBackend.flush();
const lastActiveTickets = controller.transfer.lastActiveTickets;
expect(controller.transfer).toBeDefined();
expect(lastActiveTickets).toBeDefined();
expect(lastActiveTickets[0].id).toEqual(4);
});
});
2019-09-24 12:03:30 +00:00
describe('newOrderFromTicket()', () => {
it('should make an HTTP post query and then open the new order on a new tab', () => {
const params = {ticketFk: 1};
const expectedResponse = {id: 123};
window.open = jasmine.createSpy('open');
controller.$state.href = jasmine.createSpy('href')
.and.returnValue('/somePath');
$httpBackend.when('POST', `Orders/newFromTicket`, params).respond(expectedResponse);
$httpBackend.expect('POST', `Orders/newFromTicket`, params).respond(expectedResponse);
$httpBackend.whenGET(`Tickets/1/subtotal`).respond(200, 227.5);
$httpBackend.whenGET(`Tickets/1/getVAT`).respond(200, 10.5);
$httpBackend.whenGET(`Tickets/1/isEditable`).respond();
2020-02-27 11:31:38 +00:00
$httpBackend.whenGET(`Tickets/1/isLocked`).respond();
2019-09-24 12:03:30 +00:00
controller.newOrderFromTicket();
$httpBackend.flush();
expect(window.open).toHaveBeenCalledWith('/somePath', '_blank');
});
});
describe('hasOneSaleSelected()', () => {
it('should return true if just one sale is selected', () => {
controller.sales[0].checked = true;
expect(controller.hasOneSaleSelected()).toBeTruthy();
});
});
describe('calculateSalePrice()', () => {
it('should make an HTTP post query ', () => {
controller.sales[0].checked = true;
2019-11-21 11:00:56 +00:00
$httpBackend.when('POST', `Sales/4/recalculatePrice`).respond(200);
$httpBackend.whenGET(`Tickets/1/subtotal`).respond(200, 227.5);
$httpBackend.whenGET(`Tickets/1/getVAT`).respond(200, 10.5);
$httpBackend.whenGET(`Tickets/1/isEditable`).respond();
2020-02-27 11:31:38 +00:00
$httpBackend.whenGET(`Tickets/1/isLocked`).respond();
controller.calculateSalePrice();
$httpBackend.flush();
});
});
});
});