Skip to content

Commit 88c75d4

Browse files
committed
feat(portail-usager): testing edge cases where server data is different
1 parent f9b556a commit 88c75d4

File tree

6 files changed

+87
-17
lines changed

6 files changed

+87
-17
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { StructureInformation } from "./../interfaces/StructureInformation.interface";
2+
import { createDate } from "../../_core";
3+
import { UserStructureResume } from "../../users";
4+
import { isWithinInterval } from "date-fns";
5+
6+
export class StructureInformationMessage implements StructureInformation {
7+
public title: string;
8+
public description: string;
9+
public startDate: Date;
10+
public endDate: Date | null;
11+
public type: "closing" | "opening-hours" | "general" | "other";
12+
public createdBy: UserStructureResume;
13+
public structureId: number;
14+
public isTemporary: boolean;
15+
public isExpired: boolean;
16+
public uuid?: string | undefined;
17+
public createdAt?: Date;
18+
public updatedAt?: Date;
19+
public version?: number;
20+
constructor(options: StructureInformation) {
21+
this.title = options.title;
22+
this.description = options.description;
23+
this.startDate = options.startDate;
24+
this.endDate = options.endDate;
25+
this.type = options.type;
26+
this.createdAt = createDate(options.createdAt) ?? undefined;
27+
this.updatedAt = createDate(options.updatedAt) ?? undefined;
28+
this.createdBy = options.createdBy;
29+
this.structureId = options.structureId;
30+
this.isTemporary = options.isTemporary;
31+
this.isExpired = this.isMessageExpired();
32+
this.uuid = options.uuid;
33+
this.version = options.version;
34+
}
35+
36+
public isMessageExpired(): boolean {
37+
if (!this.endDate || !this.startDate || !this.isTemporary) return false;
38+
const today = new Date();
39+
return !isWithinInterval(today, {
40+
start: new Date(this.startDate),
41+
end: new Date(this.endDate),
42+
});
43+
}
44+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./StructureInformation.class";

packages/common/src/structure-information/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
export * from "./constants";
33
export * from "./interfaces";
44
export * from "./types";
5+
export * from "./classes";

packages/portail-usagers/src/_tests/mocks/STRUCTURE_INFORMATION.mock.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,25 @@ export const unMessageFutur: StructureInformation = {
4444
days: 4,
4545
}),
4646
};
47+
48+
export const unMessageFuturAvecIsoDate = {
49+
...unMessageCourant,
50+
title: "Un message futur",
51+
startDate: add(new Date(), {
52+
days: 2,
53+
}).toISOString(),
54+
endDate: add(new Date(), {
55+
days: 4,
56+
}).toISOString(),
57+
};
58+
59+
export const unMessageAvecIsoDate = {
60+
...unMessageCourant,
61+
title: "Un message courant avec text date",
62+
startDate: sub(new Date(), {
63+
days: 4,
64+
}).toISOString(),
65+
endDate: add(new Date(), {
66+
days: 1,
67+
}).toISOString(),
68+
};

packages/portail-usagers/src/app/modules/usager-account/components/home-usager/home-usager.component.spec.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ import { StructureInformationService } from "../../services/structure-informatio
1313
import { BehaviorSubject, of } from "rxjs";
1414
import { PortailUsagerProfile, StructureInformation } from "@domifa/common";
1515
import {
16+
unMessageAvecIsoDate,
1617
unMessageCourant,
1718
unMessageFutur,
19+
unMessageFuturAvecIsoDate,
1820
unMessagePasse,
1921
} from "../../../../../_tests/mocks/STRUCTURE_INFORMATION.mock";
2022
import { unProfilUsager } from "../../../../../_tests/mocks/PORTAIL_USAGER_PROFILE.mock";
@@ -26,6 +28,8 @@ describe("HomeUsagerComponent", () => {
2628
unMessageCourant,
2729
unMessagePasse,
2830
unMessageFutur,
31+
unMessageAvecIsoDate as unknown as StructureInformation, // un message avec date string
32+
unMessageFuturAvecIsoDate as unknown as StructureInformation,
2933
];
3034
const structureInformationService = {
3135
getAllStructureInformation: jest.fn(() => of(messagesStructures)),
@@ -69,9 +73,12 @@ describe("HomeUsagerComponent", () => {
6973

7074
it("Filter out obsolete messages", fakeAsync(() => {
7175
tick();
72-
expect(component.structureInformation.length).toEqual(1);
76+
expect(component.structureInformation.length).toEqual(2);
7377
expect(component.structureInformation[0].title).toEqual(
7478
"Un message courant"
7579
);
80+
expect(component.structureInformation[1].title).toEqual(
81+
"Un message courant avec text date"
82+
);
7683
}));
7784
});

packages/portail-usagers/src/app/modules/usager-account/components/home-usager/home-usager.component.ts

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import { Component, OnInit } from "@angular/core";
22
import { Title } from "@angular/platform-browser";
3-
import { PortailUsagerProfile, StructureInformation } from "@domifa/common";
3+
import {
4+
PortailUsagerProfile,
5+
StructureInformation,
6+
StructureInformationMessage,
7+
} from "@domifa/common";
48
import { UsagerAuthService } from "../../../usager-auth/services/usager-auth.service";
59
import { Router } from "@angular/router";
610
import { Subscription } from "rxjs";
7-
import { isWithinInterval } from "date-fns";
811
import { StructureInformationService } from "../../services/structure-information.service";
912

1013
@Component({
@@ -49,20 +52,12 @@ export class HomeUsagerComponent implements OnInit {
4952
this.subscription.add(
5053
this.structureInformationService.getAllStructureInformation().subscribe({
5154
next: (structureInformation: StructureInformation[]) => {
52-
const today = new Date();
53-
this.structureInformation = structureInformation.filter((info) => {
54-
if (!info.isTemporary) {
55-
return true;
56-
}
57-
58-
if (info.endDate && info.startDate) {
59-
return isWithinInterval(today, {
60-
start: new Date(info.startDate),
61-
end: new Date(info.endDate),
62-
});
63-
}
64-
return false;
65-
});
55+
this.structureInformation = structureInformation
56+
.map(
57+
(_structureInfo) =>
58+
new StructureInformationMessage(_structureInfo)
59+
)
60+
.filter((info) => !info.isExpired);
6661
},
6762
})
6863
);

0 commit comments

Comments
 (0)