How to check the properties set in the controller. $ OnInit method in angular js

I have a scenario in which I initialize some of the variables in the controller. Method $ onInit. How to check if these properties are set correctly?

controller

 ctrl.$onInit = function () {
    ctrl.devices = _.cloneDeep(ctrl.formDevices);
};

Unit test

it('should expose device related properties to view', function () {
        expect(controller.devices).toBeDefined();
};

Output

Unit test output

+4
source share
1 answer

You must explicitly call the $ onInit function. $ componentController does not call this function as part of a lifecycle hook that fails.

There are ways that $ onInit is called an implication, which can be found here .

, $onInit , - $onInit. , $onInit , , AngularJS .

it('should expose device related properties to view', function () {
    controller.$onInit();
    expect(controller.devices).toBeDefined();
};
+3

Source: https://habr.com/ru/post/1655428/


All Articles