Added event validation at time of processing in Events constructor. Events must have a valid date and title to be included. Updated tests.
This commit is contained in:
parent
df41f60101
commit
665de3f8b1
@ -20,8 +20,6 @@ const mockSortedEvents = [
|
|||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
console.log(EventGrouping);
|
|
||||||
|
|
||||||
describe("groupEventsByDay should", () => {
|
describe("groupEventsByDay should", () => {
|
||||||
const eventGrouping = new EventGrouping(mockSortedEvents, GroupTypes.Day);
|
const eventGrouping = new EventGrouping(mockSortedEvents, GroupTypes.Day);
|
||||||
|
|
||||||
|
|||||||
57
src/Events/index.test.ts
Normal file
57
src/Events/index.test.ts
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
const Events = require("./index").default;
|
||||||
|
|
||||||
|
const mockEventsAllValid = [
|
||||||
|
{
|
||||||
|
date: "2022-3-27",
|
||||||
|
title: "d's birthday",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
date: "2022-3-25",
|
||||||
|
title: "my birthday",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
date: "2022-3-25",
|
||||||
|
title: "buy cake for 27th",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
date: "2022-3-20",
|
||||||
|
title: "first day of spring",
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
const mockEventsAllInvalid = [
|
||||||
|
{
|
||||||
|
date: new Date("haha"),
|
||||||
|
title: "first day of spring",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
date: "2022-3-25",
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
describe("Events constructor should", () => {
|
||||||
|
describe("When the events are valid", () => {
|
||||||
|
const events = new Events(mockEventsAllValid);
|
||||||
|
|
||||||
|
test("generate the correct number of events", () => {
|
||||||
|
expect(events.sortedEvents.length).toEqual(4);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("sort the events by date, ascending", () => {
|
||||||
|
const firstEventDate = new Date(events.sortedEvents[0].date).getDate();
|
||||||
|
const lastEventDate = new Date(
|
||||||
|
events.sortedEvents[events.sortedEvents.length - 1].date
|
||||||
|
).getDate();
|
||||||
|
expect(firstEventDate).toEqual(20);
|
||||||
|
expect(lastEventDate).toEqual(27);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("When the events are invalid", () => {
|
||||||
|
const events = new Events(mockEventsAllInvalid);
|
||||||
|
|
||||||
|
test("generate the correct number of events", () => {
|
||||||
|
expect(events.sortedEvents.length).toEqual(0);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@ -1,15 +1,24 @@
|
|||||||
|
import { isValid } from "date-fns";
|
||||||
import Event from "./Event";
|
import Event from "./Event";
|
||||||
|
|
||||||
export default class Events {
|
export default class Events {
|
||||||
sortedEvents: Event[];
|
public sortedEvents: Event[];
|
||||||
|
|
||||||
constructor(events: Event[]) {
|
constructor(events: Event[]) {
|
||||||
this.sortedEvents = this.processEvents(events);
|
this.sortedEvents = this.processEvents(events);
|
||||||
}
|
}
|
||||||
|
|
||||||
processEvents(events): Event[] {
|
private processEvents(events): Event[] {
|
||||||
const _events = events.map((event) => new Event(event));
|
const _events = events.reduce((collection, event) => {
|
||||||
|
// only include valid events
|
||||||
|
if (event.title && event.date && isValid(new Date(event.date))) {
|
||||||
|
collection.push(new Event(event));
|
||||||
|
}
|
||||||
|
|
||||||
|
return collection;
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
// sort events by date, in ascending order
|
||||||
return _events.sort((a, b) => a.date - b.date);
|
return _events.sort((a, b) => a.date - b.date);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user