Skip to content

Commit

Permalink
chore: Refactor of routerService.go 8169 (#12638)
Browse files Browse the repository at this point in the history
* Refactor of routerService.go

Co-authored-by: Krzysztof Platis <[email protected]>
  • Loading branch information
ren-tham and Platonn authored Jun 15, 2021
1 parent 4e343b1 commit 8af89f6
Show file tree
Hide file tree
Showing 19 changed files with 98 additions and 63 deletions.
4 changes: 4 additions & 0 deletions docs/migration/4_0.md
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,10 @@ The following properties where removed in 4.0:

`AuthRedirectService` is a new, required constructor dependency.

### RoutingService
`RoutingService.go` - Removed 2nd argument `query`. Use `extras.queryParams` instead.
`RoutingService.navigate` - Removed 2nd argument `query`. Use `extras.queryParams` instead.

### AuthRedirectService

- `#reportNotAuthGuard` - method not needed anymore. Every visited URL is now remembered automatically as redirect URL on `NavigationEnd` event.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ const navParamsOverview: any = {
params: { ownerType: 'cartEntry', entityKey: CART_ENTRY_KEY },
};

const attributes = {};
const mockOwner = mockProductConfiguration.owner;

const mockRouterData: ConfiguratorRouter.Data = {
Expand Down Expand Up @@ -249,10 +248,7 @@ describe('ConfigAddToCartButtonComponent', () => {
it('should navigate to OV in case configuration is cart bound and we are on product config page', () => {
mockRouterData.pageType = ConfiguratorRouter.PageType.CONFIGURATION;
performUpdateCart();
expect(routingService.go).toHaveBeenCalledWith(
navParamsOverview,
attributes
);
expect(routingService.go).toHaveBeenCalledWith(navParamsOverview);

expect(
configuratorGroupsService.setGroupStatusVisited
Expand Down Expand Up @@ -298,10 +294,7 @@ describe('ConfigAddToCartButtonComponent', () => {
it('should navigate to overview in case configuration has not been added yet and we are on configuration page', () => {
ensureProductBound();
component.onAddToCart(mockProductConfiguration, mockRouterData);
expect(routingService.go).toHaveBeenCalledWith(
navParamsOverview,
attributes
);
expect(routingService.go).toHaveBeenCalledWith(navParamsOverview);
});

it('should remove one configuration (cart bound) in case configuration has not yet been added and we are on configuration page', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,10 @@ export class ConfiguratorAddToCartButtonComponent {
configuratorType: string,
owner: CommonConfigurator.Owner
): void {
this.routingService.go(
{
cxRoute: 'configureOverview' + configuratorType,
params: { ownerType: 'cartEntry', entityKey: owner.id },
},
{}
);
this.routingService.go({
cxRoute: 'configureOverview' + configuratorType,
params: { ownerType: 'cartEntry', entityKey: owner.id },
});
}

protected displayConfirmationMessage(key: string): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import { I18nTestingModule, RoutingService } from '@spartacus/core';
import { StoreFinderSearchComponent } from './store-finder-search.component';
import { ICON_TYPE } from '@spartacus/storefront';

const query = 'address';
const query = {
queryParams: {
query: 'address',
},
};

const keyEvent = {
key: 'Enter',
Expand Down Expand Up @@ -77,19 +81,21 @@ describe('StoreFinderSearchComponent', () => {
});

it('should dispatch new query', () => {
component.searchBox.setValue(query);
component.searchBox.setValue(query.queryParams.query);
component.findStores(component.searchBox.value);
expect(routingService.go).toHaveBeenCalledWith(['store-finder/find'], {
query,
});
expect(routingService.go).toHaveBeenCalledWith(
['store-finder/find'],
query
);
});

it('should call onKey and dispatch query', () => {
component.searchBox.setValue(query);
component.searchBox.setValue(query.queryParams.query);
component.onKey(keyEvent);
expect(routingService.go).toHaveBeenCalledWith(['store-finder/find'], {
query,
});
expect(routingService.go).toHaveBeenCalledWith(
['store-finder/find'],
query
);
});

it('should only call onKey', () => {
Expand All @@ -100,14 +106,16 @@ describe('StoreFinderSearchComponent', () => {
it('should view stores near by my location', () => {
component.viewStoresWithMyLoc();
expect(routingService.go).toHaveBeenCalledWith(['store-finder/find'], {
useMyLocation: true,
queryParams: {
useMyLocation: true,
},
});
});

it('should call findStores if search value provided and Enter is an event', () => {
spyOn(component, 'findStores');
component.searchBox.setValue(query);
component.searchBox.setValue(query.queryParams.query);
component.onKey(keyEvent);
expect(component.findStores).toHaveBeenCalledWith(query);
expect(component.findStores).toHaveBeenCalledWith(query.queryParams.query);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,19 @@ export class StoreFinderSearchComponent {
constructor(private routingService: RoutingService) {}

findStores(address: string) {
this.routingService.go(['store-finder/find'], { query: address });
this.routingService.go(['store-finder/find'], {
queryParams: {
query: address,
},
});
}

viewStoresWithMyLoc() {
this.routingService.go(['store-finder/find'], { useMyLocation: true });
this.routingService.go(['store-finder/find'], {
queryParams: {
useMyLocation: true,
},
});
}

onKey(event: any) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ describe('UpdateEmailComponentService', () => {
authService.coreLogout().then(() => {
expect(routingService.go).toHaveBeenCalledWith(
{ cxRoute: 'login' },
undefined,
{
state: {
newUid: '[email protected]',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,14 @@ export class UpdateEmailComponentService {
this.form.reset();
// TODO(#9638): Use logout route when it will support passing redirect url
this.authService.coreLogout().then(() => {
this.routingService.go({ cxRoute: 'login' }, undefined, {
state: {
newUid,
},
});
this.routingService.go(
{ cxRoute: 'login' },
{
state: {
newUid,
},
}
);
});
}

Expand Down
2 changes: 0 additions & 2 deletions projects/core/src/routing/facade/routing.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ describe('RoutingService', () => {
expect(store.dispatch).toHaveBeenCalledWith(
new RoutingActions.RouteGoAction({
path: ['generated', 'path'],
query: undefined,
extras: undefined,
})
);
Expand Down Expand Up @@ -142,7 +141,6 @@ describe('RoutingService', () => {
expect(store.dispatch).toHaveBeenCalledWith(
new RoutingActions.RouteGoAction({
path: ['/'],
query: undefined,
extras: undefined,
})
);
Expand Down
12 changes: 3 additions & 9 deletions projects/core/src/routing/facade/routing.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,9 @@ export class RoutingService {
* @param query
* @param extras: Represents the extra options used during navigation.
*/
go(commands: UrlCommands, query?: object, extras?: NavigationExtras): void {
go(commands: UrlCommands, extras?: NavigationExtras): void {
const path = this.semanticPathService.transform(commands);

return this.navigate(path, query, extras);
return this.navigate(path, extras);
}

/**
Expand Down Expand Up @@ -146,15 +145,10 @@ export class RoutingService {
* @param query
* @param extras: Represents the extra options used during navigation.
*/
protected navigate(
path: any[],
query?: object,
extras?: NavigationExtras
): void {
protected navigate(path: any[], extras?: NavigationExtras): void {
this.store.dispatch(
new RoutingActions.RouteGoAction({
path,
query,
extras,
})
);
Expand Down
1 change: 0 additions & 1 deletion projects/core/src/routing/store/actions/router.action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export class RouteGoAction implements Action {
constructor(
public payload: {
path: string[];
query?: object;
extras?: NavigationExtras;
}
) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ describe('Router Effects', () => {
spyOn(router, 'navigate');
spyOn(router, 'navigateByUrl');
effects.navigate$.subscribe(() => {
expect(router.navigate).toHaveBeenCalledWith(['/test'], {
queryParams: undefined,
});
expect(router.navigate).toHaveBeenCalledWith(['/test'], {});
});
});
});
Expand Down
4 changes: 2 additions & 2 deletions projects/core/src/routing/store/effects/router.effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ export class RouterEffects {
navigate$: Observable<any> = this.actions$.pipe(
ofType(RoutingActions.ROUTER_GO),
map((action: RoutingActions.RouteGoAction) => action.payload),
tap(({ path, query: queryParams, extras }) => {
this.router.navigate(path, { queryParams, ...extras });
tap(({ path, extras }) => {
this.router.navigate(path, { ...extras });
})
);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {
TODO_SPARTACUS,
SPARTACUS_CORE,
ROUTING_SERVICE,
GO,
} from '../../../../shared/constants';
import { MethodPropertyDeprecation } from '../../../../shared/utils/file-utils';

// projects/core/src/routing/facade/routing.service.ts
export const ROUTING_SERVICE_MIGRATION: MethodPropertyDeprecation[] = [
{
class: ROUTING_SERVICE,
importPath: SPARTACUS_CORE,
deprecatedNode: GO,
comment: `// ${TODO_SPARTACUS} The second argument of the method ${GO} has been removed. Use extras.queryParams instead.`,
},
];
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { ADDED_TO_CART_DIALOG_COMPONENT_MIGRATION } from './data/added-to-cart-d
import { CONFIGURATOR_ATTRIBUTE_DROP_DOWN_COMPONENT_MIGRATION } from './data/configurator-attribute-drop-down.component.migration';
import { CONFIGURATOR_ATTRIBUTE_NUMERIC_INPUT_FIELD_COMPONENT_MIGRATION } from './data/configurator-attribute-numeric-input-field.component.migration';
import { CONFIGURATOR_ATTRIBUTE_RADIO_BUTTON_COMPONENT_MIGRATION } from './data/configurator-attribute-radio-button.component.migration';
import { CONFIGURATOR_PRODUCT_TITLE_COMPONENT_MIGRATION } from './data/configurator-product-title.component.migration';
import { CONFIGURATOR_GROUP_MENU_COMPONENT_MIGRATION } from './data/configurator-group-menu.component.migration';
import { CONFIGURATOR_PRODUCT_TITLE_COMPONENT_MIGRATION } from './data/configurator-product-title.component.migration';
import {
CONTENT_PAGE_META_RESOLVER_MIGRATION,
PAGE_META_SERVICE_MIGRATION,
Expand All @@ -19,6 +19,7 @@ import {
OCC_ENDPOINTS_SERVICE_MIGRATION,
} from './data/occ-endpoints.service.migration';
import { PAGE_EVENT_BUILDER_MIGRATION } from './data/page-event.builder.ts.migration';
import { ROUTING_SERVICE_MIGRATION } from './data/routing.service.ts.migration';
import { SELECTIVE_CART_SERVICE_MIGRATION } from './data/selective-cart.service.migration';

export const METHOD_PROPERTY_DATA: MethodPropertyDeprecation[] = [
Expand All @@ -36,6 +37,7 @@ export const METHOD_PROPERTY_DATA: MethodPropertyDeprecation[] = [
...CONFIGURATOR_ATTRIBUTE_NUMERIC_INPUT_FIELD_COMPONENT_MIGRATION,
...CONFIGURATOR_ATTRIBUTE_RADIO_BUTTON_COMPONENT_MIGRATION,
...CONFIGURATOR_PRODUCT_TITLE_COMPONENT_MIGRATION,
...ROUTING_SERVICE_MIGRATION,
...CONFIGURATOR_GROUP_MENU_COMPONENT_MIGRATION,
];

Expand Down
1 change: 1 addition & 0 deletions projects/schematics/src/shared/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,7 @@ export const GET_LOADED = 'getLoaded';
export const IS_STABLE = 'isStable';

export const STOREFINDER_MODULE = 'StoreFinderModule';
export const GO = 'go';

export const CREATE_EVENT_FROM_INPUT = 'createEventFromInput';
export const ON_SELECT = 'onSelect';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ describe('MyCouponsComponentService', () => {
cxRoute: 'search',
params: { query: RELEVANCE },
},
{ couponcode: 'CouponForAllProduct' }
{
queryParams: {
couponcode: 'CouponForAllProduct',
},
}
);
});

Expand All @@ -64,7 +68,11 @@ describe('MyCouponsComponentService', () => {
query: RELEVANCE + CUSTOMER_COUPON_CODE + 'CouponForPartProduct',
},
},
{ couponcode: 'CouponForPartProduct' }
{
queryParams: {
couponcode: 'CouponForPartProduct',
},
}
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ export class MyCouponsComponentService {
cxRoute: 'search',
params: { query: this.buildSearchParam(coupon) },
},
{ couponcode: coupon.couponId }
{
queryParams: {
couponcode: coupon.couponId,
},
}
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ describe('ReturnRequestService', () => {
service.backToList();
expect(routingService.go).toHaveBeenCalledWith(
{ cxRoute: 'orders' },
null,
{
state: {
activeTab: 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,13 @@ export class ReturnRequestService {
}

backToList(): void {
this.routingService.go({ cxRoute: 'orders' }, null, {
state: {
activeTab: 1,
},
});
this.routingService.go(
{ cxRoute: 'orders' },
{
state: {
activeTab: 1,
},
}
);
}
}

0 comments on commit 8af89f6

Please sign in to comment.