Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

fix(ngMock#$controller): properly assign bindings to all types of controllers (e.g. class-based) #14439

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/ngMock/angular-mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2181,9 +2181,15 @@ angular.mock.$RootElementProvider = function() {
angular.mock.$ControllerDecorator = ['$delegate', function($delegate) {
return function(expression, locals, later, ident) {
if (later && typeof later === 'object') {
var create = $delegate(expression, locals, true, ident);
angular.extend(create.instance, later);
return create();
var instantiate = $delegate(expression, locals, true, ident);
angular.extend(instantiate.instance, later);

var instance = instantiate();
if (instance !== instantiate.instance) {
angular.extend(instance, later);
}

return instance;
}
return $delegate(expression, locals, later, ident);
};
Expand Down
48 changes: 48 additions & 0 deletions test/ngMock/angular-mocksSpec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

/* globals support: false */

describe('ngMock', function() {
var noop = angular.noop;

Expand Down Expand Up @@ -1888,6 +1890,52 @@ describe('ngMock', function() {
expect(called).toBe(true);
});
});

it('should support assigning bindings when a value is returned from the constructor',
function() {
var called = false;
var data = [
{ name: 'derp1', id: 0 },
{ name: 'testname', id: 1 },
{ name: 'flurp', id: 2 }
];
module(function($controllerProvider) {
$controllerProvider.register('testCtrl', function() {
called = true;
expect(this.data).toBe(data);

return {};
});
});
inject(function($controller, $rootScope) {
var ctrl = $controller('testCtrl', { scope: $rootScope }, { data: data });
expect(ctrl.data).toBe(data);
expect(called).toBe(true);
});
}
);

if (/chrome/.test(navigator.userAgent)) {
it('should support assigning bindings to class-based controller', function() {
var called = false;
var data = [
{ name: 'derp1', id: 0 },
{ name: 'testname', id: 1 },
{ name: 'flurp', id: 2 }
];
module(function($controllerProvider) {
//jshint evil: true
var TestCtrl = eval('(class { constructor() { called = true; } })');
//jshint evil: false
$controllerProvider.register('testCtrl', TestCtrl);
});
inject(function($controller, $rootScope) {
var ctrl = $controller('testCtrl', { scope: $rootScope }, { data: data });
expect(ctrl.data).toBe(data);
expect(called).toBe(true);
});
});
}
});


Expand Down