Exploring the world of unit testing in Angular2 with Karma and Jasmine has been quite a journey for me as I navigate through some challenges. Testing static properties is a breeze, but when it comes to properties that rely on functions within the component, things get tricky. I find myself needing to call these functions to properly set certain variables. Here's what I'm struggling with:
Here's a snippet of my component:
export class LoginComponent implements OnInit {
formLoginId: string;
loginUrl: string;
email: string;
password: string;
// Constructor and other methods...
I want to create a unit test to ensure that the loginUrl property receives a value. Here's my test code snippet:
describe('Login Component', ()=> {
let component:LoginComponent;
let fixture:any;
beforeEach(async(()=> {
TestBed.configureTestingModule({
//declarations,imports and providers
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(LoginComponent);
component = fixture.componentInstance;
});
it('should have login url', fakeAsync(()=> {
component.ngOnInit();
tick(1000);
expect(component.loginUrl).toBeDefined();
}));
});
Despite my efforts, the test doesn't seem to be functioning as expected. The loginUrl variable remains undefined. Any pointers on how to effectively call a method from a component and verify its results?
Appreciate any help!