client side unit test for textfield

This commit is contained in:
Carlos 2017-10-04 16:02:53 +02:00
parent dd004fdc89
commit 1b47d85828
3 changed files with 64 additions and 1 deletions

View File

@ -13,6 +13,7 @@ export function directive(vnPopover) {
}
};
}
module.directive('vnPopover', directive);
export class Popover {
@ -69,6 +70,7 @@ export class Popover {
width = innerWidth - dblMargin;
style.width = width + 'px';
}
if (height + dblMargin > innerHeight) {
height = innerHeight - dblMargin;
style.height = height + 'px';
@ -82,11 +84,13 @@ export class Popover {
if (left + width > innerWidth)
left -= (left + width) - innerWidth + margin;
if (top + height > innerHeight)
top -= height + parentNode.offsetHeight + spacing * 2;
if (left < 0)
left = screenMargin;
if (top < 0)
top = screenMargin;
@ -102,6 +106,7 @@ export class Popover {
}
return popoverId;
}
showComponent(childComponent, $scope, parent) {
let childElement = this.document.createElement(childComponent);
let id = 'popover-' + this.popOpens;
@ -111,12 +116,14 @@ export class Popover {
this.show(childElement, parent, id);
return childElement;
}
_checkOpens() {
this.popOpens = this.document.querySelectorAll('*[id^="popover-"]').length;
if (this.popOpens === 0) {
this._destroy();
}
}
_removeElement(val) {
if (!val) return;
let element = angular.element(val);
@ -136,6 +143,7 @@ export class Popover {
}
this._checkOpens();
}
hideChilds(id) {
let popovers = this.document.querySelectorAll('*[id^="popover-"]');
let idNumber = parseInt(id.split('-')[1], 10);
@ -147,6 +155,7 @@ export class Popover {
);
this._checkOpens();
}
hideAll() {
let popovers = this.document.querySelectorAll('*[id^="popover-"]');
popovers.forEach(
@ -156,6 +165,7 @@ export class Popover {
);
this._checkOpens();
}
_findPopOver(node) {
while (node != null) {
if (node.id && node.id.startsWith('popover-')) {
@ -165,6 +175,7 @@ export class Popover {
}
return null;
}
onDocMouseDown(event) {
let targetId = this._findPopOver(event.target);
if (targetId) {
@ -173,6 +184,7 @@ export class Popover {
this.hideAll();
}
}
onDocKeyDown(event) {
if (event.keyCode === 27) {
let targetId = this._findPopOver(this.lastTarget);
@ -184,9 +196,12 @@ export class Popover {
this.lastTarget = null;
}
}
onPopoverMouseDown(event) {
this.lastTarget = event.target;
}
}
Popover.$inject = ['$document', '$compile', '$transitions'];
module.service('vnPopover', Popover);

View File

@ -48,6 +48,7 @@ export default class TextfieldController extends Component {
this.input.focus();
}
}
TextfieldController.$inject = ['$element', '$scope', '$attrs', '$timeout', normalizerFactory.NAME];
module.component('vnTextfield', {

View File

@ -0,0 +1,47 @@
import './textfield.js';
describe('Component vnTextfield', () => {
let $componentController;
let $scope;
let $attrs;
let $timeout;
let $element;
beforeEach(() => {
angular.mock.module('client');
});
beforeEach(angular.mock.inject((_$componentController_, $rootScope, _$httpBackend_, _$timeout_) => {
$componentController = _$componentController_;
$scope = $rootScope.$new();
$attrs = {};
$timeout = _$timeout_;
$element = angular.element('<div><input></div>');
}));
describe('value() setter', () => {
it(`should set _value, input.value and hasValue properties to null, '' and false then call mdlUpdate()`, () => {
let controller = $componentController('vnTextfield', {$scope, $element, $attrs, $timeout});
spyOn(controller, 'mdlUpdate');
let testValue = '';
controller.value = testValue;
expect(controller._value).toEqual(null);
expect(controller.input.value).toEqual(testValue);
expect(controller.hasValue).toEqual(Boolean(testValue));
expect(controller.mdlUpdate).toHaveBeenCalledWith();
});
it(`should set _value, input.value and hasValue propertiest to test, test and true then call mdlUpdate()`, () => {
let controller = $componentController('vnTextfield', {$scope, $element, $attrs, $timeout});
spyOn(controller, 'mdlUpdate');
let testValue = 'test';
controller.value = testValue;
expect(controller._value).toEqual(testValue);
expect(controller.input.value).toEqual(testValue);
expect(controller.hasValue).toEqual(Boolean(testValue));
expect(controller.mdlUpdate).toHaveBeenCalledWith();
});
});
});