[UXP-10] Test cases

This commit is contained in:
Sufiyan Shaikh
2022-06-13 18:43:50 +05:30
committed by Sufiyan Shaikh
parent 50e849dd44
commit 2532e37010
5 changed files with 56 additions and 11 deletions

View File

@@ -537,7 +537,7 @@ describe('EPersonFormComponent', () => {
}); });
it('should call epersonRegistrationService.registerEmail', () => { it('should call epersonRegistrationService.registerEmail', () => {
expect(epersonRegistrationService.registerEmail).toHaveBeenCalledWith(ePersonEmail); expect(epersonRegistrationService.registerEmail).toHaveBeenCalledWith(ePersonEmail, null);
}); });
}); });
}); });

View File

@@ -78,7 +78,7 @@ describe('EpersonRegistrationService', () => {
describe('registerEmail', () => { describe('registerEmail', () => {
it('should send an email registration', () => { it('should send an email registration', () => {
const expected = service.registerEmail('test@mail.org'); const expected = service.registerEmail('test@mail.org', null);
expect(requestService.send).toHaveBeenCalledWith(new PostRequest('request-id', 'rest-url/registrations', registration)); expect(requestService.send).toHaveBeenCalledWith(new PostRequest('request-id', 'rest-url/registrations', registration));
expect(expected).toBeObservable(cold('(a|)', { a: rd })); expect(expected).toBeObservable(cold('(a|)', { a: rd }));

View File

@@ -14,6 +14,8 @@ import { RouterStub } from '../shared/testing/router.stub';
import { NotificationsServiceStub } from '../shared/testing/notifications-service.stub'; import { NotificationsServiceStub } from '../shared/testing/notifications-service.stub';
import { RegisterEmailFormComponent } from './register-email-form.component'; import { RegisterEmailFormComponent } from './register-email-form.component';
import { createSuccessfulRemoteDataObject$ } from '../shared/remote-data.utils'; import { createSuccessfulRemoteDataObject$ } from '../shared/remote-data.utils';
import { ConfigurationDataService } from '../core/data/configuration-data.service';
import { GoogleRecaptchaService } from '../core/data/google-recaptcha.service';
describe('RegisterEmailComponent', () => { describe('RegisterEmailComponent', () => {
@@ -24,6 +26,17 @@ describe('RegisterEmailComponent', () => {
let epersonRegistrationService: EpersonRegistrationService; let epersonRegistrationService: EpersonRegistrationService;
let notificationsService; let notificationsService;
const configurationDataService = jasmine.createSpyObj('configurationDataService', {
findByPropertyName: jasmine.createSpy('findByPropertyName')
});
const googleRecaptchaService = jasmine.createSpyObj('googleRecaptchaService', {
getRecaptchaToken: jasmine.createSpy('getRecaptchaToken')
});
const confResponse$ = createSuccessfulRemoteDataObject$({ values: ['true'] });
const confResponseDisabled$ = createSuccessfulRemoteDataObject$({ values: ['false'] });
beforeEach(waitForAsync(() => { beforeEach(waitForAsync(() => {
router = new RouterStub(); router = new RouterStub();
@@ -39,8 +52,10 @@ describe('RegisterEmailComponent', () => {
providers: [ providers: [
{provide: Router, useValue: router}, {provide: Router, useValue: router},
{provide: EpersonRegistrationService, useValue: epersonRegistrationService}, {provide: EpersonRegistrationService, useValue: epersonRegistrationService},
{provide: ConfigurationDataService, useValue: configurationDataService},
{provide: FormBuilder, useValue: new FormBuilder()}, {provide: FormBuilder, useValue: new FormBuilder()},
{provide: NotificationsService, useValue: notificationsService}, {provide: NotificationsService, useValue: notificationsService},
{provide: GoogleRecaptchaService, useValue: googleRecaptchaService},
], ],
schemas: [CUSTOM_ELEMENTS_SCHEMA] schemas: [CUSTOM_ELEMENTS_SCHEMA]
}).compileComponents(); }).compileComponents();
@@ -48,6 +63,8 @@ describe('RegisterEmailComponent', () => {
beforeEach(() => { beforeEach(() => {
fixture = TestBed.createComponent(RegisterEmailFormComponent); fixture = TestBed.createComponent(RegisterEmailFormComponent);
comp = fixture.componentInstance; comp = fixture.componentInstance;
configurationDataService.findByPropertyName.and.returnValues(confResponse$, confResponse$, confResponse$, confResponse$, confResponse$, confResponse$, confResponse$, confResponse$, confResponse$, confResponse$);
googleRecaptchaService.getRecaptchaToken.and.returnValue(observableOf('googleRecaptchaToken'));
fixture.detectChanges(); fixture.detectChanges();
}); });
@@ -71,21 +88,47 @@ describe('RegisterEmailComponent', () => {
}); });
}); });
describe('register', () => { describe('register', () => {
it('should send a registration to the service and on success display a message and return to home', () => { it('should send a registration to the service with google recaptcha and on success display a message and return to home', () => {
comp.form.patchValue({email: 'valid@email.org'}); comp.form.patchValue({email: 'valid@email.org'});
comp.register(); comp.register();
expect(epersonRegistrationService.registerEmail).toHaveBeenCalledWith('valid@email.org'); expect(epersonRegistrationService.registerEmail).toHaveBeenCalledWith('valid@email.org', 'googleRecaptchaToken');
expect(notificationsService.success).toHaveBeenCalled(); expect(notificationsService.success).toHaveBeenCalled();
expect(router.navigate).toHaveBeenCalledWith(['/home']); expect(router.navigate).toHaveBeenCalledWith(['/home']);
}); });
it('should send a registration to the service and on error display a message', () => { it('should send a registration to the service with google recaptcha and on error display a message', () => {
(epersonRegistrationService.registerEmail as jasmine.Spy).and.returnValue(observableOf(new RestResponse(false, 400, 'Bad Request'))); (epersonRegistrationService.registerEmail as jasmine.Spy).and.returnValue(observableOf(new RestResponse(false, 400, 'Bad Request')));
comp.form.patchValue({email: 'valid@email.org'}); comp.form.patchValue({email: 'valid@email.org'});
comp.register(); comp.register();
expect(epersonRegistrationService.registerEmail).toHaveBeenCalledWith('valid@email.org'); expect(epersonRegistrationService.registerEmail).toHaveBeenCalledWith('valid@email.org', 'googleRecaptchaToken');
expect(notificationsService.error).toHaveBeenCalled();
expect(router.navigate).not.toHaveBeenCalled();
});
});
describe('register', () => {
beforeEach(waitForAsync(() => {
configurationDataService.findByPropertyName.and.returnValues(confResponseDisabled$, confResponseDisabled$, confResponseDisabled$, confResponseDisabled$, confResponseDisabled$, confResponseDisabled$, confResponseDisabled$, confResponseDisabled$, confResponseDisabled$, confResponseDisabled$);
comp.ngOnInit();
fixture.detectChanges();
}));
it('should send a registration to the service without google recaptcha and on success display a message and return to home', () => {
comp.form.patchValue({email: 'valid@email.org'});
comp.register();
expect(epersonRegistrationService.registerEmail).toHaveBeenCalledWith('valid@email.org', null);
expect(notificationsService.success).toHaveBeenCalled();
expect(router.navigate).toHaveBeenCalledWith(['/home']);
});
it('should send a registration to the service without google recaptcha and on error display a message', () => {
(epersonRegistrationService.registerEmail as jasmine.Spy).and.returnValue(observableOf(new RestResponse(false, 400, 'Bad Request')));
comp.form.patchValue({email: 'valid@email.org'});
comp.register();
expect(epersonRegistrationService.registerEmail).toHaveBeenCalledWith('valid@email.org', null);
expect(notificationsService.error).toHaveBeenCalled(); expect(notificationsService.error).toHaveBeenCalled();
expect(router.navigate).not.toHaveBeenCalled(); expect(router.navigate).not.toHaveBeenCalled();
}); });

View File

@@ -74,9 +74,9 @@ export class RegisterEmailFormComponent implements OnInit {
register() { register() {
if (!this.form.invalid) { if (!this.form.invalid) {
if (this.registrationVerification) { if (this.registrationVerification) {
this.googleRecaptchaService.getRecaptchaToken('register_email').subscribe(res => { this.googleRecaptchaService.getRecaptchaToken('register_email').subscribe(captcha => {
if (isNotEmpty(res)) { if (isNotEmpty(captcha)) {
this.registeration(res); this.registeration(captcha);
} else { } else {
this.notificationService.error(this.translateService.get(`${this.MESSAGE_PREFIX}.error.head`), this.notificationService.error(this.translateService.get(`${this.MESSAGE_PREFIX}.error.head`),
this.translateService.get(`${this.MESSAGE_PREFIX}.error.recaptcha`, {email: this.email.value})); this.translateService.get(`${this.MESSAGE_PREFIX}.error.recaptcha`, {email: this.email.value}));
@@ -91,7 +91,7 @@ export class RegisterEmailFormComponent implements OnInit {
/** /**
* Register an email address * Register an email address
*/ */
registeration(captchaToken) { registeration(captchaToken) {
this.epersonRegistrationService.registerEmail(this.email.value, captchaToken).subscribe((response: RemoteData<Registration>) => { this.epersonRegistrationService.registerEmail(this.email.value, captchaToken).subscribe((response: RemoteData<Registration>) => {
if (response.hasSucceeded) { if (response.hasSucceeded) {
this.notificationService.success(this.translateService.get(`${this.MESSAGE_PREFIX}.success.head`), this.notificationService.success(this.translateService.get(`${this.MESSAGE_PREFIX}.success.head`),
@@ -102,7 +102,7 @@ export class RegisterEmailFormComponent implements OnInit {
this.translateService.get(`${this.MESSAGE_PREFIX}.error.content`, {email: this.email.value})); this.translateService.get(`${this.MESSAGE_PREFIX}.error.content`, {email: this.email.value}));
} }
}); });
} }
get email() { get email() {
return this.form.get('email'); return this.form.get('email');

View File

@@ -13,6 +13,8 @@ export const environment: BuildConfig = {
time: false time: false
}, },
recaptchaSiteKey: '6LfmfEsgAAAAACNqQ0aHqJa0HOHcUsvv2OCiEbV4',
// Angular Universal server settings. // Angular Universal server settings.
ui: { ui: {
ssl: false, ssl: false,