Skip to content

Commit

Permalink
allow to use (message) string as parameter in convenience methods
Browse files Browse the repository at this point in the history
  • Loading branch information
antoniave committed Nov 19, 2024
1 parent abbc136 commit 5514b4f
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 8 deletions.
47 changes: 46 additions & 1 deletion src/packages/notifier/NotificationServiceImpl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ it("dispatches events to the notification handler", async () => {
expect(events).toHaveLength(0);
});

it("dispatches events with convenience methods", async () => {
it("dispatches events with convenience methods with object parameter", async () => {
const service = await createService(NotificationServiceImpl, {});
const events: unknown[] = [];
service.registerHandler({
Expand Down Expand Up @@ -94,6 +94,51 @@ it("dispatches events with convenience methods", async () => {
`);
});

it("dispatches events with convenience methods with string parameter", async () => {
const service = await createService(NotificationServiceImpl, {});
const events: unknown[] = [];
service.registerHandler({
showNotification(notification: Notification) {
events.push(notification);
},
closeAll() {}
});

service.success("test1");
service.info("test2");
service.warning("test3");
service.error("test4");

expect(events).toMatchInlineSnapshot(`
[
{
"displayDuration": undefined,
"level": "success",
"message": "test1",
"title": undefined,
},
{
"displayDuration": undefined,
"level": "info",
"message": "test2",
"title": undefined,
},
{
"displayDuration": undefined,
"level": "warning",
"message": "test3",
"title": undefined,
},
{
"displayDuration": undefined,
"level": "error",
"message": "test4",
"title": undefined,
},
]
`);
});

it("dispatches events to a later registered notification handler", async () => {
const service = await createService(NotificationServiceImpl, {});
const events: unknown[] = [];
Expand Down
15 changes: 11 additions & 4 deletions src/packages/notifier/NotificationServiceImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,26 @@ export class NotificationServiceImpl implements InternalNotificationAPI {
}

success(options: SimpleNotificationOptions): void {
this.notify({ ...options, level: "success" });
this.#sendSimpleNotification("success", options);
}

info(options: SimpleNotificationOptions): void {
this.notify({ ...options, level: "info" });
this.#sendSimpleNotification("info", options);
}

warning(options: SimpleNotificationOptions): void {
this.notify({ ...options, level: "warning" });
this.#sendSimpleNotification("warning", options);
}

error(options: SimpleNotificationOptions): void {
this.notify({ ...options, level: "error" });
this.#sendSimpleNotification("error", options);
}

#sendSimpleNotification(level: NotificationLevel, options: SimpleNotificationOptions): void {
if (typeof options === "string") {
options = { message: options };
}
this.notify({ ...options, level });
}

closeAll(): void {
Expand Down
8 changes: 5 additions & 3 deletions src/packages/notifier/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ export interface NotificationOptions {
}

/**
* Same as {@link NotificationOptions}, but without the `level` property.
* Options used when emitting a new notification via {@link NotificationService}
* using convenience methods like `warning` and `error`.
*
* Used for convenience methods like `warning` and `error`.
* Options can either be an object that is the same as {@link NotificationOptions}, but without the `level` property
* or a string, which will be used as the `message` of the Notification.
*/
export type SimpleNotificationOptions = Omit<NotificationOptions, "level">;
export type SimpleNotificationOptions = Omit<NotificationOptions, "level"> | string;

/**
* The `NotificationService` allows any part of the application to emit
Expand Down

0 comments on commit 5514b4f

Please sign in to comment.