Skip to content
Open
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
5 changes: 4 additions & 1 deletion src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ export function ɵgetDefaultInstanceOf<T= unknown>(identifier: string, provided:
if (provided) {
// Was provide* only called once? If so grab that
if (provided.length === 1) { return provided[0]; }
const providedUsingDefaultApp = provided.filter((it: any) => it.app === defaultApp);

// An entry is null where its product is unavailable, such as messaging on the server.
const providedUsingDefaultApp = provided.filter((it: any) => it?.app === defaultApp);

// Was provide* only called once, using the default app? If so use that
if (providedUsingDefaultApp.length === 1) { return providedUsingDefaultApp[0]; }
}
Expand Down
12 changes: 10 additions & 2 deletions src/messaging/messaging.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,16 @@ export function defaultMessagingInstanceFactory(provided: FirebaseMessaging[]|un
export function messagingInstanceFactory(fn: (injector: Injector) => FirebaseMessaging) {
return (zone: NgZone, injector: Injector, platformId: object) => {
if (isPlatformServer(platformId)) { return null; }
const messaging = zone.runOutsideAngular(() => fn(injector));
return new Messaging(messaging);

try {
const messaging = zone.runOutsideAngular(() => fn(injector));
return new Messaging(messaging);
} catch (error) {
// Firebase's `getMessaging` throws where `navigator.serviceWorker` is missing.
if ('serviceWorker' in navigator) { throw error; }

return null;
}
};
}

Expand Down
75 changes: 75 additions & 0 deletions src/messaging/messaging.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,79 @@ describe('Messaging', () => {

});

describe('when the factory throws', () => {

it('should rethrow in a browser with service workers', () => {
TestBed.configureTestingModule({
providers: [
provideFirebaseApp(() => initializeApp(COMMON_CONFIG, rando())),
provideMessaging(() => { throw new Error('thrown by the factory'); }),
],
});

expect(() => TestBed.inject(Messaging)).toThrowError('thrown by the factory');
});

});

describe('in a browser without service workers', () => {
let serviceWorkerProperty: PropertyDescriptor | undefined;

beforeEach(() => {
// A page that is not a secure context has no `serviceWorker` property at all.
serviceWorkerProperty = Object.getOwnPropertyDescriptor(Navigator.prototype, 'serviceWorker');
Reflect.deleteProperty(Navigator.prototype, 'serviceWorker');

appName = rando();
});

afterEach(() => {
if (serviceWorkerProperty) {
Object.defineProperty(Navigator.prototype, 'serviceWorker', serviceWorkerProperty);
}
});

it('should inject null where getMessaging throws', () => {
TestBed.configureTestingModule({
providers: [
provideFirebaseApp(() => initializeApp(COMMON_CONFIG, appName)),
provideMessaging(() => getMessaging(getApp(appName))),
],
});

expect('serviceWorker' in navigator).toBeFalse();
expect(TestBed.inject(Messaging)).toBeNull();
});

it('should inject what the factory returns when it does not throw', () => {
let returnedByFactory: Messaging | undefined;

TestBed.configureTestingModule({
providers: [
provideFirebaseApp(() => initializeApp(COMMON_CONFIG, appName)),
provideMessaging(() => {
returnedByFactory = { app: getApp(appName) };
return returnedByFactory;
}),
],
});

expect(TestBed.inject(Messaging)).toBe(returnedByFactory);
});

it('should inject null with two registrations', () => {
TestBed.configureTestingModule({
providers: [
provideFirebaseApp(() => initializeApp(COMMON_CONFIG)),
provideMessaging(() => getMessaging()),
provideFirebaseApp(() => initializeApp(COMMON_CONFIG, appName)),
provideMessaging(() => getMessaging(getApp(appName))),
],
});

expect(TestBed.inject(Messaging)).toBeNull();
});

});

});
Loading