Skip to content

Commit 0a6ad04

Browse files
Matt Gauntjshcrowthe
authored andcommitted
Tidy firebase-messaging Tests (#232)
* Tidied tests to more reliably close and delete indexedDB databases before and after each test * Update package.json
1 parent 3ca20c7 commit 0a6ad04

18 files changed

+474
-487
lines changed

packages/messaging/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"scripts": {
88
"dev": "gulp dev",
99
"test": "karma start --single-run",
10+
"test:debug": "karma start --browsers=Chrome --auto-watch",
1011
"prepare": "gulp build"
1112
},
1213
"license": "Apache-2.0",

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ export default class ControllerInterface {
177177
* It closes any currently open indexdb database connections.
178178
*/
179179
delete() {
180-
this.tokenManager_.closeDatabase();
180+
return this.tokenManager_.closeDatabase();
181181
}
182182

183183
/**

packages/messaging/src/models/token-manager.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@ import Errors from './errors';
2121
import arrayBufferToBase64 from '../helpers/array-buffer-to-base64';
2222
import FCMDetails from './fcm-details';
2323

24-
const FCM_TOKEN_DETAILS_DB = 'fcm_token_details_db';
2524
const FCM_TOKEN_OBJ_STORE = 'fcm_token_object_Store';
2625
const FCM_TOKEN_DETAILS_DB_VERSION = 1;
2726

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

31+
static DB_NAME: 'fcm_token_details_db';
32+
3233
constructor() {
3334
this.errorFactory_ = new ErrorFactory('messaging', 'Messaging', Errors.map);
3435
this.openDbPromise_ = null;
@@ -46,7 +47,7 @@ export default class TokenManager {
4647

4748
this.openDbPromise_ = new Promise((resolve, reject) => {
4849
const request = indexedDB.open(
49-
FCM_TOKEN_DETAILS_DB,
50+
TokenManager.DB_NAME,
5051
FCM_TOKEN_DETAILS_DB_VERSION
5152
);
5253
request.onerror = event => {
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
/**
2+
* Copyright 2017 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
import { assert } from 'chai';
17+
import * as sinon from 'sinon';
18+
import makeFakeApp from './make-fake-app';
19+
import makeFakeSWReg from './make-fake-sw-reg';
20+
import dbTMHelper from './testing-utils/db-token-manager';
21+
import { deleteDatabase } from './testing-utils/db-helper';
22+
import Errors from '../src/models/errors';
23+
import TokenManager from '../src/models/token-manager';
24+
import WindowController from '../src/controllers/window-controller';
25+
import SWController from '../src/controllers/sw-controller';
26+
27+
const EXAMPLE_TOKEN_SAVE = {
28+
fcmToken: 'ExampleFCMToken1337',
29+
fcmSenderId: '1234567890',
30+
endpoint: 'https://example.google.com/1234',
31+
swScope: 'firebase-messaging-sw-scope',
32+
auth: '12345',
33+
p256dh: '123456789098765642421'
34+
};
35+
36+
describe('Firebase Messaging > *Controller.deleteToken()', function() {
37+
const sandbox = sinon.sandbox.create();
38+
39+
const app = makeFakeApp({
40+
messagingSenderId: EXAMPLE_TOKEN_SAVE.fcmSenderId
41+
});
42+
43+
const configureRegistrationMocks = (ServiceClass, fakeReg) => {
44+
sandbox.stub(
45+
ServiceClass.prototype,
46+
'getSWRegistration_'
47+
).callsFake(() => {
48+
return fakeReg;
49+
});
50+
};
51+
52+
const generateFakeReg = getSubResult => {
53+
const registration = makeFakeSWReg();
54+
Object.defineProperty(registration, 'pushManager', {
55+
value: {
56+
getSubscription: () => {
57+
if (typeof getSubResult === 'function') {
58+
return getSubResult();
59+
}
60+
61+
return getSubResult;
62+
},
63+
}
64+
});
65+
return Promise.resolve(registration);
66+
};
67+
68+
let globalMessagingService;
69+
const cleanUp = () => {
70+
sandbox.restore();
71+
72+
const deletePromises = [dbTMHelper.closeDatabase()];
73+
if (globalMessagingService) {
74+
deletePromises.push(globalMessagingService.delete());
75+
}
76+
return Promise.all(deletePromises)
77+
.then(() => deleteDatabase(TokenManager.DB_NAME))
78+
.then(() => globalMessagingService = null);
79+
}
80+
81+
beforeEach(function() {
82+
return cleanUp();
83+
});
84+
85+
after(function() {
86+
return cleanUp();
87+
});
88+
89+
it('should handle no token to delete', function() {
90+
globalMessagingService = new WindowController(app);
91+
return globalMessagingService.deleteToken()
92+
.then(
93+
() => {
94+
throw new Error('Expected error to be thrown.');
95+
},
96+
err => {
97+
assert.equal(
98+
'messaging/' + Errors.codes.INVALID_DELETE_TOKEN,
99+
err.code
100+
);
101+
}
102+
);
103+
});
104+
105+
it('should handle no registration', function() {
106+
configureRegistrationMocks(WindowController, Promise.resolve(null));
107+
108+
return dbTMHelper.addObjectToIndexDB(EXAMPLE_TOKEN_SAVE)
109+
.then(() => {
110+
globalMessagingService = new WindowController(app);
111+
return globalMessagingService.deleteToken(EXAMPLE_TOKEN_SAVE.fcmToken);
112+
});
113+
});
114+
115+
it('should handle get subscription error', function() {
116+
configureRegistrationMocks(WindowController,
117+
generateFakeReg(() => Promise.reject(new Error('Unknown error')))
118+
);
119+
120+
dbTMHelper.addObjectToIndexDB(EXAMPLE_TOKEN_SAVE);
121+
122+
globalMessagingService = new WindowController(app);
123+
return globalMessagingService.deleteToken(EXAMPLE_TOKEN_SAVE.fcmToken).then(
124+
() => {
125+
throw new Error('Expected this to reject');
126+
},
127+
err => {
128+
assert.equal('Unknown error', err.message);
129+
}
130+
);
131+
});
132+
133+
[WindowController, SWController].forEach((ServiceClass) => {
134+
it(`should handle null getSubscription() ${ServiceClass.name}`, function() {
135+
configureRegistrationMocks(ServiceClass, generateFakeReg(Promise.resolve(null)));
136+
137+
return dbTMHelper.addObjectToIndexDB(EXAMPLE_TOKEN_SAVE)
138+
.then(() => {
139+
globalMessagingService = new ServiceClass(app);
140+
return globalMessagingService.deleteToken(EXAMPLE_TOKEN_SAVE.fcmToken);
141+
});
142+
});
143+
144+
it(`should handle error on unsubscribe ${ServiceClass.name}`, function() {
145+
const errorMsg = 'unsubscribe-error-1234567890';
146+
const fakeSubscription = {
147+
endpoint: EXAMPLE_TOKEN_SAVE.endpoint,
148+
unsubscribe: () => Promise.reject(new Error(errorMsg))
149+
};
150+
151+
configureRegistrationMocks(
152+
ServiceClass,
153+
generateFakeReg(Promise.resolve(fakeSubscription))
154+
);
155+
156+
return dbTMHelper.addObjectToIndexDB(EXAMPLE_TOKEN_SAVE)
157+
.then(() => {
158+
globalMessagingService = new ServiceClass(app);
159+
return globalMessagingService.deleteToken(EXAMPLE_TOKEN_SAVE.fcmToken)
160+
.then(
161+
() => {
162+
throw new Error('Expected this to reject');
163+
},
164+
err => {
165+
assert.equal(errorMsg, err.message);
166+
}
167+
);
168+
});
169+
});
170+
171+
it(`should delete with valid unsubscribe ${ServiceClass.name}`, function() {
172+
const fakeSubscription = {
173+
endpoint: EXAMPLE_TOKEN_SAVE.endpoint,
174+
unsubscribe: () => Promise.resolve()
175+
};
176+
177+
configureRegistrationMocks(
178+
ServiceClass,
179+
generateFakeReg(Promise.resolve(fakeSubscription))
180+
);
181+
182+
return dbTMHelper.addObjectToIndexDB(EXAMPLE_TOKEN_SAVE)
183+
.then(() => {
184+
globalMessagingService = new ServiceClass(app);
185+
return globalMessagingService.deleteToken(EXAMPLE_TOKEN_SAVE.fcmToken);
186+
});
187+
});
188+
});
189+
});

0 commit comments

Comments
 (0)