Reputation: 11
this is my first approach to unit test effects and I don't know how to write test for my Store Dependent Effect, here is the effect code:
validaCellulare$ = createEffect(() =>
this.actions$.pipe(
ofType(checkPIvaSuccess, inviaOtpIntent),
concatMap(action => of(action).pipe(
withLatestFrom(this.onboardingSelector.modelStep1$)
)),
exhaustMap(([action, datiStep1]) =>
this.onboardingService.postValidateCellulare(datiStep1.cellulare, OnboardingFactory.getCellulareWidgetId(this.centricoState.widgetStep1)).pipe(
map((res) => {
if (!!res && res.result.status === ApplicationParams.StatusResponse.success) {
if (action.type === checkPIvaSuccess.type) {
this.modalService.open(ApplicationParams.templateNames.OTP, {
isClosable: false
});
}
return inviaOtpSuccess();
}
return action.type === checkPIvaSuccess.type ? errorThrowing({statusCode: res.result.statusCode}) : inviaOtpIntentError();
}
),
catchError(() => of(inviaOtpIntentError()))
)
)
));
Above my unit-test:
it('checkPIvaSuccess should dispatch inviaOtpSuccess, on success call postValidateCellulare and open modal OTP', () => {
store.setState({onboarding: {modelStep1: {partitaIva: '12345678960', email: '[email protected]', cellulare: '1204014001', flagPrivacy: true}}});
const mockResponse = Mock.onboarding.validate.cellulare[1].body;
const action = checkPIvaSuccess({guid: '1592314141805665835746662667089'});
const completion = inviaOtpSuccess();
actions$ = hot('a', {a: action});
const response = cold('(a|)', {a: mockResponse});
const expected = cold('b', {b: completion});
onboardingService.postValidateCellulare = jest.fn(() => response);
expect(effects.validaCellulare$).toBeObservable(expected);
expect(modalService.open).toHaveBeenCalledWith(ApplicationParams.templateNames.OTP, {isClosable: false});
});
Debugging this code in test environment it's look like blocked inside the concatMap and stop immediatelly without call the internal server call postValidateCellulare. Someone can help me please?
--------------- EDIT NOW IT WORKS -------------
Thanks to @BenLesh I double check my code and I miss to set my appKeyStore = 'entityCache'
. So replacing the spec as follow it completely works:
it('checkPIvaSuccess should dispatch inviaOtpSuccess, on success call postValidateCellulare and open modal OTP', () => {
store.setState({[appStoreKey]: {onboarding: {modelStep1: {partitaIva: '12345678960', email: '[email protected]', cellulare: '1204014001', flagPrivacy: true}}}});
const mockResponse = Mock.onboarding.validate.cellulare[1].body;
const action = checkPIvaSuccess({guid: '1592314141805665835746662667089'});
const completion = inviaOtpSuccess();
actions$ = hot('a', {a: action});
const response = cold('(a|)', {a: mockResponse});
const expected = cold('b', {b: completion});
onboardingService.postValidateCellulare = jest.fn(() => response);
expect(effects.validaCellulare$).toBeObservable(expected);
expect(modalService.open).toHaveBeenCalledWith(ApplicationParams.templateNames.OTP, {isClosable: false});
});
Upvotes: 1
Views: 111