Skip to content

Commit

Permalink
fix: refactor code and test it for pushpad provider
Browse files Browse the repository at this point in the history
  • Loading branch information
collimarco committed Oct 13, 2023
1 parent f9d5892 commit 192beb3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
29 changes: 29 additions & 0 deletions providers/pushpad/src/lib/pushpad.provider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,33 @@ test('should trigger pushpad library correctly', async () => {
apiKey: 'test-token',
appId: '012345',
});

const mockDeliverTo = jest.fn();
const mockBroadcast = jest.fn();
const mockBuildNotification = jest
.spyOn(provider, 'buildNotification' as any)
.mockReturnValue({
deliverTo: mockDeliverTo,
broadcast: mockBroadcast,
});

const result = await provider.sendMessage({
title: 'Test',
content: 'Test push',
target: ['tester'],
payload: {},
subscriber: {},
step: {
digest: false,
events: [{}],
total_count: 1,
},
});

expect(mockBuildNotification).toHaveBeenCalled();
expect(mockDeliverTo).toHaveBeenCalledTimes(1);
expect(mockBroadcast).not.toHaveBeenCalled();

expect(result).toHaveProperty('id');
expect(result).toHaveProperty('date');
});
14 changes: 9 additions & 5 deletions providers/pushpad/src/lib/pushpad.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,7 @@ export class PushpadPushProvider implements IPushProvider {
async sendMessage(
options: IPushOptions
): Promise<ISendMessageSuccessResponse> {
const notification = new Pushpad.Notification({
project: this.pushpad,
body: options.content,
title: options.title,
});
const notification = this.buildNotification(options);

let notificationId = null;

Expand All @@ -56,4 +52,12 @@ export class PushpadPushProvider implements IPushProvider {
date: new Date().toISOString(),
};
}

private buildNotification(options: IPushOptions): Pushpad.Notification {
return new Pushpad.Notification({
project: this.pushpad,
body: options.content,
title: options.title,
});
}
}

0 comments on commit 192beb3

Please sign in to comment.