Skip to content

Move getPushSubscription method to ControllerInterface. #579

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions packages/firebase/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,10 @@ declare namespace firebase.auth {
applyActionCode(code: string): Promise<any>;
checkActionCode(code: string): Promise<any>;
confirmPasswordReset(code: string, newPassword: string): Promise<any>;
createUserAndRetrieveDataWithEmailAndPassword(
email: string,
password: string
): Promise<any>;
createUserAndRetrieveDataWithEmailAndPassword(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jshcrowthe this prettier auto stuff really drags on PRs :(

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, @schmidt-sebastian and I have both proposed solutions to handle this (#585, #580). Both scope the change down to only your package so other components changes shouldn't be included as much.

email: string,
password: string
): Promise<any>;
createUserWithEmailAndPassword(
email: string,
password: string
Expand Down Expand Up @@ -202,7 +202,10 @@ declare namespace firebase.auth {
signInWithCustomToken(token: string): Promise<any>;
signInAndRetrieveDataWithCustomToken(token: string): Promise<any>;
signInWithEmailAndPassword(email: string, password: string): Promise<any>;
signInAndRetrieveDataWithEmailAndPassword(email: string, password: string): Promise<any>;
signInAndRetrieveDataWithEmailAndPassword(
email: string,
password: string
): Promise<any>;
signInWithPhoneNumber(
phoneNumber: string,
applicationVerifier: firebase.auth.ApplicationVerifier
Expand Down
24 changes: 18 additions & 6 deletions packages/messaging/src/controllers/controller-interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ export default class ControllerInterface {
return this.getPublicVapidKey_()
.then(publicKey => {
publicVapidKey = publicKey;
return this.getPushSubscription_(swReg, publicVapidKey);
return this.getPushSubscription(swReg, publicVapidKey);
})
.then(pushSubscription => {
subscription = pushSubscription;
Expand Down Expand Up @@ -192,7 +192,7 @@ export default class ControllerInterface {
return this.getPublicVapidKey_()
.then(publicKey => {
publicVapidKey = publicKey;
return this.getPushSubscription_(swReg, publicVapidKey);
return this.getPushSubscription(swReg, publicVapidKey);
})
.then(pushSubscription => {
subscription = pushSubscription;
Expand Down Expand Up @@ -273,11 +273,23 @@ export default class ControllerInterface {
throw this.errorFactory_.create(Errors.codes.AVAILABLE_IN_WINDOW);
}

getPushSubscription_(
registration,
publicVapidKey
/**
* Gets a PushSubscription for the current user.
*/
getPushSubscription(
swRegistration: ServiceWorkerRegistration,
publicVapidKey: Uint8Array
): Promise<PushSubscription> {
throw this.errorFactory_.create(Errors.codes.AVAILABLE_IN_WINDOW);
return swRegistration.pushManager.getSubscription().then(subscription => {
if (subscription) {
return subscription;
}

return swRegistration.pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey: publicVapidKey
});
});
}

/**
Expand Down
22 changes: 0 additions & 22 deletions packages/messaging/src/controllers/window-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,28 +368,6 @@ export default class WindowController extends ControllerInterface
return Promise.resolve(FCMDetails.DEFAULT_PUBLIC_VAPID_KEY);
}

/**
* Gets a PushSubscription for the current user.
* @private
* @param {ServiceWorkerRegistration} registration
* @return {Promise<PushSubscription>}
*/
getPushSubscription_(swRegistration, publicVapidKey) {
// Check for existing subscription first
let subscription;
let fcmTokenDetails;
return swRegistration.pushManager.getSubscription().then(subscription => {
if (subscription) {
return subscription;
}

return swRegistration.pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey: publicVapidKey
});
});
}

/**
* This method will set up a message listener to handle
* events from the service worker that should trigger
Expand Down
12 changes: 6 additions & 6 deletions packages/messaging/test/controller-get-token.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ describe('Firebase Messaging > *Controller.getToken()', function() {
mockGetReg(Promise.resolve(registration));

sandbox
.stub(ServiceClass.prototype, 'getPushSubscription_')
.stub(ServiceClass.prototype, 'getPushSubscription')
.callsFake(() => Promise.resolve(subscription));

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

sandbox
.stub(ServiceClass.prototype, 'getPushSubscription_')
.stub(ServiceClass.prototype, 'getPushSubscription')
.callsFake(() => Promise.resolve(subscription));
sandbox
.stub(ServiceClass.prototype, 'getPublicVapidKey_')
Expand Down Expand Up @@ -298,7 +298,7 @@ describe('Firebase Messaging > *Controller.getToken()', function() {
.callsFake(() => Promise.resolve(FCMDetails.DEFAULT_PUBLIC_VAPID_KEY));

sandbox
.stub(ServiceClass.prototype, 'getPushSubscription_')
.stub(ServiceClass.prototype, 'getPushSubscription')
.callsFake(() => Promise.resolve(subscription));

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

sandbox
.stub(ServiceClass.prototype, 'getPushSubscription_')
.stub(ServiceClass.prototype, 'getPushSubscription')
.callsFake(() => Promise.resolve(subscription));

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

sandbox
.stub(ServiceClass.prototype, 'getPushSubscription_')
.stub(ServiceClass.prototype, 'getPushSubscription')
.callsFake(() => Promise.resolve(subscription));

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

sandbox
.stub(ServiceClass.prototype, 'getPushSubscription_')
.stub(ServiceClass.prototype, 'getPushSubscription')
.callsFake(() => Promise.resolve(subscription));

sandbox
Expand Down
78 changes: 67 additions & 11 deletions packages/messaging/test/controller-interface.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,18 @@
import { expect } from 'chai';
import * as sinon from 'sinon';
import makeFakeApp from './make-fake-app';
import makeFakeSWReg from './make-fake-sw-reg';

import FCMDetails from '../src/models/fcm-details';
import WindowController from '../src/controllers/window-controller';
import SWController from '../src/controllers/sw-controller';
import ControllerInterface from '../src/controllers/controller-interface';
import TokenDetailsModel from '../src/models/token-details-model';
import VapidDetailsModel from '../src/models/vapid-details-model';
import IIDModel from '../src/models/iid-model';

const controllersToTest = [WindowController, SWController];

describe('Firebase Messaging > *ControllerInterface', function() {
const sandbox = sinon.sandbox.create();
const app = makeFakeApp({
Expand Down Expand Up @@ -91,17 +97,67 @@ describe('Firebase Messaging > *ControllerInterface', function() {
});
});

describe('getPushSubscription_()', function() {
it(`should throw`, function() {
const controller = new ControllerInterface(app);
let thrownError;
try {
controller.getPushSubscription_(null, null);
} catch (err) {
thrownError = err;
}
expect(thrownError).to.exist;
expect(thrownError.code).to.equal('messaging/only-available-in-window');
describe('getPushSubscription()', function() {
controllersToTest.forEach(ControllerInTest => {
it(`should return rejection error in ${
ControllerInTest.name
}`, function() {
const injectedError = new Error('Inject error.');
const reg = makeFakeSWReg();
sandbox.stub(reg, 'pushManager').value({
getSubscription: () => Promise.reject(injectedError)
});

const controller = new ControllerInTest(app);
return controller
.getPushSubscription(reg, FCMDetails.DEFAULT_PUBLIC_VAPID_KEY)
.then(
() => {
throw new Error('Expected an error.');
},
err => {
expect(err).to.equal(injectedError);
}
);
});

it(`should return PushSubscription if returned`, function() {
const exampleSubscription = {};
const reg = makeFakeSWReg();
sandbox.stub(reg, 'pushManager').value({
getSubscription: () => Promise.resolve(exampleSubscription)
});

const controller = new ControllerInTest(app);
return controller
.getPushSubscription(reg, FCMDetails.DEFAULT_PUBLIC_VAPID_KEY)
.then(subscription => {
expect(subscription).to.equal(exampleSubscription);
});
});

it('should call subscribe() if no subscription', function() {
const exampleSubscription = {};
const reg = makeFakeSWReg();
sandbox.stub(reg, 'pushManager').value({
getSubscription: async () => {},
subscribe: options => {
expect(options).to.deep.equal({
userVisibleOnly: true,
applicationServerKey: FCMDetails.DEFAULT_PUBLIC_VAPID_KEY
});

return Promise.resolve(exampleSubscription);
}
});

const controller = new ControllerInTest(app);
return controller
.getPushSubscription(reg, FCMDetails.DEFAULT_PUBLIC_VAPID_KEY)
.then(subscription => {
expect(subscription).to.equal(exampleSubscription);
});
});
});
});

Expand Down
60 changes: 0 additions & 60 deletions packages/messaging/test/window-controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,66 +346,6 @@ describe('Firebase Messaging > *WindowController', function() {
});
});

describe('getPushSubscription_()', function() {
it(`should return rejection error`, function() {
const injectedError = new Error('Inject error.');
const reg = makeFakeSWReg();
sandbox.stub(reg, 'pushManager').value({
getSubscription: () => Promise.reject(injectedError)
});

const controller = new WindowController(app);
return controller
.getPushSubscription_(reg, FCMDetails.DEFAULT_PUBLIC_VAPID_KEY)
.then(
() => {
throw new Error('Expected an error.');
},
err => {
expect(err).to.equal(injectedError);
}
);
});

it(`should return PushSubscription if returned`, function() {
const exampleSubscription = {};
const reg = makeFakeSWReg();
sandbox.stub(reg, 'pushManager').value({
getSubscription: () => Promise.resolve(exampleSubscription)
});

const controller = new WindowController(app);
return controller
.getPushSubscription_(reg, FCMDetails.DEFAULT_PUBLIC_VAPID_KEY)
.then(subscription => {
expect(subscription).to.equal(exampleSubscription);
});
});

it('should call subscribe() if no subscription', function() {
const exampleSubscription = {};
const reg = makeFakeSWReg();
sandbox.stub(reg, 'pushManager').value({
getSubscription: async () => {},
subscribe: options => {
expect(options).to.deep.equal({
userVisibleOnly: true,
applicationServerKey: FCMDetails.DEFAULT_PUBLIC_VAPID_KEY
});

return Promise.resolve(exampleSubscription);
}
});

const controller = new WindowController(app);
return controller
.getPushSubscription_(reg, FCMDetails.DEFAULT_PUBLIC_VAPID_KEY)
.then(subscription => {
expect(subscription).to.equal(exampleSubscription);
});
});
});

describe('setupSWMessageListener_()', function() {
it('should not do anything is no service worker support', function() {
sandbox.stub(window, 'navigator').value({});
Expand Down