import { Test, TestingModule } from '@nestjs/testing' import { lorem, datatype, random } from 'faker' import { Firebender } from '../firebender' import { FLUTTER_CLICK_ACTION, MessagingService, PushNotificationTemplate } from './messaging.service' import { PushPayload } from './value-objects' jest.mock('../firebender', () => ({ Firebender: jest.fn(), })) describe('MessagingService', () => { let service: MessagingService let pushStub: jest.SpyInstance const result = { results: [] } beforeAll(async () => { const app: TestingModule = await Test.createTestingModule({ providers: [MessagingService], }).compile() service = app.get(MessagingService) pushStub = jest.spyOn(service, 'push') }) afterEach(() => { jest.resetAllMocks() }) afterAll(() => { jest.restoreAllMocks() }) describe('#OnModuleInit()', () => { let spyFirebender: jest.SpyInstance beforeEach(() => { spyFirebender = jest.spyOn(Firebender.prototype, 'constructor') service.onModuleInit() }) it('firebender is instancied', () => { expect(spyFirebender).toBeCalled() }) }) describe('#sendPushNotificationByTemplate()', () => { let template: PushNotificationTemplate let deviceId: string let sendPushNotificationStub: jest.SpyInstance beforeAll(async () => { sendPushNotificationStub = jest.spyOn(service, 'sendPushNotification') deviceId = datatype.uuid() template = { title: random.words(), message: random.words(), page: random.word(), } }) afterAll(() => { sendPushNotificationStub.mockRestore() }) it('send push notification', async () => { await service.sendPushNotificationByTemplate(template, deviceId) expect(sendPushNotificationStub).toBeCalledWith(deviceId, { notification: { title: template.title, body: template.message, }, data: { click_action: FLUTTER_CLICK_ACTION, page: template.page, }, }) }) }) describe('#sendPushNotification()', () => { let payload: PushPayload let devices: Array const stubPushNotification = () => pushStub.mockResolvedValue(result) beforeAll(() => { devices = ['0001', '0002', '0003'] payload = new PushPayload({ notification: { title: lorem.sentence(), body: lorem.sentences(), }, }) }) beforeEach(async () => { stubPushNotification() await service.sendPushNotification(devices, payload) }) afterAll(() => { pushStub.mockRestore() }) it('sends a Push notification to user`s registered devices', () => { expect(pushStub).toHaveBeenCalledWith(payload, devices) }) describe('with no devices', () => { beforeAll(() => { devices = [] }) it('does not send a Push notification to user`s registered devices', () => { expect(pushStub).not.toHaveBeenCalled() }) }) describe('when payload is not given', () => { beforeAll(() => { payload = null }) it('does not send a Push notification to user`s registered devices', () => { expect(pushStub).not.toHaveBeenCalled() }) }) }) describe('#push()', () => { let payload: PushPayload let devices: Array let sendToDevice: jest.SpyInstance beforeAll(() => { devices = ['0001', '0002', '0003'] payload = new PushPayload({ notification: { title: lorem.sentence(), body: lorem.sentences(), }, }) }) beforeEach(async () => { sendToDevice = jest.fn() Object.defineProperty(service['firebender'], 'firebaseAdmin', { value: { messaging: jest.fn().mockReturnValue({ sendToDevice }), }, }) try { await service['push'](payload, devices) } catch (error) { console.log(error) } }) it('push with firebase messaging', () => { expect(sendToDevice).toBeCalledWith(devices, payload) }) }) })