Currently, I am creating a test for an AppleComponent which has the following type:
<T,U extends BananaComponent<T>>
. This component also contains BananaComponent<T>
.
Target Component
export class AppleComponent<T,U extends BananaComponent<T>>{
public appleA = 'appleA';
public appleB = 'appleB';
public appleC !: U;
}
Extended Component
export abstract class BananaComponent<T> implements OnInit{
public bananaA = 'bananaA';
public bananaB = 'bananaB';
}
In this case, my spec file is used to perform testing on the AppleComponent.
import { CommonModule } from '@angular/common';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import {AppleComponent} from '....';
import {BananaComponent} from '....';
describe('AppleComponent', () => {
let component: AppleComponent;
let fixture: ComponentFixture<AppleComponent>;
beforeEach(()=>{
TestBed.configureTestingModule({
declarations:[AppleComponent],
imports:[BananaComponent],
});
fixture = TestBed.createComponent(AppleComponent);
component = fixture.componentInstance;
});
it('should have default values',() => {
expect(component.appleA).toBe('appleA','appleA has appleA as default value'); // this will pass
});
});
However, I encountered an issue with the spec file not being compilable.
After examining the logic, I realized that the types of the component and fixture were incorrect. To address this, I made some modifications as detailed below:
// Initially, define a random type within the test and assign it to AppleComponent and BananaComponent.
type typeA = { };
type typeModel = AppleComponent<typeA, BananaComponent<typeA>>;
let component: typeModel;
let fixture: ComponentFixture<typeModel>