Added unit tests
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Joan Sanchez 2020-06-22 10:01:55 +02:00
parent fa73911452
commit 3cb783740f
4 changed files with 325 additions and 7 deletions

View File

@ -76,7 +76,7 @@ class Controller extends Component {
let events = value.events;
if (events) {
for (event of events) {
for (let event of events) {
event.dated = toStamp(event.dated);
event.ended = toStamp(event.ended);
event.started = toStamp(event.started);

View File

@ -0,0 +1,155 @@
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');
});
});
});

View File

@ -4,13 +4,11 @@ describe('component vnZoneEvents', () => {
let $scope;
let controller;
let $httpBackend;
let $httpParamSerializer;
beforeEach(ngModule('zone'));
beforeEach(angular.mock.inject(($componentController, $rootScope, _$httpBackend_, _$httpParamSerializer_) => {
beforeEach(angular.mock.inject(($componentController, $rootScope, _$httpBackend_) => {
$httpBackend = _$httpBackend_;
$httpParamSerializer = _$httpParamSerializer_;
$scope = $rootScope.$new();
const $element = angular.element(`<vn-zone-events></vn-zone-events>`);
controller = $componentController('vnZoneEvents', {$element, $scope});
@ -89,15 +87,130 @@ describe('component vnZoneEvents', () => {
});
});
describe('create()', () => {
it('shoud set the selected property and then call the dialog show() method', () => {
controller.$.dialog = {show: jest.fn()};
const type = 'weekday';
const days = [new Date()];
const weekday = 1;
controller.create(type, days, weekday);
const selection = controller.selected;
const firstWeekday = selection.wdays[weekday];
expect(selection.type).toEqual('indefinitely');
expect(firstWeekday).toBeTruthy();
expect(controller.isNew).toBeTruthy();
expect(controller.$.dialog.show).toHaveBeenCalledWith();
});
it('shoud set the selected property with the first day and then call the dialog show() method', () => {
controller.$.dialog = {show: jest.fn()};
const type = 'nonListedType';
const days = [new Date()];
const weekday = 1;
controller.create(type, days, weekday);
const selection = controller.selected;
expect(selection.type).toEqual('day');
expect(selection.dated).toEqual(days[0]);
expect(controller.isNew).toBeTruthy();
expect(controller.$.dialog.show).toHaveBeenCalledWith();
});
});
describe('onIncludeResponse()', () => {
it('shoud call the onDelete() method', () => {
jest.spyOn(controller, 'onDelete').mockReturnValue(
new Promise(accept => accept())
);
controller.selected = {id: 1};
controller.onIncludeResponse('delete');
expect(controller.onDelete).toHaveBeenCalledWith(1);
});
it('shoud make an HTTP POST query to create a new one and then call the refresh() method', () => {
jest.spyOn(controller, 'refresh').mockReturnThis();
controller.selected = {id: 1};
controller.isNew = true;
$httpBackend.when('POST', `Zones/1/events`).respond(200);
controller.onIncludeResponse('accept');
$httpBackend.flush();
expect(controller.refresh).toHaveBeenCalledWith();
});
it('shoud make an HTTP PUT query and then call the refresh() method', () => {
jest.spyOn(controller, 'refresh').mockReturnThis();
controller.selected = {id: 1};
controller.isNew = false;
const eventId = 1;
$httpBackend.when('PUT', `Zones/1/events/${eventId}`).respond(200);
controller.onIncludeResponse('accept');
$httpBackend.flush();
expect(controller.refresh).toHaveBeenCalledWith();
});
});
describe('onDeleteResponse()', () => {
it('', () => { });
it('shoud make an HTTP DELETE query and then call the refresh() method', () => {
jest.spyOn(controller, 'refresh').mockReturnThis();
const eventId = 1;
$httpBackend.expect('DELETE', `Zones/1/events/1`).respond({id: 1});
controller.onDeleteResponse('accept', eventId);
$httpBackend.flush();
expect(controller.refresh).toHaveBeenCalledWith();
});
});
describe('exclusionCreate()', () => {
it('', () => { });
it('shoud make an HTTP POST query and then call the refresh() method', () => {
jest.spyOn(controller, 'refresh').mockReturnThis();
const dates = [new Date()];
$httpBackend.expect('POST', `Zones/1/exclusions`).respond({id: 1});
controller.exclusionCreate(dates);
$httpBackend.flush();
expect(controller.refresh).toHaveBeenCalledWith();
});
});
describe('exclusionDelete()', () => {
it('', () => { });
it('shoud make an HTTP DELETE query once and then call the refresh() method', () => {
jest.spyOn(controller, 'refresh').mockReturnThis();
const exclusions = [{id: 1}];
const firstExclusionId = 1;
$httpBackend.when('DELETE', `Zones/1/exclusions/${firstExclusionId}`).respond(200);
controller.exclusionDelete(exclusions);
$httpBackend.flush();
expect(controller.refresh).toHaveBeenCalledWith();
});
it('shoud make an HTTP DELETE query for every event and then call the refresh() method', () => {
jest.spyOn(controller, 'refresh').mockReturnThis();
jest.spyOn(controller.$http, 'delete').mockReturnValue(200);
const exclusions = [{id: 1}, {id: 2}, {id: 3}, {id: 4}];
controller.exclusionDelete(exclusions);
$scope.$apply();
expect(controller.$http.delete).toHaveBeenCalledTimes(4);
expect(controller.refresh).toHaveBeenCalledWith();
});
});
});

View File

@ -0,0 +1,50 @@
import './index';
import crudModel from 'core/mocks/crud-model';
describe('component vnZoneLocation', () => {
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-location></vn-zone-location>`);
controller = $componentController('vnZoneLocation', {$element, $scope});
controller.$.model = crudModel;
controller.zone = {id: 1};
}));
describe('onSearch()', () => {
it('should call the applyFilter() method and then set the data', () => {
controller.$.treeview = {};
controller.onSearch({});
const treeviewData = controller.$.treeview.data;
expect(treeviewData).toBeDefined();
expect(treeviewData.length).toEqual(3);
});
});
describe('onFetch()', () => {
it('should call the applyFilter() method and then return the model data', () => {
const result = controller.onFetch();
expect(result.length).toEqual(3);
});
});
describe('onSelection()', () => {
it('should make an HTTP POST query', () => {
const item = {id: 123};
const expectedParams = {geoId: 123, isIncluded: true};
$httpBackend.expect('POST', `zones/1/toggleIsIncluded`, expectedParams).respond(200);
controller.onSelection(true, item);
$httpBackend.flush();
});
});
});