Skip to content

Commit

Permalink
error bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
OgunyemiO committed Aug 28, 2024
1 parent 18299a7 commit 7704dda
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,25 @@ describe('ServiceMessagesComponent', () => {
let fixture: ComponentFixture<ServiceMessagesComponent>;
const mockFeatureToggleService = jasmine.createSpyObj('FeatureToggleService', ['getValue']);

const
serviceMessagesFake: ServiceMessages[] = [
{
roles: 'caseworker-divorce',
index: 2,
message_en: 'Alert services notification',
message_cy: 'Anyyu',
begin: '2024-04-18T00:00:00',
end: '2034-05-19T00:00:00'
},
{
roles: 'caseworker-probate',
index: 3,
message_en: 'Please submit all required forms today ',
message_cy: 'Anyyu',
begin: '2024-04-18T00:00:00',
end: '2044-04-20T00:00:00'
}
];
const
serviceMessagesFake: ServiceMessages[] = [
{
roles: 'caseworker-divorce',
index: 2,
message_en: 'Alert services notification',
message_cy: 'Anyyu',
begin: '2024-04-18T00:00:00',
end: '2034-05-19T00:00:00'
},
{
roles: 'caseworker-probate',
index: 3,
message_en: 'Please submit all required forms today ',
message_cy: 'Anyyu',
begin: '2024-04-18T00:00:00',
end: '2044-04-20T00:00:00'
}
];

beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
Expand All @@ -58,6 +58,7 @@ describe('ServiceMessagesComponent', () => {
fixture = TestBed.createComponent(ServiceMessagesComponent);
component = fixture.componentInstance;
component.userRoles = ['caseworker-divorce', 'caseworker-probate'];
component.originalMessages = [];
fixture.detectChanges();
});

Expand Down Expand Up @@ -221,8 +222,9 @@ describe('ServiceMessagesComponent', () => {
expect(component.filteredMessages.length).toBe(0);
fixture.detectChanges();
expect(component.isBannerError).toBeTruthy();
expect(component.bannerErrorMsgs).toContain({message:`Invalid start/end date OR The start date is greater than the end date. Message index: ${serviceMessagesFake[0].index}`, index: 2});
expect(component.bannerErrorMsgs).toContain({ message: `Invalid start/end date OR The start date is greater than the end date. Message index: ${serviceMessagesFake[0].index}`, index: 2 });
});

it('should show error message if start date or end date is not well formed', () => {
const serviceMessagesFake: ServiceMessages[] = [
{
Expand All @@ -234,11 +236,11 @@ describe('ServiceMessagesComponent', () => {
end: '2024-04-24T00:00:00'
}
];
component['createFilteredMessages'](serviceMessagesFake);
component.originalMessages = serviceMessagesFake;
component['compareDates'](serviceMessagesFake[0]);
fixture.autoDetectChanges();
expect(component.filteredMessages.length).toBe(0);
fixture.detectChanges();
expect(component.isBannerError).toBeTruthy();
expect(component.bannerErrorMsgs).toContain({message:`Invalid start/end date OR The start date is greater than the end date. Message index: ${serviceMessagesFake[0].index}`, index: 10});
expect(component.isBannerError).toBeTrue();
expect(component.bannerErrorMsgs.length).toBe(1);
expect(component.bannerErrorMsgs).toContain({ message: `Invalid start/end date OR The start date is greater than the end date. Message index: ${serviceMessagesFake[0].index}`, index: 10 });
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ export class ServiceMessagesComponent implements OnInit {
public hiddenBanners: string[];
public filteredMessages: ServiceMessages[] = [];
public isBannerError: boolean;
public bannerErrorMsgs: {message: string, index: number}[] = [];
public bannerErrorMsgs: { message: string, index: number }[] = [];
public originalMessages: ServiceMessages[] = [];

@Input() public userRoles: string[];
@Input() public featureToggleKey: string;
Expand All @@ -26,6 +27,7 @@ export class ServiceMessagesComponent implements OnInit {
public getServiceMessages(): void {
this.featureToggleService.getValue<ServiceMessages[]>(this.featureToggleKey, null)
.subscribe(messages => {
this.originalMessages = messages;
if (!!messages) {
this.createFilteredMessages(messages);
}
Expand All @@ -48,7 +50,7 @@ export class ServiceMessagesComponent implements OnInit {
let currentDateTime = new Date();
let beginDate = null;
let endDate = null;
const findUnique = this.bannerErrorMsgs.filter(rst => rst.index === msg.index);
this.bannerErrorMsgs = [];

if (msg.begin !== undefined && !isNaN(Date.parse(msg.begin))) {
beginDate = new Date(msg.begin);
Expand All @@ -57,28 +59,21 @@ export class ServiceMessagesComponent implements OnInit {
if (msg.end !== undefined && !isNaN(Date.parse(msg.end))) {
endDate = new Date(msg.end);
}
const findAll = this.originalMessages.filter((msg) => (msg.begin || msg.end) && beginDate > endDate || (msg.begin && isNaN(Date.parse(msg.begin))) || (msg.end && isNaN(Date.parse(msg.end))));

if (!isNaN(Date.parse(msg.begin)) && !isNaN(Date.parse(msg.end)) && !(beginDate > endDate)) {
this.bannerErrorMsgs = this.bannerErrorMsgs.filter(ind => ind.index !== msg.index);
}

if ((msg.begin || msg.end) && (
(!isNaN(Date.parse(msg.begin)) && !isNaN(Date.parse(msg.end)) && beginDate > endDate)
|| (msg.begin && isNaN(Date.parse(msg.begin)))
|| (msg.end && isNaN(Date.parse(msg.end)))
)) {
if (findAll.length > 0) {
this.isBannerError = true;
if(findUnique.length === 0) {
this.bannerErrorMsgs = [...this.bannerErrorMsgs, {message:`Invalid start/end date OR The start date is greater than the end date. Message index: ${msg.index}`, index: msg.index}];
}
findAll.forEach(msg => {
this.bannerErrorMsgs = [...this.bannerErrorMsgs, { message: `Invalid start/end date OR The start date is greater than the end date. Message index: ${msg.index}`, index: msg.index }];
})
} else {
this.isBannerError = false;
}

const beginDateOK = !msg.begin || (beginDate && beginDate < currentDateTime);
const endDateOK = !msg.end || (endDate && endDate >= currentDateTime);
return beginDateOK && endDateOK;
}
}

public hideMessage(msg: ServiceMessages): void {
this.filteredMessages = this.filteredMessages.filter(f => f.index !== msg.index)
Expand Down

0 comments on commit 7704dda

Please sign in to comment.