salix/modules/zone/front/delivery-days/index.spec.js

127 lines
4.6 KiB
JavaScript
Raw Normal View History

2020-02-26 06:02:03 +00:00
import './index.js';
import popover from 'core/mocks/popover';
import crudModel from 'core/mocks/crud-model';
2020-02-26 10:05:29 +00:00
describe('Zone Component vnZoneDeliveryDays', () => {
let $httpBackend;
2020-02-26 06:02:03 +00:00
let controller;
let $element;
beforeEach(ngModule('zone'));
2020-07-23 14:46:16 +00:00
beforeEach(inject(($componentController, _$httpBackend_) => {
2020-02-26 10:05:29 +00:00
$httpBackend = _$httpBackend_;
2020-02-26 06:02:03 +00:00
$element = angular.element('<vn-zone-delivery-days></vn-zone-delivery-days');
controller = $componentController('vnZoneDeliveryDays', {$element});
controller.$.zoneEvents = popover;
2020-03-26 16:41:40 +00:00
controller.$.params = {};
2020-03-26 08:07:17 +00:00
controller.$.zoneModel = crudModel;
2020-02-26 06:02:03 +00:00
}));
2020-03-26 16:41:40 +00:00
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}]);
2020-03-26 16:41:40 +00:00
controller.deliveryMethodFk = 'pickUp';
$httpBackend.flush();
2020-03-26 16:41:40 +00:00
expect(controller.agencyFilter).toEqual({deliveryMethodFk: {inq: [999]}});
2020-03-26 16:41:40 +00:00
});
});
2020-03-26 16:41:40 +00:00
describe('setParams()', () => {
it('should do nothing when no params are received', () => {
controller.setParams();
2020-03-26 16:41:40 +00:00
expect(controller.deliveryMethodFk).toBeUndefined();
expect(controller.geoFk).toBeUndefined();
expect(controller.agencyModeFk).toBeUndefined();
});
2020-03-26 16:41:40 +00:00
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);
2020-03-26 16:41:40 +00:00
});
});
describe('fetchData()', () => {
2020-02-26 10:05:29 +00:00
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';
2020-02-26 10:05:29 +00:00
const expectedData = {events: []};
const url = 'Zones/getEvents?agencyModeFk=1&deliveryMethodFk=2&geoFk=3';
2020-02-26 10:05:29 +00:00
$httpBackend.when('GET', 'DeliveryMethods').respond([]);
$httpBackend.expect('GET', url).respond({events: []});
controller.fetchData();
2020-02-26 10:05:29 +00:00
$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
}
);
2020-02-26 10:05:29 +00:00
});
});
2020-05-26 08:37:52 +00:00
describe('onSelection()', () => {
2020-02-26 11:24:48 +00:00
it('should not call the show popover method if events array is empty', () => {
2020-02-26 10:05:29 +00:00
jest.spyOn(controller.$.zoneEvents, 'show');
2020-02-26 11:24:48 +00:00
const event = new Event('click');
2020-02-26 10:05:29 +00:00
const target = document.createElement('div');
2020-02-26 11:24:48 +00:00
target.dispatchEvent(event);
const events = [];
controller.onSelection(event, events);
2020-02-26 10:05:29 +00:00
expect(controller.$.zoneEvents.show).not.toHaveBeenCalled();
});
it('should call the show() method and call getZoneClosing() with the expected ids', () => {
2020-02-26 06:02:03 +00:00
jest.spyOn(controller.$.zoneEvents, 'show');
2020-02-26 11:24:48 +00:00
const event = new Event('click');
2020-02-26 10:05:29 +00:00
const target = document.createElement('div');
2020-02-26 12:06:35 +00:00
target.dispatchEvent(event);
const day = new Date();
2020-02-26 11:24:48 +00:00
const events = [
2020-02-26 06:02:03 +00:00
{zoneFk: 1},
{zoneFk: 2},
{zoneFk: 8}
];
const params = {
zonesId: [1, 2, 8],
date: [day][0]
};
const response = [{id: 1, hour: ''}];
$httpBackend.when('POST', 'Zones/getZoneClosing', params).respond({response});
2021-08-30 12:14:21 +00:00
controller.onSelection(event, events, [day]);
$httpBackend.flush();
2020-02-26 06:02:03 +00:00
2020-02-26 10:05:29 +00:00
expect(controller.$.zoneEvents.show).toHaveBeenCalledWith(target);
expect(controller.zoneClosing.id).toEqual(response.id);
2020-02-26 06:02:03 +00:00
});
});
});