Skip to content

Commit

Permalink
Merge branch 'develop-6.6.x' into epic/b2b-commerce-quotes
Browse files Browse the repository at this point in the history
  • Loading branch information
Larisa-Staroverova committed Oct 9, 2023
2 parents 5162bdc + 0d36166 commit d8a3cdf
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import { HttpClient, HTTP_INTERCEPTORS } from '@angular/common/http';
import { HTTP_INTERCEPTORS, HttpClient } from '@angular/common/http';
import {
HttpClientTestingModule,
HttpTestingController,
} from '@angular/common/http/testing';
import { inject, TestBed } from '@angular/core/testing';
import { defaultOccConfig } from '@spartacus/core';
import { TestBed, inject } from '@angular/core/testing';
import {
FeatureConfigService,
PageContext,
PageType,
RoutingService,
defaultOccConfig,
} from '@spartacus/core';
import { Observable, of } from 'rxjs';
import { SmartEditLauncherService } from '../services/smart-edit-launcher.service';
import { CmsTicketInterceptor } from './cms-ticket.interceptor';

Expand All @@ -16,10 +23,21 @@ class MockSmartEditLauncherService {
return this._cmsTicketId;
}
}
const mockPageContext = {
id: 'mockCode',
type: PageType.CATEGORY_PAGE,
};
class MockRoutingService implements Partial<RoutingService> {
getPageContext(): Observable<PageContext> {
return of(mockPageContext);
}
}

describe('CmsTicketInterceptor', () => {
let httpMock: HttpTestingController;
let service: SmartEditLauncherService;
let routingService: RoutingService;
let featureConfig: FeatureConfigService;

beforeEach(() => {
TestBed.configureTestingModule({
Expand All @@ -29,7 +47,10 @@ describe('CmsTicketInterceptor', () => {
provide: SmartEditLauncherService,
useClass: MockSmartEditLauncherService,
},

{
provide: RoutingService,
useClass: MockRoutingService,
},
{
provide: HTTP_INTERCEPTORS,
useClass: CmsTicketInterceptor,
Expand All @@ -40,6 +61,8 @@ describe('CmsTicketInterceptor', () => {

httpMock = TestBed.inject(HttpTestingController);
service = TestBed.inject(SmartEditLauncherService);
routingService = TestBed.inject(RoutingService);
featureConfig = TestBed.inject(FeatureConfigService);
});

afterEach(() => {
Expand Down Expand Up @@ -122,4 +145,61 @@ describe('CmsTicketInterceptor', () => {
mockReq.flush('somedata');
}
));

it('should add parameters for productList requests: cmsTicketId, pageType, code', inject(
[HttpClient],
(http: HttpClient) => {
spyOn(featureConfig, 'isLevel').and.returnValue(true);
spyOnProperty(service, 'cmsTicketId', 'get').and.returnValue(
'mockCmsTicketId'
);

http.get(`${OccUrl}/productList`).subscribe((result) => {
expect(result).toBeTruthy();
});
const mockReq = httpMock.expectOne((req) => {
return req.method === 'GET';
});

expect(mockReq.request.params.get('cmsTicketId')).toEqual(
'mockCmsTicketId'
);
expect(mockReq.request.params.get('pageType')).toEqual(
PageType.CATEGORY_PAGE
);
expect(mockReq.request.params.get('code')).toEqual('mockCode');
mockReq.flush('somedata');
}
));

it('should add only one parameter for productList requests when pageContext is partial: cmsTicketId', inject(
[HttpClient],
(http: HttpClient) => {
spyOn(featureConfig, 'isLevel').and.returnValue(true);
spyOn(routingService, 'getPageContext').and.returnValue(
of({
...mockPageContext,
id: '',
})
);

spyOnProperty(service, 'cmsTicketId', 'get').and.returnValue(
'mockCmsTicketId'
);

http.get(`${OccUrl}/productList`).subscribe((result) => {
expect(result).toBeTruthy();
});
const mockReq = httpMock.expectOne((req) => {
return req.method === 'GET';
});

expect(mockReq.request.params.get('cmsTicketId')).toEqual(
'mockCmsTicketId'
);
expect(mockReq.request.params.get('pageType')).toBeFalsy();
expect(mockReq.request.params.get('code')).toBeFalsy();
mockReq.flush('somedata');
}
));
});
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,72 @@ import {
HttpInterceptor,
HttpRequest,
} from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Injectable, inject } from '@angular/core';
import {
FeatureConfigService,
PageContext,
RoutingService,
} from '@spartacus/core';
import { Observable } from 'rxjs';
import { switchMap, take } from 'rxjs/operators';
import { SmartEditLauncherService } from '../services/smart-edit-launcher.service';

@Injectable({ providedIn: 'root' })
export class CmsTicketInterceptor implements HttpInterceptor {
constructor(private service: SmartEditLauncherService) {}
routingService = inject(RoutingService);
featureConfig = inject(FeatureConfigService);
constructor(protected service: SmartEditLauncherService) {}

intercept(
request: HttpRequest<any>,
next: HttpHandler
): Observable<HttpEvent<any>> {
const cmsTicketId = this.service.cmsTicketId;
if (!cmsTicketId) {
return next.handle(request);
}
if (
this.service.cmsTicketId &&
(request.url.includes('/cms/') || request.url.includes('/products/'))
this.featureConfig.isLevel('6.6') &&
request.url.includes('/productList')
) {
return this.setRequestForProductListPage(request, next, cmsTicketId);
}
if (request.url.includes('/cms/') || request.url.includes('/products/')) {
request = request.clone({
setParams: {
cmsTicketId: this.service.cmsTicketId,
cmsTicketId,
},
});
}

return next.handle(request);
}

protected setRequestForProductListPage(
request: HttpRequest<any>,
next: HttpHandler,
cmsTicketId: string
) {
return this.routingService.getPageContext().pipe(
take(1),
switchMap((pageContext: PageContext) => {
request = request.clone(
!!pageContext.id && !!pageContext.type
? {
setParams: {
cmsTicketId,
pageType: pageContext.type,
code: pageContext.id,
},
}
: {
setParams: {
cmsTicketId,
},
}
);
return next.handle(request);
})
);
}
}

0 comments on commit d8a3cdf

Please sign in to comment.