Skip to content

Commit

Permalink
Merge branch 'main' into initialize-caip-multichain
Browse files Browse the repository at this point in the history
  • Loading branch information
jiexi authored Oct 21, 2024
2 parents 738c291 + fe28a23 commit 0c89e31
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ import type { PushNotificationEnv } from './types';

const MOCK_JWT = 'mockJwt';
const MOCK_FCM_TOKEN = 'mockFcmToken';
const MOCK_MOBILE_FCM_TOKEN = 'mockMobileFcmToken';
const MOCK_TRIGGERS = ['uuid1', 'uuid2'];

describe('NotificationServicesPushController', () => {
const arrangeServicesMocks = () => {
const arrangeServicesMocks = (token?: string) => {
const activatePushNotificationsMock = jest
.spyOn(services, 'activatePushNotifications')
.mockResolvedValue(MOCK_FCM_TOKEN);
.mockResolvedValue(token ?? MOCK_FCM_TOKEN);

const deactivatePushNotificationsMock = jest
.spyOn(services, 'deactivatePushNotifications')
Expand Down Expand Up @@ -58,6 +59,20 @@ describe('NotificationServicesPushController', () => {

expect(services.listenToPushNotifications).toHaveBeenCalled();
});

it('should update the state with provided mobile fcmToken', async () => {
arrangeServicesMocks(MOCK_MOBILE_FCM_TOKEN);
const { controller, messenger } = arrangeMockMessenger();
mockAuthBearerTokenCall(messenger);

await controller.enablePushNotifications(
MOCK_TRIGGERS,
MOCK_MOBILE_FCM_TOKEN,
);
expect(controller.state.fcmToken).toBe(MOCK_MOBILE_FCM_TOKEN);

expect(services.listenToPushNotifications).toHaveBeenCalled();
});
});

describe('disablePushNotifications', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,9 @@ export default class NotificationServicesPushController extends BaseController<
* 3. Sending the FCM token to the server responsible for sending notifications, to register the device.
*
* @param UUIDs - An array of UUIDs to enable push notifications for.
* @param fcmToken - The optional FCM token to use for push notifications.
*/
async enablePushNotifications(UUIDs: string[]) {
async enablePushNotifications(UUIDs: string[], fcmToken?: string) {
if (!this.#config.isPushEnabled) {
return;
}
Expand All @@ -267,6 +268,7 @@ export default class NotificationServicesPushController extends BaseController<
bearerToken,
triggers: UUIDs,
env: this.#env,
fcmToken,
createRegToken,
platform: this.#config.platform,
}).catch(() => null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {

const MOCK_REG_TOKEN = 'REG_TOKEN';
const MOCK_NEW_REG_TOKEN = 'NEW_REG_TOKEN';
const MOCK_MOBILE_FCM_TOKEN = 'mockMobileFcmToken';
const MOCK_TRIGGERS = ['1', '2', '3'];
const MOCK_JWT = 'MOCK_JWT';

Expand Down Expand Up @@ -78,8 +79,15 @@ describe('NotificationServicesPushController Services', () => {
env: {} as PushNotificationEnv,
};

const mobileParams = {
...params,
fcmToken: MOCK_MOBILE_FCM_TOKEN,
platform: 'mobile' as const,
};

return {
params,
mobileParams,
apis: {
mockGet: mockEndpointGetPushNotificationLinks(override?.mockGet),
mockPut: mockEndpointUpdatePushNotificationLinks(override?.mockPut),
Expand All @@ -98,6 +106,17 @@ describe('NotificationServicesPushController Services', () => {
expect(result).toBe(MOCK_NEW_REG_TOKEN);
});

it('should successfully call APIs and add provided mobile fcmToken', async () => {
const { mobileParams, apis } = arrangeMocks();
const result = await activatePushNotifications(mobileParams);

expect(apis.mockGet.isDone()).toBe(true);
expect(mobileParams.createRegToken).not.toHaveBeenCalled();
expect(apis.mockPut.isDone()).toBe(true);

expect(result).toBe(MOCK_MOBILE_FCM_TOKEN);
});

it('should return null if unable to get links from API', async () => {
const { params, apis } = arrangeMocks({ mockGet: { status: 500 } });
const result = await activatePushNotifications(params);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ type ActivatePushNotificationsParams = {
env: PushNotificationEnv;
createRegToken: CreateRegToken;
platform: 'extension' | 'mobile' | 'portfolio';
fcmToken?: string;
};

/**
Expand All @@ -102,15 +103,16 @@ type ActivatePushNotificationsParams = {
export async function activatePushNotifications(
params: ActivatePushNotificationsParams,
): Promise<string | null> {
const { bearerToken, triggers, env, createRegToken, platform } = params;
const { bearerToken, triggers, env, createRegToken, platform, fcmToken } =
params;

const notificationLinks = await getPushNotificationLinks(bearerToken);

if (!notificationLinks) {
return null;
}

const regToken = await createRegToken(env).catch(() => null);
const regToken = fcmToken ?? (await createRegToken(env).catch(() => null));
if (!regToken) {
return null;
}
Expand Down

0 comments on commit 0c89e31

Please sign in to comment.