Skip to content

Commit

Permalink
Remove native notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
FrederikBolding committed Apr 27, 2022
1 parent 304b8c8 commit 79a468b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 103 deletions.
65 changes: 4 additions & 61 deletions src/notification/NotificationController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
ControllerActions,
NotificationController,
NotificationControllerStateChange,
NotificationType,
} from './NotificationController';

const name = 'NotificationController';
Expand Down Expand Up @@ -38,43 +37,17 @@ const origin = 'snap_test';
const message = 'foo';

describe('NotificationController', () => {
it('action: NotificationController:show native notifications', async () => {
it('action: NotificationController:show', async () => {
const unrestricted = getUnrestrictedMessenger();
const messenger = getRestrictedMessenger(unrestricted);

const showNativeNotification = jest.fn();
new NotificationController({
showNativeNotification,
messenger,
});

expect(
await unrestricted.call('NotificationController:show', origin, {
type: NotificationType.Native,
message,
}),
).toBeUndefined();
expect(showNativeNotification).toHaveBeenCalledTimes(1);
expect(showNativeNotification).toHaveBeenCalledWith(origin, message);
});

it('action: NotificationController:show in-app notifications', async () => {
const unrestricted = getUnrestrictedMessenger();
const messenger = getRestrictedMessenger(unrestricted);

const showNativeNotification = jest.fn();
const controller = new NotificationController({
showNativeNotification,
messenger,
});

expect(
await unrestricted.call('NotificationController:show', origin, {
type: NotificationType.InApp,
message,
}),
await unrestricted.call('NotificationController:show', origin, message),
).toBeUndefined();
expect(showNativeNotification).toHaveBeenCalledTimes(0);
const notifications = Object.values(controller.state.notifications);
expect(notifications).toHaveLength(1);
expect(notifications).toContainEqual({
Expand All @@ -83,27 +56,20 @@ describe('NotificationController', () => {
message,
origin,
readDate: null,
type: NotificationType.InApp,
});
});

it('action: NotificationController:markViewed', async () => {
const unrestricted = getUnrestrictedMessenger();
const messenger = getRestrictedMessenger(unrestricted);

const showNativeNotification = jest.fn();
const controller = new NotificationController({
showNativeNotification,
messenger,
});

expect(
await unrestricted.call('NotificationController:show', origin, {
type: NotificationType.InApp,
message,
}),
await unrestricted.call('NotificationController:show', origin, message),
).toBeUndefined();
expect(showNativeNotification).toHaveBeenCalledTimes(0);
const notifications = Object.values(controller.state.notifications);
expect(notifications).toHaveLength(1);
expect(
Expand All @@ -126,19 +92,13 @@ describe('NotificationController', () => {
const unrestricted = getUnrestrictedMessenger();
const messenger = getRestrictedMessenger(unrestricted);

const showNativeNotification = jest.fn();
const controller = new NotificationController({
showNativeNotification,
messenger,
});

expect(
await unrestricted.call('NotificationController:show', origin, {
type: NotificationType.InApp,
message,
}),
await unrestricted.call('NotificationController:show', origin, message),
).toBeUndefined();
expect(showNativeNotification).toHaveBeenCalledTimes(0);
const notifications = Object.values(controller.state.notifications);
expect(notifications).toHaveLength(1);
expect(
Expand All @@ -150,21 +110,4 @@ describe('NotificationController', () => {

expect(Object.values(controller.state.notifications)).toHaveLength(0);
});

it('uses showNativeNotification to show a notification', () => {
const messenger = getRestrictedMessenger();

const showNativeNotification = jest.fn();
const controller = new NotificationController({
showNativeNotification,
messenger,
});
expect(
controller.show(origin, {
type: NotificationType.Native,
message,
}),
).toBeUndefined();
expect(showNativeNotification).toHaveBeenCalledWith(origin, message);
});
});
45 changes: 3 additions & 42 deletions src/notification/NotificationController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,19 @@ export type NotificationControllerState = {
/**
* @typedef Notification - Stores information about in-app notifications, to be shown in the UI
* @property id - A UUID that identifies the notification
* @property type - The notification type
* @property origin - The origin that requested the notification
* @property createdDate - The notification creation date in milliseconds elapsed since the UNIX epoch
* @property readDate - The notification read date in milliseconds elapsed since the UNIX epoch or null if unread
* @property message - The notification message
*/
export type Notification = {
id: string;
type: NotificationType;
origin: string;
createdDate: number;
readDate: number | null;
message: string;
};

export enum NotificationType {
Native = 'native',
InApp = 'in-app',
}

export interface NotificationArgs {
/**
* Enum type to determine notification type.
*/
type: NotificationType;

/**
* A message to show on the notification.
*/
message: string;
}

const name = 'NotificationController';

export type NotificationControllerStateChange = {
Expand Down Expand Up @@ -106,36 +87,30 @@ export class NotificationController extends BaseController<
NotificationControllerState,
NotificationControllerMessenger
> {
private showNativeNotification;

/**
* Creates a NotificationController instance.
*
* @param options - Constructor options.
* @param options.messenger - A reference to the messaging system.
* @param options.state - Initial state to set on this controller.
* @param options.showNativeNotification - Function that shows a native notification in the consumer
*/
constructor({
messenger,
state,
showNativeNotification,
}: {
messenger: NotificationControllerMessenger;
state?: Partial<NotificationControllerState>;
showNativeNotification: (origin: string, message: string) => void;
}) {
super({
name,
metadata,
messenger,
state: { ...defaultState, ...state },
});
this.showNativeNotification = showNativeNotification;

this.messagingSystem.registerActionHandler(
`${name}:show` as const,
(origin: string, args: NotificationArgs) => this.show(origin, args),
(origin: string, message: string) => this.show(origin, message),
);

this.messagingSystem.registerActionHandler(
Expand All @@ -153,26 +128,12 @@ export class NotificationController extends BaseController<
* Shows a notification.
*
* @param origin - The origin trying to send a notification
* @param args - Notification arguments, containing the notification message etc.
* @param message - A message to show on the notification
*/
show(origin: string, args: NotificationArgs) {
switch (args.type) {
case NotificationType.Native:
this.showNativeNotification(origin, args.message);
break;
case NotificationType.InApp:
this.add(origin, args.message);
break;
default:
throw new Error('Invalid notification type');
}
}

private add(origin: string, message: string) {
show(origin: string, message: string) {
const id = nanoid();
const notification = {
id,
type: NotificationType.InApp,
origin,
createdDate: Date.now(),
readDate: null,
Expand Down

0 comments on commit 79a468b

Please sign in to comment.