I am currently facing an issue with mocking a dependency in a service. I am not sure what is causing the problem. It's not the most ideal class to test, but I am mainly focused on code coverage. Below is the code for the service:
@Injectable()
export class SessionService {
constructor(private sessionStore: UserStore){}
login(userData: User) {
this.sessionStore.login(userData);
}
logout(){
this.sessionStore.logout();
}
}
The dependency UserStore is defined as follows:
export function createInitialState(): User {
return {
id: 0, password: "", role: undefined, username: "",
}
}
@Injectable()
@StoreConfig({ name: 'session', resettable: true })
export class UserStore extends Store<User> {
constructor() {
super(createInitialState());
}
login(user: User){
this.update(user);
}
logout(){
this.reset();
this.update(createInitialState());
}
getCurrentUser(){
return this.getValue();
}
}
Below is the unit test:
describe('SessionService', () => {
// @ts-ignore
let sessionService: SessionService;
let userStoreMock;
beforeEach(() => {
userStoreMock = {
login: jest.fn(),
logout: jest.fn()
};
TestBed.configureTestingModule({
providers:[{ provide: UserStore, useValue: userStoreMock}]
});
sessionService = TestBed.get(SessionService);
});
it('should be defined', () => {
expect(sessionService).toBeDefined();
})
});
The error that I am encountering is:
TypeError: Cannot read property 'getComponentFromError' of null