Ways to examine attributes assigned in the controller.$onInit function in AngularJS

In a certain scenario, I am initializing some variables in the controller's $onInit method. How can I verify that these properties are being set correctly?

Controller

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

Unit test

it('should ensure that device related properties are accessible in the view', function () {
        expect(controller.devices).toBeDefined();
};

Output

View Unit Test Results

Answer №1

It is important to remember to explicitly invoke the $onInit function in your code. Unlike some other lifecycle hooks, $componentController does not automatically trigger this function during initialization.

However, there are ways to implicitly call $onInit, which can be explored further here.

To ensure that your $onInit function correctly sets properties, a simple way is to manually call $onInit in your testing. Remember, the focus should be on testing the functionality of the $onInit function itself rather than AngularJS lifecycle hooks.

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to position a popup div in CSS?

Currently, I am in the process of developing a website that displays DVD details when hovering over an image, similar to what is shown in picture 1. However, I've encountered an issue where the content gets cut off for DVDs located on the right side o ...

The observable did not trigger the next() callback

I'm currently working on incorporating a global loading indicator that can be utilized throughout the entire application. I have created an injectable service with show and hide functions: import { Injectable } from '@angular/core'; import ...

Dealing with numerous promises simultaneously using AngularJS Factory

I have created a code that makes multiple $http calls recursively and saves all the promises it returns in an array. Then, I resolve all of them and save the responses in another array. Now, my question is: How can I efficiently return this final array to ...

Difficulty with Drag Events in Angular Bootstrap Calendar

I am encountering an issue with the drag event functionality of Angular Bootstrap Calendar. I am utilizing this link as a reference. Here is the HTML code snippet: <div ng-controller="scheduleCtrl as vm"> <div class="calender-mid"> <div ...

Can someone guide me on how to make an array filled with dates using Javascript?

I am working on developing a Javascript code that can identify all the days leading up to the current date. Here is what I have managed so far: var titleArray = [ "title1", "title2", ]; var pictureArray = today.toString(); var thumbArray = today.toString ...

The NativeAppEventEmitter does not return any value

I've been grappling with obtaining a logged in user access token for quite some time. I initially faced challenges with it in JavaScript, so I switched to Objective-C and managed to succeed. Following that, I referred to this RN guide: https://facebo ...

Use JavaScript to retrieve a value and display it on a PHP page

I am trying to create a system that can generate and deliver JSON data. Here is the PHP code I have so far: <?php header("Content-Type:application/json"); require "data.php"; if(!empty($_GET['name'])) { $name=$_GET['name']; ...

Encountering an issue with Next.js React SSR using styled-jsx: unable to access the 'state' property as

I've come across a problem that I can't seem to figure out. Despite my attempts to search for a solution here, I haven't been able to help myself. I'm new to javascript and react, so please bear with me. Issue: I'm using React (1 ...

When utilizing json.loads in Python 2.7, it yields a unicode object rather than a dictionary

I'm currently facing a challenge with converting JSON data into a dictionary, and I'm struggling to find a solution. My situation involves connecting to a Tornado websocket from JavaScript and sending the following data inputted into a textfield ...

Communication between parent and child elements in jQuery

I am developing a plugin that focuses on the manipulation of checkboxes. However, I am currently facing an issue. After retrieving JSON data, I need to establish connections between children and parents. Each child contains a "data-parent" attribute with a ...

Creating two separate divs that can scroll independently while also limiting each other's scroll depth can be achieved by utilizing

I am attempting to replicate the unique scrolling feature seen on this particular page. Essentially, there are two columns above the fold that can be scrolled independently, but I want their scroll depths to be linked. When a certain depth is reached whil ...

The ion-input border seems to be fluctuating as the keyboard appears on the screen

I'm currently working with Ionic 3 and experiencing an issue where the selected ion-input's border in the ion-content is shifting when the device keyboard appears on the screen. Here is a visual representation of the problem: https://i.stack.imgu ...

can you explain the concept of a backing instance in react?

Although the concept of a "backing instance" is frequently mentioned in React documentation, I found it difficult to grasp its meaning. According to the React docs: In order to interact with the browser, you need a reference to a DOM node. By attaching ...

Having trouble with Silverlight running JavaScript?

When I try to call a JavaScript function from a SL component using the HtmlPage.Window.Invoke api, it works fine if the function is defined in the page (html). For example: HtmlPage.Window.Invoke("publishValue", topic, jsonObject); However, if I place th ...

An error has occurred: Expected a string as the element type in React when attempting to update the state

I encountered an issue while trying to update the state after fetching data. Error: Element type is invalid - expected a string (for built-in components) or a class/function (for composite components), but received: undefined. This could be due to forgett ...

Controlling and Monitoring Selenium Tests

We are exploring the use of Selenium tests to streamline our testing processes. Is there a tool or service available that can assist in managing and documenting these tests effectively? If there are no automated solutions, are there any resources you wou ...

Tips for managing, showcasing, and modifying checkbox controls within an AngularJS environment

I have successfully completed the saving part of the code. Below, I am demonstrating how I displayed the saved data and my attempts to edit the form upon clicking the edit button. Here is my AngularJS code: var module = angular.module('myApp&apos ...

Setting up the MEAN (Mongo, Express, Angular, Node) stack on Nitrous.IO: A Comprehensive Guide

This weekend, I am planning to tackle two items on my 2013 project list: Experiment with Cloud Development Learn about ANGULAR.JS My plan is to set up the MEAN stack using Nitrous.IO and then use it to complete an Angularjs tutorial project. Questions: ...

Steps for selectively extracting objects from an array containing nested objectsNeed a way to isolate specific objects

Currently, I am working on a project in JavaScript and have created an array called folders that holds multiple objects: folders = [folder1, folder2, folder3...] Each object within the array has various properties, one of which is docs that is an array o ...

Choose a dynamically generated html element without having to click on it

I've been looking for a way to target a dynamically-generated element (specifically a div with the class "date") using jQuery. While I keep finding references to the .on('click') method, it appears that this only selects the div once the pag ...