From be28a6ad8e55141a4dab7b016be74889e470422e Mon Sep 17 00:00:00 2001 From: George Kalpakas Date: Thu, 25 Apr 2019 16:51:08 +0300 Subject: [PATCH] test(service-worker): expand `SwRegistrationOptions` tests and move to separate file (#21842) PR Close #21842 --- packages/service-worker/test/comm_spec.ts | 41 +------- packages/service-worker/test/module_spec.ts | 109 ++++++++++++++++++++ 2 files changed, 110 insertions(+), 40 deletions(-) create mode 100644 packages/service-worker/test/module_spec.ts diff --git a/packages/service-worker/test/comm_spec.ts b/packages/service-worker/test/comm_spec.ts index 3e313bc445..09b9af248e 100644 --- a/packages/service-worker/test/comm_spec.ts +++ b/packages/service-worker/test/comm_spec.ts @@ -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 {ServiceWorkerModule, SwRegistrationOptions, ngswCommChannelFactory} from '@angular/service-worker/src/module'; +import {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,45 +47,6 @@ 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({ diff --git a/packages/service-worker/test/module_spec.ts b/packages/service-worker/test/module_spec.ts new file mode 100644 index 0000000000..14e1e86b09 --- /dev/null +++ b/packages/service-worker/test/module_spec.ts @@ -0,0 +1,109 @@ +/** + * @license + * Copyright Google Inc. All Rights Reserved. + * + * Use of this source code is governed by an MIT-style license that can be + * found in the LICENSE file at https://angular.io/license + */ + +import {ApplicationRef, PLATFORM_ID} from '@angular/core'; +import {TestBed} from '@angular/core/testing'; +import {filter, take} from 'rxjs/operators'; + +import {ServiceWorkerModule, SwRegistrationOptions} from '../src/module'; +import {SwUpdate} from '../src/update'; + + +describe('ServiceWorkerModule', () => { + // Skip environments that don't support the minimum APIs needed to run these SW tests. + if ((typeof navigator === 'undefined') || (typeof navigator.serviceWorker === 'undefined')) { + return; + } + + let swRegisterSpy: jasmine.Spy; + + beforeEach(() => swRegisterSpy = spyOn(navigator.serviceWorker, 'register')); + + describe('register()', () => { + const configTestBed = async(opts: SwRegistrationOptions) => { + TestBed.configureTestingModule({ + imports: [ServiceWorkerModule.register('sw.js', opts)], + providers: [{provide: PLATFORM_ID, useValue: 'browser'}], + }); + + const appRef: ApplicationRef = TestBed.get(ApplicationRef); + await appRef.isStable.pipe(filter(Boolean), take(1)).toPromise(); + }; + + it('sets the registration options', async() => { + await configTestBed({enabled: true, scope: 'foo'}); + + expect(TestBed.get(SwRegistrationOptions)).toEqual({enabled: true, scope: 'foo'}); + expect(swRegisterSpy).toHaveBeenCalledWith('sw.js', {scope: 'foo'}); + }); + + it('can disable the SW', async() => { + await configTestBed({enabled: false}); + + expect(TestBed.get(SwUpdate).isEnabled).toBe(false); + expect(swRegisterSpy).not.toHaveBeenCalled(); + }); + + it('can enable the SW', async() => { + await configTestBed({enabled: true}); + + expect(TestBed.get(SwUpdate).isEnabled).toBe(true); + expect(swRegisterSpy).toHaveBeenCalledWith('sw.js', {scope: undefined}); + }); + + it('defaults to enabling the SW', async() => { + await configTestBed({}); + expect(TestBed.get(SwUpdate).isEnabled).toBe(true); + expect(swRegisterSpy).toHaveBeenCalledWith('sw.js', {scope: undefined}); + }); + }); + + describe('SwRegistrationOptions', () => { + const configTestBed = + async(providerOpts: SwRegistrationOptions, staticOpts?: SwRegistrationOptions) => { + TestBed.configureTestingModule({ + imports: [ServiceWorkerModule.register('sw.js', staticOpts || {scope: 'static'})], + providers: [ + {provide: PLATFORM_ID, useValue: 'browser'}, + {provide: SwRegistrationOptions, useFactory: () => providerOpts}, + ], + }); + + const appRef: ApplicationRef = TestBed.get(ApplicationRef); + await appRef.isStable.pipe(filter(Boolean), take(1)).toPromise(); + }; + + it('sets the registration options (and overwrites those set via `.register()`', async() => { + await configTestBed({enabled: true, scope: 'provider'}); + + expect(TestBed.get(SwRegistrationOptions)).toEqual({enabled: true, scope: 'provider'}); + expect(swRegisterSpy).toHaveBeenCalledWith('sw.js', {scope: 'provider'}); + }); + + it('can disable the SW', async() => { + await configTestBed({enabled: false}, {enabled: true}); + + expect(TestBed.get(SwUpdate).isEnabled).toBe(false); + expect(swRegisterSpy).not.toHaveBeenCalled(); + }); + + it('can enable the SW', async() => { + await configTestBed({enabled: true}, {enabled: false}); + + expect(TestBed.get(SwUpdate).isEnabled).toBe(true); + expect(swRegisterSpy).toHaveBeenCalledWith('sw.js', {scope: undefined}); + }); + + it('defaults to enabling the SW', async() => { + await configTestBed({}, {enabled: false}); + + expect(TestBed.get(SwUpdate).isEnabled).toBe(true); + expect(swRegisterSpy).toHaveBeenCalledWith('sw.js', {scope: undefined}); + }); + }); +});