Hello im working on Angular 13 and ngRxStore , my code works fine but my Unit Test not working properly , this is my code :
myState: UserState | null = null;
constructor(private store: Store<any>) { }
getUserData(){
this.store.dispatch(new SearchAction(new SearchCriteria(seachTerm)));
this.store.subscribe(state => {
this.myState = state.userState;
if (this.myState?.dataState == MyEnum.LOADED) {
if (this.myState.user != null) {
this.myforM.patchValue({
username: this.myState.user.username
});
}
}
});
}
and this is my Unit test that trigger Exception :
fdescribe('MyComponent', () => {
const userDtoMock: UserDTO = {
'username': 'myusername',
'email': 'test',
'adress': 'adress'
}
const expectedState: UserState = {
user: userDtoMock,
errorMessage: '',
dataState: MyEnum.LOADED
};
let storeMock = {
userState: expectedState
};
const testStore = of(storeMock);
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [
// imports...
providers: [
// providers...
{
provide: Store,
useValue: testStore
},
],
declarations: [
//..components
]
})
.compileComponents();
});
it('#getUserData', () => {
//Given
var mockStore = fixture.debugElement.injector.get(Store);
const storeSpy = spyOn(mockStore, 'dispatch').and.callThrough();
//When
component.getUserData();
//Then
expect(storeSpy).toHaveBeenCalledTimes(1);
});
}
And this is the error during the test :
Error: <spyOn> : dispatch() method does not exist
Usage: spyOn(<object>, <methodName>)
Error: <spyOn> : dispatch() method does not exist
Usage: spyOn(<object>, <methodName>)
at <Jasmine>
To correct this Test i removed this provider :
{
provide: Store,
useValue: testStore
}
but in this case my store.subscribe is not covered in my test.
do you have any idea how i can mock store dispatch method And My Store Data Object ?
Thanks in advance
integration testing
, that would be the most complete. - AliF50