components icon indicating copy to clipboard operation
components copied to clipboard

bug(MatSnackBar): snackbar duration does not work in tests when there is no component

Open catfireparty opened this issue 5 years ago • 3 comments

Reproduction

Repro repo -> https://github.com/jdpearce/snack-test

Basically:

@Injectable({
  providedIn: 'root',
})
export class SnackbarService {
  constructor(private snackbar: MatSnackBar) {}

  open(): void {
    const ref = this.snackbar.open('This is a snackbar', 'Dismiss', {
      duration: 1000,
    });

    ref.onAction().subscribe(() => {
      console.log('snackbar dismissed with action');
    });
  }
}

and:

describe('snackbar service', () => {
  beforeEach(async () => {
    await TestBed.configureTestingModule({
      imports: [MatSnackBarModule, NoopAnimationsModule],
      providers: [SnackbarService],
    }).compileComponents();
  });

  it('should load the snackbar on and go away after 1 second', (done) => {
    const service = TestBed.inject(SnackbarService);

    expect(document.querySelector('simple-snack-bar')).toBeFalsy();

    service.open();

    expect(document.querySelector('simple-snack-bar')).toBeTruthy();

    setTimeout(() => {
      expect(document.querySelector('simple-snack-bar')).toBeFalsy('oh dear');
      done();
    }, 1000);
  });
});

(please forgive the setTimeouts, I tried doing this with fakeAsync as well but that doesn't work either)

Steps to reproduce:

  1. ng test

Note that when the there is a component under test (app.component.spec.ts) everything works as expected, but in snackbar.service.spec.ts the snackbar doesn't go away.

Expected Behavior

I expect duration to be paid attention to when the snackbar is instantiated in a service under test.

Actual Behavior

The snackbar just sits there at the top of the screen until manually dismissed.

Screenshot 2021-03-01 at 13 07 03

Environment

  • Angular: 11.2.3
  • CDK/Material: 11.2.2
  • Browser(s): Chrome, FireFox
  • Operating System (e.g. Windows, macOS, Ubuntu): macOS

catfireparty avatar Mar 01 '21 15:03 catfireparty

This is a little odd, considering that the duration is basically a setTimeout around a MatSnackBarRef.dismiss call.

crisbeto avatar Mar 01 '21 19:03 crisbeto

We are closing this issue, because we've determined, using a semi-automated process, that it is no longer relevant, has low engagement, or it does not have enough information to proceed. Feel free to comment if you think the issue should be reopened, or open a new issue with the latest version of our code instead.

crisbeto avatar May 29 '26 12:05 crisbeto

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.