salix/modules/item/front/botanical/index.spec.js

187 lines
6.8 KiB
JavaScript
Raw Normal View History

import './index.js';
describe('vnItemBotanical', () => {
describe('Component vnItemBotanical', () => {
let $httpBackend;
let $scope;
let controller;
let vnApp;
beforeEach(ngModule('item'));
beforeEach(inject(($componentController, $rootScope, _$httpBackend_, _vnApp_) => {
$httpBackend = _$httpBackend_;
vnApp = _vnApp_;
jest.spyOn(vnApp, 'showError');
$scope = $rootScope.$new();
const $element = angular.element('<vn-item-botanical></vn-item-botanical>');
controller = $componentController('vnItemBotanical', {$element, $scope});
controller.item = {id: 5};
controller.$params = {itemFk: 5};
controller.$ = {
watcher: {
check: () => {},
notifySaved: () => {},
updateOriginalData: () => {}},
genus: {
show: () => {}
},
species: {
show: () => {}
}};
}));
describe('showGenus()', () => {
it('should do nothing in genus field if it default is prevented', () => {
const event = {
defaultPrevented: true,
preventDefault: () => {}
};
jest.spyOn(event, 'preventDefault');
jest.spyOn(controller.$.genus, 'show');
controller.showGenus(event);
expect(event.preventDefault).not.toHaveBeenCalledWith();
expect(controller.$.genus.show).not.toHaveBeenCalledWith();
});
it('should call show function in genus field when the default is not prevented', () => {
const event = {
defaultPrevented: false,
preventDefault: () => {}
};
jest.spyOn(event, 'preventDefault');
jest.spyOn(controller.$.genus, 'show');
controller.showGenus(event);
expect(event.preventDefault).toHaveBeenCalledWith();
expect(controller.$.genus.show).toHaveBeenCalledWith();
});
});
describe('showSpecies()', () => {
it('should do nothing in species field if it default is prevented', () => {
const event = {
defaultPrevented: true,
preventDefault: () => {}
};
jest.spyOn(event, 'preventDefault');
jest.spyOn(controller.$.species, 'show');
controller.showSpecies(event);
expect(event.preventDefault).not.toHaveBeenCalledWith();
expect(controller.$.species.show).not.toHaveBeenCalledWith();
});
it('should call show function in species field when the default is not prevented', () => {
const event = {
defaultPrevented: false,
preventDefault: () => {}
};
jest.spyOn(event, 'preventDefault');
jest.spyOn(controller.$.species, 'show');
controller.showSpecies(event);
expect(event.preventDefault).toHaveBeenCalledWith();
expect(controller.$.species.show).toHaveBeenCalledWith();
});
});
describe('onGenusAccept()', () => {
it('should throw an error if genus field has not name', () => {
jest.spyOn(controller.vnApp, 'showMessage');
controller.data = {};
controller.onGenusAccept();
expect(controller.vnApp.showError).toHaveBeenCalledWith(`The name of the genus can't be empty`);
});
it('should do add the new genus', () => {
const response = {data: 'MyResult'};
$httpBackend.whenRoute('GET', 'ItemBotanicals').respond(response);
$httpBackend.resetExpectations();
controller.data = {
id: 4,
name: 'Anilius'
};
$httpBackend.expectPOST('genera', controller.data).respond(200, controller.data);
controller.onGenusAccept();
$httpBackend.flush();
2021-03-08 12:06:35 +00:00
controller.onGenusResponse(controller.data);
});
});
describe('onSpeciesAccept()', () => {
it('should throw an error if species field has not name', () => {
jest.spyOn(controller.vnApp, 'showMessage');
controller.data = {};
controller.onSpeciesAccept();
expect(controller.vnApp.showError).toHaveBeenCalledWith(`The name of the species can't be empty`);
});
it('should do add the new species', () => {
const response = {data: 'MyResult'};
$httpBackend.whenRoute('GET', 'ItemBotanicals').respond(response);
$httpBackend.resetExpectations();
controller.data = {
id: 2,
name: 'Spasiva'
};
$httpBackend.expectPOST('species', controller.data).respond(200, controller.data);
controller.onSpeciesAccept();
$httpBackend.flush();
2021-03-08 12:06:35 +00:00
controller.onSpeciesResponse(controller.data);
});
});
describe('onSubmit()', () => {
it('should make HTTP POST request to save the genus and species data', () => {
const response = {data: 'MyResult'};
$httpBackend.whenRoute('GET', 'ItemBotanicals').respond(response);
$httpBackend.resetExpectations();
jest.spyOn(controller.$.watcher, 'updateOriginalData');
jest.spyOn(controller.$.watcher, 'check');
jest.spyOn(controller.$.watcher, 'notifySaved');
controller.botanical = [{itemFk: 5, genusFk: 3, specieFk: 2}];
$httpBackend.expectPATCH('ItemBotanicals', controller.botanical).respond();
controller.onSubmit();
$httpBackend.flush();
expect(controller.$.watcher.updateOriginalData).toHaveBeenCalledWith();
expect(controller.$.watcher.check).toHaveBeenCalledWith();
expect(controller.$.watcher.notifySaved).toHaveBeenCalledWith();
});
});
describe('getBotanicalData()', () => {
it('should get the species and genus data references of the item', () => {
const response = ['MyResult'];
$httpBackend.whenRoute('GET', `ItemBotanicals`).respond(response);
controller.getBotanicalData();
$httpBackend.flush();
expect(controller.botanical).toEqual(response[0]);
});
});
});
});