From df80d85510d20efc6decc70e231b6b00c5facde1 Mon Sep 17 00:00:00 2001 From: Scar Ai <38693320+scarai-sap@users.noreply.github.com> Date: Wed, 7 Aug 2024 14:17:35 +0800 Subject: [PATCH] fix: Show error msg when B2B register failed (#19084) --- .../translations/en/userRegistration.json | 3 +- .../user-registration-form.component.spec.ts | 43 ++++++++++++++++++- .../form/user-registration-form.component.ts | 26 +++++++++-- 3 files changed, 66 insertions(+), 6 deletions(-) diff --git a/feature-libs/organization/user-registration/assets/translations/en/userRegistration.json b/feature-libs/organization/user-registration/assets/translations/en/userRegistration.json index 35cbf115612..04fe4cb0136 100644 --- a/feature-libs/organization/user-registration/assets/translations/en/userRegistration.json +++ b/feature-libs/organization/user-registration/assets/translations/en/userRegistration.json @@ -60,6 +60,7 @@ "goToLoginButtonLabel": "Already registered? Go to Sign in", "httpHandlers": { "conflict": "User with this e-mail address already exists." - } + }, + "messageToFailedToRegister": "Failed to register. Please contact your admin for further assistance." } } diff --git a/feature-libs/organization/user-registration/components/form/user-registration-form.component.spec.ts b/feature-libs/organization/user-registration/components/form/user-registration-form.component.spec.ts index 68272c5bbc0..6f3c7a66c45 100644 --- a/feature-libs/organization/user-registration/components/form/user-registration-form.component.spec.ts +++ b/feature-libs/organization/user-registration/components/form/user-registration-form.component.spec.ts @@ -4,7 +4,15 @@ import { FormControl, FormGroup, ReactiveFormsModule } from '@angular/forms'; import { By } from '@angular/platform-browser'; import { RouterTestingModule } from '@angular/router/testing'; import { NgSelectModule } from '@ng-select/ng-select'; -import { Country, I18nTestingModule, Region, Title } from '@spartacus/core'; +import { + Country, + GlobalMessageService, + GlobalMessageType, + I18nTestingModule, + Region, + Title, + Translatable, +} from '@spartacus/core'; import { OrganizationUserRegistrationForm } from '@spartacus/organization/user-registration/root'; import { FormErrorsModule, @@ -12,7 +20,7 @@ import { SpinnerComponent, } from '@spartacus/storefront'; import { MockFeatureDirective } from 'projects/storefrontlib/shared/test/mock-feature-directive'; -import { Observable, of } from 'rxjs'; +import { Observable, of, throwError } from 'rxjs'; import { UserRegistrationFormComponent } from './user-registration-form.component'; import { UserRegistrationFormService } from './user-registration-form.service'; @@ -36,6 +44,10 @@ const mockTitles: Title[] = [ }, ]; +class MockGlobalMessageService implements Partial { + add(_: string | Translatable, __: GlobalMessageType, ___?: number): void {} +} + const mockCountries: Country[] = [ { isocode: 'CA', @@ -113,6 +125,7 @@ describe('UserRegistrationFormComponent', () => { let component: UserRegistrationFormComponent; let fixture: ComponentFixture; let el: DebugElement; + let msgServcie: MockGlobalMessageService; let userRegistrationFormService: UserRegistrationFormService; @@ -137,10 +150,15 @@ describe('UserRegistrationFormComponent', () => { provide: UserRegistrationFormService, useClass: MockUserRegistrationFormService, }, + { + provide: GlobalMessageService, + useClass: MockGlobalMessageService, + }, ], }); userRegistrationFormService = TestBed.inject(UserRegistrationFormService); + msgServcie = TestBed.inject(GlobalMessageService); })); beforeEach(() => { @@ -203,6 +221,27 @@ describe('UserRegistrationFormComponent', () => { ); }); + it('should show error message if service response failed ', () => { + spyOn(userRegistrationFormService, 'registerUser').and.returnValue( + throwError(() => new Error('Simulated error')) + ); + component.registerForm.patchValue({ + ...mockOrganizationUser, + companyName: 'New Company Inc.', + }); + spyOn(msgServcie, 'add').and.callThrough(); + component.registerForm.markAllAsTouched(); + + component.submit(); + + const spinner = fixture.debugElement.query(By.css('cx-spinner')); + expect(spinner).toBeNull(); + expect(msgServcie.add).toHaveBeenCalledWith( + { key: 'userRegistrationForm.messageToFailedToRegister' }, + GlobalMessageType.MSG_TYPE_ERROR + ); + }); + it('should not register organization user with invalid form', () => { spyOn(userRegistrationFormService, 'registerUser').and.callThrough(); component.registerForm.reset(); diff --git a/feature-libs/organization/user-registration/components/form/user-registration-form.component.ts b/feature-libs/organization/user-registration/components/form/user-registration-form.component.ts index faf4051d160..09bdf4511f7 100644 --- a/feature-libs/organization/user-registration/components/form/user-registration-form.component.ts +++ b/feature-libs/organization/user-registration/components/form/user-registration-form.component.ts @@ -4,9 +4,19 @@ * SPDX-License-Identifier: Apache-2.0 */ -import { ChangeDetectionStrategy, Component, OnDestroy } from '@angular/core'; +import { + ChangeDetectionStrategy, + Component, + inject, + OnDestroy, +} from '@angular/core'; import { FormGroup } from '@angular/forms'; -import { Country, Region } from '@spartacus/core'; +import { + Country, + GlobalMessageService, + GlobalMessageType, + Region, +} from '@spartacus/core'; import { Title } from '@spartacus/user/profile/root'; import { BehaviorSubject, Observable, Subscription } from 'rxjs'; import { UserRegistrationFormService } from './user-registration-form.service'; @@ -31,6 +41,10 @@ export class UserRegistrationFormComponent implements OnDestroy { protected subscriptions = new Subscription(); + protected globalMessageService = inject(GlobalMessageService, { + optional: true, + }); + constructor( protected userRegistrationFormService: UserRegistrationFormService ) {} @@ -43,7 +57,13 @@ export class UserRegistrationFormComponent implements OnDestroy { .registerUser(this.registerForm) .subscribe({ complete: () => this.isLoading$.next(false), - error: () => this.isLoading$.next(false), + error: () => { + this.isLoading$.next(false); + this.globalMessageService?.add( + { key: 'userRegistrationForm.messageToFailedToRegister' }, + GlobalMessageType.MSG_TYPE_ERROR + ); + }, }) ); } else {