Skip to content

Commit

Permalink
fix: Change the order of password validators depend on feature toggle (
Browse files Browse the repository at this point in the history
  • Loading branch information
rmch91 authored Sep 25, 2024
1 parent 36843d7 commit 0e9ee9d
Show file tree
Hide file tree
Showing 8 changed files with 184 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
import { ReactiveFormsModule } from '@angular/forms';
import { ReactiveFormsModule, UntypedFormControl } from '@angular/forms';
import {
AuthService,
FeatureConfigService,
I18nTestingModule,
RoutingService,
} from '@spartacus/core';
Expand Down Expand Up @@ -77,4 +78,54 @@ describe('OrderGuestRegisterFormComponent', () => {
);
expect(routingService.go).toHaveBeenCalledWith({ cxRoute: 'home' });
});

describe('password validators', () => {
let featureConfigService: FeatureConfigService;

it('should have new validators when feature flag is enabled', () => {
featureConfigService = TestBed.inject(FeatureConfigService);
spyOn(featureConfigService, 'isEnabled').and.returnValue(true);

fixture = TestBed.createComponent(OrderGuestRegisterFormComponent);
component = fixture.componentInstance;

fixture.detectChanges();

const passwordControl = component.guestRegisterForm.get(
'password'
) as UntypedFormControl;
const validators = passwordControl.validator
? passwordControl.validator({} as any)
: [];

expect(passwordControl).toBeTruthy();
expect(validators).toEqual({
required: true,
cxMinOneDigit: true,
cxMinOneUpperCaseCharacter: true,
cxMinOneSpecialCharacter: true,
cxMinSixCharactersLength: true,
});
});

it('should have old validators when feature flag is not enabled', () => {
featureConfigService = TestBed.inject(FeatureConfigService);
spyOn(featureConfigService, 'isEnabled').and.returnValue(false);

fixture = TestBed.createComponent(OrderGuestRegisterFormComponent);
component = fixture.componentInstance;

fixture.detectChanges();

const passwordControl = component.guestRegisterForm.get(
'password'
) as UntypedFormControl;
const validators = passwordControl.validator
? passwordControl.validator({} as any)
: [];

expect(passwordControl).toBeTruthy();
expect(validators).toEqual({ required: true, cxInvalidPassword: true });
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ export class OrderGuestRegisterFormComponent implements OnDestroy {
protected passwordValidators = this.featureConfigService?.isEnabled(
'formErrorsDescriptiveMessages'
)
? [CustomFormValidators.passwordValidator]
: CustomFormValidators.passwordValidators;
? CustomFormValidators.passwordValidators
: [CustomFormValidators.passwordValidator];

@Input() guid: string;
@Input() email: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { Component, Pipe, PipeTransform } from '@angular/core';
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
import { AbstractControl, ReactiveFormsModule } from '@angular/forms';
import {
AbstractControl,
ReactiveFormsModule,
UntypedFormControl,
} from '@angular/forms';
import { By } from '@angular/platform-browser';
import { RouterTestingModule } from '@angular/router/testing';
import { NgSelectModule } from '@ng-select/ng-select';
Expand All @@ -10,18 +14,19 @@ import {
AnonymousConsentsConfig,
AnonymousConsentsService,
AuthConfigService,
BaseSite,
BaseSiteService,
ConsentTemplate,
FeatureConfigService,
GlobalMessageEntities,
GlobalMessageService,
GlobalMessageType,
I18nTestingModule,
LanguageService,
OAuthFlow,
RoutingService,
Title,
BaseSite,
SiteAdapter,
BaseSiteService,
LanguageService,
Title,
} from '@spartacus/core';
import {
CaptchaModule,
Expand Down Expand Up @@ -439,4 +444,54 @@ describe('RegisterComponent', () => {
expect(component.registerUser).toHaveBeenCalledTimes(1);
});
});

describe('password validators', () => {
let featureConfigService: FeatureConfigService;

it('should have new validators when feature flag is enabled', () => {
featureConfigService = TestBed.inject(FeatureConfigService);
spyOn(featureConfigService, 'isEnabled').and.returnValue(true);

fixture = TestBed.createComponent(RegisterComponent);
component = fixture.componentInstance;

fixture.detectChanges();

const passwordControl = component.registerForm.get(
'password'
) as UntypedFormControl;
const validators = passwordControl.validator
? passwordControl.validator({} as any)
: [];

expect(passwordControl).toBeTruthy();
expect(validators).toEqual({
required: true,
cxMinOneDigit: true,
cxMinOneUpperCaseCharacter: true,
cxMinOneSpecialCharacter: true,
cxMinSixCharactersLength: true,
});
});

it('should have old validators when feature flag is not enabled', () => {
featureConfigService = TestBed.inject(FeatureConfigService);
spyOn(featureConfigService, 'isEnabled').and.returnValue(false);

fixture = TestBed.createComponent(RegisterComponent);
component = fixture.componentInstance;

fixture.detectChanges();

const passwordControl = component.registerForm.get(
'password'
) as UntypedFormControl;
const validators = passwordControl.validator
? passwordControl.validator({} as any)
: [];

expect(passwordControl).toBeTruthy();
expect(validators).toEqual({ required: true, cxInvalidPassword: true });
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ export class RegisterComponent implements OnInit, OnDestroy {
protected passwordValidators = this.featureConfigService?.isEnabled(
'formErrorsDescriptiveMessages'
)
? [CustomFormValidators.passwordValidator]
: CustomFormValidators.passwordValidators;
? CustomFormValidators.passwordValidators
: [CustomFormValidators.passwordValidator];

titles$: Observable<Title[]>;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { TestBed } from '@angular/core/testing';
import { AbstractControl, ReactiveFormsModule } from '@angular/forms';
import {
AbstractControl,
ReactiveFormsModule,
UntypedFormControl,
} from '@angular/forms';
import {
FeatureConfigService,
FeaturesConfigModule,
GlobalMessageService,
GlobalMessageType,
Expand Down Expand Up @@ -49,6 +54,7 @@ describe('ResetPasswordComponentService', () => {
let globalMessageService: GlobalMessageService;
let passwordConfirm: AbstractControl;
let password: AbstractControl;
let featureConfigService: FeatureConfigService;

beforeEach(() => {
TestBed.configureTestingModule({
Expand Down Expand Up @@ -78,6 +84,9 @@ describe('ResetPasswordComponentService', () => {
});

beforeEach(() => {
featureConfigService = TestBed.inject(FeatureConfigService);
spyOn(featureConfigService, 'isEnabled').and.returnValue(true);

service = TestBed.inject(ResetPasswordComponentService);

userPasswordService = TestBed.inject(UserPasswordFacade);
Expand Down Expand Up @@ -214,4 +223,24 @@ describe('ResetPasswordComponentService', () => {
expect(routingService.go).not.toHaveBeenCalled();
});
});

describe('password validators', () => {
it('should have new validators when feature flag isEnabled', () => {
const passwordControl = service.form.get(
'password'
) as UntypedFormControl;
const validators = passwordControl.validator
? passwordControl.validator({} as any)
: [];

expect(passwordControl).toBeTruthy();
expect(validators).toEqual({
required: true,
cxMinOneDigit: true,
cxMinOneUpperCaseCharacter: true,
cxMinOneSpecialCharacter: true,
cxMinSixCharactersLength: true,
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ export class ResetPasswordComponentService {
protected passwordValidators = this.featureConfigService?.isEnabled(
'formErrorsDescriptiveMessages'
)
? [CustomFormValidators.passwordValidator]
: CustomFormValidators.passwordValidators;
? CustomFormValidators.passwordValidators
: [CustomFormValidators.passwordValidator];

constructor(
protected userPasswordService: UserPasswordFacade,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { fakeAsync, TestBed, tick } from '@angular/core/testing';
import { AbstractControl, ReactiveFormsModule } from '@angular/forms';
import {
AbstractControl,
ReactiveFormsModule,
UntypedFormControl,
} from '@angular/forms';
import {
AuthRedirectService,
AuthService,
FeatureConfigService,
GlobalMessageService,
GlobalMessageType,
I18nTestingModule,
Expand Down Expand Up @@ -42,6 +47,7 @@ describe('UpdatePasswordComponentService', () => {
let globalMessageService: GlobalMessageService;
let authRedirectService: AuthRedirectService;
let authService: AuthService;
let featureConfigService: FeatureConfigService;

let oldPassword: AbstractControl;
let newPassword: AbstractControl;
Expand Down Expand Up @@ -77,6 +83,9 @@ describe('UpdatePasswordComponentService', () => {
});

beforeEach(() => {
featureConfigService = TestBed.inject(FeatureConfigService);
spyOn(featureConfigService, 'isEnabled').and.returnValue(true);

service = TestBed.inject(UpdatePasswordComponentService);
userPasswordFacade = TestBed.inject(UserPasswordFacade);
routingService = TestBed.inject(RoutingService);
Expand All @@ -98,16 +107,16 @@ describe('UpdatePasswordComponentService', () => {
service['busy$'].next(true);
let result;
service.isUpdating$.subscribe((value) => (result = value)).unsubscribe();
expect(result).toBeTrue();
expect(service.form.disabled).toBeTrue();
expect(result).toBeTruthy();
expect(service.form.disabled).toBeTruthy();
});

it('should return false', () => {
service['busy$'].next(false);
let result;
service.isUpdating$.subscribe((value) => (result = value)).unsubscribe();
expect(result).toBeFalse();
expect(service.form.disabled).toBeFalse();
expect(result).toBeFalsy();
expect(service.form.disabled).toBeFalsy();
});
});

Expand Down Expand Up @@ -175,5 +184,25 @@ describe('UpdatePasswordComponentService', () => {
expect(authService.coreLogout).not.toHaveBeenCalled();
});
});

describe('password validators', () => {
it('should have new validators when feature flag isEnabled', () => {
const newPasswordControl = service.form.get(
'newPassword'
) as UntypedFormControl;
const validators = newPasswordControl.validator
? newPasswordControl.validator({} as any)
: [];

expect(newPasswordControl).toBeTruthy();
expect(validators).toEqual({
required: true,
cxMinOneDigit: true,
cxMinOneUpperCaseCharacter: true,
cxMinOneSpecialCharacter: true,
cxMinSixCharactersLength: true,
});
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ export class UpdatePasswordComponentService {
protected passwordValidators = this.featureConfigService?.isEnabled(
'formErrorsDescriptiveMessages'
)
? [CustomFormValidators.passwordValidator]
: CustomFormValidators.passwordValidators;
? CustomFormValidators.passwordValidators
: [CustomFormValidators.passwordValidator];

constructor(
protected userPasswordService: UserPasswordFacade,
Expand Down

0 comments on commit 0e9ee9d

Please sign in to comment.