2021-03-04 19:11:10 +00:00
|
|
|
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: () => {}
|
|
|
|
}};
|
|
|
|
}));
|
|
|
|
|
2021-03-08 16:49:04 +00:00
|
|
|
beforeEach(() => {
|
|
|
|
const response = {data: 'MyResult'};
|
|
|
|
$httpBackend.whenRoute('GET', 'ItemBotanicals').respond(response);
|
|
|
|
});
|
|
|
|
|
2021-03-04 19:11:10 +00:00
|
|
|
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();
|
|
|
|
});
|
|
|
|
|
2021-03-08 11:40:13 +00:00
|
|
|
it('should call show function in genus field when the default is not prevented', () => {
|
2021-03-04 19:11:10 +00:00
|
|
|
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();
|
|
|
|
});
|
|
|
|
|
2021-03-08 11:40:13 +00:00
|
|
|
it('should call show function in species field when the default is not prevented', () => {
|
2021-03-04 19:11:10 +00:00
|
|
|
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()', () => {
|
2021-03-08 16:49:04 +00:00
|
|
|
it('should throw an error if the item botanical has no genus name', () => {
|
2021-03-04 19:11:10 +00:00
|
|
|
jest.spyOn(controller.vnApp, 'showMessage');
|
|
|
|
|
|
|
|
controller.data = {};
|
|
|
|
|
|
|
|
controller.onGenusAccept();
|
|
|
|
|
|
|
|
expect(controller.vnApp.showError).toHaveBeenCalledWith(`The name of the genus can't be empty`);
|
|
|
|
});
|
|
|
|
|
2021-03-08 16:49:04 +00:00
|
|
|
it('should add the new genus', () => {
|
2021-03-04 19:11:10 +00:00
|
|
|
controller.data = {
|
2021-03-08 11:40:13 +00:00
|
|
|
id: 4,
|
2021-03-04 19:11:10 +00:00
|
|
|
name: 'Anilius'
|
|
|
|
};
|
|
|
|
|
|
|
|
$httpBackend.expectPOST('genera', controller.data).respond(200, controller.data);
|
|
|
|
|
|
|
|
controller.onGenusAccept();
|
|
|
|
$httpBackend.flush();
|
2021-03-08 16:49:04 +00:00
|
|
|
|
2021-03-08 12:06:35 +00:00
|
|
|
controller.onGenusResponse(controller.data);
|
2021-03-08 16:49:04 +00:00
|
|
|
|
|
|
|
expect(controller.botanical.genusFk).toEqual(controller.data.id);
|
2021-03-04 19:11:10 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('onSpeciesAccept()', () => {
|
2021-03-08 16:49:04 +00:00
|
|
|
it('should throw an error if the item botanical has no species name', () => {
|
2021-03-04 19:11:10 +00:00
|
|
|
jest.spyOn(controller.vnApp, 'showMessage');
|
|
|
|
|
|
|
|
controller.data = {};
|
|
|
|
|
|
|
|
controller.onSpeciesAccept();
|
|
|
|
|
2021-03-08 11:40:13 +00:00
|
|
|
expect(controller.vnApp.showError).toHaveBeenCalledWith(`The name of the species can't be empty`);
|
2021-03-04 19:11:10 +00:00
|
|
|
});
|
|
|
|
|
2021-03-08 16:49:04 +00:00
|
|
|
it('should add the new species', () => {
|
2021-03-04 19:11:10 +00:00
|
|
|
controller.data = {
|
2021-03-08 11:40:13 +00:00
|
|
|
id: 2,
|
2021-03-04 19:11:10 +00:00
|
|
|
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);
|
2021-03-04 19:11:10 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('onSubmit()', () => {
|
2021-03-08 11:40:13 +00:00
|
|
|
it('should make HTTP POST request to save the genus and species data', () => {
|
2021-03-04 19:11:10 +00:00
|
|
|
jest.spyOn(controller.$.watcher, 'updateOriginalData');
|
|
|
|
jest.spyOn(controller.$.watcher, 'check');
|
|
|
|
jest.spyOn(controller.$.watcher, 'notifySaved');
|
|
|
|
|
2021-03-08 12:12:13 +00:00
|
|
|
$httpBackend.expectPATCH('ItemBotanicals').respond();
|
2021-03-04 19:11:10 +00:00
|
|
|
controller.onSubmit();
|
|
|
|
$httpBackend.flush();
|
|
|
|
|
|
|
|
expect(controller.$.watcher.updateOriginalData).toHaveBeenCalledWith();
|
|
|
|
expect(controller.$.watcher.check).toHaveBeenCalledWith();
|
|
|
|
expect(controller.$.watcher.notifySaved).toHaveBeenCalledWith();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getBotanicalData()', () => {
|
2021-03-08 11:40:13 +00:00
|
|
|
it('should get the species and genus data references of the item', () => {
|
2021-03-04 19:11:10 +00:00
|
|
|
controller.getBotanicalData();
|
|
|
|
$httpBackend.flush();
|
|
|
|
|
2021-03-08 16:49:04 +00:00
|
|
|
expect(controller.botanical).toEqual(controller.$params);
|
2021-03-04 19:11:10 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|