Skip to content
This repository has been archived by the owner on Jun 14, 2018. It is now read-only.

Commit

Permalink
fix(contextualmenu): disabled not watched
Browse files Browse the repository at this point in the history
References #268.
  • Loading branch information
s-KaiNet authored and andrewconnell committed Apr 25, 2016
1 parent 8af391f commit c914b96
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 54 deletions.
102 changes: 56 additions & 46 deletions src/components/contextualmenu/contextualMenu.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ describe('contextualmenu: <uif-contextual-menu />', () => {
expect($element).toHaveClass('is-open');
}));

it('should return menu visibility status', inject(($compile: Function, $rootScope: ng.IRootScopeService) => {
$scope.isOpen = true;
$scope.$digest();
let contextMenuCtrl: any = angular.element($element[0]).controller('uifContextualMenu');

expect(contextMenuCtrl.isMenuOpened()).toBe(true);
}));

it('should have enhanced content', inject(($compile: Function, $rootScope: ng.IRootScopeService) => {
let content: JQuery = $element.find('li a').first().find('.uif-content');

Expand Down Expand Up @@ -104,89 +112,52 @@ describe('contextualmenu: <uif-contextual-menu />', () => {
}));

it('should write an error - invalid \'uif-is-open\' attribute usage', inject(
($compile: Function,
$rootScope: ng.IRootScopeService,
/* tslint:disable:variable-name */
_$log_: any) => {
/* tslint:enable:variable-name */
($compile: Function, $rootScope: ng.IRootScopeService, $log: any) => {

$scope = $rootScope.$new();

spyOn(_$log_, 'error');
spyOn($log, 'error');

$scope.isOpen = 'invalid';

$compile(ng.element(`
<uif-contextual-menu uif-is-open="isOpen">
<uif-contextual-menu-item uif-text="'Item1'"></uif-contextual-menu-item>
</uif-contextual-menu>`))($scope);
$scope.$digest();

expect(_$log_.error).toHaveBeenCalled();
expect($log.error).toHaveBeenCalled();
}));

it('should write an error - invalid \'uif-is-selected\' attribute usage', inject(
($compile: Function,
$rootScope: ng.IRootScopeService,
/* tslint:disable:variable-name */
_$log_: any) => {
/* tslint:enable:variable-name */
($compile: Function, $rootScope: ng.IRootScopeService, $log: any) => {

$scope = $rootScope.$new();

spyOn(_$log_, 'error');
spyOn($log, 'error');

$scope.isSelected = 'invalid';

$compile(ng.element(`
<uif-contextual-menu>
<uif-contextual-menu-item uif-text="'Item1'" uif-is-selected="isSelected"></uif-contextual-menu-item>
</uif-contextual-menu>`))($scope);
$scope.$digest();

expect(_$log_.error).toHaveBeenCalled();
}));

it('should write an error - invalid \'disabled\' attribute usage', inject(
($compile: Function,
$rootScope: ng.IRootScopeService,
/* tslint:disable:variable-name */
_$log_: any) => {
/* tslint:enable:variable-name */

$scope = $rootScope.$new();

spyOn(_$log_, 'error');

$scope.isDisabled = 'invalid';

$compile(ng.element(`
<uif-contextual-menu>
<uif-contextual-menu-item uif-text="'Item1'" disabled="isDisabled"></uif-contextual-menu-item>
</uif-contextual-menu>`))($scope);
$scope.$digest();

expect(_$log_.error).toHaveBeenCalled();
expect($log.error).toHaveBeenCalled();
}));

it('should write an error - no text for menu item provided', inject(
($compile: Function,
$rootScope: ng.IRootScopeService,
/* tslint:disable:variable-name */
_$log_: any) => {
/* tslint:enable:variable-name */

($compile: Function, $rootScope: ng.IRootScopeService, $log: any) => {
$scope = $rootScope.$new();

spyOn(_$log_, 'error');
spyOn($log, 'error');

$compile(ng.element(`
<uif-contextual-menu>
<uif-contextual-menu-item></uif-contextual-menu-item>
</uif-contextual-menu>`))($scope);
$scope.$digest();

expect(_$log_.error).toHaveBeenCalled();
expect($log.error).toHaveBeenCalled();
}));
});

Expand Down Expand Up @@ -248,6 +219,45 @@ describe('contextualmenu: <uif-contextual-menu />', () => {
$scope.$digest();
expect($element).not.toHaveClass('is-open');
}));
});

describe('disabled items test', () => {
let $element: JQuery;
let $scope: any;

beforeEach(inject(($rootScope: ng.IRootScopeService, $compile: Function) => {
$element = ng.element(`
<uif-contextual-menu uif-is-open="isOpen">
<uif-contextual-menu-item disabled="disabled" uif-text="'Item1'"></uif-contextual-menu-item>
<uif-contextual-menu-item ng-disabled="itemDisabled" uif-text="'Item2'"></uif-contextual-menu-item>
</uif-contextual-menu>`);
$scope = $rootScope;
$scope.isOpen = true;
$scope.$digest();
$compile($element)($scope);
$element = jQuery($element[0]);
$scope.$digest();
}));

it('should change disabled state', inject(($compile: Function, $rootScope: ng.IRootScopeService) => {
$scope.itemDisabled = true;
$scope.$apply();

let $secondItem: JQuery = $element.find('.ms-ContextualMenu-link').eq(1);

expect($secondItem).toHaveClass('is-disabled');

$scope.itemDisabled = false;
$scope.$apply();

expect($secondItem).not.toHaveClass('is-disabled');
}));

it('should set initial disabled state', inject(($compile: Function, $rootScope: ng.IRootScopeService) => {
let $firstItem: JQuery = $element.find('.ms-ContextualMenu-link').eq(0);

expect($firstItem).toHaveClass('is-disabled');
}));

});
});
12 changes: 4 additions & 8 deletions src/components/contextualmenu/contextualMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ export class ContextualMenuItemDirective implements ng.IDirective {
public replace: boolean = true;

public scope: {} = {
isDisabled: '=?disabled',
isSelected: '=?uifIsSelected',
onClick: '&ngClick',
text: '=?uifText',
Expand Down Expand Up @@ -119,20 +118,17 @@ export class ContextualMenuItemDirective implements ng.IDirective {
contextualMenuController: ContextualMenuController,
$transclude: ng.ITranscludeFunction): void => {

if (typeof $scope.isDisabled !== 'boolean' && $scope.isDisabled !== undefined) {
contextualMenuController.$log.error('Error [ngOfficeUiFabric] officeuifabric.components.contextualmenu - ' +
'invalid attribute type: \'uif-is-disabled\'.\n' +
'The type \'' + typeof $scope.isDisabled + '\' is not supported as valid type for \'uif-is-disabled\' attribute for ' +
'<uif-contextual-menu-item />. The valid type is boolean.');
}

if (typeof $scope.isSelected !== 'boolean' && $scope.isSelected !== undefined) {
contextualMenuController.$log.error('Error [ngOfficeUiFabric] officeuifabric.components.contextualmenu - ' +
'invalid attribute type: \'uif-is-selected\'.\n' +
'The type \'' + typeof $scope.isSelected + '\' is not supported as valid type for \'uif-is-selected\' attribute for ' +
'<uif-contextual-menu-item />. The valid type is boolean.');
}

$attrs.$observe('disabled', (disabled) => {
$scope.isDisabled = !!disabled;
});

this.transcludeChilds($scope, $element, $transclude);

$scope.selectItem = ($event: MouseEvent) => {
Expand Down

0 comments on commit c914b96

Please sign in to comment.