File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -11,6 +11,7 @@ import {
1111 EnvironmentName ,
1212} from "./environment" ;
1313import { Clock } from "../datetime/clock" ;
14+ import { Notifier } from "../notification" ;
1415
1516/**
1617 * Abstract base class for shared functionality across different environments
@@ -70,6 +71,22 @@ export abstract class AbstractEnvironment<
7071 return localizer ;
7172 }
7273
74+ /**
75+ * Get an instance of the global notifier
76+ */
77+ getNotifier ( ) : Notifier {
78+ const notifier = this . getInjectable < Notifier > ( DependencyName . Notifier ) ;
79+ if ( ! notifier ) {
80+ throw new Error (
81+ "No notifier configured for the current environment"
82+ ) ;
83+ }
84+ return notifier ;
85+ }
86+
87+ /**
88+ * Get the currently configured HTTP client
89+ */
7390 getHttpClient < T extends HttpClient > ( ) : T {
7491 const httpClient = this . getInjectable < T > ( DependencyName . HttpClient ) ;
7592 if ( ! httpClient ) {
Original file line number Diff line number Diff line change @@ -46,6 +46,11 @@ export interface Environment<C extends EnvironmentConfig> {
4646 */
4747 getLocalizer ( ) : Localizer ;
4848
49+ /**
50+ * Gets the notifier for the current environment
51+ */
52+ getNotifier ( ) : Notifier ;
53+
4954 /**
5055 * Gets the HTTP client for the current environment
5156 */
Original file line number Diff line number Diff line change @@ -8,13 +8,13 @@ import { createMockLogger } from "../logging";
88import { FakeResourceGroupService } from "../resource-group" ;
99import { FakeStorageAccountService } from "../storage" ;
1010import { FakeSubscriptionService } from "../subscription" ;
11- import { AlertNotifier } from "../notification/alert-notifier" ;
1211import { AbstractEnvironment } from "./abstract-environment" ;
1312import {
1413 EnvironmentName ,
1514 EnvironmentConfig ,
1615 EnvironmentMode ,
1716} from "./environment" ;
17+ import { FakeNotifier } from "../notification/fake-notifier" ;
1818
1919export const mockEnvironmentConfig : EnvironmentConfig = {
2020 mode : EnvironmentMode . Development ,
@@ -30,7 +30,7 @@ export const mockDependencyFactories: DependencyFactories = {
3030 resourceGroupService : ( ) => new FakeResourceGroupService ( ) ,
3131 storageAccountService : ( ) => new FakeStorageAccountService ( ) ,
3232 subscriptionService : ( ) => new FakeSubscriptionService ( ) ,
33- notifier : ( ) => new AlertNotifier ( ) ,
33+ notifier : ( ) => new FakeNotifier ( ) ,
3434} ;
3535
3636export class MockEnvironment <
Original file line number Diff line number Diff line change @@ -32,6 +32,7 @@ export {
3232} from "./http" ;
3333export * from "./location" ;
3434export { getLogger } from "./logging/logging-util" ;
35+ export { getNotifier } from "./notification" ;
3536export * from "./resource-group" ;
3637export * from "./service" ;
3738export * from "./storage" ;
Original file line number Diff line number Diff line change 11import { initMockEnvironment } from "../../environment" ;
22import { FakeNotifier } from "../fake-notifier" ;
3+ import { getNotifier } from "../notification-util" ;
34
45describe ( "Fake notification tests" , ( ) => {
56 let notifier : FakeNotifier ;
67
78 beforeEach ( ( ) => {
89 initMockEnvironment ( ) ;
9- notifier = new FakeNotifier ( ) ;
10- } ) ;
11-
12- afterEach ( ( ) => {
13- jest . restoreAllMocks ( ) ;
10+ notifier = getNotifier ( ) as FakeNotifier ;
11+ notifier . enableChecking = true ;
1412 } ) ;
1513
1614 test ( "Each notifier function works as expected" , ( ) => {
Original file line number Diff line number Diff line change @@ -12,6 +12,11 @@ export interface ExpectedNotification {
1212}
1313
1414export class FakeNotifier implements Notifier {
15+ /**
16+ * Enable only when specifically testing what notifications are sent
17+ */
18+ enableChecking : boolean = false ;
19+
1520 expectedNotifications : ExpectedNotification [ ] = [ ] ;
1621
1722 expectInfo ( notification : string , config : NotificationConfig ) : void {
@@ -39,6 +44,11 @@ export class FakeNotifier implements Notifier {
3944 expectedNotification : string ,
4045 config : NotificationConfig
4146 ) : void {
47+ if ( ! this . enableChecking ) {
48+ throw new Error (
49+ "Set `FakeNotifier.enableChecking = true` to enable notification assertions"
50+ ) ;
51+ }
4252 this . expectedNotifications . push ( {
4353 level : level ,
4454 notification : expectedNotification ,
Original file line number Diff line number Diff line change 11export * from "./notifier" ;
2+ export * from "./notification-util" ;
Original file line number Diff line number Diff line change 1+ import { getEnvironment } from "../environment" ;
2+ import { Notifier } from "./notifier" ;
3+
4+ /**
5+ * Gets the notifier for the current environment
6+ *
7+ * @returns The globally-configured notifier instance
8+ */
9+ export function getNotifier ( ) : Notifier {
10+ return getEnvironment ( ) . getNotifier ( ) ;
11+ }
Original file line number Diff line number Diff line change 1- import { createForm } from "@azure/bonito-core" ;
1+ import { createForm , getNotifier } from "@azure/bonito-core" ;
22import { AbstractAction } from "@azure/bonito-core/lib/action" ;
33import {
44 Form ,
@@ -143,7 +143,8 @@ export class CreateAccountAction extends AbstractAction<CreateAccountFormValues>
143143 }
144144
145145 async onExecute ( formValues : CreateAccountFormValues ) : Promise < void > {
146- alert (
146+ getNotifier ( ) . info (
147+ "Account created" ,
147148 "Would write form values:\n" +
148149 JSON . stringify ( formValues , undefined , 4 )
149150 ) ;
You can’t perform that action at this time.
0 commit comments