Skip to content

Commit 19d4497

Browse files
committed
A few more tests
1 parent 45d9e29 commit 19d4497

File tree

5 files changed

+90
-10
lines changed

5 files changed

+90
-10
lines changed

packages/messaging/src/controllers/sw-controller.test.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,20 @@ describe('SwController', () => {
178178
});
179179

180180
describe('onPush', () => {
181-
it('does not throw if push is not from FCM', async () => {
181+
it('does nothing if push is not from FCM', async () => {
182+
const showNotificationSpy = spy(self.registration, 'showNotification');
183+
const matchAllSpy = spy(self.clients, 'matchAll');
184+
185+
await callEventListener(makeEvent('push', {}));
186+
182187
await callEventListener(
183188
makeEvent('push', {
184189
data: {}
185190
})
186191
);
192+
193+
expect(showNotificationSpy).not.to.have.been.called;
194+
expect(matchAllSpy).not.to.have.been.called;
187195
});
188196

189197
it('sends a message to window clients if a window client is visible', async () => {
@@ -267,6 +275,32 @@ describe('SwController', () => {
267275

268276
expect(bgMessageHandlerSpy).to.have.been.calledWith();
269277
});
278+
279+
it('warns if there are more action buttons than the browser limit', async () => {
280+
stub(Notification, 'maxActions').value(1);
281+
282+
const warnStub = stub(console, 'warn');
283+
284+
await callEventListener(
285+
makeEvent('push', {
286+
data: {
287+
json: () => ({
288+
notification: {
289+
...NOTIFICATION_MESSAGE_PAYLOAD,
290+
actions: [
291+
{ action: 'like', title: 'Like' },
292+
{ action: 'favorite', title: 'Favorite' }
293+
]
294+
}
295+
})
296+
}
297+
})
298+
);
299+
300+
expect(warnStub).to.have.been.calledOnceWith(
301+
'This browser only supports 1 actions. The remaining actions will not be displayed.'
302+
);
303+
});
270304
});
271305

272306
describe('setBackgrounMessageHandler', () => {

packages/messaging/src/controllers/sw-controller.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,8 +342,7 @@ function showNotification(details: NotificationDetails): Promise<void> {
342342
const { maxActions } = Notification;
343343
if (actions && maxActions && actions.length > maxActions) {
344344
console.warn(
345-
`This browser only supports ${maxActions} actions.` +
346-
`The remaining actions will not be displayed.`
345+
`This browser only supports ${maxActions} actions. The remaining actions will not be displayed.`
347346
);
348347
}
349348

packages/messaging/src/core/token-management.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,43 @@ describe('Token Management', () => {
204204
expect(tokenFromDb).to.deep.equal(expectedTokenDetails);
205205
});
206206

207+
it('deletes the token if the update fails', async () => {
208+
stub(Notification, 'permission').value('granted');
209+
210+
// Change create time to be older than a week.
211+
tokenDetails.createTime = Date.now() - 8 * 24 * 60 * 60 * 1000; // 8 days
212+
213+
await dbSet(firebaseDependencies, tokenDetails);
214+
215+
requestUpdateTokenStub.rejects(new Error('Update failed.'));
216+
217+
await expect(
218+
getToken(
219+
firebaseDependencies,
220+
swRegistration,
221+
tokenDetails.subscriptionOptions!.vapidKey
222+
)
223+
).to.be.rejectedWith('Update failed.');
224+
225+
const expectedTokenDetails: TokenDetails = {
226+
...tokenDetails,
227+
createTime: Date.now()
228+
};
229+
230+
expect(requestGetTokenStub).not.to.have.been.called;
231+
expect(requestUpdateTokenStub).to.have.been.calledOnceWith(
232+
firebaseDependencies,
233+
expectedTokenDetails
234+
);
235+
expect(requestDeleteTokenStub).to.have.been.calledOnceWith(
236+
firebaseDependencies,
237+
tokenDetails.token
238+
);
239+
240+
const tokenFromDb = await dbGet(firebaseDependencies);
241+
expect(tokenFromDb).to.be.undefined;
242+
});
243+
207244
it('returns the token if it is valid', async () => {
208245
stub(Notification, 'permission').value('granted');
209246

packages/messaging/src/helpers/migrate-old-database.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,21 @@ describe('migrateOldDb', () => {
8282
const tokenDetails = await migrateOldDatabase('321321321');
8383
expect(tokenDetails).to.be.null;
8484
});
85+
86+
it('does not migrate an entry with missing optional values', async () => {
87+
const v2TokenDetails: V2TokenDetails = {
88+
fcmToken: 'token-value',
89+
swScope: '/scope-value',
90+
vapidKey: base64ToArrayBuffer('dmFwaWQta2V5LXZhbHVl'),
91+
fcmSenderId: '1234567890',
92+
fcmPushSet: '7654321',
93+
subscription: new FakePushSubscription()
94+
};
95+
await put(2, v2TokenDetails);
96+
97+
const tokenDetails = await migrateOldDatabase('1234567890');
98+
expect(tokenDetails).to.be.null;
99+
});
85100
});
86101

87102
describe('version 3', () => {

packages/messaging/src/testing/fakes/service-worker.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,16 +108,11 @@ export class FakeServiceWorkerRegistration
108108
sync = (null as unknown) as SyncManager;
109109
updateViaCache = (null as unknown) as ServiceWorkerUpdateViaCache;
110110

111-
private readonly notifications: Notification[] = [];
112-
113111
async getNotifications() {
114-
return this.notifications;
112+
return [];
115113
}
116114

117-
async showNotification(title: string, options: NotificationOptions) {
118-
const notification = new Notification(title, options);
119-
this.notifications.push(notification);
120-
}
115+
async showNotification() {}
121116

122117
async update() {}
123118

0 commit comments

Comments
 (0)