I have tried to test Angular component by using Karma/Jasmine. To be frank I don't have much idea about karma/jasmine and while testing getting error like "Uncaught TypeError: Cannot read property 'coSearchCriteria' of undefined thrown". But normal component functionality is working fine. If any idea please help me.
This is my Testing code, Here basic 'should create' test case is working but 2nd one giving error.
describe('SearchPanelComponent', () => {
let component: SearchPanelComponent;
let fixture: ComponentFixture<SearchPanelComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [ReactiveFormsModule, FormsModule, HttpModule, CarbonDatepickerModule, CarbonModalModule,CarbonIconModule, StoreModule.forRoot({})],
declarations: [ SearchPanelComponent, UploadsearchcriteriaComponent ],
providers: [ Store, StoreModule, CustomerorderService,ConnectionBackend, ApiConnectorService, HttpClient, HttpHandler,Http, CarbonModalService] })
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(SearchPanelComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
it('RSSD To should be invalid', async( () => {
const dateValue = component.orderUnitForm.controls['rssddateto'];
dateValue.setValue('12-02/2012');
fixture.detectChanges();
expect(component.orderUnitForm.valid).toEqual(false);
}));
And 'coSearchCriteria' I have used at .ts file only like this
this.store.select(selectorCOCriteriaState)
.subscribe((coSearchCriteria: SearchCriteriaState) => {
this.searchState = coSearchCriteria.lastUsedCriteria;
});
if(this.searchState){
this.fillSearchStateData();
}
of undefined
, it usually means you're trying to read it from an undefined parent. In this case, it's being passed in, so you're not referencing any parent. – user184994schemas: [NO_ERRORS_SCHEMA]
, if you are unsure about it and want to get rid of it. My guess is that it could be from child components ofSearchPanelComponent
. Also be warned that addingNO_ERROR_SCHEMA
would hide other errors as well. – Amit Chigadani