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

93 lines
3.3 KiB
JavaScript

import './index.js';
import popover from 'core/mocks/popover';
import crudModel from 'core/mocks/crud-model';
describe('Zone Component vnZoneDeliveryDays', () => {
let $componentController;
let $httpBackend;
let controller;
let $element;
beforeEach(ngModule('zone'));
beforeEach(angular.mock.inject((_$componentController_, _$httpBackend_) => {
$componentController = _$componentController_;
$httpBackend = _$httpBackend_;
$element = angular.element('<vn-zone-delivery-days></vn-zone-delivery-days');
controller = $componentController('vnZoneDeliveryDays', {$element});
controller.$.zoneEvents = popover;
controller.$.zoneIndex = {
$scope: {
model: crudModel
}
};
}));
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.$.zoneIndex.$scope.model;
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);
});
});
});