1

We've recently upgraded to Angular 1.5-rc0. One recent change to ngMocks destroys the $rootScope after every test. However, I'm struggling to figure out why promises we use inside our app never fire after the first time.

Here's a test which simulates a right-click event to test a context menu directive. That directive uses another utility to load an HTML template, cache it, etc. That service resolves a promise when the template was loaded.

However, with Angular/ngMock 1.5, the promise only resolves for the very first "it" block. The $rootScope should be re-created every test so I don't understand what's holding this up.

describe('context-menu directive', function() {
    beforeEach(function() {
        module('templates', 'contextMenu');

        inject(function(_$compile_, _$rootScope_) {
            var scope = _$rootScope_.$new();
            scope.choices = [{
                text: 'Test',
                handler: function() {}
            }];

            $template = _$compile_('<div context-menu="choices"></div>')(scope);
        });
    });

    it('compiles without choices', function() {

    });

    it('right click triggers menu', function() {
        helpers.rightClick($template);
        // The event triggers a template to load and resolve a promise
        // However, that promise ONLY fires if the previous "it" block is removed

        $menu = $('.context-menu');
        console.log($menu.length);
    });
});

I've tried manually calling $rootScope.$apply() to force promise resolution but that doesn't help here.

Here's the important bit of our template helper that resolves a promise:

var deferred = $q.defer();

// Check cache
var template = $templateCache.get(templateUrl);
if (template) {
    deferred.resolve(template);
}

I've verified that deferred.resolve is called, but the then listeners never do.

helion3
  • 34,737
  • 15
  • 57
  • 100

0 Answers0