Skip to content

Commit 633eb2b

Browse files
dpwatrousgingi
authored andcommitted
Notifier usability improvements
- Add getNotifier() helper function - Default to using FakeNotifier in tests - FakeNotifier doesn't require expect() calls unless opted-in
1 parent f117d34 commit 633eb2b

9 files changed

Lines changed: 53 additions & 9 deletions

File tree

packages/bonito-core/src/environment/abstract-environment.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
EnvironmentName,
1212
} from "./environment";
1313
import { 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) {

packages/bonito-core/src/environment/environment.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff 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
*/

packages/bonito-core/src/environment/mock-environment.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ import { createMockLogger } from "../logging";
88
import { FakeResourceGroupService } from "../resource-group";
99
import { FakeStorageAccountService } from "../storage";
1010
import { FakeSubscriptionService } from "../subscription";
11-
import { AlertNotifier } from "../notification/alert-notifier";
1211
import { AbstractEnvironment } from "./abstract-environment";
1312
import {
1413
EnvironmentName,
1514
EnvironmentConfig,
1615
EnvironmentMode,
1716
} from "./environment";
17+
import { FakeNotifier } from "../notification/fake-notifier";
1818

1919
export 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

3636
export class MockEnvironment<

packages/bonito-core/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export {
3232
} from "./http";
3333
export * from "./location";
3434
export { getLogger } from "./logging/logging-util";
35+
export { getNotifier } from "./notification";
3536
export * from "./resource-group";
3637
export * from "./service";
3738
export * from "./storage";

packages/bonito-core/src/notification/__tests__/fake-notifier.spec.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
import { initMockEnvironment } from "../../environment";
22
import { FakeNotifier } from "../fake-notifier";
3+
import { getNotifier } from "../notification-util";
34

45
describe("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", () => {

packages/bonito-core/src/notification/fake-notifier.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ export interface ExpectedNotification {
1212
}
1313

1414
export 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,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from "./notifier";
2+
export * from "./notification-util";
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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+
}

packages/react/src/account/create-account-action.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createForm } from "@azure/bonito-core";
1+
import { createForm, getNotifier } from "@azure/bonito-core";
22
import { AbstractAction } from "@azure/bonito-core/lib/action";
33
import {
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
);

0 commit comments

Comments
 (0)