0

I am trying to write test cases for the effects. Can anyone please help me on how to test the switchMap function inside it. Thanks in advance. Here is my function

loadMovies$ = createEffect(() => this.actions$.pipe(
    ofType('[Login Page] Login'),
    switchMap(() => this.exampleService.getAll()
      .pipe(
        map(movies => ({ type: '[Movies API] Movies Loaded Success', payload: movies })),
        catchError(() => EMPTY)
      ))
    )
  );

And my spec file

it('should return an AddUserSuccess action, with the user, on success', () => {

   actions$ = of({ type: '[Customers Page] Get Customers' });  

    service.getAll();
     effects.loadMovies$.subscribe(action => {
        expect(action).toEqual({
            type: '[Customers API] Get Customers Success',
            payload: [],
        });
    });
  });
sravanthi
  • 175
  • 1
  • 1
  • 15
  • are you testing the switchmap function? usually I trust the people who developed it know what they are doing. Instead, I test that the function I'm calling returns the expected outcome. are you getting an error, what is happening? – rmjoia Feb 18 '20 at 11:41
  • i am not getting any error. i am trying to test the function with the expected returns. But some home my function is not covering the switchMap code. – sravanthi Feb 18 '20 at 11:45
  • 1
    ah, I get it, so you're having issues with code coverage. you need to create a test fixture with the case that gets triggers the switchmap I suppose that would be something like `actions$.next(new actions.LoadMovies());` or something similar. You're subscribing to the observable but you're not emitting, so the expect is never triggered because nothing happened (I think) – rmjoia Feb 18 '20 at 11:48
  • @rmjoia it worked. Thank you – sravanthi Feb 25 '20 at 05:24

0 Answers0