Skip to content

Commit d35dd55

Browse files
committed
Modify messaging tests to be enabled on Safari.
1 parent 5f38cfc commit d35dd55

12 files changed

+74
-32
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export class WindowController extends ControllerInterface {
7575
*/
7676
getToken(): Promise<string | null> {
7777
// Check that the required API's are available
78-
if (!this.isSupported_()) {
78+
if (!WindowController.isSupported_()) {
7979
return Promise.reject(
8080
errorFactory.create(ERROR_CODES.UNSUPPORTED_BROWSER)
8181
);
@@ -391,7 +391,7 @@ export class WindowController extends ControllerInterface {
391391
*/
392392
// Visible for testing
393393
// TODO: Make private
394-
isSupported_(): boolean {
394+
static isSupported_(): boolean {
395395
return (
396396
'serviceWorker' in navigator &&
397397
'PushManager' in window &&

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

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,10 @@ import { makeFakeApp } from './testing-utils/make-fake-app';
2929
import { makeFakeSubscription } from './testing-utils/make-fake-subscription';
3030
import { makeFakeSWReg } from './testing-utils/make-fake-sw-reg';
3131

32-
const FAKE_SUBSCRIPTION = makeFakeSubscription();
33-
const EXAMPLE_TOKEN_SAVE = {
34-
swScope: '/example-scope',
35-
vapidKey: base64ToArrayBuffer(
36-
'BNJxw7sCGkGLOUP2cawBaBXRuWZ3lw_PmQMgreLVVvX_b' +
37-
'4emEWVURkCF8fUTHEFe2xrEgTt5ilh5xD94v0pFe_I'
38-
),
39-
fcmSenderId: '1234567',
40-
fcmToken: 'qwerty',
41-
fcmPushSet: '7654321',
42-
endpoint: FAKE_SUBSCRIPTION.endpoint,
43-
auth: FAKE_SUBSCRIPTION.getKey('auth')!,
44-
p256dh: FAKE_SUBSCRIPTION.getKey('p256dh')!,
45-
createTime: Date.now()
46-
};
32+
import { describe } from './testing-utils/messaging-test-runner';
33+
34+
let FAKE_SUBSCRIPTION: PushSubscription;
35+
let EXAMPLE_TOKEN_SAVE: any;
4736

4837
describe('Firebase Messaging > *Controller.deleteToken()', () => {
4938
let sandbox: sinon.SinonSandbox;
@@ -71,6 +60,24 @@ describe('Firebase Messaging > *Controller.deleteToken()', () => {
7160
return registration;
7261
}
7362

63+
before(() => {
64+
FAKE_SUBSCRIPTION = makeFakeSubscription();
65+
EXAMPLE_TOKEN_SAVE = {
66+
swScope: '/example-scope',
67+
vapidKey: base64ToArrayBuffer(
68+
'BNJxw7sCGkGLOUP2cawBaBXRuWZ3lw_PmQMgreLVVvX_b' +
69+
'4emEWVURkCF8fUTHEFe2xrEgTt5ilh5xD94v0pFe_I'
70+
),
71+
fcmSenderId: '1234567',
72+
fcmToken: 'qwerty',
73+
fcmPushSet: '7654321',
74+
endpoint: FAKE_SUBSCRIPTION.endpoint,
75+
auth: FAKE_SUBSCRIPTION.getKey('auth')!,
76+
p256dh: FAKE_SUBSCRIPTION.getKey('p256dh')!,
77+
createTime: Date.now()
78+
};
79+
});
80+
7481
beforeEach(() => {
7582
sandbox = sinon.sandbox.create();
7683
app = makeFakeApp({

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ describe('Firebase Messaging > *Controller.getToken()', () => {
132132

133133
it('should throw on unsupported browsers', () => {
134134
sandbox
135-
.stub(WindowController.prototype, 'isSupported_')
135+
.stub(WindowController, 'isSupported_')
136136
.callsFake(() => false);
137137

138138
const messagingService = new WindowController(app);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ import { VapidDetailsModel } from '../src/models/vapid-details-model';
2929
import { makeFakeApp } from './testing-utils/make-fake-app';
3030
import { makeFakeSWReg } from './testing-utils/make-fake-sw-reg';
3131

32+
import { describe } from './testing-utils/messaging-test-runner';
33+
3234
const controllersToTest = [WindowController, SWController];
3335

3436
/**

packages/messaging/test/get-sw-reg.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import { ERROR_CODES } from '../src/models/errors';
2323
import { makeFakeApp } from './testing-utils/make-fake-app';
2424
import { makeFakeSWReg } from './testing-utils/make-fake-sw-reg';
2525

26+
import { describe } from './testing-utils/messaging-test-runner';
27+
2628
const EXAMPLE_SENDER_ID = '1234567890';
2729

2830
const app = makeFakeApp({

packages/messaging/test/iid-model.test.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ import { IIDModel } from '../src/models/iid-model';
2323
import { makeFakeSubscription } from './testing-utils/make-fake-subscription';
2424
import { fetchMock } from './testing-utils/mock-fetch';
2525

26+
import { describe } from './testing-utils/messaging-test-runner';
27+
2628
const fcmSenderId = '1234567';
2729
const fcmToken = 'qwerty';
2830
const fcmPushSet = '7654321';
29-
const subscription = makeFakeSubscription();
31+
let subscription: PushSubscription;
3032
// prettier-ignore
3133
const appPubKey = new Uint8Array([
3234
255, 237, 107, 177, 171, 78, 84, 131, 221, 231, 87, 188, 22, 232, 71, 15
@@ -35,6 +37,11 @@ const appPubKey = new Uint8Array([
3537
describe('Firebase Messaging > IIDModel', () => {
3638
let sandbox: sinon.SinonSandbox;
3739
let globalIIDModel: IIDModel;
40+
41+
before(() => {
42+
subscription = makeFakeSubscription();
43+
});
44+
3845
beforeEach(() => {
3946
sandbox = sinon.sandbox.create();
4047
globalIIDModel = new IIDModel();

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ import { base64ToArrayBuffer } from '../src/helpers/base64-to-array-buffer';
3333
import { DEFAULT_PUBLIC_VAPID_KEY } from '../src/models/fcm-details';
3434
import { VapidDetailsModel } from '../src/models/vapid-details-model';
3535

36+
import { describe } from './testing-utils/messaging-test-runner';
37+
3638
const VALID_VAPID_KEY =
3739
'BJzVfWqLoALJdgV20MYy6lrj0OfhmE16PI1qLIIYx2ZZL3FoQWJJL8L0rf7rS7tqd92j_3xN3fmejKK5Eb7yMYw';
3840

packages/messaging/test/testing-utils/make-fake-subscription.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,6 @@
1414
* limitations under the License.
1515
*/
1616

17-
/**
18-
* FakeSubscription Constructor.
19-
*/
20-
const fakeSubscription = ((() => {}) as any) as {
21-
new (): PushSubscription;
22-
};
23-
fakeSubscription.prototype = PushSubscription.prototype;
24-
2517
function stringToArrayBuffer(str: string): ArrayBuffer {
2618
// String char codes are 16 bits (See MDN).
2719
const arrayBuffer = new ArrayBuffer(str.length * 2);
@@ -38,6 +30,11 @@ function stringToArrayBuffer(str: string): ArrayBuffer {
3830
* @return Returns a fake subscription.
3931
*/
4032
export function makeFakeSubscription(options: any = {}): PushSubscription {
33+
const fakeSubscription = ((() => {}) as any) as {
34+
new (): PushSubscription;
35+
};
36+
fakeSubscription.prototype = PushSubscription.prototype;
37+
4138
const fakeSub = new fakeSubscription();
4239

4340
// Set endpoint

packages/messaging/test/testing-utils/make-fake-sw-reg.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@
1616

1717
import * as sinon from 'sinon';
1818

19-
const fakeRegistration = ((() => {}) as any) as {
20-
new (): ServiceWorkerRegistration;
21-
};
22-
fakeRegistration.prototype = ServiceWorkerRegistration.prototype;
23-
2419
export function makeFakeSWReg(
2520
selectedState?: string,
2621
desiredValue?: object
2722
): ServiceWorkerRegistration {
23+
const fakeRegistration = ((() => {}) as any) as {
24+
new (): ServiceWorkerRegistration;
25+
};
26+
fakeRegistration.prototype = ServiceWorkerRegistration.prototype;
27+
2828
const fakeReg: ServiceWorkerRegistration = new fakeRegistration();
2929
// No need to use a sandbox to stub this object as it should be a used
3030
// once per test.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
17+
import { WindowController } from '../../src/controllers/window-controller';
18+
19+
/** Runner for tests that require service worker functionality. */
20+
const runner = WindowController.isSupported_() ? describe : describe.skip;
21+
export { runner as describe };

0 commit comments

Comments
 (0)