Skip to content

Commit f799ab7

Browse files
caitphansmaad
authored and
hansmaad
committed
feat(ngMock): allow mock $controller service to set up controller bindings
Adds a new mock for the $controller service, in order to simplify testing using the bindToController feature. ```js var dictionaryOfControllerBindings = { data: [ { id: 0, phone: '...', name: '...' }, { id: 1, phone: '...', name: '...' }, ] }; // When the MyCtrl constructor is called, `this.data ~= dictionaryOfControllerBindings.data` $controller(MyCtrl, myLocals, dictionaryOfControllerBindings); ``` Closes angular#9425 Closes angular#11239
1 parent d7cdba2 commit f799ab7

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

src/ngMock/angular-mocks.js

+72
Original file line numberDiff line numberDiff line change
@@ -1780,6 +1780,77 @@ angular.mock.$RootElementProvider = function() {
17801780
};
17811781
};
17821782

1783+
/**
1784+
* @ngdoc service
1785+
* @name $controller
1786+
* @description
1787+
* A decorator for {@link ng.$controller} with additional `bindings` parameter, useful when testing
1788+
* controllers of directives that use {@link $compile#-bindtocontroller- `bindToController`}.
1789+
*
1790+
*
1791+
* ## Example
1792+
*
1793+
* ```js
1794+
*
1795+
* // Directive definition ...
1796+
*
1797+
* myMod.directive('myDirective', {
1798+
* controller: 'MyDirectiveController',
1799+
* bindToController: {
1800+
* name: '@'
1801+
* }
1802+
* });
1803+
*
1804+
*
1805+
* // Controller definition ...
1806+
*
1807+
* myMod.controller('MyDirectiveController', ['log', function($log) {
1808+
* $log.info(this.name);
1809+
* })];
1810+
*
1811+
*
1812+
* // In a test ...
1813+
*
1814+
* describe('myDirectiveController', function() {
1815+
* it('should write the bound name to the log', inject(function($controller, $log) {
1816+
* var ctrl = $controller('MyDirective', { /* no locals */ }, { name: 'Clark Kent' });
1817+
* expect(ctrl.name).toEqual('Clark Kent');
1818+
* expect($log.info.logs).toEqual(['Clark Kent']);
1819+
* });
1820+
* });
1821+
*
1822+
* ```
1823+
*
1824+
* @param {Function|string} constructor If called with a function then it's considered to be the
1825+
* controller constructor function. Otherwise it's considered to be a string which is used
1826+
* to retrieve the controller constructor using the following steps:
1827+
*
1828+
* * check if a controller with given name is registered via `$controllerProvider`
1829+
* * check if evaluating the string on the current scope returns a constructor
1830+
* * if $controllerProvider#allowGlobals, check `window[constructor]` on the global
1831+
* `window` object (not recommended)
1832+
*
1833+
* The string can use the `controller as property` syntax, where the controller instance is published
1834+
* as the specified property on the `scope`; the `scope` must be injected into `locals` param for this
1835+
* to work correctly.
1836+
*
1837+
* @param {Object} locals Injection locals for Controller.
1838+
* @param {Object=} bindings Properties to add to the controller before invoking the constructor. This is used
1839+
* to simulate the `bindToController` feature and simplify certain kinds of tests.
1840+
* @return {Object} Instance of given controller.
1841+
*/
1842+
angular.mock.$ControllerDecorator = ['$delegate', function($delegate) {
1843+
return function(expression, locals, later, ident) {
1844+
if (later && typeof later === 'object') {
1845+
var create = $delegate(expression, locals, true, ident);
1846+
angular.extend(create.instance, later);
1847+
return create();
1848+
}
1849+
return $delegate(expression, locals, later, ident);
1850+
};
1851+
}];
1852+
1853+
17831854
/**
17841855
* @ngdoc module
17851856
* @name ngMock
@@ -1808,6 +1879,7 @@ angular.module('ngMock', ['ng']).provider({
18081879
$provide.decorator('$$rAF', angular.mock.$RAFDecorator);
18091880
$provide.decorator('$$asyncCallback', angular.mock.$AsyncCallbackDecorator);
18101881
$provide.decorator('$rootScope', angular.mock.$RootScopeDecorator);
1882+
$provide.decorator('$controller', angular.mock.$ControllerDecorator);
18111883
}]);
18121884

18131885
/**

test/ngMock/angular-mocksSpec.js

+22
Original file line numberDiff line numberDiff line change
@@ -1751,6 +1751,28 @@ describe('ngMock', function() {
17511751
}));
17521752
});
17531753
});
1754+
1755+
1756+
describe('$controllerDecorator', function() {
1757+
it('should support creating controller with bindings', function() {
1758+
var called = false;
1759+
var data = [
1760+
{ name: 'derp1', id: 0 },
1761+
{ name: 'testname', id: 1 },
1762+
{ name: 'flurp', id: 2 }
1763+
];
1764+
module(function($controllerProvider) {
1765+
$controllerProvider.register('testCtrl', function() {
1766+
called = true;
1767+
expect(this.data).toEqual(data);
1768+
});
1769+
});
1770+
inject(function($controller, $rootScope) {
1771+
$controller('testCtrl', { scope: $rootScope }, { data: data });
1772+
expect(called).toBe(true);
1773+
});
1774+
});
1775+
});
17541776
});
17551777

17561778

0 commit comments

Comments
 (0)