components icon indicating copy to clipboard operation
components copied to clipboard

help(MatSnackBarHarness): How do you use the snackbar harness when there is no fixture?

Open catfireparty opened this issue 5 years ago • 4 comments

What are you trying to do?

Write tests for an NgRx effects class which loads the snackbar on certain actions. (Or equivalently a service which does the same)

Reproduction

e.g.

@Injectable()
export class MyEffects {
  doingTheThing$ = createEffect(() =>
    this.actions$.pipe(
      ofType(MyActions.doTheThing),
      tap(() => {
        const snackBarRef = this.snackBar.open(
          `Some snackbar message`,
          'Action?',
          { duration: 5000 }
        );
        snackBarRef.onAction().subscribe(() => {
          this.store.dispatch(MyActions.someOtherAction());
        });
      })
    )
  );

  constructor(
    private actions$: Actions,
    private snackBar: MatSnackBar,
    private store: Store<MyDataSlice>
  ) {}
}

or

@Injectable()
export class MyService {
  constructor(
    private snackBar: MatSnackBar
  ) {}

  doTheThing() {
        const snackBarRef = this.snackBar.open(
          `Some snackbar message`,
          'Action?',
          { duration: 5000 }
        );
        snackBarRef.onAction().subscribe(() => {
          doSomeOtherThing();
        });
      }

  doSomeOtherThing() {
    // todo
  }
}

How do I test either of these cases? Do I have to create a test host that does nothing?

Environment

  • Angular: 11.2.3
  • CDK/Material: 11.2.2

catfireparty avatar Mar 01 '21 11:03 catfireparty

I'm not sure that's possible at the moment since you don't have a way of triggering change detection without the fixture. @mmalerba should know more about it.

crisbeto avatar Mar 01 '21 19:03 crisbeto

Yeah, the harness needs a fixture to be able to trigger change detection. Creating a dummy component like you suggested is probably the best way to handle this.

mmalerba avatar Mar 02 '21 00:03 mmalerba

😞 @crisbeto @mmalerba Thanks for the quick response and the confirmation. I guess I'll do that then.

catfireparty avatar Mar 04 '21 16:03 catfireparty

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.