unit test for dialog directive plus small refactors

This commit is contained in:
Carlos 2017-10-23 10:47:56 +02:00
parent abb59189ff
commit 4e1d2db3cb
3 changed files with 39 additions and 3 deletions

View File

@ -1,5 +1,3 @@
import './dialog.js';
describe('Component vnDialog', () => {
let $componentController;
let $element;

View File

@ -11,7 +11,8 @@ export function directive() {
restrict: 'A',
link: function($scope, $element, $attrs) {
$element.on('click', function(event) {
let dialog = $scope[kebabToCamel($attrs.vnDialog)];
let dialogKey = kebabToCamel($attrs.vnDialog);
let dialog = $scope[dialogKey];
if (dialog instanceof Dialog)
dialog.show();
event.preventDefault();

View File

@ -0,0 +1,37 @@
describe('Directive dialog', () => {
let $scope;
let $element;
let element;
let compile;
let $componentController;
let controller;
beforeEach(() => {
angular.mock.module('client');
});
compile = _element => {
inject(($compile, $rootScope) => {
$scope = $rootScope.$new();
$scope.myDialog = controller;
element = angular.element(_element);
$compile(element)($scope);
$scope.$digest();
});
};
beforeEach(angular.mock.inject(_$componentController_ => {
$componentController = _$componentController_;
$element = angular.element('<div></div>');
controller = $componentController('vnDialog', {$element});
}));
it('should call show() function if dialog is a instance of vnDialog', () => {
let html = `<div vn-dialog="myDialog"></div>`;
spyOn(controller, 'show');
compile(html);
element[0].click();
expect(controller.show).toHaveBeenCalledWith();
});
});