Skip to content

Instantly share code, notes, and snippets.

@wescopeland
Last active June 15, 2019 19:53
Show Gist options
  • Save wescopeland/cffaaaad033d4b2a72a7326828057843 to your computer and use it in GitHub Desktop.
Save wescopeland/cffaaaad033d4b2a72a7326828057843 to your computer and use it in GitHub Desktop.
Notification service with Spectator
import { SpectatorService, SpyObject, createService } from '@netbasal/spectator/jest';
import { MatSnackBar } from '@angular/material/snack-bar';
import { NotificationService } from './notification.service';
describe('Service: NotificationService', () => {
let snackBar: SpyObject<MatSnackBar>;
let spectator: SpectatorService<NotificationService> = createService({
service: NotificationService,
mocks: [MatSnackBar]
});
beforeEach(() => {
snackBar = spectator.get(MatSnackBar);
});
it('exists', () => {
expect(spectator.service).toBeDefined();
});
it('can pop open a snackbar notification', () => {
spectator.service.notify('mock notification');
expect(snackBar.open).toHaveBeenCalledWith('mock notification', 'CLOSE', {
duration: 7000
});
});
});
import { Injectable } from '@angular/core';
import { MatSnackBar } from '@angular/material/snack-bar';
@Injectable({ providedIn: 'root' })
export class NotificationService {
constructor(private _snackBar: MatSnackBar) {}
notify(message: string, duration = 7000): void {
this._snackBar.open(message, 'CLOSE', { duration });
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment