Skip to content

Commit

Permalink
Epic: CXSPA-2897 (#17399)
Browse files Browse the repository at this point in the history
Co-authored-by: Pawel Fras <[email protected]>
Co-authored-by: kpawelczak <[email protected]>
  • Loading branch information
3 people authored Oct 24, 2023
1 parent 9090f16 commit 32e7d6d
Show file tree
Hide file tree
Showing 187 changed files with 1,694 additions and 1,330 deletions.
9 changes: 2 additions & 7 deletions docs/libs/creating-lib.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,15 @@ Just copy paste the following and and make sure to rename `TODO:` to you lib's n
module.exports = function (config) {
config.set({
basePath: '',
frameworks: ['parallel', 'jasmine', '@angular-devkit/build-angular'],
frameworks: ['jasmine', '@angular-devkit/build-angular'],
plugins: [
require('karma-parallel'),
require('karma-jasmine'),
require('karma-coverage'),
require('karma-chrome-launcher'),
require('karma-jasmine-html-reporter'),
require('@angular-devkit/build-angular/plugins/karma'),
require('karma-junit-reporter'),
],
parallelOptions: {
executors: 2,
shardStrategy: 'round-robin',
},
client: {
clearContext: false, // leave Jasmine Spec Runner output visible in browser
},
Expand Down Expand Up @@ -220,7 +215,7 @@ Use the following template:
"peerDependencies": {
"@angular/common": "^10.1.0",
"@angular/core": "^10.1.0",
"rxjs": "^6.6.0",
"rxjs": "^7.8.0",
"@spartacus/core": "3.0.0-next.0",
"@spartacus/storefront": "3.0.0-next.0"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,9 @@ describe('AsmBindCartComponent', () => {
it('should alert through global messsages when the bind cart fails', () => {
const expectedErrorMessage = 'mock-error-message';
(asmBindCartFacade.bindCart as jasmine.Spy).and.returnValue(
throwError({ details: [{ message: expectedErrorMessage }] })
throwError(() => ({
details: [{ message: expectedErrorMessage }],
}))
);

component.bindCartToCustomer();
Expand Down
7 changes: 1 addition & 6 deletions feature-libs/asm/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,15 @@
module.exports = function (config) {
config.set({
basePath: '',
frameworks: ['parallel', 'jasmine', '@angular-devkit/build-angular'],
frameworks: ['jasmine', '@angular-devkit/build-angular'],
plugins: [
require('karma-parallel'),
require('karma-jasmine'),
require('karma-coverage'),
require('karma-chrome-launcher'),
require('karma-jasmine-html-reporter'),
require('@angular-devkit/build-angular/plugins/karma'),
require('karma-junit-reporter'),
],
parallelOptions: {
executors: 2,
shardStrategy: 'round-robin',
},
client: {
clearContext: false, // leave Jasmine Spec Runner output visible in browser
jasmine: {
Expand Down
34 changes: 17 additions & 17 deletions feature-libs/asm/occ/adapters/occ-asm.adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
User,
normalizeHttpError,
} from '@spartacus/core';
import { Observable, throwError } from 'rxjs';
import { Observable } from 'rxjs';
import { catchError } from 'rxjs/operators';

@Injectable()
Expand Down Expand Up @@ -75,7 +75,9 @@ export class OccAsmAdapter implements AsmAdapter {
);

return this.http.get<CustomerListsPage>(url, { headers, params }).pipe(
catchError((error) => throwError(normalizeHttpError(error, this.logger))),
catchError((error) => {
throw normalizeHttpError(error, this.logger);
}),
this.converterService.pipeable(CUSTOMER_LISTS_NORMALIZER)
);
}
Expand Down Expand Up @@ -123,7 +125,9 @@ export class OccAsmAdapter implements AsmAdapter {
);

return this.http.get<CustomerSearchPage>(url, { headers, params }).pipe(
catchError((error) => throwError(normalizeHttpError(error, this.logger))),
catchError((error) => {
throw normalizeHttpError(error, this.logger);
}),
this.converterService.pipeable(CUSTOMER_SEARCH_PAGE_NORMALIZER)
);
}
Expand All @@ -148,13 +152,11 @@ export class OccAsmAdapter implements AsmAdapter {
}
);

return this.http
.post<void>(url, {}, { headers, params })
.pipe(
catchError((error) =>
throwError(normalizeHttpError(error, this.logger))
)
);
return this.http.post<void>(url, {}, { headers, params }).pipe(
catchError((error) => {
throw normalizeHttpError(error, this.logger);
})
);
}

createCustomer(user: CustomerRegistrationForm): Observable<User> {
Expand All @@ -172,12 +174,10 @@ export class OccAsmAdapter implements AsmAdapter {
prefix: false,
}
);
return this.http
.post<User>(url, user, { headers, params })
.pipe(
catchError((error) =>
throwError(normalizeHttpError(error, this.logger))
)
);
return this.http.post<User>(url, user, { headers, params }).pipe(
catchError((error) => {
throw normalizeHttpError(error, this.logger);
})
);
}
}
2 changes: 1 addition & 1 deletion feature-libs/asm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"@spartacus/storefront": "6.6.0-1",
"@spartacus/styles": "6.6.0-1",
"@spartacus/user": "6.6.0-1",
"rxjs": "^6.6.0"
"rxjs": "^7.8.0"
},
"publishConfig": {
"access": "public"
Expand Down
9 changes: 4 additions & 5 deletions feature-libs/asm/root/services/asm-auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
StateWithClientAuth,
UserIdService,
} from '@spartacus/core';
import { combineLatest, from, Observable, of } from 'rxjs';
import { combineLatest, from, lastValueFrom, Observable, of } from 'rxjs';
import { map, switchMap, take } from 'rxjs/operators';
import { AsmAuthStorageService, TokenTarget } from './asm-auth-storage.service';

Expand Down Expand Up @@ -109,9 +109,8 @@ export class AsmAuthService extends AuthService {
* To perform logout it is best to use `logout` method. Use this method with caution.
*/
coreLogout(): Promise<any> {
return this.userIdService
.isEmulated()
.pipe(
return lastValueFrom(
this.userIdService.isEmulated().pipe(
take(1),
switchMap((isEmulated) => {
if (isEmulated) {
Expand All @@ -124,7 +123,7 @@ export class AsmAuthService extends AuthService {
}
})
)
.toPromise();
);
}

/**
Expand Down
13 changes: 6 additions & 7 deletions feature-libs/cart/base/core/cart-persistence.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,29 @@
*/

import { NgModule } from '@angular/core';
import { Action, ActionReducer, MetaReducer, META_REDUCERS } from '@ngrx/store';
import { Action, ActionReducer, META_REDUCERS, MetaReducer } from '@ngrx/store';
import { CartType } from '@spartacus/cart/base/root';
import {
Config,
ConfigInitializerService,
MODULE_INITIALIZER,
} from '@spartacus/core';
import { lastValueFrom } from 'rxjs';
import { tap } from 'rxjs/operators';
import { MultiCartStatePersistenceService } from './services/multi-cart-state-persistence.service';

export function cartStatePersistenceFactory(
cartStatePersistenceService: MultiCartStatePersistenceService,
configInit: ConfigInitializerService
): () => Promise<Config> {
const result = () =>
configInit
.getStable('context')
.pipe(
return () =>
lastValueFrom(
configInit.getStable('context').pipe(
tap(() => {
cartStatePersistenceService.initSync();
})
)
.toPromise();
return result;
);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions feature-libs/cart/base/core/facade/multi-cart.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
OrderEntry,
} from '@spartacus/cart/base/root';
import { isNotUndefined, StateUtils, UserIdService } from '@spartacus/core';
import { EMPTY, Observable, timer } from 'rxjs';
import { Observable, of, timer } from 'rxjs';
import {
debounce,
distinctUntilChanged,
Expand Down Expand Up @@ -76,7 +76,7 @@ export class MultiCartService implements MultiCartFacade {
// This flickering should only be avoided when switching from false to true
// Start of loading should be showed instantly (no debounce)
// Extra actions are only dispatched after some loading
debounce((isStable) => (isStable ? timer(0) : EMPTY)),
debounce((isStable) => (isStable ? timer(0) : of(undefined))),
distinctUntilChanged()
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ describe('Cart Voucher effect', () => {

it('should fail', () => {
const error = new HttpErrorResponse({ error: 'error' });
cartVoucherConnector.add = createSpy().and.returnValue(throwError(error));
cartVoucherConnector.add = createSpy().and.returnValue(
throwError(() => error)
);
const action = new CartActions.CartAddVoucher({
userId,
cartId,
Expand Down
6 changes: 3 additions & 3 deletions feature-libs/cart/base/core/store/effects/cart.effect.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,13 @@ describe('Cart effect', () => {
extraData: { active: true },
});
loadMock.and.returnValue(
throwError({
throwError(() => ({
error: {
errors: [
{ reason: 'notFound', subjectType: 'cart', subject: '123456' },
],
},
})
}))
);
const removeCartCompletion = new CartActions.RemoveCart({ cartId });
actions$ = hot('-a', { a: action });
Expand Down Expand Up @@ -199,7 +199,7 @@ describe('Cart effect', () => {
},
});
const action = new CartActions.LoadCart(payload);
loadMock.and.returnValue(throwError(httpError));
loadMock.and.returnValue(throwError(() => httpError));
const removeCartCompletion = new CartActions.LoadCartFail({
...payload,
error: normalizeHttpError(httpError),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
OccEndpointsService,
normalizeHttpError,
} from '@spartacus/core';
import { Observable, throwError } from 'rxjs';
import { Observable } from 'rxjs';
import { catchError } from 'rxjs/operators';

@Injectable()
Expand All @@ -36,7 +36,9 @@ export class OccCartValidationAdapter implements CartValidationAdapter {
});

return this.http.post<any>(url, null).pipe(
catchError((error) => throwError(normalizeHttpError(error, this.logger))),
catchError((error) => {
throw normalizeHttpError(error, this.logger);
}),
this.converter.pipeable(CART_VALIDATION_NORMALIZER)
);
}
Expand Down
22 changes: 15 additions & 7 deletions feature-libs/cart/base/occ/adapters/occ-cart-voucher.adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,25 @@
*/

import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Injectable, inject } from '@angular/core';
import { CartVoucherAdapter } from '@spartacus/cart/base/core';
import { CART_VOUCHER_NORMALIZER } from '@spartacus/cart/base/root';
import {
ConverterService,
InterceptorUtil,
OccEndpointsService,
LoggerService,
OCC_USER_ID_ANONYMOUS,
OccEndpointsService,
USE_CLIENT_TOKEN,
normalizeHttpError,
} from '@spartacus/core';
import { Observable, throwError } from 'rxjs';
import { Observable } from 'rxjs';
import { catchError } from 'rxjs/operators';

@Injectable()
export class OccCartVoucherAdapter implements CartVoucherAdapter {
protected logger = inject(LoggerService);

constructor(
protected http: HttpClient,
protected occEndpoints: OccEndpointsService,
Expand Down Expand Up @@ -54,7 +58,9 @@ export class OccCartVoucherAdapter implements CartVoucherAdapter {
const headers = this.getHeaders(userId);

return this.http.post(url, toAdd, { headers, params }).pipe(
catchError((error: any) => throwError(error)),
catchError((error: any) => {
throw normalizeHttpError(error, this.logger);
}),
this.converter.pipeable(CART_VOUCHER_NORMALIZER)
);
}
Expand All @@ -67,8 +73,10 @@ export class OccCartVoucherAdapter implements CartVoucherAdapter {

const headers = this.getHeaders(userId);

return this.http
.delete(url, { headers })
.pipe(catchError((error: any) => throwError(error)));
return this.http.delete(url, { headers }).pipe(
catchError((error: any) => {
throw normalizeHttpError(error, this.logger);
})
);
}
}
2 changes: 1 addition & 1 deletion feature-libs/cart/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"@spartacus/styles": "6.6.0-1",
"@spartacus/user": "6.6.0-1",
"bootstrap": "^4.6.2",
"rxjs": "^6.6.0"
"rxjs": "^7.8.0"
},
"publishConfig": {
"access": "public"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,11 @@ describe('QuickOrderOrderEntriesContext', () => {
it('should catch unknown identifier error', () => {
canAdd$.next(true);
productConnector.get = createSpy().and.returnValue(
throwError({
throwError(() => ({
error: {
errors: [{ type: 'UnknownIdentifierError' }],
},
})
}))
);

const unableToAddProductsData: ProductData[] = [
Expand Down Expand Up @@ -240,7 +240,9 @@ describe('QuickOrderOrderEntriesContext', () => {

it('should catch unknown errors', () => {
canAdd$.next(true);
productConnector.get = createSpy().and.returnValue(throwError({}));
productConnector.get = createSpy().and.returnValue(
throwError(() => ({}))
);

const unableToAddProductsData: ProductData[] = [
{ productCode: unhandledItemErrorId, quantity: 1 },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ describe(`OccCheckoutCostCenterAdapter`, () => {

describe(`back-off`, () => {
it(`should unsuccessfully backOff on Jalo error`, fakeAsync(() => {
spyOn(httpClient, 'put').and.returnValue(throwError(mockJaloError));
spyOn(httpClient, 'put').and.returnValue(
throwError(() => mockJaloError)
);

let result: HttpErrorModel | undefined;
const subscription = service
Expand All @@ -127,7 +129,7 @@ describe(`OccCheckoutCostCenterAdapter`, () => {
if (calledTimes === 3) {
return of(cartData);
}
return throwError(mockJaloError);
return throwError(() => mockJaloError);
})
);

Expand Down
Loading

0 comments on commit 32e7d6d

Please sign in to comment.