-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
93 additions
and
20 deletions.
There are no files selected for viewing
49 changes: 32 additions & 17 deletions
49
projects/ngx-pwa-install/src/lib/ngx-pwa-install.component.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,40 @@ | ||
import { async, ComponentFixture, TestBed } from '@angular/core/testing'; | ||
|
||
import { createHostFactory, SpectatorHost } from '@ngneat/spectator'; | ||
import { NgxPwaInstallComponent } from './ngx-pwa-install.component'; | ||
import { fakeAsync, tick } from '@angular/core/testing'; | ||
|
||
describe('NgxPwaInstallComponent', () => { | ||
let component: NgxPwaInstallComponent; | ||
let fixture: ComponentFixture<NgxPwaInstallComponent>; | ||
function createEvent() { | ||
const event = new CustomEvent('beforeinstallprompt'); | ||
let userChoiceResolve; | ||
(event as any).userChoice = new Promise((resolve) => userChoiceResolve = resolve); | ||
(event as any).prompt = () => { | ||
userChoiceResolve({outcome: 'accepted'}); | ||
}; | ||
return event; | ||
} | ||
|
||
beforeEach(async(() => { | ||
TestBed.configureTestingModule({ | ||
declarations: [ NgxPwaInstallComponent ] | ||
}) | ||
.compileComponents(); | ||
})); | ||
describe('NgxPwaInstallComponent', () => { | ||
let spectator: SpectatorHost<NgxPwaInstallComponent>; | ||
const createHost = createHostFactory(NgxPwaInstallComponent); | ||
|
||
beforeEach(() => { | ||
fixture = TestBed.createComponent(NgxPwaInstallComponent); | ||
component = fixture.componentInstance; | ||
fixture.detectChanges(); | ||
spectator = createHost(`<ngx-pwa-install><button class="install-me">Install me</button></ngx-pwa-install>`); | ||
}); | ||
|
||
it('should create', () => { | ||
expect(component).toBeTruthy(); | ||
it('should not display content until BeforeInstallPromptEvent fired', () => { | ||
spectator.detectChanges(); | ||
expect(spectator.query('.install-me')).toBeNull(); | ||
}); | ||
it('should display content when BeforeInstallPromptEvent fired', () => { | ||
window.dispatchEvent(createEvent()); | ||
spectator.detectChanges(); | ||
expect(spectator.query('.install-me')).not.toBeNull(); | ||
}); | ||
it('should hide content when .install() called', fakeAsync(() => { | ||
const event = createEvent(); | ||
window.dispatchEvent(event); | ||
spectator.detectChanges(); | ||
spectator.component.install(); | ||
tick(0); | ||
spectator.detectChanges(); | ||
expect(spectator.query('.install-me')).toBeNull(); | ||
})); | ||
}); |
59 changes: 59 additions & 0 deletions
59
projects/ngx-pwa-install/src/lib/ngx-pwa-install.module.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { Component, ModuleWithProviders, NgModule, Type } from '@angular/core'; | ||
import { createComponentFactory, Spectator } from '@ngneat/spectator'; | ||
import { BeforeInstallPrompt, NgxPwaInstallModule } from '../public-api'; | ||
|
||
@Component({ | ||
template: '' | ||
}) | ||
class FakeComponent { | ||
} | ||
|
||
function configureComponentFactory( | ||
targetModule: ModuleWithProviders<NgxPwaInstallModule> | Type<any>, | ||
beforeInstallPromptFactory: () => Promise<void>) { | ||
return createComponentFactory({ | ||
component: FakeComponent, | ||
imports: [targetModule], | ||
providers: [ | ||
{provide: BeforeInstallPrompt, useFactory: beforeInstallPromptFactory} | ||
] | ||
}); | ||
} | ||
|
||
describe('NgxPwaInstallModule', () => { | ||
let spectator: Spectator<FakeComponent>; | ||
let spyInit: jasmine.Spy; | ||
const beforeInstallPromptProvider = { | ||
init() { | ||
return Promise.resolve(); | ||
} | ||
}; | ||
const beforeInstallPromptFactory = () => beforeInstallPromptProvider.init(); | ||
|
||
describe('without .forRoot()', () => { | ||
const createComponent = configureComponentFactory(NgxPwaInstallModule, beforeInstallPromptFactory); | ||
|
||
beforeEach(() => { | ||
spyInit = spyOn(beforeInstallPromptProvider, 'init'); | ||
spectator = createComponent(); | ||
}); | ||
|
||
it('should not setup window event listener', () => { | ||
expect(spyInit).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
describe('with .forRoot()', () => { | ||
const targetModule = NgxPwaInstallModule.forRoot(); | ||
const createComponent = configureComponentFactory(targetModule, beforeInstallPromptFactory); | ||
|
||
beforeEach(() => { | ||
spyInit = spyOn(beforeInstallPromptProvider, 'init'); | ||
spectator = createComponent(); | ||
}); | ||
|
||
it('should setup window event listener', () => { | ||
expect(spyInit).toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters