fixed karma unit tests
gitea/salix/dev This commit looks good Details

This commit is contained in:
Joan Sanchez 2019-09-09 10:57:10 +02:00
parent 7a1ae9177b
commit 1f5a75b285
7 changed files with 84 additions and 18 deletions

View File

@ -11,8 +11,8 @@ describe('Component vnInputFile', () => {
beforeEach(angular.mock.inject(($componentController, $rootScope) => { beforeEach(angular.mock.inject(($componentController, $rootScope) => {
$scope = $rootScope.$new(); $scope = $rootScope.$new();
$attrs = {}; $attrs = {field: '$ctrl.dms.file'};
$element = angular.element('<div><input type="file"><div class="infix"><div class="rightIcons"></div></div>'); $element = angular.element('<vn-input-file label="File" field="$ctrl.dms.file"><input type="file"><div class="infix"><div class="rightIcons"></div></vn-input-file>');
controller = $componentController('vnInputFile', {$element, $scope, $attrs, $timeout, $transclude: () => {}}); controller = $componentController('vnInputFile', {$element, $scope, $attrs, $timeout, $transclude: () => {}});
controller.input = $element[0].querySelector('input'); controller.input = $element[0].querySelector('input');
controller.validate = () => {}; controller.validate = () => {};

View File

@ -11,8 +11,8 @@ describe('Component vnInputNumber', () => {
beforeEach(angular.mock.inject(($componentController, $rootScope) => { beforeEach(angular.mock.inject(($componentController, $rootScope) => {
$scope = $rootScope.$new(); $scope = $rootScope.$new();
$attrs = {}; $attrs = {field: '$ctrl.client.socialName'};
$element = angular.element('<div><input type="number"><div class="infix"><div class="rightIcons"></div></div>'); $element = angular.element('<vn-input-number label="SocialName" field="$ctrl.client.socialName"><input type="number"><div class="infix"><div class="rightIcons"></div></vn-input-number>');
controller = $componentController('vnInputNumber', {$element, $scope, $attrs, $timeout, $transclude: () => {}}); controller = $componentController('vnInputNumber', {$element, $scope, $attrs, $timeout, $transclude: () => {}});
controller.input = $element[0].querySelector('input'); controller.input = $element[0].querySelector('input');
controller.validate = () => {}; controller.validate = () => {};

View File

@ -45,6 +45,8 @@ export default class InputTime extends Input {
this.element.classList.add('not-empty'); this.element.classList.add('not-empty');
else else
this.element.classList.remove('not-empty'); this.element.classList.remove('not-empty');
this.validateValue();
} }
/** /**
@ -83,7 +85,7 @@ export default class InputTime extends Input {
* Gets min step value * Gets min step value
*/ */
get step() { get step() {
return this.input.step; return parseInt(this.input.step);
} }
/** /**
@ -111,6 +113,66 @@ export default class InputTime extends Input {
return date; return date;
} }
/**
* Returns native validation message
*/
get validationError() {
return this.input.validationMessage;
}
/**
* Validates a valid input value
*
* @return {Boolean} - True if has a valid value
*/
hasValidValue() {
return this.input.checkValidity();
}
/**
* Changes the input element
* if has a validation error
*/
validateValue() {
if (!this.hasValidValue()) {
this.hideError();
this.showError();
} else
this.hideError();
}
/**
* Shows the input validation error
*/
showError() {
const infixElement = this.element.querySelector('.infix');
const infixClassList = infixElement.classList;
const errorSpan = document.createElement('span');
errorSpan.className = 'mdl-textfield__error';
const errorText = document.createTextNode(this.validationError);
errorSpan.append(errorText);
infixElement.append(errorSpan);
infixClassList.add('validated', 'invalid');
}
/**
* Hides the input validation error
*/
hideError() {
const infixElement = this.element.querySelector('.infix');
const infixClassList = infixElement.classList;
const errorElement = this.element.querySelector('.infix span.mdl-textfield__error');
if (errorElement)
errorElement.remove();
infixClassList.remove('validated', 'invalid');
}
} }
InputTime.$inject = ['$element', '$scope', '$attrs', 'vnTemplate', '$transclude']; InputTime.$inject = ['$element', '$scope', '$attrs', 'vnTemplate', '$transclude'];

View File

@ -1,6 +1,6 @@
import './index.js'; import './index.js';
describe('Component vnInputNumber', () => { describe('Component vnInputTime', () => {
let $scope; let $scope;
let $attrs; let $attrs;
let $timeout; let $timeout;
@ -11,18 +11,19 @@ describe('Component vnInputNumber', () => {
beforeEach(angular.mock.inject(($componentController, $rootScope) => { beforeEach(angular.mock.inject(($componentController, $rootScope) => {
$scope = $rootScope.$new(); $scope = $rootScope.$new();
$attrs = {}; $attrs = {field: '$ctrl.zone.hour'};
$element = angular.element('<div><input><div class="infix invalid validated"><div class="rightIcons"></div></div>'); $element = angular.element('<vn-input-time label="Hour" field="$ctrl.zone.hour"><input><div class="infix invalid validated"><div class="rightIcons"></div></vn-input-time>');
controller = $componentController('vnInputNumber', {$element, $scope, $attrs, $timeout, $transclude: () => {}}); controller = $componentController('vnInputTime', {$element, $scope, $attrs, $timeout, $transclude: () => {}});
})); }));
describe('value() setter', () => { describe('value() setter', () => {
it(`should set _value to a given value, add the class not-empty and remove invalid and validated`, () => { it(`should set _value to a given value, add the class not-empty and remove invalid and validated`, () => {
controller.value = 'pepino'; const today = new Date();
controller.value = today;
let classes = controller.element.classList.toString(); let classes = controller.element.classList.toString();
expect(classes).toContain('not-empty'); expect(classes).toContain('not-empty');
expect(controller._value).toEqual('pepino'); expect(controller._value).toEqual(today);
classes = controller.element.querySelector('.infix').classList.toString(); classes = controller.element.querySelector('.infix').classList.toString();
@ -40,15 +41,15 @@ describe('Component vnInputNumber', () => {
describe('step() setter/getter', () => { describe('step() setter/getter', () => {
it(`should set input.step to a given value`, () => { it(`should set input.step to a given value`, () => {
controller.step = 50; controller.step = 2;
expect(controller.input.step).toEqual('50'); expect(controller.input.step).toEqual('2');
}); });
it(`should return a number`, () => { it(`should return a number`, () => {
controller.step = 50; controller.step = 2;
expect(controller.step).toEqual(50); expect(controller.step).toEqual(2);
expect(typeof controller.step).toEqual('number'); expect(typeof controller.step).toEqual('number');
}); });
}); });

View File

@ -10,7 +10,7 @@ describe('Component vnTextarea', () => {
beforeEach(angular.mock.inject(($componentController, $rootScope) => { beforeEach(angular.mock.inject(($componentController, $rootScope) => {
$scope = $rootScope.$new(); $scope = $rootScope.$new();
$attrs = {}; $attrs = {field: '$ctrl.claim.observation'};
$element = angular.element('<vn-textarea vn-three label="Observation" field="$ctrl.claim.observation" rows="9"><textarea></vn-textarea>'); $element = angular.element('<vn-textarea vn-three label="Observation" field="$ctrl.claim.observation" rows="9"><textarea></vn-textarea>');
$element[0].firstChild.MaterialTextfield = {updateClasses_: () => {}}; $element[0].firstChild.MaterialTextfield = {updateClasses_: () => {}};
controller = $componentController('vnTextarea', {$scope, $element, $attrs}); controller = $componentController('vnTextarea', {$scope, $element, $attrs});

View File

@ -11,8 +11,8 @@ describe('Component vnTextfield', () => {
beforeEach(angular.mock.inject(($componentController, $rootScope) => { beforeEach(angular.mock.inject(($componentController, $rootScope) => {
$scope = $rootScope.$new(); $scope = $rootScope.$new();
$attrs = {}; $attrs = {field: '$ctrl.client.phone'};
$element = angular.element('<div><input><div class="leftIcons"><div class="rightIcons"></div></div>'); $element = angular.element('<vn-textfield label="Phone" field="$ctrl.client.phone"><input><div class="leftIcons"><div class="rightIcons"></div></vn-textfield>');
controller = $componentController('vnTextfield', {$scope, $element, $attrs, $timeout, $transclude: () => {}}); controller = $componentController('vnTextfield', {$scope, $element, $attrs, $timeout, $transclude: () => {}});
})); }));

View File

@ -189,6 +189,9 @@ class Controller {
onStateChange(value) { onStateChange(value) {
let params = {ticketFk: this.$state.params.id, stateFk: value}; let params = {ticketFk: this.$state.params.id, stateFk: value};
this.$http.post(`/api/TicketTrackings/changeState`, params).then(() => { this.$http.post(`/api/TicketTrackings/changeState`, params).then(() => {
if (this.newInstances().length === 0)
this.$scope.watcher.updateOriginalData();
this.card.reload(); this.card.reload();
this.vnApp.showSuccess(this.$translate.instant('Data saved!')); this.vnApp.showSuccess(this.$translate.instant('Data saved!'));
}); });