-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: rework lazy-loading for Angular 9
- Loading branch information
Showing
16 changed files
with
307 additions
and
267 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
29 changes: 4 additions & 25 deletions
29
src/app/extensions/captcha/exports/captcha-exports.module.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,35 +1,14 @@ | ||
import { CommonModule } from '@angular/common'; | ||
import { NgModule } from '@angular/core'; | ||
import { ReactiveComponentLoaderModule } from '@wishtack/reactive-component-loader'; | ||
import { take } from 'rxjs/operators'; | ||
|
||
import { whenTruthy } from 'ish-core/utils/operators'; | ||
|
||
import { CaptchaFacade } from '../facades/captcha.facade'; | ||
import { SitekeyProviderService } from '../services/sitekey-provider/sitekey-provider.service'; | ||
|
||
import { LazyCaptchaComponent } from './captcha/lazy-captcha/lazy-captcha.component'; | ||
|
||
@NgModule({ | ||
imports: [ | ||
CommonModule, | ||
ReactiveComponentLoaderModule.withModule({ | ||
moduleId: 'ish-extensions-captcha', | ||
loadChildren: '../captcha.module#CaptchaModule', | ||
}), | ||
], | ||
imports: [CommonModule], | ||
declarations: [LazyCaptchaComponent], | ||
exports: [LazyCaptchaComponent], | ||
providers: [SitekeyProviderService], | ||
}) | ||
export class CaptchaExportsModule { | ||
siteKey: string; | ||
|
||
constructor(captchaFacade: CaptchaFacade) { | ||
// synchronize asynchronous site key so we can provide it synchronously for the recaptcha service later on. | ||
captchaFacade.captchaSiteKey$ | ||
.pipe( | ||
whenTruthy(), | ||
take(1) | ||
) | ||
.subscribe(siteKey => (this.siteKey = siteKey)); | ||
} | ||
} | ||
export class CaptchaExportsModule {} |
9 changes: 1 addition & 8 deletions
9
src/app/extensions/captcha/exports/captcha/lazy-captcha/lazy-captcha.component.html
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,8 +1 @@ | ||
<wt-lazy | ||
*ngIf="captchaActive$ | async" | ||
[location]="componentLocation" | ||
[inputs]="{ | ||
form: form, | ||
cssClass: cssClass | ||
}" | ||
></wt-lazy> | ||
<ng-template #anchor></ng-template> |
113 changes: 113 additions & 0 deletions
113
src/app/extensions/captcha/exports/captcha/lazy-captcha/lazy-captcha.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 |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import { ComponentFixture, TestBed, async, fakeAsync, tick } from '@angular/core/testing'; | ||
import { FormControl, FormGroup } from '@angular/forms'; | ||
import { By } from '@angular/platform-browser'; | ||
import { RouterTestingModule } from '@angular/router/testing'; | ||
import { TranslateModule } from '@ngx-translate/core'; | ||
import { MockComponent } from 'ng-mocks'; | ||
import { RECAPTCHA_V3_SITE_KEY, RecaptchaComponent } from 'ng-recaptcha'; | ||
import { EMPTY, of } from 'rxjs'; | ||
import { anyString, instance, mock, when } from 'ts-mockito'; | ||
|
||
import { configurationReducer } from 'ish-core/store/configuration/configuration.reducer'; | ||
import { ngrxTesting } from 'ish-core/utils/dev/ngrx-testing'; | ||
|
||
import { CaptchaFacade } from '../../../facades/captcha.facade'; | ||
import { CaptchaV2Component, CaptchaV2ComponentModule } from '../../../shared/captcha/captcha-v2/captcha-v2.component'; | ||
import { CaptchaV3Component, CaptchaV3ComponentModule } from '../../../shared/captcha/captcha-v3/captcha-v3.component'; | ||
|
||
import { LazyCaptchaComponent } from './lazy-captcha.component'; | ||
|
||
describe('Lazy Captcha Component', () => { | ||
let fixture: ComponentFixture<LazyCaptchaComponent>; | ||
let component: LazyCaptchaComponent; | ||
let element: HTMLElement; | ||
let captchaFacade: CaptchaFacade; | ||
|
||
beforeEach(async(() => { | ||
captchaFacade = mock(CaptchaFacade); | ||
when(captchaFacade.captchaVersion$).thenReturn(EMPTY); | ||
when(captchaFacade.captchaSiteKey$).thenReturn(of('captchaSiteKeyASDF')); | ||
when(captchaFacade.captchaActive$(anyString())).thenReturn(of(true)); | ||
|
||
TestBed.configureTestingModule({ | ||
declarations: [MockComponent(RecaptchaComponent)], | ||
imports: [ | ||
CaptchaV2ComponentModule, | ||
CaptchaV3ComponentModule, | ||
RouterTestingModule, | ||
TranslateModule.forRoot(), | ||
ngrxTesting({ | ||
reducers: { configuration: configurationReducer }, | ||
}), | ||
], | ||
providers: [ | ||
{ provide: CaptchaFacade, useFactory: () => instance(captchaFacade) }, | ||
{ provide: RECAPTCHA_V3_SITE_KEY, useValue: 'captchaSiteKeyQWERTY' }, | ||
], | ||
}) | ||
.overrideModule(CaptchaV2ComponentModule, { set: { entryComponents: [CaptchaV2Component] } }) | ||
.overrideModule(CaptchaV3ComponentModule, { set: { entryComponents: [CaptchaV3Component] } }) | ||
// .overrideComponent(LazyCaptchaComponent, { set: { entryComponents: [CaptchaV2Component, CaptchaV3Component] } }) | ||
.compileComponents(); | ||
})); | ||
|
||
beforeEach(() => { | ||
fixture = TestBed.createComponent(LazyCaptchaComponent); | ||
component = fixture.componentInstance; | ||
element = fixture.nativeElement; | ||
component.form = new FormGroup({ | ||
captcha: new FormControl(''), | ||
captchaAction: new FormControl(''), | ||
}); | ||
component.cssClass = 'd-none'; | ||
component.topic = 'register'; | ||
}); | ||
|
||
it('should be created', () => { | ||
expect(component).toBeTruthy(); | ||
expect(element).toBeTruthy(); | ||
expect(() => fixture.detectChanges()).not.toThrow(); | ||
}); | ||
|
||
it('should render v2 component when configured', fakeAsync(() => { | ||
when(captchaFacade.captchaVersion$).thenReturn(of(2 as 2)); | ||
fixture.detectChanges(); | ||
|
||
tick(500); | ||
expect(element).toMatchInlineSnapshot(`<ish-captcha-v2></ish-captcha-v2>`); | ||
const v2Cmp: CaptchaV2Component = fixture.debugElement.query(By.css('ish-captcha-v2'))?.componentInstance; | ||
expect(v2Cmp).toBeTruthy(); | ||
expect(v2Cmp.cssClass).toEqual('d-none'); | ||
})); | ||
it('should render v3 component when configured', fakeAsync(() => { | ||
when(captchaFacade.captchaVersion$).thenReturn(of(3 as 3)); | ||
fixture.detectChanges(); | ||
|
||
tick(500); | ||
expect(element).toMatchInlineSnapshot(`<ish-captcha-v3></ish-captcha-v3>`); | ||
const v3Cmp: CaptchaV3Component = fixture.debugElement.query(By.css('ish-captcha-v3'))?.componentInstance; | ||
expect(v3Cmp).toBeTruthy(); | ||
})); | ||
|
||
// errors are thrown if required input parameters are missing | ||
it('should throw an error if there is no form set as input parameter', () => { | ||
component.form = undefined; | ||
expect(() => fixture.detectChanges()).toThrowErrorMatchingInlineSnapshot( | ||
`"required input parameter <form> is missing for FormElementComponent"` | ||
); | ||
}); | ||
|
||
it('should throw an error if there is no control "captcha" in the given form', () => { | ||
delete component.form.controls.captcha; | ||
expect(() => fixture.detectChanges()).toThrowErrorMatchingInlineSnapshot( | ||
`"form control 'captcha' does not exist in the given form for LazyCaptchaComponent"` | ||
); | ||
}); | ||
|
||
it('should throw an error if there is no control "captchaAction" in the given form', () => { | ||
delete component.form.controls.captchaAction; | ||
expect(() => fixture.detectChanges()).toThrowErrorMatchingInlineSnapshot( | ||
`"form control 'captchaAction' does not exist in the given form for LazyCaptchaComponent"` | ||
); | ||
}); | ||
}); |
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
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
21 changes: 21 additions & 0 deletions
21
src/app/extensions/captcha/services/sitekey-provider/sitekey-provider.service.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,21 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { take } from 'rxjs/operators'; | ||
|
||
import { whenTruthy } from 'ish-core/utils/operators'; | ||
|
||
import { CaptchaFacade } from '../../facades/captcha.facade'; | ||
|
||
@Injectable() | ||
export class SitekeyProviderService { | ||
// not-dead-code | ||
siteKey: string; | ||
|
||
constructor(captchaFacade: CaptchaFacade) { | ||
// synchronize asynchronous site key so we can provide it synchronously for the recaptcha service later on. | ||
captchaFacade.captchaSiteKey$.pipe(whenTruthy(), take(1)).subscribe(storeSiteKey => (this.siteKey = storeSiteKey)); | ||
} | ||
} | ||
|
||
export function getSynchronizedSiteKey(service: SitekeyProviderService) { | ||
return service.siteKey; | ||
} |
2 changes: 1 addition & 1 deletion
2
src/app/extensions/captcha/shared/captcha/captcha-v2/captcha-v2.component.html
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
Oops, something went wrong.