Skip to content

Commit

Permalink
chore: Cleanup deprecated toPromise() (#17383)
Browse files Browse the repository at this point in the history
CXSPA-3076
  • Loading branch information
ercultimate authored May 11, 2023
1 parent 0599c86 commit 2a7b2b5
Show file tree
Hide file tree
Showing 26 changed files with 153 additions and 198 deletions.
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Pipe, PipeTransform } from '@angular/core';
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
import { UntypedFormGroup, ReactiveFormsModule } from '@angular/forms';
import { ReactiveFormsModule, UntypedFormGroup } from '@angular/forms';
import { RouterTestingModule } from '@angular/router/testing';
import { I18nTestingModule, RoutingService } from '@spartacus/core';
import { OrderFacade } from '@spartacus/order/root';
Expand All @@ -16,7 +16,7 @@ class MockOrderFacade implements Partial<OrderFacade> {
}

class MockRoutingService implements Partial<RoutingService> {
go = createSpy().and.returnValue(of(true).toPromise());
go = createSpy().and.returnValue(Promise.resolve(true));
}

class MockLaunchDialogService implements Partial<LaunchDialogService> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const cartNotFoundError = {
};

class MockRoutingService implements Partial<RoutingService> {
go = createSpy().and.returnValue(EMPTY.toPromise());
go = createSpy().and.returnValue(Promise.resolve(undefined));

getRouterState = createSpy().and.returnValue(EMPTY);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class MockCheckoutReplenishmentFormService
}

class MockRoutingService implements Partial<RoutingService> {
go = createSpy().and.returnValue(of(true).toPromise());
go = createSpy().and.returnValue(Promise.resolve(true));
}

class MockLaunchDialogService implements Partial<LaunchDialogService> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import {
RoutingService,
SemanticPathService,
} from '@spartacus/core';
import { Observable, of } from 'rxjs';
import { take } from 'rxjs/operators';
import { firstValueFrom, Observable, of } from 'rxjs';
import { AccountSummaryPageMetaResolver } from './account-summary-page-meta.resolver';

class MockRoutingService {
Expand Down Expand Up @@ -78,7 +77,7 @@ describe('AccountSummaryPageMetaResolver', () => {

describe('resolveTitle', () => {
it('should emit title of CMS content page ', async () => {
expect(await service.resolveTitle().pipe(take(1)).toPromise()).toBe(
expect(await firstValueFrom(service.resolveTitle())).toBe(
'testContentPageTitle'
);
});
Expand All @@ -92,7 +91,7 @@ describe('AccountSummaryPageMetaResolver', () => {
);
});
it('should NOT return breadcrumb for the Account Summary page', async () => {
let val = await service.resolveBreadcrumbs().pipe(take(1)).toPromise();
let val = await firstValueFrom(service.resolveBreadcrumbs());
expect(val).toEqual([testHomeBreadcrumb, organizationBreadcrumb]);
});
});
Expand All @@ -114,7 +113,7 @@ describe('AccountSummaryPageMetaResolver', () => {
});

it('should insert breadcrumb for the Account Summary page right after the Homepage breadcrumb', async () => {
let val = await service.resolveBreadcrumbs().pipe(take(1)).toPromise();
let val = await firstValueFrom(service.resolveBreadcrumbs());
expect(val).toEqual([
testHomeBreadcrumb,
organizationBreadcrumb,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { TestBed } from '@angular/core/testing';
import { I18nTestingModule } from '@spartacus/core';
import { Budget } from '@spartacus/organization/administration/core';
import { Observable, of } from 'rxjs';
import { take } from 'rxjs/operators';
import { firstValueFrom, Observable, of } from 'rxjs';
import { BudgetRoutePageMetaResolver } from './budget-route-page-meta.resolver';
import { CurrentBudgetService } from './current-budget.service';

Expand All @@ -29,13 +28,12 @@ describe('BudgetRoutePageMetaResolver', () => {

it('should emit breadcrumb with translated i18n key, using current item as params', async () => {
expect(
await resolver
.resolveBreadcrumbs({
await firstValueFrom(
resolver.resolveBreadcrumbs({
url: 'testPath',
pageMetaConfig: { breadcrumb: { i18n: 'testTranslation' } },
})
.pipe(take(1))
.toPromise()
)
).toEqual([{ label: 'testTranslation code:testCode', link: 'testPath' }]);
});
});
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { TestBed } from '@angular/core/testing';
import { CostCenter, I18nTestingModule } from '@spartacus/core';
import { Observable, of } from 'rxjs';
import { take } from 'rxjs/operators';
import { firstValueFrom, Observable, of } from 'rxjs';
import { CostCenterRoutePageMetaResolver } from './cost-center-route-page-meta.resolver';
import { CurrentCostCenterService } from './current-cost-center.service';

Expand All @@ -28,13 +27,12 @@ describe('CostCenterRouteBreadcrumbResolver', () => {

it('should emit breadcrumb with translated i18n key, using current item as params', async () => {
expect(
await resolver
.resolveBreadcrumbs({
await firstValueFrom(
resolver.resolveBreadcrumbs({
url: 'testPath',
pageMetaConfig: { breadcrumb: { i18n: 'testTranslation' } },
})
.pipe(take(1))
.toPromise()
)
).toEqual([{ label: 'testTranslation code:testCode', link: 'testPath' }]);
});
});
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { TestBed } from '@angular/core/testing';
import { I18nTestingModule } from '@spartacus/core';
import { Permission } from '@spartacus/organization/administration/core';
import { BehaviorSubject, Observable } from 'rxjs';
import { take } from 'rxjs/operators';
import { BehaviorSubject, firstValueFrom, Observable } from 'rxjs';
import { CurrentPermissionService } from './current-permission.service';
import { PermissionRoutePageMetaResolver } from './permission-route-page-meta.resolver';

Expand Down Expand Up @@ -33,26 +32,24 @@ describe('PermissionRoutePageMetaResolver', () => {
it('should emit breadcrumb with translated i18n key, using current item as params', async () => {
currentItem$.next({ code: 'testCode' });
expect(
await resolver
.resolveBreadcrumbs({
await firstValueFrom(
resolver.resolveBreadcrumbs({
url: 'testPath',
pageMetaConfig: { breadcrumb: { i18n: 'testTranslation' } },
})
.pipe(take(1))
.toPromise()
)
).toEqual([{ label: 'testTranslation code:testCode', link: 'testPath' }]);
});

it('should emit breadcrumb with translated i18n key, using {} as params', async () => {
currentItem$.next(undefined);
expect(
await resolver
.resolveBreadcrumbs({
await firstValueFrom(
resolver.resolveBreadcrumbs({
url: 'testPath',
pageMetaConfig: { breadcrumb: { i18n: 'testTranslation' } },
})
.pipe(take(1))
.toPromise()
)
).toEqual([{ label: 'testTranslation', link: 'testPath' }]);
});
});
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { TestBed } from '@angular/core/testing';
import { Address, I18nTestingModule } from '@spartacus/core';
import { Observable, of } from 'rxjs';
import { take } from 'rxjs/operators';
import { firstValueFrom, Observable, of } from 'rxjs';
import { CurrentUnitAddressService } from '../links/addresses/services/current-unit-address.service';
import { UnitAddressRoutePageMetaResolver } from './unit-address-route-page-meta.resolver';

Expand All @@ -28,13 +27,12 @@ describe('UnitAddressRoutePageMetaResolver', () => {

it('should emit breadcrumb with translated i18n key, using current item as params', async () => {
expect(
await resolver
.resolveBreadcrumbs({
await firstValueFrom(
resolver.resolveBreadcrumbs({
url: 'testPath',
pageMetaConfig: { breadcrumb: { i18n: 'testTranslation' } },
})
.pipe(take(1))
.toPromise()
)
).toEqual([
{
label: 'testTranslation formattedAddress:testAddress',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { TestBed } from '@angular/core/testing';
import { B2BUnit, I18nTestingModule } from '@spartacus/core';
import { Observable, of } from 'rxjs';
import { take } from 'rxjs/operators';
import { firstValueFrom, Observable, of } from 'rxjs';
import { CurrentUnitService } from './current-unit.service';
import { UnitRoutePageMetaResolver } from './unit-route-page-meta.resolver';

Expand All @@ -28,13 +27,12 @@ describe('UnitRoutePageMetaResolver', () => {

it('should emit breadcrumb with translated i18n key, using current item as params', async () => {
expect(
await resolver
.resolveBreadcrumbs({
await firstValueFrom(
resolver.resolveBreadcrumbs({
url: 'testPath',
pageMetaConfig: { breadcrumb: { i18n: 'testTranslation' } },
})
.pipe(take(1))
.toPromise()
)
).toEqual([{ label: 'testTranslation name:testName', link: 'testPath' }]);
});
});
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { TestBed } from '@angular/core/testing';
import { I18nTestingModule } from '@spartacus/core';
import { UserGroup } from '@spartacus/organization/administration/core';
import { Observable, of } from 'rxjs';
import { take } from 'rxjs/operators';
import { firstValueFrom, Observable, of } from 'rxjs';
import { CurrentUserGroupService } from './current-user-group.service';
import { UserGroupRoutePageMetaResolver } from './user-group-route-page-meta.resolver';

Expand All @@ -29,13 +28,12 @@ describe('UserGroupRoutePageMetaResolver', () => {

it('should emit breadcrumb with translated i18n key, using current item as params', async () => {
expect(
await resolver
.resolveBreadcrumbs({
await firstValueFrom(
resolver.resolveBreadcrumbs({
url: 'testPath',
pageMetaConfig: { breadcrumb: { i18n: 'testTranslation' } },
})
.pipe(take(1))
.toPromise()
)
).toEqual([{ label: 'testTranslation name:testName', link: 'testPath' }]);
});
});
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { TestBed } from '@angular/core/testing';
import { B2BUser, I18nTestingModule } from '@spartacus/core';
import { Observable, of } from 'rxjs';
import { take } from 'rxjs/operators';
import { firstValueFrom, Observable, of } from 'rxjs';
import { CurrentUserService } from './current-user.service';
import { UserRoutePageMetaResolver } from './user-route-page-meta.resolver';

Expand All @@ -28,13 +27,12 @@ describe('UserRoutePageMetaResolver', () => {

it('should emit breadcrumb with translated i18n key, using current item as params', async () => {
expect(
await resolver
.resolveBreadcrumbs({
await firstValueFrom(
resolver.resolveBreadcrumbs({
url: 'testPath',
pageMetaConfig: { breadcrumb: { i18n: 'testTranslation' } },
})
.pipe(take(1))
.toPromise()
)
).toEqual([{ label: 'testTranslation name:testName', link: 'testPath' }]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import {
RoutingService,
SemanticPathService,
} from '@spartacus/core';
import { Observable, of } from 'rxjs';
import { take } from 'rxjs/operators';
import { firstValueFrom, Observable, of } from 'rxjs';
import { OrganizationPageMetaResolver } from './organization-page-meta.resolver';

class MockRoutingService {
Expand Down Expand Up @@ -74,7 +73,7 @@ describe('OrganizationPageMetaResolver', () => {

describe('resolveTitle', () => {
it('should emit title of CMS content page ', async () => {
expect(await service.resolveTitle().pipe(take(1)).toPromise()).toBe(
expect(await firstValueFrom(service.resolveTitle())).toBe(
'testContentPageTitle'
);
});
Expand All @@ -89,9 +88,9 @@ describe('OrganizationPageMetaResolver', () => {
});

it('should NOT return breadcrumb for the Organization page', async () => {
expect(
await service.resolveBreadcrumbs().pipe(take(1)).toPromise()
).toEqual([testHomeBreadcrumb]);
expect(await firstValueFrom(service.resolveBreadcrumbs())).toEqual([
testHomeBreadcrumb,
]);
});
});

Expand All @@ -112,9 +111,7 @@ describe('OrganizationPageMetaResolver', () => {
});

it('should insert breadcrumb for the Organization page right after the Homepage breadcrumb', async () => {
expect(
await service.resolveBreadcrumbs().pipe(take(1)).toPromise()
).toEqual([
expect(await firstValueFrom(service.resolveBreadcrumbs())).toEqual([
testHomeBreadcrumb,
organizationBreadcrumb,
testBudgetsBreadcrumb,
Expand Down
12 changes: 5 additions & 7 deletions projects/core/src/auth/user-auth/user-auth.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import { CommonModule } from '@angular/common';
import { APP_INITIALIZER, ModuleWithProviders, NgModule } from '@angular/core';
import { OAuthModule, OAuthStorage } from 'angular-oauth2-oidc';
import { lastValueFrom } from 'rxjs';
import { switchMap } from 'rxjs/operators';
import { ConfigInitializerService } from '../../config/config-initializer/config-initializer.service';
import { provideDefaultConfig } from '../../config/config-providers';
Expand All @@ -26,18 +27,15 @@ export function checkOAuthParamsInUrl(
authService: AuthService,
configInit: ConfigInitializerService
): () => Promise<void> {
const result = () =>
configInit
.getStable()
.pipe(
return () =>
lastValueFrom(
configInit.getStable().pipe(
switchMap(() =>
// Wait for stable config is used, because with auth redirect would kick so quickly that the page would not be loaded correctly
authService.checkOAuthParamsInUrl()
)
)
.toPromise();

return result;
);
}

export function authStatePersistenceFactory(
Expand Down
Loading

0 comments on commit 2a7b2b5

Please sign in to comment.