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.