salix/modules/zone/front/calendar/index.spec.js

173 lines
5.3 KiB
JavaScript
Raw Normal View History

2020-06-22 08:01:55 +00:00
import './index';
import crudModel from 'core/mocks/crud-model';
describe('component vnZoneCalendar', () => {
let $scope;
let controller;
beforeEach(ngModule('zone'));
2021-01-20 06:38:09 +00:00
beforeEach(inject(($componentController, $rootScope) => {
2020-06-22 08:01:55 +00:00
$scope = $rootScope.$new();
const $element = angular.element(`<vn-zone-calendar></vn-zone-calendar>`);
controller = $componentController('vnZoneCalendar', {$element, $scope});
controller.$.model = crudModel;
controller.zone = {id: 1};
controller.days = [];
controller.exclusions = [];
2022-05-27 11:54:35 +00:00
controller.geoExclusions = [];
2020-06-22 08:01:55 +00:00
}));
describe('date() setter', () => {
it('should set the month property and then call the refreshEvents() method', () => {
jest.spyOn(controller, 'refreshEvents').mockReturnThis();
2023-01-16 14:18:24 +00:00
controller.date = Date.vnNew();
2020-06-22 08:01:55 +00:00
expect(controller.refreshEvents).toHaveBeenCalledWith();
expect(controller.months.length).toEqual(4);
});
});
describe('step()', () => {
it('should set the date month to 4 months backwards', () => {
2023-01-16 14:18:24 +00:00
const now = Date.vnNew();
2020-06-30 07:08:08 +00:00
now.setDate(15);
2020-06-22 08:01:55 +00:00
now.setMonth(now.getMonth() - 4);
controller.step(-1);
const expectedMonth = now.getMonth();
const currentMonth = controller.date.getMonth();
expect(currentMonth).toEqual(expectedMonth);
});
it('should set the date month to 4 months forwards', () => {
2023-01-16 14:18:24 +00:00
const now = Date.vnNew();
2020-06-30 07:15:08 +00:00
now.setDate(15);
2020-06-22 08:01:55 +00:00
now.setMonth(now.getMonth() + 4);
controller.step(1);
const expectedMonth = now.getMonth();
const currentMonth = controller.date.getMonth();
expect(currentMonth).toEqual(expectedMonth);
});
});
describe('data() setter', () => {
2022-06-10 06:29:10 +00:00
it('should set the events, exclusions and geoExclusions and then call the refreshEvents() method', () => {
2020-06-22 08:01:55 +00:00
jest.spyOn(controller, 'refreshEvents').mockReturnThis();
controller.data = {
exclusions: [{
2023-01-16 14:18:24 +00:00
dated: Date.vnNew()
2020-06-22 08:01:55 +00:00
}],
events: [{
2023-01-16 14:18:24 +00:00
dated: Date.vnNew()
2022-06-10 06:29:10 +00:00
}],
geoExclusions: [{
2023-01-16 14:18:24 +00:00
dated: Date.vnNew()
2022-06-10 06:29:10 +00:00
}],
2020-06-22 08:01:55 +00:00
};
expect(controller.refreshEvents).toHaveBeenCalledWith();
expect(controller.events).toBeDefined();
expect(controller.events.length).toEqual(1);
expect(controller.exclusions).toBeDefined();
2022-06-10 06:29:10 +00:00
expect(controller.geoExclusions).toBeDefined();
2020-06-22 08:01:55 +00:00
expect(Object.keys(controller.exclusions).length).toEqual(1);
});
});
describe('refreshEvents()', () => {
it('should fill the days property with the events.', () => {
controller.data = [];
2023-01-16 14:18:24 +00:00
controller.firstDay = Date.vnNew();
2020-06-22 08:01:55 +00:00
2023-01-16 14:18:24 +00:00
const lastDay = Date.vnNew();
2020-06-22 08:01:55 +00:00
lastDay.setDate(lastDay.getDate() + 10);
controller.lastDay = lastDay;
const firstEventStamp = controller.firstDay.getTime();
const lastEventStamp = controller.lastDay.getTime();
controller.events = [{
type: 'day',
dated: firstEventStamp
},
{
type: 'day',
dated: lastEventStamp
}];
controller.refreshEvents();
const expectedDays = Object.keys(controller.days);
expect(expectedDays.length).toEqual(2);
});
});
describe('onSelection()', () => {
it('should call the emit() method', () => {
jest.spyOn(controller, 'emit');
const $event = {};
2023-01-16 14:18:24 +00:00
const $days = [Date.vnNew()];
2020-06-22 08:01:55 +00:00
const $type = 'day';
const $weekday = 1;
controller.onSelection($event, $days, $type, $weekday);
expect(controller.emit).toHaveBeenCalledWith('selection',
{
$days: $days,
$event: {},
$events: [],
$exclusions: [],
$type: 'day',
2022-05-27 11:54:35 +00:00
$weekday: 1,
$geoExclusions: [],
2020-06-22 08:01:55 +00:00
}
);
});
});
describe('hasEvents()', () => {
it('should return true for an existing event on a date', () => {
2023-01-16 14:18:24 +00:00
const dated = Date.vnNew();
2020-06-22 08:01:55 +00:00
controller.days[dated.getTime()] = true;
const result = controller.hasEvents(dated);
expect(result).toBeTruthy();
});
});
describe('getClass()', () => {
it('should return the className "excluded" for an excluded date', () => {
2023-01-16 14:18:24 +00:00
const dated = Date.vnNew();
2020-06-22 08:01:55 +00:00
controller.exclusions = [];
controller.exclusions[dated.getTime()] = true;
const result = controller.getClass(dated);
expect(result).toEqual('excluded');
});
2022-06-10 06:29:10 +00:00
it('should return the className "geoExcluded" for a date with geo excluded', () => {
2023-01-16 14:18:24 +00:00
const dated = Date.vnNew();
2022-06-10 06:29:10 +00:00
controller.geoExclusions = [];
controller.geoExclusions[dated.getTime()] = true;
const result = controller.getClass(dated);
expect(result).toEqual('geoExcluded');
});
2020-06-22 08:01:55 +00:00
});
});