Skip to content

Commit

Permalink
switch to CMS guards
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristophHi committed Jul 27, 2023
1 parent 7674113 commit 3b0bc64
Show file tree
Hide file tree
Showing 20 changed files with 150 additions and 132 deletions.
3 changes: 1 addition & 2 deletions feature-libs/cart/base/root/cart-base-root.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import {
MINI_CART_FEATURE,
} from './feature-name';
import { ActiveCartOrderEntriesContextToken } from './tokens/context';
import { QuoteCartGuard } from './guards/quote-cart.guard';

export function defaultCartComponentsConfig() {
const config = {
Expand Down Expand Up @@ -64,7 +63,7 @@ export function defaultCartComponentsConfig() {
{
// @ts-ignore
path: null,
canActivate: [CmsPageGuard, QuoteCartGuard],
canActivate: [CmsPageGuard],
component: PageLayoutComponent,
data: {
cxRoute: 'cart',
Expand Down
8 changes: 0 additions & 8 deletions feature-libs/cart/base/root/guards/index.ts

This file was deleted.

40 changes: 0 additions & 40 deletions feature-libs/cart/base/root/guards/quote-cart.guard.ts

This file was deleted.

37 changes: 0 additions & 37 deletions feature-libs/cart/base/root/guards/quote-cart.service.ts

This file was deleted.

1 change: 0 additions & 1 deletion feature-libs/cart/base/root/public_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export * from './facade/index';
export * from './feature-name';
export * from './models/index';
export * from './tokens/index';
export * from './guards/index';

/** AUGMENTABLE_TYPES_START */
export { Cart, DeliveryMode, OrderEntry } from './models/cart.model';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
import { CommonModule } from '@angular/common';
import { NgModule } from '@angular/core';
import { CmsConfig, provideDefaultConfig } from '@spartacus/core';
import { CartNotEmptyGuard } from '../guards/cart-not-empty.guard';
import { CheckoutOrchestratorComponent } from './checkout-orchestrator.component';
import { CheckoutAuthGuard } from '../guards/checkout-auth.guard';
import { CartNotEmptyGuard } from '../guards/cart-not-empty.guard';
import { CheckoutGuard } from '../guards/checkout.guard';
import { CheckoutOrchestratorComponent } from './checkout-orchestrator.component';
import { QuoteCartGuard } from '@spartacus/cart/base/root';

@NgModule({
imports: [CommonModule],
Expand All @@ -20,12 +19,7 @@ import { QuoteCartGuard } from '@spartacus/cart/base/root';
cmsComponents: {
CheckoutOrchestrator: {
component: CheckoutOrchestratorComponent,
guards: [
QuoteCartGuard,
CheckoutAuthGuard,
CartNotEmptyGuard,
CheckoutGuard,
],
guards: [CheckoutAuthGuard, CartNotEmptyGuard, CheckoutGuard],
},
},
}),
Expand Down
123 changes: 101 additions & 22 deletions feature-libs/quote/core/facade/quote.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
Comment,
Quote,
QuoteActionType,
QuoteCartService,
QuoteDetailsReloadQueryEvent,
QuoteList,
QuoteMetadata,
Expand Down Expand Up @@ -82,6 +83,18 @@ class MockEventService implements Partial<EventService> {
dispatch = createSpy();
}

let isQuoteCartActive: any;
let quoteId: any;
class MockQuoteCartService {
setQuoteCartActive = createSpy();
setQuoteId = createSpy();
getQuoteCartActive() {
return of(isQuoteCartActive);
}
getQuoteId() {
return of(quoteId);
}
}
class MockViewConfig implements ViewConfig {
view = { defaultPageSize: mockPagination.pageSize };
}
Expand Down Expand Up @@ -116,6 +129,7 @@ describe('QuoteService', () => {
let config: ViewConfig;
let multiCartFacade: MultiCartFacade;
let routingService: RoutingService;
let quoteCartService: QuoteCartService;

beforeEach(() => {
TestBed.configureTestingModule({
Expand All @@ -132,6 +146,7 @@ describe('QuoteService', () => {
{ provide: ActiveCartFacade, useClass: MockActiveCartService },
{ provide: GlobalMessageService, useClass: MockGlobalMessageService },
{ provide: MultiCartFacade, useClass: MockMultiCartFacade },
{ provide: QuoteCartService, useClass: MockQuoteCartService },
],
});

Expand All @@ -141,6 +156,10 @@ describe('QuoteService', () => {
config = TestBed.inject(ViewConfig);
multiCartFacade = TestBed.inject(MultiCartFacade);
routingService = TestBed.inject(RoutingService);
quoteCartService = TestBed.inject(QuoteCartService);

isQuoteCartActive = false;
quoteId = '';
});

it('should inject CommerceQuotesService', inject(
Expand Down Expand Up @@ -214,17 +233,38 @@ describe('QuoteService', () => {
});
});

it('should return quote details after calling quoteConnector.getQuote', () => {
service
.getQuoteDetails()
.pipe(take(1))
.subscribe((details) => {
expect(connector.getQuote).toHaveBeenCalledWith(
mockUserId,
mockParams.quoteId
);
expect(details).toEqual(mockQuote);
});
describe('getQuoteDetails', () => {
it('should return quote details after calling quoteConnector.getQuote', () => {
service
.getQuoteDetails()
.pipe(take(1))
.subscribe((details) => {
expect(connector.getQuote).toHaveBeenCalledWith(
mockUserId,
mockParams.quoteId
);
expect(details).toEqual(mockQuote);
});
});

it('should load quote cart in quote is linked to current quote cart', (done) => {
isQuoteCartActive = true;
quoteId = mockQuote.code;
service
.getQuoteDetails()
.pipe(take(1))
.subscribe((details) => {
expect(multiCartFacade.loadCart).toHaveBeenCalledWith({
userId: mockUserId,
cartId: mockQuote.cartId,
extraData: {
active: true,
},
});
expect(details).toEqual(mockQuote);
done();
});
});
});

it('should call createQuote command', () => {
Expand Down Expand Up @@ -266,17 +306,56 @@ describe('QuoteService', () => {
});
});

it('should call performQuoteAction command', (done) => {
service
.performQuoteAction(mockQuote.code, mockAction.type)
.subscribe(() => {
expect(connector.performQuoteAction).toHaveBeenCalledWith(
mockUserId,
mockQuote.code,
mockAction.type
);
done();
});
describe('performQuoteAction', () => {
it('should call respective connector method', (done) => {
service
.performQuoteAction(mockQuote.code, mockAction.type)
.subscribe(() => {
expect(connector.performQuoteAction).toHaveBeenCalledWith(
mockUserId,
mockQuote.code,
mockAction.type
);
done();
});
});

it('should raise re-load event', (done) => {
service
.performQuoteAction(mockQuote.code, mockAction.type)
.subscribe(() => {
expect(eventService.dispatch).toHaveBeenCalledWith(
{},
QuoteDetailsReloadQueryEvent
);
done();
});
});

it('should reset cart quote mode on submit', (done) => {
service
.performQuoteAction(mockQuote.code, QuoteActionType.SUBMIT)
.subscribe(() => {
expect(quoteCartService.setQuoteCartActive).toHaveBeenCalledWith(
false
);
done();
});
});

it('should set cart quote mode on edit', (done) => {
service
.performQuoteAction(mockQuote.code, QuoteActionType.EDIT)
.subscribe(() => {
expect(quoteCartService.setQuoteCartActive).toHaveBeenCalledWith(
true
);
expect(quoteCartService.setQuoteId).toHaveBeenCalledWith(
mockQuote.code
);
done();
});
});
});

it('should call requote command and return new quote', () => {
Expand Down
7 changes: 2 additions & 5 deletions feature-libs/quote/core/facade/quote.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@
*/

import { Injectable } from '@angular/core';
import {
ActiveCartFacade,
MultiCartFacade,
QuoteCartService,
} from '@spartacus/cart/base/root';
import { QuoteCartService } from '@spartacus/quote/root';
import { ActiveCartFacade, MultiCartFacade } from '@spartacus/cart/base/root';
import {
Comment,
QuoteFacade,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { Injectable } from '@angular/core';
import { QuoteCartService } from '@spartacus/cart/base/root';
import { QuoteCartService } from '@spartacus/quote/root';
import { Converter } from '@spartacus/core';
import { QuoteCoreConfig } from '@spartacus/quote/core';
import {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
<ng-container >
</ng-container>
<ng-container> </ng-container>
1 change: 0 additions & 1 deletion feature-libs/quote/quote-aware/public_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@
*/

export * from './quote-aware.module';

4 changes: 2 additions & 2 deletions feature-libs/quote/quote-aware/quote-aware.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { CommonModule } from '@angular/common';
import { NgModule } from '@angular/core';
import { QuoteAwareComponentModule } from './component/quote-aware.component.module';

@NgModule({
imports: [CommonModule],
imports: [QuoteAwareComponentModule],
})
export class QuoteAwareModule {}
1 change: 1 addition & 0 deletions feature-libs/quote/root/feature-name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
*/

export const QUOTE_FEATURE = 'quote';
export const QUOTE_AWARE_FEATURE = 'quote_aware';
13 changes: 12 additions & 1 deletion feature-libs/quote/root/quote-root.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
PageLayoutComponent,
} from '@spartacus/storefront';
import { QuoteEventModule } from './events/quote-event.module';
import { QUOTE_FEATURE } from './feature-name';
import { QUOTE_AWARE_FEATURE, QUOTE_FEATURE } from './feature-name';

export function defaultQuoteComponentsConfig() {
return {
Expand All @@ -39,6 +39,16 @@ export function defaultQuoteComponentsConfig() {
};
}

export function defaultQuoteAwareComponentsConfig() {
return {
featureModules: {
[QUOTE_AWARE_FEATURE]: {
cmsComponents: ['QuoteAwareComponent'],
},
},
};
}

export const defaultQuoteRoutingConfig: RoutingConfig = {
routing: {
routes: {
Expand Down Expand Up @@ -91,6 +101,7 @@ export const defaultQuoteConfigLayoutConfig: LayoutConfig = {
],
providers: [
provideDefaultConfigFactory(defaultQuoteComponentsConfig),
provideDefaultConfigFactory(defaultQuoteAwareComponentsConfig),
provideDefaultConfig(defaultQuoteRoutingConfig),
provideDefaultConfig(defaultQuoteConfigLayoutConfig),
],
Expand Down
Loading

0 comments on commit 3b0bc64

Please sign in to comment.