156 lines
4.7 KiB
JavaScript
156 lines
4.7 KiB
JavaScript
|
import './index';
|
||
|
import crudModel from 'core/mocks/crud-model';
|
||
|
|
||
|
describe('component vnZoneCalendar', () => {
|
||
|
let $scope;
|
||
|
let controller;
|
||
|
let $httpBackend;
|
||
|
|
||
|
beforeEach(ngModule('zone'));
|
||
|
|
||
|
beforeEach(angular.mock.inject(($componentController, $rootScope, _$httpBackend_) => {
|
||
|
$httpBackend = _$httpBackend_;
|
||
|
$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 = [];
|
||
|
}));
|
||
|
|
||
|
describe('date() setter', () => {
|
||
|
it('should set the month property and then call the refreshEvents() method', () => {
|
||
|
jest.spyOn(controller, 'refreshEvents').mockReturnThis();
|
||
|
|
||
|
controller.date = new Date();
|
||
|
|
||
|
expect(controller.refreshEvents).toHaveBeenCalledWith();
|
||
|
expect(controller.months.length).toEqual(4);
|
||
|
});
|
||
|
});
|
||
|
|
||
|
describe('step()', () => {
|
||
|
it('should set the date month to 4 months backwards', () => {
|
||
|
const now = new Date();
|
||
|
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', () => {
|
||
|
const now = new Date();
|
||
|
now.setMonth(now.getMonth() + 4);
|
||
|
|
||
|
controller.step(1);
|
||
|
|
||
|
const expectedMonth = now.getMonth();
|
||
|
const currentMonth = controller.date.getMonth();
|
||
|
|
||
|
expect(currentMonth).toEqual(expectedMonth);
|
||
|
});
|
||
|
});
|
||
|
|
||
|
describe('data() setter', () => {
|
||
|
it('should set the events and exclusions and then call the refreshEvents() method', () => {
|
||
|
jest.spyOn(controller, 'refreshEvents').mockReturnThis();
|
||
|
|
||
|
controller.data = {
|
||
|
exclusions: [{
|
||
|
dated: new Date()
|
||
|
}],
|
||
|
events: [{
|
||
|
dated: new Date()
|
||
|
}]
|
||
|
};
|
||
|
|
||
|
expect(controller.refreshEvents).toHaveBeenCalledWith();
|
||
|
expect(controller.events).toBeDefined();
|
||
|
expect(controller.events.length).toEqual(1);
|
||
|
expect(controller.exclusions).toBeDefined();
|
||
|
expect(Object.keys(controller.exclusions).length).toEqual(1);
|
||
|
});
|
||
|
});
|
||
|
|
||
|
describe('refreshEvents()', () => {
|
||
|
it('should fill the days property with the events.', () => {
|
||
|
controller.data = [];
|
||
|
controller.firstDay = new Date();
|
||
|
|
||
|
const lastDay = new Date();
|
||
|
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 = {};
|
||
|
const $days = [new Date()];
|
||
|
const $type = 'day';
|
||
|
const $weekday = 1;
|
||
|
|
||
|
controller.onSelection($event, $days, $type, $weekday);
|
||
|
|
||
|
expect(controller.emit).toHaveBeenCalledWith('selection',
|
||
|
{
|
||
|
$days: $days,
|
||
|
$event: {},
|
||
|
$events: [],
|
||
|
$exclusions: [],
|
||
|
$type: 'day',
|
||
|
$weekday: 1
|
||
|
}
|
||
|
);
|
||
|
});
|
||
|
});
|
||
|
|
||
|
describe('hasEvents()', () => {
|
||
|
it('should return true for an existing event on a date', () => {
|
||
|
const dated = new Date();
|
||
|
|
||
|
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', () => {
|
||
|
const dated = new Date();
|
||
|
|
||
|
controller.exclusions = [];
|
||
|
controller.exclusions[dated.getTime()] = true;
|
||
|
|
||
|
const result = controller.getClass(dated);
|
||
|
|
||
|
expect(result).toEqual('excluded');
|
||
|
});
|
||
|
});
|
||
|
});
|