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

107 lines
3.9 KiB
JavaScript

import './index.js';
import popover from 'core/mocks/popover';
import crudModel from 'core/mocks/crud-model';
describe('Zone Component vnZoneDeliveryDays', () => {
let $httpBackend;
let controller;
let $element;
beforeEach(ngModule('zone'));
beforeEach(angular.mock.inject(($componentController, _$httpBackend_) => {
$httpBackend = _$httpBackend_;
$element = angular.element('<vn-zone-delivery-days></vn-zone-delivery-days');
controller = $componentController('vnZoneDeliveryDays', {$element});
controller.$.zoneEvents = popover;
controller.$.params = {};
controller.$.zoneModel = crudModel;
}));
describe('deliveryMethodFk() setter', () => {
it(`should set the deliveryMethodFk property and check just agencymode focus`, () => {
controller.$.agencymode = {focus: jasmine.createSpy('focus')};
controller.deliveryMethodFk = 'pickUp';
expect(controller.$.agencymode.focus).toHaveBeenCalledWith();
});
it(`should set the deliveryMethodFk property, call method http and sets the agencyfilter`, () => {
$httpBackend.when('GET', 'DeliveryMethods').respond([{id: 'id'}]);
controller.deliveryMethodFk = 'no pickUp';
$httpBackend.flush();
expect(controller.agencyFilter).toEqual({deliveryMethodFk: {inq: ['id']}});
});
});
describe('onSubmit()', () => {
it('should make an HTTP GET query and then call the showMessage() method', () => {
jest.spyOn(controller.vnApp, 'showMessage');
const expectedData = {events: []};
$httpBackend.when('GET', 'Zones/getEvents').respond({events: []});
controller.onSubmit();
$httpBackend.flush();
expect(controller.$.data).toBeDefined();
expect(controller.$.data).toEqual(expectedData);
expect(controller.vnApp.showMessage).toHaveBeenCalledWith('No service for the specified zone');
});
it('should make an HTTP GET query and then set the data property', () => {
const expectedData = {events: [{zoneFk: 1}]};
$httpBackend.when('GET', 'Zones/getEvents').respond({events: [{zoneFk: 1}]});
controller.onSubmit();
$httpBackend.flush();
expect(controller.$.data).toBeDefined();
expect(controller.$.data).toEqual(expectedData);
});
});
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 then call the applyFilter() method with the expected ids', () => {
const zoneModel = controller.$.zoneModel;
jest.spyOn(controller.$.zoneEvents, 'show');
jest.spyOn(zoneModel, 'applyFilter');
const event = new Event('click');
const target = document.createElement('div');
target.dispatchEvent(event);
const events = [
{zoneFk: 1},
{zoneFk: 2},
{zoneFk: 8}
];
controller.onSelection(event, events);
const expectedFilter = {
include: {
relation: 'agencyMode',
scope: {fields: ['name']}
},
where: {
id: {inq: [1, 2, 8]}
}
};
expect(controller.$.zoneEvents.show).toHaveBeenCalledWith(target);
expect(zoneModel.applyFilter).toHaveBeenCalledWith(expectedFilter);
});
});
});