Skip to content

Commit

Permalink
chore: code changes
Browse files Browse the repository at this point in the history
  • Loading branch information
anjana-bl committed Jan 23, 2025
1 parent 1c29fdc commit 0b6d99f
Show file tree
Hide file tree
Showing 27 changed files with 630 additions and 125 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ import { of } from 'rxjs';
import { RegisterComponentService } from './register-component.service';

import createSpy = jasmine.createSpy;
const mockRegisterFormData: any = {
titleCode: 'Mr',
firstName: 'John',
lastName: 'Doe',
email: '[email protected]',
email_lowercase: '[email protected]',
termsandconditions: true,
password: 'strongPass$!123',
passwordconf: 'strongPass$!123',
newsletter: true,
captcha: true,
};

class MockUserRegisterFacade implements Partial<UserRegisterFacade> {
getTitles = createSpy().and.returnValue(of([]));
Expand Down Expand Up @@ -98,4 +110,14 @@ describe('RegisterComponentService', () => {
let result = service.getAdditionalConsents();
expect(result).toEqual([]);
});
it('collectDataFromRegisterForm()', () => {
const form = mockRegisterFormData;
expect(service.collectDataFromRegisterForm(form)).toEqual({
firstName: form.firstName,
lastName: form.lastName,
uid: form.email_lowercase,
password: form.password,
titleCode: form.titleCode,
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,15 @@ export class RegisterComponentService {
generateAdditionalConsentsFormControl(): UntypedFormArray | undefined {
return this.fb?.array([]) ?? undefined;
}

collectDataFromRegisterForm(formData: any): UserSignUp {
const { firstName, lastName, email, password, titleCode } = formData;
return {
firstName,
lastName,
uid: email.toLowerCase(),
password,
titleCode,
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@
<div
class="form-check"
*ngFor="let control of additionalConsents.controls; let i = index"
[formGroupName]="i"
>
<div *ngIf="consents[i]?.template?.id as id">
<label>
Expand All @@ -257,12 +258,18 @@
[required]="consents[i].required"
[name]="id"
(change)="updateAdditionalConsents($any($event), i)"
[formControlName]="i"
formControlName="isConsentGranted"
/>
<span class="form-check-label">
{{ consents[i].template.description }}
<ng-template
*ngIf="consents[i].required"
[ngTemplateOutlet]="requiredAsterisk"
></ng-template>
</span>
<cx-form-errors [control]="control"></cx-form-errors>
<cx-form-errors
[control]="control.get('isConsentGranted')"
></cx-form-errors>
</label>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ class MockRegisterComponentService
postRegisterMessage = createSpy();
getAdditionalConsents = createSpy();
generateAdditionalConsentsFormControl = createSpy();
collectDataFromRegisterForm = createSpy();
}

class MockSiteAdapter {
Expand Down Expand Up @@ -309,14 +310,10 @@ describe('RegisterComponent', () => {
describe('collectDataFromRegisterForm()', () => {
it('should return correct register data', () => {
const form = mockRegisterFormData;

expect(component.collectDataFromRegisterForm(form)).toEqual({
firstName: form.firstName,
lastName: form.lastName,
uid: form.email_lowercase,
password: form.password,
titleCode: form.titleCode,
});
component.collectDataFromRegisterForm(form);
expect(
registerComponentService.collectDataFromRegisterForm
).toHaveBeenCalledWith(form);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ export class RegisterComponent implements OnInit, OnDestroy {

updateAdditionalConsents(event: MouseEvent, index: number) {
const { checked } = event.target as HTMLInputElement;
this.registerForm.value.additionalConsents[index] = checked;
this.registerForm.value.additionalConsents[index].isConsentGranted =
checked;
}

constructor(
Expand Down Expand Up @@ -214,15 +215,7 @@ export class RegisterComponent implements OnInit, OnDestroy {
}

collectDataFromRegisterForm(formData: any): UserSignUp {
const { firstName, lastName, email, password, titleCode } = formData;

return {
firstName,
lastName,
uid: email.toLowerCase(),
password,
titleCode,
};
return this.registerComponentService.collectDataFromRegisterForm(formData);
}

isConsentGiven(consent: AnonymousConsent | undefined): boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import createSpy = jasmine.createSpy;
const consentTemplateId = 'xxxx';
const consentTemplateVersion = 0;
class MockCdcUserConsentService implements Partial<CdcUserConsentService> {
updateCdcConsent = createSpy();
updateCdcConsentV2 = createSpy();
}
class MockCdcConsentsLocalStorageService
implements Partial<CdcConsentsLocalStorageService>
Expand Down Expand Up @@ -55,16 +55,15 @@ describe('CdcUserConsentAdapter', () => {
describe('giveConsent()', () => {
it('should update cdc consent', () => {
storage.checkIfConsentExists = createSpy().and.returnValue(true);
cdcUserConsentService.updateCdcConsent = createSpy().and.returnValue(
cdcUserConsentService.updateCdcConsentV2 = createSpy().and.returnValue(
of({ errorCode: 0 })
);
service
.giveConsent('current', consentTemplateId, consentTemplateVersion)
.subscribe();
expect(cdcUserConsentService.updateCdcConsent).toHaveBeenCalledWith(
true,
['xxxx']
);
expect(cdcUserConsentService.updateCdcConsentV2).toHaveBeenCalledWith([
{ id: 'xxxx', isConsentGranted: true },
]);
httpMock.expectOne((req) => {
return (
req.method === 'POST' &&
Expand All @@ -76,25 +75,23 @@ describe('CdcUserConsentAdapter', () => {
});
it('should not call CDC SDK', () => {
storage.checkIfConsentExists = createSpy().and.returnValue(false);
cdcUserConsentService.updateCdcConsent = createSpy().and.returnValue(
cdcUserConsentService.updateCdcConsentV2 = createSpy().and.returnValue(
of({ errorCode: 0 })
);
service.giveConsent('current', 'xxxx', 0).subscribe();
expect(cdcUserConsentService.updateCdcConsent).not.toHaveBeenCalledWith(
true,
['xxxx']
expect(cdcUserConsentService.updateCdcConsentV2).not.toHaveBeenCalledWith(
[{ id: 'xxxx', isConsentGranted: true }]
);
});
it('should not call Commerce API', () => {
storage.checkIfConsentExists = createSpy().and.returnValue(true);
cdcUserConsentService.updateCdcConsent = createSpy().and.returnValue(
cdcUserConsentService.updateCdcConsentV2 = createSpy().and.returnValue(
of({ errorCode: 2 })
);
service.giveConsent('current', 'xxxx', 0).subscribe();
expect(cdcUserConsentService.updateCdcConsent).toHaveBeenCalledWith(
true,
['xxxx']
);
expect(cdcUserConsentService.updateCdcConsentV2).toHaveBeenCalledWith([
{ id: 'xxxx', isConsentGranted: true },
]);
httpMock.expectNone((req) => {
return (
req.method === 'POST' &&
Expand All @@ -108,40 +105,37 @@ describe('CdcUserConsentAdapter', () => {
describe('withdrawConsent()', () => {
it('should update cdc consent', () => {
storage.checkIfConsentExists = createSpy().and.returnValue(true);
cdcUserConsentService.updateCdcConsent = createSpy().and.returnValue(
cdcUserConsentService.updateCdcConsentV2 = createSpy().and.returnValue(
of({ errorCode: 0 })
);
service.withdrawConsent('current', 'code', 'xxxx').subscribe();
expect(cdcUserConsentService.updateCdcConsent).toHaveBeenCalledWith(
false,
['xxxx']
);
expect(cdcUserConsentService.updateCdcConsentV2).toHaveBeenCalledWith([
{ id: 'xxxx', isConsentGranted: false },
]);
httpMock.expectOne((req) => {
return req.method === 'DELETE';
});
httpMock.verify();
});
it('should not call CDC SDK', () => {
storage.checkIfConsentExists = createSpy().and.returnValue(false);
cdcUserConsentService.updateCdcConsent = createSpy().and.returnValue(
cdcUserConsentService.updateCdcConsentV2 = createSpy().and.returnValue(
of({ errorCode: 0 })
);
service.withdrawConsent('current', 'code', 'xxxx').subscribe();
expect(cdcUserConsentService.updateCdcConsent).not.toHaveBeenCalledWith(
false,
['xxxx']
expect(cdcUserConsentService.updateCdcConsentV2).not.toHaveBeenCalledWith(
[{ id: 'xxxx', isConsentGranted: false }]
);
});
it('should not call Commerce API', () => {
storage.checkIfConsentExists = createSpy().and.returnValue(true);
cdcUserConsentService.updateCdcConsent = createSpy().and.returnValue(
cdcUserConsentService.updateCdcConsentV2 = createSpy().and.returnValue(
of({ errorCode: 2 })
);
service.withdrawConsent('current', 'code', 'xxxx').subscribe();
expect(cdcUserConsentService.updateCdcConsent).toHaveBeenCalledWith(
false,
['xxxx']
);
expect(cdcUserConsentService.updateCdcConsentV2).toHaveBeenCalledWith([
{ id: 'xxxx', isConsentGranted: false },
]);
httpMock.expectNone((req) => {
return req.method === 'DELETE';
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class CdcUserConsentAdapter extends OccUserConsentAdapter {
);
} else {
return this.cdcUserConsentService
.updateCdcConsent(true, [consentTemplateId])
.updateCdcConsentV2([{ id: consentTemplateId, isConsentGranted: true }])
.pipe(
catchError((error: any) => throwError(error)),
switchMap((result) => {
Expand All @@ -71,7 +71,7 @@ export class CdcUserConsentAdapter extends OccUserConsentAdapter {
return super.withdrawConsent(userId, consentCode);
} else {
return this.cdcUserConsentService
.updateCdcConsent(false, consentId ? [consentId] : [])
.updateCdcConsentV2([{ id: consentId ?? '', isConsentGranted: false }])
.pipe(
catchError((error: any) => throwError(error)),
switchMap((result) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,16 @@ export interface CdcSiteConsentTemplate {
};
}

export interface CdcConsent {
isConsentGranted?: boolean;
}

export interface CdcLocalStorageTemplate {
id: string;
required: boolean;
}

export interface CdcConsentWithStatus {
id: string;
isConsentGranted: boolean;
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,14 @@ describe('CdcConsentManagementService', () => {
expect(service.getCdcConsentIDs).toHaveBeenCalled();
});
});
describe('checkIfMandatory', () => {
it('should return true if consent is mandatory', () => {
service.getCdcConsentIDs = createSpy().and.returnValue(['a']);
expect(service.checkIfMandatory('a')).toEqual(true);
});
it('should return false if consent is not mandatory', () => {
service.getCdcConsentIDs = createSpy().and.returnValue(['a']);
expect(service.checkIfMandatory('b')).toEqual(false);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,8 @@ export class CdcConsentManagementComponentService extends ConsentManagementCompo
});
return consentIDs;
}

checkIfMandatory(id: string): boolean {
return this.getCdcConsentIDs(true).includes(id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ describe('CdcUserConsentService()', () => {
cdcJsService.setUserConsentPreferences = createSpy().and.returnValue(
of(mockCdcSdkOutput)
);
service.updateCdcConsent(true, ['others.survey']);
service.updateCdcConsentV2([
{ id: 'others.survey', isConsentGranted: true },
]);
expect(cdcJsService.setUserConsentPreferences).toHaveBeenCalledWith(
'[email protected]',
'en',
Expand All @@ -119,7 +121,9 @@ describe('CdcUserConsentService()', () => {
cdcJsService.setUserConsentPreferences = createSpy().and.returnValue(
of(mockCdcSdkOutput)
);
service.updateCdcConsent(false, ['others.survey']);
service.updateCdcConsentV2([
{ id: 'others.survey', isConsentGranted: false },
]);
expect(cdcJsService.setUserConsentPreferences).toHaveBeenCalledWith(
'[email protected]',
'en',
Expand Down
Loading

0 comments on commit 0b6d99f

Please sign in to comment.