|
| 1 | +/* eslint-disable @typescript-eslint/no-shadow */ |
| 2 | +import {expect} from '@loopback/testlab'; |
| 3 | +import * as admin from 'firebase-admin'; |
| 4 | +import {FcmMessage, FcmProvider} from '../../providers'; |
| 5 | + |
| 6 | +describe('FCM Service', () => { |
| 7 | + const app = admin.initializeApp(); |
| 8 | + const fcmProvider = new FcmProvider(app); |
| 9 | + |
| 10 | + describe('fcm configration addition', () => { |
| 11 | + it('returns error message when no firebase config', async () => { |
| 12 | + try { |
| 13 | + /* eslint-disable @typescript-eslint/no-unused-vars */ |
| 14 | + const fcmProvider = new FcmProvider(); |
| 15 | + } catch (err) { |
| 16 | + const result = err.message; |
| 17 | + expect(result).which.eql('Firebase Config missing !'); |
| 18 | + } |
| 19 | + }); |
| 20 | + |
| 21 | + it('returns error message on passing reciever length as zero', async () => { |
| 22 | + const message: FcmMessage = { |
| 23 | + receiver: { |
| 24 | + to: [], |
| 25 | + }, |
| 26 | + body: 'test', |
| 27 | + sentDate: new Date(), |
| 28 | + type: 0, |
| 29 | + options: {}, |
| 30 | + }; |
| 31 | + try { |
| 32 | + const result = fcmProvider.initialValidations(message); |
| 33 | + } catch (err) { |
| 34 | + expect(err.message).which.eql( |
| 35 | + 'Message receiver, topic or condition not found in request !', |
| 36 | + ); |
| 37 | + } |
| 38 | + }); |
| 39 | + |
| 40 | + it('returns error message on passing reciever length as zero in value function', async () => { |
| 41 | + const message: FcmMessage = { |
| 42 | + receiver: { |
| 43 | + to: [], |
| 44 | + }, |
| 45 | + body: 'test', |
| 46 | + sentDate: new Date(), |
| 47 | + type: 0, |
| 48 | + options: {}, |
| 49 | + }; |
| 50 | + try { |
| 51 | + const result = fcmProvider.value().publish(message); |
| 52 | + } catch (err) { |
| 53 | + expect(err.message).which.eql( |
| 54 | + 'Message receiver, topic or condition not found in request !', |
| 55 | + ); |
| 56 | + } |
| 57 | + }); |
| 58 | + |
| 59 | + it('returns error message on having no message subject', async () => { |
| 60 | + const message: FcmMessage = { |
| 61 | + receiver: { |
| 62 | + to: [ |
| 63 | + { |
| 64 | + id: 'dummy', |
| 65 | + type: 0, |
| 66 | + }, |
| 67 | + ], |
| 68 | + }, |
| 69 | + body: 'test', |
| 70 | + sentDate: new Date(), |
| 71 | + type: 0, |
| 72 | + options: {}, |
| 73 | + }; |
| 74 | + try { |
| 75 | + const result = fcmProvider.initialValidations(message); |
| 76 | + } catch (err) { |
| 77 | + expect(err.message).which.eql('Message title not found !'); |
| 78 | + } |
| 79 | + }); |
| 80 | + |
| 81 | + it('returns error message on having no message subject using value function', async () => { |
| 82 | + const message: FcmMessage = { |
| 83 | + receiver: { |
| 84 | + to: [ |
| 85 | + { |
| 86 | + id: 'dummy', |
| 87 | + type: 0, |
| 88 | + }, |
| 89 | + ], |
| 90 | + }, |
| 91 | + body: 'test', |
| 92 | + sentDate: new Date(), |
| 93 | + type: 0, |
| 94 | + options: {}, |
| 95 | + }; |
| 96 | + try { |
| 97 | + const result = fcmProvider.value().publish(message); |
| 98 | + } catch (err) { |
| 99 | + expect(err.message).which.eql('Message title not found !'); |
| 100 | + } |
| 101 | + }); |
| 102 | + |
| 103 | + it('returns array for sending push to conditions', async () => { |
| 104 | + const message: FcmMessage = { |
| 105 | + receiver: { |
| 106 | + to: [ |
| 107 | + { |
| 108 | + id: 'dummy', |
| 109 | + type: 0, |
| 110 | + }, |
| 111 | + ], |
| 112 | + }, |
| 113 | + body: 'test', |
| 114 | + sentDate: new Date(), |
| 115 | + type: 0, |
| 116 | + options: {}, |
| 117 | + subject: 'test', |
| 118 | + }; |
| 119 | + |
| 120 | + const generalMessageObj = { |
| 121 | + notification: { |
| 122 | + title: 'test', |
| 123 | + body: 'test', |
| 124 | + }, |
| 125 | + }; |
| 126 | + const result = fcmProvider.sendingPushToConditions( |
| 127 | + message, |
| 128 | + generalMessageObj, |
| 129 | + ); |
| 130 | + expect(result).which.eql([]); |
| 131 | + }).timeout(5000); |
| 132 | + |
| 133 | + it('returns array for sending push to receive tokens', async () => { |
| 134 | + const message: FcmMessage = { |
| 135 | + receiver: { |
| 136 | + to: [ |
| 137 | + { |
| 138 | + id: 'dummy', |
| 139 | + type: 0, |
| 140 | + }, |
| 141 | + ], |
| 142 | + }, |
| 143 | + body: 'test', |
| 144 | + sentDate: new Date(), |
| 145 | + type: 0, |
| 146 | + options: {}, |
| 147 | + subject: 'test', |
| 148 | + }; |
| 149 | + |
| 150 | + const generalMessageObj = { |
| 151 | + notification: { |
| 152 | + title: 'test', |
| 153 | + body: 'test', |
| 154 | + }, |
| 155 | + }; |
| 156 | + const result = fcmProvider.sendingPushToReceiverTokens( |
| 157 | + message, |
| 158 | + generalMessageObj, |
| 159 | + ); |
| 160 | + expect(result).to.have.Array(); |
| 161 | + }).timeout(5000); |
| 162 | + |
| 163 | + it('returns array for sending push to topics', async () => { |
| 164 | + const message: FcmMessage = { |
| 165 | + receiver: { |
| 166 | + to: [ |
| 167 | + { |
| 168 | + id: 'dummy', |
| 169 | + type: 0, |
| 170 | + }, |
| 171 | + ], |
| 172 | + }, |
| 173 | + body: 'test', |
| 174 | + sentDate: new Date(), |
| 175 | + type: 0, |
| 176 | + options: {}, |
| 177 | + subject: 'test', |
| 178 | + }; |
| 179 | + |
| 180 | + const generalMessageObj = { |
| 181 | + notification: { |
| 182 | + title: 'test', |
| 183 | + body: 'test', |
| 184 | + }, |
| 185 | + }; |
| 186 | + const result = fcmProvider.sendingPushToTopics( |
| 187 | + message, |
| 188 | + generalMessageObj, |
| 189 | + ); |
| 190 | + expect(result).which.eql([]); |
| 191 | + }).timeout(5000); |
| 192 | + |
| 193 | + it('returns array for sending in value function', async () => { |
| 194 | + const message: FcmMessage = { |
| 195 | + receiver: { |
| 196 | + to: [ |
| 197 | + { |
| 198 | + id: 'dummy', |
| 199 | + type: 0, |
| 200 | + }, |
| 201 | + ], |
| 202 | + }, |
| 203 | + body: 'test', |
| 204 | + sentDate: new Date(), |
| 205 | + type: 0, |
| 206 | + options: {}, |
| 207 | + subject: 'test', |
| 208 | + }; |
| 209 | + const result = fcmProvider.value().publish(message); |
| 210 | + expect(result).to.have.Promise(); |
| 211 | + }).timeout(5000); |
| 212 | + }); |
| 213 | +}); |
0 commit comments