Angular 1.5.0
The component test examples set at https://docs.angularjs.org/guide/component implies that $onInit() is supposed to be automatically triggered when instanciating a controller through $componentController , as seen below
it('should set the default values of the hero', function() {
component = $componentController('heroDetail', {$scope: scope});
expect(component.hero).toEqual({
name: undefined,
location: 'unknown'
});
});
Issue : The $onInit() method is never called even if the component has a this.$onInit() method defined in its controller. Either a bug or documentation mistake.
If $onInit() is called manually, such a test works.
it('should set the default values of the hero', function() {
component = $componentController('heroDetail', {$scope: scope});
component.$onInit();
expect(component.hero).toEqual({
name: undefined,
location: 'unknown'
});
});
A test which actually compile the component, not using $componentController , doesn't encounter this kind of issue.
it('should set the default values of the hero', function() {
var element = "<hero-detail></hero-detail>";
element = $compile(element )(scope);
scope.$digest();
var controller = element.controller('heroDetail');
expect(controller.hero).toEqual({
name: undefined,
location: 'unknown'
});
})