From 14b8b1d4d3e26adaff70bcf11aab358b0e27a49b Mon Sep 17 00:00:00 2001 From: Arman Ozak Date: Mon, 13 Jul 2020 14:32:46 +0300 Subject: [PATCH] test: add tests for replacing when reload is true --- .../states/replaceable-components.state.ts | 32 ++++++++++++------- .../replaceable-components.state.spec.ts | 24 ++++++++++++-- 2 files changed, 41 insertions(+), 15 deletions(-) diff --git a/npm/ng-packs/packages/core/src/lib/states/replaceable-components.state.ts b/npm/ng-packs/packages/core/src/lib/states/replaceable-components.state.ts index 0e48104f87..1f583f233f 100644 --- a/npm/ng-packs/packages/core/src/lib/states/replaceable-components.state.ts +++ b/npm/ng-packs/packages/core/src/lib/states/replaceable-components.state.ts @@ -1,4 +1,4 @@ -import { Injectable } from '@angular/core'; +import { Injectable, NgZone } from '@angular/core'; import { Router } from '@angular/router'; import { Action, createSelector, Selector, State, StateContext } from '@ngxs/store'; import snq from 'snq'; @@ -29,10 +29,26 @@ export class ReplaceableComponentsState { return selector; } - constructor(private router: Router) {} + constructor(private ngZone: NgZone, private router: Router) {} + + // TODO: Create a shared service for route reload and more + private reloadRoute() { + const { shouldReuseRoute } = this.router.routeReuseStrategy; + const setRouteReuse = (reuse: typeof shouldReuseRoute) => { + this.router.routeReuseStrategy.shouldReuseRoute = reuse; + }; + + setRouteReuse(() => false); + this.router.navigated = false; + + this.ngZone.run(async () => { + await this.router.navigateByUrl(this.router.url).catch(); + setRouteReuse(shouldReuseRoute); + }); + } @Action(AddReplaceableComponent) - async replaceableComponentsAction( + replaceableComponentsAction( { getState, patchState }: StateContext, { payload, reload }: AddReplaceableComponent, ) { @@ -52,14 +68,6 @@ export class ReplaceableComponentsState { replaceableComponents, }); - if (reload) { - // TODO: Create a shared service for route reload and more - const { shouldReuseRoute } = this.router.routeReuseStrategy; - this.router.routeReuseStrategy.shouldReuseRoute = () => false; - this.router.navigated = false; - - await this.router.navigateByUrl(this.router.url).catch(); - this.router.routeReuseStrategy.shouldReuseRoute = shouldReuseRoute; - } + if (reload) this.reloadRoute(); } } diff --git a/npm/ng-packs/packages/core/src/lib/tests/replaceable-components.state.spec.ts b/npm/ng-packs/packages/core/src/lib/tests/replaceable-components.state.spec.ts index c294b131e1..dc113c433c 100644 --- a/npm/ng-packs/packages/core/src/lib/tests/replaceable-components.state.spec.ts +++ b/npm/ng-packs/packages/core/src/lib/tests/replaceable-components.state.spec.ts @@ -1,21 +1,27 @@ +import { APP_BASE_HREF } from '@angular/common'; +import { Component } from '@angular/core'; +import { Router, RouterModule } from '@angular/router'; +import { SpyObject } from '@ngneat/spectator'; import { createHostFactory, SpectatorHost } from '@ngneat/spectator/jest'; import { NgxsModule, Store } from '@ngxs/store'; -import { ReplaceableComponentsState } from '../states/replaceable-components.state'; -import { Component } from '@angular/core'; import { AddReplaceableComponent } from '../actions'; +import { ReplaceableComponentsState } from '../states/replaceable-components.state'; @Component({ selector: 'abp-dummy', template: 'dummy works' }) class DummyComponent {} describe('ReplaceableComponentsState', () => { let spectator: SpectatorHost; + let router: SpyObject; const createHost = createHostFactory({ component: DummyComponent, - imports: [NgxsModule.forRoot([ReplaceableComponentsState])], + providers: [{ provide: APP_BASE_HREF, useValue: '/' }], + imports: [RouterModule.forRoot([]), NgxsModule.forRoot([ReplaceableComponentsState])], }); beforeEach(() => { spectator = createHost(''); + router = spectator.inject(Router); }); it('should add a component to the state', () => { @@ -38,4 +44,16 @@ describe('ReplaceableComponentsState', () => { }); expect(store.selectSnapshot(ReplaceableComponentsState.getAll)).toHaveLength(1); }); + + it('should call reloadRoute when reload parameter is given as true to AddReplaceableComponent', async () => { + const spy = jest.spyOn(router, 'navigateByUrl'); + const store = spectator.get(Store); + store.dispatch(new AddReplaceableComponent({ component: DummyComponent, key: 'Dummy' })); + store.dispatch(new AddReplaceableComponent({ component: null, key: 'Dummy' }, true)); + + await spectator.fixture.whenStable(); + + expect(spy).toHaveBeenCalledTimes(1); + expect(spy).toHaveBeenCalledWith(router.url); + }); });