feat(service-worker): expose SwRegistrationOptions
token to allow runtime config (#21842)
Previously, the ServiceWorker registration options should be defined as an object literal (in order for them to be compatible with Ahead-of-Time compilation), thus making it impossible to base the ServiceWorker behavior on runtime conditions. This commit allows specifying the registration options using a regular provider, which means that it can take advantage of the `useFactory` option to determine the config at runtime, while still remaining compatible with AoT compilation. PR Close #21842
This commit is contained in:
@ -9,7 +9,7 @@
|
||||
import {PLATFORM_ID} from '@angular/core';
|
||||
import {TestBed} from '@angular/core/testing';
|
||||
import {NgswCommChannel} from '@angular/service-worker/src/low_level';
|
||||
import {SwRegistrationOptions, ngswCommChannelFactory} from '@angular/service-worker/src/module';
|
||||
import {ServiceWorkerModule, SwRegistrationOptions, ngswCommChannelFactory} from '@angular/service-worker/src/module';
|
||||
import {SwPush} from '@angular/service-worker/src/push';
|
||||
import {SwUpdate} from '@angular/service-worker/src/update';
|
||||
import {MockPushManager, MockPushSubscription, MockServiceWorkerContainer, MockServiceWorkerRegistration, patchDecodeBase64} from '@angular/service-worker/testing/mock';
|
||||
@ -47,6 +47,45 @@ import {async_fit, async_it} from './async';
|
||||
});
|
||||
});
|
||||
|
||||
describe('ServiceWorkerModule config', () => {
|
||||
it('SwUpdate isEnabled is false when configuring via static method', () => {
|
||||
TestBed.configureTestingModule(
|
||||
{imports: [ServiceWorkerModule.register('', {enabled: false})]});
|
||||
|
||||
expect(TestBed.get(SwUpdate).isEnabled).toEqual(false);
|
||||
});
|
||||
|
||||
it('SwUpdate isEnabled is true when configuring via static method', () => {
|
||||
TestBed.configureTestingModule({
|
||||
imports: [ServiceWorkerModule.register('', {enabled: true})],
|
||||
providers: [{provide: NgswCommChannel, useValue: comm}]
|
||||
});
|
||||
|
||||
expect(TestBed.get(SwUpdate).isEnabled).toEqual(true);
|
||||
});
|
||||
|
||||
it('SwUpdate isEnabled is false when configuring directly via token', () => {
|
||||
TestBed.configureTestingModule({
|
||||
imports: [ServiceWorkerModule.register('')],
|
||||
providers: [{provide: SwRegistrationOptions, useFactory: () => ({enabled: false})}]
|
||||
});
|
||||
|
||||
expect(TestBed.get(SwUpdate).isEnabled).toEqual(false);
|
||||
});
|
||||
|
||||
it('SwUpdate isEnabled is true when configuring directly via token', () => {
|
||||
TestBed.configureTestingModule({
|
||||
imports: [ServiceWorkerModule.register('')],
|
||||
providers: [
|
||||
{provide: NgswCommChannel, useValue: comm},
|
||||
{provide: SwRegistrationOptions, useFactory: () => ({enabled: true})}
|
||||
]
|
||||
});
|
||||
|
||||
expect(TestBed.get(SwUpdate).isEnabled).toEqual(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('ngswCommChannelFactory', () => {
|
||||
it('gives disabled NgswCommChannel for platform-server', () => {
|
||||
TestBed.configureTestingModule({
|
||||
|
Reference in New Issue
Block a user