Skip to content

Tidy firebase-messaging Tests #232

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 2 commits into from
Oct 18, 2017
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
1 change: 1 addition & 0 deletions packages/messaging/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"scripts": {
"dev": "gulp dev",
"test": "karma start --single-run",
"test:debug": "karma start --browsers=Chrome --auto-watch",
"prepare": "gulp build"
},
"license": "Apache-2.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/messaging/src/controllers/controller-interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ export default class ControllerInterface {
* It closes any currently open indexdb database connections.
*/
delete() {
this.tokenManager_.closeDatabase();
return this.tokenManager_.closeDatabase();
}

/**
Expand Down
5 changes: 3 additions & 2 deletions packages/messaging/src/models/token-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ import Errors from './errors';
import arrayBufferToBase64 from '../helpers/array-buffer-to-base64';
import FCMDetails from './fcm-details';

const FCM_TOKEN_DETAILS_DB = 'fcm_token_details_db';
const FCM_TOKEN_OBJ_STORE = 'fcm_token_object_Store';
const FCM_TOKEN_DETAILS_DB_VERSION = 1;

export default class TokenManager {
private errorFactory_: ErrorFactory<string>;
private openDbPromise_: Promise<IDBDatabase>;

static DB_NAME: 'fcm_token_details_db';

constructor() {
this.errorFactory_ = new ErrorFactory('messaging', 'Messaging', Errors.map);
this.openDbPromise_ = null;
Expand All @@ -46,7 +47,7 @@ export default class TokenManager {

this.openDbPromise_ = new Promise((resolve, reject) => {
const request = indexedDB.open(
FCM_TOKEN_DETAILS_DB,
TokenManager.DB_NAME,
FCM_TOKEN_DETAILS_DB_VERSION
);
request.onerror = event => {
Expand Down
189 changes: 189 additions & 0 deletions packages/messaging/test/controller-deleteToken.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
/**
* Copyright 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { assert } from 'chai';
import * as sinon from 'sinon';
import makeFakeApp from './make-fake-app';
import makeFakeSWReg from './make-fake-sw-reg';
import dbTMHelper from './testing-utils/db-token-manager';
import { deleteDatabase } from './testing-utils/db-helper';
import Errors from '../src/models/errors';
import TokenManager from '../src/models/token-manager';
import WindowController from '../src/controllers/window-controller';
import SWController from '../src/controllers/sw-controller';

const EXAMPLE_TOKEN_SAVE = {
fcmToken: 'ExampleFCMToken1337',
fcmSenderId: '1234567890',
endpoint: 'https://example.google.com/1234',
swScope: 'firebase-messaging-sw-scope',
auth: '12345',
p256dh: '123456789098765642421'
};

describe('Firebase Messaging > *Controller.deleteToken()', function() {
const sandbox = sinon.sandbox.create();

const app = makeFakeApp({
messagingSenderId: EXAMPLE_TOKEN_SAVE.fcmSenderId
});

const configureRegistrationMocks = (ServiceClass, fakeReg) => {
sandbox.stub(
ServiceClass.prototype,
'getSWRegistration_'
).callsFake(() => {
return fakeReg;
});
};

const generateFakeReg = getSubResult => {
const registration = makeFakeSWReg();
Object.defineProperty(registration, 'pushManager', {
value: {
getSubscription: () => {
if (typeof getSubResult === 'function') {
return getSubResult();
}

return getSubResult;
},
}
});
return Promise.resolve(registration);
};

let globalMessagingService;
const cleanUp = () => {
sandbox.restore();

const deletePromises = [dbTMHelper.closeDatabase()];
if (globalMessagingService) {
deletePromises.push(globalMessagingService.delete());
}
return Promise.all(deletePromises)
.then(() => deleteDatabase(TokenManager.DB_NAME))
.then(() => globalMessagingService = null);
}

beforeEach(function() {
return cleanUp();
});

after(function() {
return cleanUp();
});

it('should handle no token to delete', function() {
globalMessagingService = new WindowController(app);
return globalMessagingService.deleteToken()
.then(
() => {
throw new Error('Expected error to be thrown.');
},
err => {
assert.equal(
'messaging/' + Errors.codes.INVALID_DELETE_TOKEN,
err.code
);
}
);
});

it('should handle no registration', function() {
configureRegistrationMocks(WindowController, Promise.resolve(null));

return dbTMHelper.addObjectToIndexDB(EXAMPLE_TOKEN_SAVE)
.then(() => {
globalMessagingService = new WindowController(app);
return globalMessagingService.deleteToken(EXAMPLE_TOKEN_SAVE.fcmToken);
});
});

it('should handle get subscription error', function() {
configureRegistrationMocks(WindowController,
generateFakeReg(() => Promise.reject(new Error('Unknown error')))
);

dbTMHelper.addObjectToIndexDB(EXAMPLE_TOKEN_SAVE);

globalMessagingService = new WindowController(app);
return globalMessagingService.deleteToken(EXAMPLE_TOKEN_SAVE.fcmToken).then(
() => {
throw new Error('Expected this to reject');
},
err => {
assert.equal('Unknown error', err.message);
}
);
});

[WindowController, SWController].forEach((ServiceClass) => {
it(`should handle null getSubscription() ${ServiceClass.name}`, function() {
configureRegistrationMocks(ServiceClass, generateFakeReg(Promise.resolve(null)));

return dbTMHelper.addObjectToIndexDB(EXAMPLE_TOKEN_SAVE)
.then(() => {
globalMessagingService = new ServiceClass(app);
return globalMessagingService.deleteToken(EXAMPLE_TOKEN_SAVE.fcmToken);
});
});

it(`should handle error on unsubscribe ${ServiceClass.name}`, function() {
const errorMsg = 'unsubscribe-error-1234567890';
const fakeSubscription = {
endpoint: EXAMPLE_TOKEN_SAVE.endpoint,
unsubscribe: () => Promise.reject(new Error(errorMsg))
};

configureRegistrationMocks(
ServiceClass,
generateFakeReg(Promise.resolve(fakeSubscription))
);

return dbTMHelper.addObjectToIndexDB(EXAMPLE_TOKEN_SAVE)
.then(() => {
globalMessagingService = new ServiceClass(app);
return globalMessagingService.deleteToken(EXAMPLE_TOKEN_SAVE.fcmToken)
.then(
() => {
throw new Error('Expected this to reject');
},
err => {
assert.equal(errorMsg, err.message);
}
);
});
});

it(`should delete with valid unsubscribe ${ServiceClass.name}`, function() {
const fakeSubscription = {
endpoint: EXAMPLE_TOKEN_SAVE.endpoint,
unsubscribe: () => Promise.resolve()
};

configureRegistrationMocks(
ServiceClass,
generateFakeReg(Promise.resolve(fakeSubscription))
);

return dbTMHelper.addObjectToIndexDB(EXAMPLE_TOKEN_SAVE)
.then(() => {
globalMessagingService = new ServiceClass(app);
return globalMessagingService.deleteToken(EXAMPLE_TOKEN_SAVE.fcmToken);
});
});
});
});
Loading