Skip to content

Commit ab0faeb

Browse files
pinarxMatt Gaunt
authored andcommitted
Move getPushSubscription method to ControllerInterface. (#579)
* rename getPushSubscription * [AUTOMATED]: Prettier Code Styling * replace renamed variable * all * [AUTOMATED]: Prettier Code Styling
1 parent 84896ec commit ab0faeb

File tree

5 files changed

+91
-105
lines changed

5 files changed

+91
-105
lines changed

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

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ export default class ControllerInterface {
145145
return this.getPublicVapidKey_()
146146
.then(publicKey => {
147147
publicVapidKey = publicKey;
148-
return this.getPushSubscription_(swReg, publicVapidKey);
148+
return this.getPushSubscription(swReg, publicVapidKey);
149149
})
150150
.then(pushSubscription => {
151151
subscription = pushSubscription;
@@ -192,7 +192,7 @@ export default class ControllerInterface {
192192
return this.getPublicVapidKey_()
193193
.then(publicKey => {
194194
publicVapidKey = publicKey;
195-
return this.getPushSubscription_(swReg, publicVapidKey);
195+
return this.getPushSubscription(swReg, publicVapidKey);
196196
})
197197
.then(pushSubscription => {
198198
subscription = pushSubscription;
@@ -273,11 +273,23 @@ export default class ControllerInterface {
273273
throw this.errorFactory_.create(Errors.codes.AVAILABLE_IN_WINDOW);
274274
}
275275

276-
getPushSubscription_(
277-
registration,
278-
publicVapidKey
276+
/**
277+
* Gets a PushSubscription for the current user.
278+
*/
279+
getPushSubscription(
280+
swRegistration: ServiceWorkerRegistration,
281+
publicVapidKey: Uint8Array
279282
): Promise<PushSubscription> {
280-
throw this.errorFactory_.create(Errors.codes.AVAILABLE_IN_WINDOW);
283+
return swRegistration.pushManager.getSubscription().then(subscription => {
284+
if (subscription) {
285+
return subscription;
286+
}
287+
288+
return swRegistration.pushManager.subscribe({
289+
userVisibleOnly: true,
290+
applicationServerKey: publicVapidKey
291+
});
292+
});
281293
}
282294

283295
/**

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

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -368,28 +368,6 @@ export default class WindowController extends ControllerInterface
368368
return Promise.resolve(FCMDetails.DEFAULT_PUBLIC_VAPID_KEY);
369369
}
370370

371-
/**
372-
* Gets a PushSubscription for the current user.
373-
* @private
374-
* @param {ServiceWorkerRegistration} registration
375-
* @return {Promise<PushSubscription>}
376-
*/
377-
getPushSubscription_(swRegistration, publicVapidKey) {
378-
// Check for existing subscription first
379-
let subscription;
380-
let fcmTokenDetails;
381-
return swRegistration.pushManager.getSubscription().then(subscription => {
382-
if (subscription) {
383-
return subscription;
384-
}
385-
386-
return swRegistration.pushManager.subscribe({
387-
userVisibleOnly: true,
388-
applicationServerKey: publicVapidKey
389-
});
390-
});
391-
}
392-
393371
/**
394372
* This method will set up a message listener to handle
395373
* events from the service worker that should trigger

packages/messaging/test/controller-get-token.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ describe('Firebase Messaging > *Controller.getToken()', function() {
222222
mockGetReg(Promise.resolve(registration));
223223

224224
sandbox
225-
.stub(ServiceClass.prototype, 'getPushSubscription_')
225+
.stub(ServiceClass.prototype, 'getPushSubscription')
226226
.callsFake(() => Promise.resolve(subscription));
227227

228228
let vapidKeyToUse = FCMDetails.DEFAULT_PUBLIC_VAPID_KEY;
@@ -256,7 +256,7 @@ describe('Firebase Messaging > *Controller.getToken()', function() {
256256
mockGetReg(Promise.resolve(registration));
257257

258258
sandbox
259-
.stub(ServiceClass.prototype, 'getPushSubscription_')
259+
.stub(ServiceClass.prototype, 'getPushSubscription')
260260
.callsFake(() => Promise.resolve(subscription));
261261
sandbox
262262
.stub(ServiceClass.prototype, 'getPublicVapidKey_')
@@ -298,7 +298,7 @@ describe('Firebase Messaging > *Controller.getToken()', function() {
298298
.callsFake(() => Promise.resolve(FCMDetails.DEFAULT_PUBLIC_VAPID_KEY));
299299

300300
sandbox
301-
.stub(ServiceClass.prototype, 'getPushSubscription_')
301+
.stub(ServiceClass.prototype, 'getPushSubscription')
302302
.callsFake(() => Promise.resolve(subscription));
303303

304304
sandbox
@@ -337,7 +337,7 @@ describe('Firebase Messaging > *Controller.getToken()', function() {
337337
.callsFake(() => NotificationPermission.granted);
338338

339339
sandbox
340-
.stub(ServiceClass.prototype, 'getPushSubscription_')
340+
.stub(ServiceClass.prototype, 'getPushSubscription')
341341
.callsFake(() => Promise.resolve(subscription));
342342

343343
let vapidKeyToUse = FCMDetails.DEFAULT_PUBLIC_VAPID_KEY;
@@ -437,7 +437,7 @@ describe('Firebase Messaging > *Controller.getToken()', function() {
437437
.callsFake(() => Promise.resolve(EXAMPLE_TOKEN_DETAILS_DEFAULT_VAPID));
438438

439439
sandbox
440-
.stub(ServiceClass.prototype, 'getPushSubscription_')
440+
.stub(ServiceClass.prototype, 'getPushSubscription')
441441
.callsFake(() => Promise.resolve(subscription));
442442

443443
const serviceInstance = new ServiceClass(app);
@@ -506,7 +506,7 @@ describe('Firebase Messaging > *Controller.getToken()', function() {
506506
.callsFake(() => Promise.resolve(EXAMPLE_EXPIRED_TOKEN_DETAILS));
507507

508508
sandbox
509-
.stub(ServiceClass.prototype, 'getPushSubscription_')
509+
.stub(ServiceClass.prototype, 'getPushSubscription')
510510
.callsFake(() => Promise.resolve(subscription));
511511

512512
sandbox

packages/messaging/test/controller-interface.test.ts

Lines changed: 67 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,18 @@
1616
import { expect } from 'chai';
1717
import * as sinon from 'sinon';
1818
import makeFakeApp from './make-fake-app';
19+
import makeFakeSWReg from './make-fake-sw-reg';
1920

21+
import FCMDetails from '../src/models/fcm-details';
22+
import WindowController from '../src/controllers/window-controller';
23+
import SWController from '../src/controllers/sw-controller';
2024
import ControllerInterface from '../src/controllers/controller-interface';
2125
import TokenDetailsModel from '../src/models/token-details-model';
2226
import VapidDetailsModel from '../src/models/vapid-details-model';
2327
import IIDModel from '../src/models/iid-model';
2428

29+
const controllersToTest = [WindowController, SWController];
30+
2531
describe('Firebase Messaging > *ControllerInterface', function() {
2632
const sandbox = sinon.sandbox.create();
2733
const app = makeFakeApp({
@@ -91,17 +97,67 @@ describe('Firebase Messaging > *ControllerInterface', function() {
9197
});
9298
});
9399

94-
describe('getPushSubscription_()', function() {
95-
it(`should throw`, function() {
96-
const controller = new ControllerInterface(app);
97-
let thrownError;
98-
try {
99-
controller.getPushSubscription_(null, null);
100-
} catch (err) {
101-
thrownError = err;
102-
}
103-
expect(thrownError).to.exist;
104-
expect(thrownError.code).to.equal('messaging/only-available-in-window');
100+
describe('getPushSubscription()', function() {
101+
controllersToTest.forEach(ControllerInTest => {
102+
it(`should return rejection error in ${
103+
ControllerInTest.name
104+
}`, function() {
105+
const injectedError = new Error('Inject error.');
106+
const reg = makeFakeSWReg();
107+
sandbox.stub(reg, 'pushManager').value({
108+
getSubscription: () => Promise.reject(injectedError)
109+
});
110+
111+
const controller = new ControllerInTest(app);
112+
return controller
113+
.getPushSubscription(reg, FCMDetails.DEFAULT_PUBLIC_VAPID_KEY)
114+
.then(
115+
() => {
116+
throw new Error('Expected an error.');
117+
},
118+
err => {
119+
expect(err).to.equal(injectedError);
120+
}
121+
);
122+
});
123+
124+
it(`should return PushSubscription if returned`, function() {
125+
const exampleSubscription = {};
126+
const reg = makeFakeSWReg();
127+
sandbox.stub(reg, 'pushManager').value({
128+
getSubscription: () => Promise.resolve(exampleSubscription)
129+
});
130+
131+
const controller = new ControllerInTest(app);
132+
return controller
133+
.getPushSubscription(reg, FCMDetails.DEFAULT_PUBLIC_VAPID_KEY)
134+
.then(subscription => {
135+
expect(subscription).to.equal(exampleSubscription);
136+
});
137+
});
138+
139+
it('should call subscribe() if no subscription', function() {
140+
const exampleSubscription = {};
141+
const reg = makeFakeSWReg();
142+
sandbox.stub(reg, 'pushManager').value({
143+
getSubscription: async () => {},
144+
subscribe: options => {
145+
expect(options).to.deep.equal({
146+
userVisibleOnly: true,
147+
applicationServerKey: FCMDetails.DEFAULT_PUBLIC_VAPID_KEY
148+
});
149+
150+
return Promise.resolve(exampleSubscription);
151+
}
152+
});
153+
154+
const controller = new ControllerInTest(app);
155+
return controller
156+
.getPushSubscription(reg, FCMDetails.DEFAULT_PUBLIC_VAPID_KEY)
157+
.then(subscription => {
158+
expect(subscription).to.equal(exampleSubscription);
159+
});
160+
});
105161
});
106162
});
107163

packages/messaging/test/window-controller.test.ts

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -346,66 +346,6 @@ describe('Firebase Messaging > *WindowController', function() {
346346
});
347347
});
348348

349-
describe('getPushSubscription_()', function() {
350-
it(`should return rejection error`, function() {
351-
const injectedError = new Error('Inject error.');
352-
const reg = makeFakeSWReg();
353-
sandbox.stub(reg, 'pushManager').value({
354-
getSubscription: () => Promise.reject(injectedError)
355-
});
356-
357-
const controller = new WindowController(app);
358-
return controller
359-
.getPushSubscription_(reg, FCMDetails.DEFAULT_PUBLIC_VAPID_KEY)
360-
.then(
361-
() => {
362-
throw new Error('Expected an error.');
363-
},
364-
err => {
365-
expect(err).to.equal(injectedError);
366-
}
367-
);
368-
});
369-
370-
it(`should return PushSubscription if returned`, function() {
371-
const exampleSubscription = {};
372-
const reg = makeFakeSWReg();
373-
sandbox.stub(reg, 'pushManager').value({
374-
getSubscription: () => Promise.resolve(exampleSubscription)
375-
});
376-
377-
const controller = new WindowController(app);
378-
return controller
379-
.getPushSubscription_(reg, FCMDetails.DEFAULT_PUBLIC_VAPID_KEY)
380-
.then(subscription => {
381-
expect(subscription).to.equal(exampleSubscription);
382-
});
383-
});
384-
385-
it('should call subscribe() if no subscription', function() {
386-
const exampleSubscription = {};
387-
const reg = makeFakeSWReg();
388-
sandbox.stub(reg, 'pushManager').value({
389-
getSubscription: async () => {},
390-
subscribe: options => {
391-
expect(options).to.deep.equal({
392-
userVisibleOnly: true,
393-
applicationServerKey: FCMDetails.DEFAULT_PUBLIC_VAPID_KEY
394-
});
395-
396-
return Promise.resolve(exampleSubscription);
397-
}
398-
});
399-
400-
const controller = new WindowController(app);
401-
return controller
402-
.getPushSubscription_(reg, FCMDetails.DEFAULT_PUBLIC_VAPID_KEY)
403-
.then(subscription => {
404-
expect(subscription).to.equal(exampleSubscription);
405-
});
406-
});
407-
});
408-
409349
describe('setupSWMessageListener_()', function() {
410350
it('should not do anything is no service worker support', function() {
411351
sandbox.stub(window, 'navigator').value({});

0 commit comments

Comments
 (0)