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

$componentController doesn't seem to trigger $onInit() #14129

@arch-lm

Description

@arch-lm

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'
    });
})

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions