Last active
June 15, 2019 19:53
-
-
Save wescopeland/cffaaaad033d4b2a72a7326828057843 to your computer and use it in GitHub Desktop.
Notification service with Spectator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
}); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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