From f91a38455099ea9751244d68ebe947fdc00fcd2d Mon Sep 17 00:00:00 2001 From: Thomas Burleson Date: Tue, 7 Jul 2015 11:15:02 -0500 Subject: [PATCH] fix(fabActions): corrected use of postLink properly attach blur/focus listeners on fab-action-items. --- src/components/fabActions/fabActions.js | 32 ++++---- .../fabSpeedDial/fabSpeedDial.spec.js | 76 ++++++++++++------- 2 files changed, 65 insertions(+), 43 deletions(-) diff --git a/src/components/fabActions/fabActions.js b/src/components/fabActions/fabActions.js index a30e2552ee6..cdecad778f0 100644 --- a/src/components/fabActions/fabActions.js +++ b/src/components/fabActions/fabActions.js @@ -33,25 +33,27 @@ if (children.attr('ng-repeat')) { children.addClass('md-fab-action-item'); } else { - // After setting up the listeners, wrap every child in a new div and add a class that we can - // scale/fling independently + // Wrap every child in a new div and add a class that we can scale/fling independently children.wrap('
'); } - }, - - link: function(scope, element, attributes, controllers) { - // Grab whichever parent controller is used - var controller = controllers[0] || controllers[1]; - - // Make the children open/close their parent directive - if (controller) { - angular.forEach(element.children(), function(child) { - angular.element(child).on('focus', controller.open); - angular.element(child).on('blur', controller.close); - }); + + return function postLink(scope, element, attributes, controllers) { + // Grab whichever parent controller is used + var controller = controllers[0] || controllers[1]; + + // Make the children open/close their parent directive + if (controller) { + angular.forEach(element.children(), function(child) { + // Attach listeners to the `md-fab-action-item` + child = angular.element(child).children()[0]; + + angular.element(child).on('focus', controller.open); + angular.element(child).on('blur', controller.close); + }); + } } } } } -})(); \ No newline at end of file +})(); diff --git a/src/components/fabSpeedDial/fabSpeedDial.spec.js b/src/components/fabSpeedDial/fabSpeedDial.spec.js index c6b2f40d425..7f597e33bff 100644 --- a/src/components/fabSpeedDial/fabSpeedDial.spec.js +++ b/src/components/fabSpeedDial/fabSpeedDial.spec.js @@ -1,21 +1,17 @@ -describe(' directive', function() { - - beforeEach(module('material.components.fabSpeedDial')); +describe(' directive', function () { var pageScope, element, controller; + var $rootScope, $animate, $timeout; - function compileAndLink(template) { - inject(function($compile, $rootScope) { - pageScope = $rootScope.$new(); - element = $compile(template)(pageScope); - controller = element.controller('mdFabSpeedDial'); - - pageScope.$apply(); - }); - } + beforeEach(module('material.components.fabSpeedDial')); + beforeEach(inject(function (_$rootScope_, _$animate_, _$timeout_) { + $rootScope = _$rootScope_; + $animate = _$animate_; + $timeout = _$timeout_; + })); - it('applies a class for each direction', inject(function() { - compileAndLink( + it('applies a class for each direction', inject(function () { + build( '' ); @@ -32,8 +28,8 @@ describe(' directive', function() { expect(element.hasClass('md-right')).toBe(true); })); - it('opens when the trigger element is focused', inject(function() { - compileAndLink( + it('opens when the trigger element is focused', inject(function () { + build( '' ); @@ -42,32 +38,46 @@ describe(' directive', function() { expect(controller.isOpen).toBe(true); })); - it('opens when the speed dial elements are focused', inject(function() { - compileAndLink( + it('opens when the speed dial elements are focused', inject(function () { + build( '' ); element.find('button').triggerHandler('focus'); pageScope.$digest(); + expect(controller.isOpen).toBe(true); })); - it('closes when the speed dial elements are blurred', inject(function() { - compileAndLink( - '' + it('closes when the speed dial elements are blurred', inject(function () { + build( + ''+ + ' ' + + ' ' + + ' ' + + ' ' + + ' ' + + ' ' + + ' ' + + '' ); element.find('button').triggerHandler('focus'); pageScope.$digest(); + expect(controller.isOpen).toBe(true); - element.find('button').triggerHandler('blur'); + var actionBtn = element.find('md-fab-actions').find('button'); + actionBtn.triggerHandler('focus'); + pageScope.$digest(); + actionBtn.triggerHandler('blur'); pageScope.$digest(); + expect(controller.isOpen).toBe(false); })); - it('allows programmatic opening through the md-open attribute', inject(function() { - compileAndLink( + it('allows programmatic opening through the md-open attribute', inject(function () { + build( '' ); @@ -83,8 +93,8 @@ describe(' directive', function() { expect(controller.isOpen).toBe(false); })); - it('properly finishes the fling animation', inject(function(mdFabSpeedDialFlingAnimation) { - compileAndLink( + it('properly finishes the fling animation', inject(function (mdFabSpeedDialFlingAnimation) { + build( '' + ' ' + ' ' + @@ -101,8 +111,8 @@ describe(' directive', function() { expect(removeDone).toHaveBeenCalled(); })); - it('properly finishes the scale animation', inject(function(mdFabSpeedDialScaleAnimation) { - compileAndLink( + it('properly finishes the scale animation', inject(function (mdFabSpeedDialScaleAnimation) { + build( '' + ' ' + ' ' + @@ -119,4 +129,14 @@ describe(' directive', function() { expect(removeDone).toHaveBeenCalled(); })); + function build(template) { + inject(function ($compile) { + pageScope = $rootScope.$new(); + element = $compile(template)(pageScope); + controller = element.controller('mdFabSpeedDial'); + + pageScope.$apply(); + }); + } + });