Testing Angular components that have protected @Input properties

Typically, we designate variables as protected when they are only used within a component and its subclasses, but not in the template itself.

This approach becomes particularly advantageous when a class contains multiple variables, as it makes it easy to differentiate between those used in the template by applying protected, private, and public identifiers.

@Input() public disablePlus: boolean = false;
@Input() public disableMinus: boolean = false;

@Input() protected jumpSize: number = 1000;

Now, as I prepare to write unit tests for this component, I've encountered an issue where I cannot directly modify these values within my it method:

it('should change jumpSize to 5000', () => {
    component.jumpSize = 5000; 
    // ts2445: Property `jumpSize` is protected and only accessible within class and its subclasses
    ...
});

it('plus should be disabled', () => {
    component.disablePlus = true; // no error
    ...
});

Should jumpSize remain protected, or should it be changed to public to facilitate testing?

Alternatively, do you believe that our use of protected @Input fields is not practical or effective?

I appreciate any guidance on this matter.

Answer №1

I have never come across an instance of using protected on an input.

In reality, these should be declared as public because, as the name suggests, an input should be able to be updated from outside the class. Are you employing AOT in your application? I would be surprised if it actually compiles that way.

From an Angular perspective, you would encounter the same issue when trying to update the property in your test. When in development mode, the use of JIT (just in time compilation) means you don't receive those types of checks from the template, which is why it may appear to be functioning.

Conclusion: It is best to make all your inputs public.

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 are the steps to integrate an in-memory cache in an Angular frontend using GraphQL queries?

In our Angular frontend application, we are utilizing GraphQL queries with Apollo Client to fetch data. We are interested in implementing caching for our data retrieval process. Initially, we will query the data and store it in the cache. Subsequent requ ...

Guide on integrating react-tether with react-dom createPortal for custom styling of tethered components based on their target components

Within a Component, I am rendering buttons each with its own tooltip. The challenge is to make the tooltip appear upon hovering over the button since the tooltip may contain more than just text and cannot be solely created with CSS. The solution involves ...

Safari experiencing issues with video player controls not functioning properly

Within my application, there is a div element that contains a video. Clicking on the div triggers an event that navigates to another page. Expectation I expect to be able to play the video and use its controls without triggering the navigation event. Ac ...

Enhancing supertest functionality with Typescript

Currently, I am working on extending the functionality of supertest. After referencing a solution from Extending SuperTest, I was able to implement the following example using javascript: const request = require('supertest'); const Test = reque ...

What could be causing my Angular app to be unable to locate the embedded component?

This particular component is called HomeTabComponent and it is embedded here. @Component({ selector: 'app-home-tab', templateUrl: './home-tab.component.html', styleUrls: ['./home-tab.component.scss'] }) export class Ho ...

Tips for capturing a screenshot of the ESRI Map using Angular

Is there a way to capture a screenshot of the Esri map in its current state on the UI and then convert it into a PDF for download using Angular? Below is my current .ts code, but I am open to any additional suggestions. esri-map.component.html <!-- Map ...

Running JavaScript code when the route changes in Angular 6

Currently, I am in the process of upgrading a website that was originally developed using vanilla JS and JQuery to a new UI built with Angular and typescript. Our site also utilizes piwik for monitoring client activity, and the piwik module was created i ...

The Angular RXJS HTTPClient fails to properly handle HttpErrorResponses

In my Angular 12 project, I am facing an issue with setting up unit tests that should capture errors from HTTP responses. However, when running the tests, instead of receiving the error as an actual error object, it is being passed within the response body ...

Fixing a wrong path in the problem matcher of vscode while compiling using $tsc-watch: A step-by-step

My project workspace directory can be found at C:\salix\fantasy. The TypeScript configuration file is located at C:\salix\fantasy\tsconfig.json Despite my efforts, I'm struggling to have the problem matcher for my project dir ...

Angular 2, Release Candidate 5 - Dropdown form includes mysterious "phantom" choice

While working with Angular 2, RC 5, I have encountered a strange issue while building a form to create a new model object. The problem arises when there is an extra blank <option> appearing in the dropdown list after transpiling the code, even though ...

Library of Functions in Angular 2

As I develop my application, I'm considering creating a functions library that can be utilized by various components. What would be the most effective approach to achieve this? Should I create a service containing all the functions, or rely on classes ...

Navigating through different components within a single page

Each segment of my webpage is a distinct component, arranged consecutively while scrolling e.g.: <sectionA></sectionA> <sectionB></sectionB> <sectionC></sectionC> All the examples I've come across involve creating ...

The Recoil Nexus encountered an error: the element type provided is not valid. It should be a string for built-in components or a class/function for composite components, but an object was

Encountered the following error message: Error - Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object. at ReactDOMServerRenderer.render ... This issue arose when integra ...

Observer function simulated by SinonStub

I am currently testing express middleware using sinon.js My goal is to verify that it sends a specific JSON response and prevents the request from moving on to the next middleware or request handler. const middleware = (req: Request, res: Response, nex ...

The error message "The type 'DynamicModule' from Nest.js cannot be assigned to the type 'ForwardReference' within the nest-modules/mailer" was encountered during development

Recently, I decided to enhance my Nest.js application by integrating the MailerModule. I thought of using the helpful guide provided at this link: Acting on this idea, I went ahead and performed the following steps: To start with, I executed the command ...

What methods can I employ to trace anonymous functions within the Angular framework?

I'm curious about how to keep track of anonymous functions for performance purposes. Is there a way to determine which piece of code an anonymous function is associated with? Here's an example of my code: <button (click)="startTimeout()&q ...

retrieving information and parsing from an API using Ionic and Angular version 4

How can I extract data from the "data" field in my API using a function? getMenu() { return this.http.get('http://site.dev/api/menu/7'); } { "id": 26, "name": "Default", "title": "default", "pageelements": [ { "id": 15, ...

Incorporating properties of the parent class through Mixin techniques

I'm encountering an issue while trying to access a property from an inherited class within a mixin class BaseItem{ public id:string; constructor(id:string) { this.id =id; } } abstract class ConfigMixin<K extends BaseItem& ...

Having trouble with Angular deeplinks - my app is opening but the routing isn't working as expected

Help needed: I'm facing an issue with deeplinks in my app where the routing is not working properly. Below is the code snippet causing the problem: import { Component, ViewChild } from ‘@angular/core’; import { App, Platform, Nav, NavController ...

Angular: Issue with FormArrays not iterating properly

Apologies for the lengthy post, but I needed to provide a detailed explanation of the issue I am facing. In my form, there is a control that contains an array of JSON data. I have created a reactive form based on this structure. Below are the JSON data an ...