Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaed Parkar committed Nov 7, 2023
1 parent 5ce9d4b commit 83f4752
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { SearchForJudicialMemberComponent } from '../search-for-judicial-member/
import { JudicialService } from '../../services/judicial.service';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { PageUrls } from 'src/app/shared/page-url.constants';
import { JudicialMemberDto } from '../models/add-judicial-member.model';

describe('AddJudicialOfficeHoldersComponent', () => {
let component: AddJudicialOfficeHoldersComponent;
Expand Down Expand Up @@ -111,4 +112,58 @@ describe('AddJudicialOfficeHoldersComponent', () => {
expect(routerSpy.navigate).toHaveBeenCalledWith([PageUrls.AddParticipants]);
});
});

describe('prepoplateFormForEdit', () => {
it('should set participantToEdit and editingJudge to true when participant role is Judge', () => {
// Arrange
const participantEmail = '[email protected]';
const judicialMember = new JudicialMemberDto('Test', 'User', 'Test User', participantEmail, '1234567890', '1234');
judicialMember.roleCode = 'Judge';
judicialMember.displayName = 'Test User display name';
component.hearing.judiciaryParticipants = [judicialMember];

// Act
component.prepoplateFormForEdit(participantEmail);

// Assert
expect(component.participantToEdit).toEqual(judicialMember);
expect(component.editingJudge).toBeTrue();
expect(component.editingPanelMember).toBeFalse();
expect(component.showAddPanelMember).toBeFalse();
});

it('should set participantToEdit, editingPanelMember and showAddPanelMember to true when participant role is not Judge', () => {
// Arrange
const participantEmail = '[email protected]';
const panelMemberParticipant = new JudicialMemberDto('Test', 'User', 'Test User', participantEmail, '1234567890', '1234');
panelMemberParticipant.roleCode = 'PanelMember';
component.hearing.judiciaryParticipants = [panelMemberParticipant];

// Act
component.prepoplateFormForEdit(participantEmail);

// Assert
expect(component.participantToEdit).toEqual(panelMemberParticipant);
expect(component.editingJudge).toBeFalse();
expect(component.editingPanelMember).toBeTrue();
expect(component.showAddPanelMember).toBeTrue();
});

it('should log warning when participant is not found', () => {
// Arrange
const participantEmail = '[email protected]';
component.hearing.judiciaryParticipants = [];

// Act
component.prepoplateFormForEdit(participantEmail);

// Assert
expect(loggerSpy.warn.calls.mostRecent().args[0].includes('Unable to find participant to edit.')).toBeTrue();
expect(loggerSpy.warn.calls.mostRecent().args[1]).toBe(participantEmail);
expect(component.participantToEdit).toBeNull();
expect(component.editingJudge).toBeFalse();
expect(component.editingPanelMember).toBeFalse();
expect(component.showAddPanelMember).toBeFalse();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ export class AddJudicialOfficeHoldersComponent implements OnInit, OnDestroy {
showAddPanelMember = false;
addPanelMemberText = this.noPanelMemberText;

editingJudge: boolean;
editingPanelMember: boolean;
editingJudge = false;
editingPanelMember = false;

destroyed$ = new Subject<void>();

Expand All @@ -51,7 +51,7 @@ export class AddJudicialOfficeHoldersComponent implements OnInit, OnDestroy {
this.removeJudiciaryParticipant(participantEmail);
});
this.participantsListComponent.$selectedForEdit.pipe(takeUntil(this.destroyed$)).subscribe(participant => {
this.editParticipant(participant);
this.prepoplateFormForEdit(participant);
});
}

Expand All @@ -60,11 +60,11 @@ export class AddJudicialOfficeHoldersComponent implements OnInit, OnDestroy {
if (!emailToEdit) {
return;
}
this.editParticipant(emailToEdit);
this.prepoplateFormForEdit(emailToEdit);
this.bookingService.removeParticipantEmail();
}

editParticipant(participantEmail: string) {
prepoplateFormForEdit(participantEmail: string) {
const participantIndex = this.hearing.judiciaryParticipants.findIndex(x => x.email === participantEmail);
if (participantIndex < 0) {
this.logger.warn(`${this.loggerPrefix} Unable to find participant to edit.`, participantEmail);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { Logger } from 'src/app/services/logger';
import { ParticipantItemComponent } from './participant-item.component';
import { VideoHearingsService } from 'src/app/services/video-hearings.service';
import { Constants } from 'src/app/common/constants';
import { ParticipantModel } from 'src/app/common/model/participant.model';
import { PageUrls } from 'src/app/shared/page-url.constants';

const router = {
navigate: jasmine.createSpy('navigate'),
Expand Down Expand Up @@ -70,11 +72,42 @@ describe('ParticipantItemComponent', () => {
it('should edit participant details', () => {
component.isSummaryPage = true;
component.participant = { representee: 'rep', is_judge: false, is_exist_person: false, isJudiciaryMember: false };
component.editParticipant({ email: '[email protected]', is_exist_person: false, is_judge: false, isJudiciaryMember: false });
const participant: ParticipantModel = {
email: '[email protected]',
is_exist_person: false,
is_judge: false,
isJudiciaryMember: false
};
component.editParticipant(participant);
fixture.detectChanges();
expect(bookingServiceSpy.setEditMode).toHaveBeenCalled();
expect(bookingServiceSpy.setParticipantEmail).toHaveBeenCalledWith(participant.email);
expect(router.navigate).toHaveBeenCalledWith([PageUrls.AddParticipants]);
});

it('should edit judicial office holder details', () => {
component.isSummaryPage = true;
component.participant = { representee: 'rep', is_judge: true, is_exist_person: false, isJudiciaryMember: true };
const participant: ParticipantModel = { email: '[email protected]', is_exist_person: false, is_judge: true, isJudiciaryMember: true };
component.editParticipant(participant);
fixture.detectChanges();
expect(bookingServiceSpy.setEditMode).toHaveBeenCalled();
expect(bookingServiceSpy.setEditMode).toHaveBeenCalledWith();
expect(router.navigate).toHaveBeenCalled();
expect(bookingServiceSpy.setParticipantEmail).toHaveBeenCalledWith(participant.email);
expect(router.navigate).toHaveBeenCalledWith([PageUrls.AddJudicialOfficeHolders]);
});

it('should emit edit event for non-summary page', () => {
component.isSummaryPage = false;
const participant: ParticipantModel = {
email: '[email protected]',
is_exist_person: false,
is_judge: false,
isJudiciaryMember: false
};
spyOn(component.edit, 'emit');
component.editParticipant(participant);
fixture.detectChanges();
expect(component.edit.emit).toHaveBeenCalledWith(participant);
});

it('should return true if participant has a representative', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { BookingParticipantListComponent } from './booking-participant-list.comp
import { HearingRoles } from '../../common/model/hearing-roles.model';
import { LinkedParticipant } from '../../services/clients/api-client';
import { ParticipantDetailsComponent } from '../participant-details/participant-details.component';
import { JudiciaryParticipantDetailsModel } from 'src/app/common/model/judiciary-participant-details.model';

describe('BookingParticipantListComponent', () => {
let component: BookingParticipantListComponent;
Expand Down Expand Up @@ -209,4 +210,48 @@ describe('BookingParticipantListComponent', () => {
}
done();
});

it('should sort judiciary participants and members', () => {
const jp1 = new JudiciaryParticipantDetailsModel(
'Mrs',
'Alan',
'Brake',
'Judge',
'[email protected]',
'[email protected]',
'Judge',
'Judge',
'Alan Brake'
);
const jp2 = new JudiciaryParticipantDetailsModel(
'Mr',
'John',
'Doe',
'Winger',
'[email protected]',
'[email protected]',
'Winger',
'PanelMember',
'John Doe'
);
const jp3 = new JudiciaryParticipantDetailsModel(
'Ms',
'Jane',
'Doe',
'Panel Member',
'[email protected]',
'[email protected]',
'Panel Member',
'PanelMember',
'Jane Doe'
);

const judiciaryParticipants = [jp1, jp2, jp3];
component.judiciaryParticipants = judiciaryParticipants;

expect(component.sortedJudiciaryMembers.length).toEqual(3);
expect(component.sortedJudiciaryMembers[0]).toEqual(jp1);
expect(component.sortedJudiciaryMembers[1]).toEqual(jp3);
expect(component.sortedJudiciaryMembers[2]).toEqual(jp2);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export class BookingParticipantListComponent {
} else if (!a.roleCode.includes('Judge') && b.roleCode.includes('Judge')) {
return 1;
} else {
return 0;
return a.firstName.localeCompare(b.firstName);
}
});
this.sortedJudiciaryMembers = sortedJohList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -707,4 +707,43 @@ describe('Video hearing service', () => {
expect(service['modelHearing'].judiciaryParticipants[0]).toEqual(judicialMember2);
});
});

fdescribe('removeJudiciaryParticipant', () => {
it('should remove judiciary participant from modelHearing', () => {
// Arrange
const participantEmail = '[email protected]';
const judicialMember = new JudicialMemberDto('Test', 'User', 'Test User', participantEmail, '1234567890', '1234');
service['modelHearing'].judiciaryParticipants = [judicialMember];

// Act
service.removeJudiciaryParticipant(participantEmail);

// Assert
expect(service['modelHearing'].judiciaryParticipants).not.toContain(judicialMember);
});

it('should not remove judiciary participant if email does not match', () => {
// Arrange
const participantEmail = '[email protected]';
const judicialMember = new JudicialMemberDto('Test', 'User', 'Test User', participantEmail, '1234567890', '1234');
service['modelHearing'].judiciaryParticipants = [judicialMember];

// Act
service.removeJudiciaryParticipant('[email protected]');

// Assert
expect(service['modelHearing'].judiciaryParticipants).toContain(judicialMember);
});

it('should not remove judiciary participant if modelHearing is undefined', () => {
// Arrange
service['modelHearing'] = undefined;

// Act
service.removeJudiciaryParticipant('[email protected]');

// Assert
expect(service['modelHearing']).toBeUndefined();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,8 @@ export class VideoHearingsService {
}

removeJudiciaryParticipant(participantEmail: string) {
const index = this.modelHearing.judiciaryParticipants.findIndex(judicialMember => judicialMember.email === participantEmail);
const index =
this.modelHearing?.judiciaryParticipants?.findIndex(judicialMember => judicialMember.email === participantEmail) ?? -1;
if (index !== -1) {
this.modelHearing.judiciaryParticipants.splice(index, 1);
}
Expand Down

0 comments on commit 83f4752

Please sign in to comment.