From 667ceb0e718ca3df287bf3e1e0b216cb36d41dd6 Mon Sep 17 00:00:00 2001 From: Arnaud Vergnet Date: Sun, 22 Mar 2020 16:05:28 +0100 Subject: [PATCH] Added remaining tests --- .gitignore | 5 +- .../All_Tests__coverage_.xml | 12 +++ utils/PlanningEventManager.js | 9 +- utils/__test__/PlanningEventManager.test.js | 82 ++++++++++++++++++- 4 files changed, 102 insertions(+), 6 deletions(-) create mode 100644 .idea/runConfigurations/All_Tests__coverage_.xml diff --git a/.gitignore b/.gitignore index 70940e0..aa13269 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,9 @@ web-build/ web-report/ /.expo-shared/ /coverage/ -/.idea/ /package-lock.json /passwords.json + +!/.idea/ +/.idea/* +!/.idea/runConfigurations diff --git a/.idea/runConfigurations/All_Tests__coverage_.xml b/.idea/runConfigurations/All_Tests__coverage_.xml new file mode 100644 index 0000000..8b07c24 --- /dev/null +++ b/.idea/runConfigurations/All_Tests__coverage_.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/utils/PlanningEventManager.js b/utils/PlanningEventManager.js index c3b64f7..b32d3af 100644 --- a/utils/PlanningEventManager.js +++ b/utils/PlanningEventManager.js @@ -24,7 +24,7 @@ export default class PlanningEventManager { * @return {string} The string representation */ static getCurrentDateString(): string { - return PlanningEventManager.dateToString(new Date()); + return PlanningEventManager.dateToString(new Date(Date.now())); } /** @@ -180,9 +180,10 @@ export default class PlanningEventManager { * @return {Object} */ static generateEmptyCalendar(numberOfMonths: number): Object { - const end = new Date(new Date().setMonth(new Date().getMonth() + numberOfMonths + 1)); + let end = new Date(Date.now()); + end.setMonth(end.getMonth() + numberOfMonths); let daysOfYear = {}; - for (let d = new Date(); d <= end; d.setDate(d.getDate() + 1)) { + for (let d = new Date(Date.now()); d <= end; d.setDate(d.getDate() + 1)) { const dateString = PlanningEventManager.getDateOnlyString( PlanningEventManager.dateToString(new Date(d))); if (dateString !== null) @@ -229,7 +230,7 @@ export default class PlanningEventManager { eventArray.push(event); else { for (let i = 0; i < eventArray.length; i++) { - if (PlanningEventManager.isEventBefore(event.date_begin, eventArray[i].date_end)) { + if (PlanningEventManager.isEventBefore(event.date_begin, eventArray[i].date_begin)) { eventArray.splice(i, 0, event); break; } else if (i === eventArray.length - 1) { diff --git a/utils/__test__/PlanningEventManager.test.js b/utils/__test__/PlanningEventManager.test.js index a125a16..0ec7ed2 100644 --- a/utils/__test__/PlanningEventManager.test.js +++ b/utils/__test__/PlanningEventManager.test.js @@ -94,6 +94,8 @@ test('isEventBefore', () => { expect(PlanningEventManager.isEventBefore( "2020-03-21 10:15", "2020-03-30 10:15")).toBeTrue(); + expect(PlanningEventManager.isEventBefore( + "2020-03-21 10:00", "2020-03-21 10:00")).toBeFalse(); expect(PlanningEventManager.isEventBefore( "2020-03-21 10:00", "2020-03-21 09:00")).toBeFalse(); expect(PlanningEventManager.isEventBefore( @@ -125,6 +127,84 @@ test('dateToString', () => { }); test('generateEmptyCalendar', () => { - + jest.spyOn(Date, 'now') + .mockImplementation(() => + new Date('2020-01-14T00:00:00.000Z').getTime() + ); + let calendar = PlanningEventManager.generateEmptyCalendar(1); + expect(calendar).toHaveProperty("2020-01-14"); + expect(calendar).toHaveProperty("2020-01-20"); + expect(calendar).toHaveProperty("2020-02-10"); + expect(Object.keys(calendar).length).toBe(32); + calendar = PlanningEventManager.generateEmptyCalendar(3); + expect(calendar).toHaveProperty("2020-01-14"); + expect(calendar).toHaveProperty("2020-01-20"); + expect(calendar).toHaveProperty("2020-02-10"); + expect(calendar).toHaveProperty("2020-02-14"); + expect(calendar).toHaveProperty("2020-03-20"); + expect(calendar).toHaveProperty("2020-04-12"); + expect(Object.keys(calendar).length).toBe(92); }); +test('pushEventInOrder', () => { + let eventArray = []; + let event1 = {date_begin: "2020-01-14 09:15"}; + PlanningEventManager.pushEventInOrder(eventArray, event1); + expect(eventArray.length).toBe(1); + expect(eventArray[0]).toBe(event1); + + let event2 = {date_begin: "2020-01-14 10:15"}; + PlanningEventManager.pushEventInOrder(eventArray, event2); + expect(eventArray.length).toBe(2); + expect(eventArray[0]).toBe(event1); + expect(eventArray[1]).toBe(event2); + + let event3 = {date_begin: "2020-01-14 10:15", title: "garbage"}; + PlanningEventManager.pushEventInOrder(eventArray, event3); + expect(eventArray.length).toBe(3); + expect(eventArray[0]).toBe(event1); + expect(eventArray[1]).toBe(event2); + expect(eventArray[2]).toBe(event3); + + let event4 = {date_begin: "2020-01-13 09:00"}; + PlanningEventManager.pushEventInOrder(eventArray, event4); + expect(eventArray.length).toBe(4); + expect(eventArray[0]).toBe(event4); + expect(eventArray[1]).toBe(event1); + expect(eventArray[2]).toBe(event2); + expect(eventArray[3]).toBe(event3); +}); + +test('generateEventAgenda', () => { + jest.spyOn(Date, 'now') + .mockImplementation(() => + new Date('2020-01-14T00:00:00.000Z').getTime() + ); + let eventList = [ + {date_begin: "2020-01-14 09:15"}, + {date_begin: "2020-02-01 09:15"}, + {date_begin: "2020-01-15 09:15"}, + {date_begin: "2020-02-01 09:30"}, + {date_begin: "2020-02-01 08:30"}, + ]; + const calendar = PlanningEventManager.generateEventAgenda(eventList, 2); + expect(calendar["2020-01-14"].length).toBe(1); + expect(calendar["2020-01-14"][0]).toBe(eventList[0]); + expect(calendar["2020-01-15"].length).toBe(1); + expect(calendar["2020-01-15"][0]).toBe(eventList[2]); + expect(calendar["2020-02-01"].length).toBe(3); + expect(calendar["2020-02-01"][0]).toBe(eventList[4]); + expect(calendar["2020-02-01"][1]).toBe(eventList[1]); + expect(calendar["2020-02-01"][2]).toBe(eventList[3]); +}); + +test('getCurrentDateString', () => { + jest.spyOn(Date, 'now') + .mockImplementation(() => { + let date = new Date(); + date.setFullYear(2020, 0, 14); + date.setHours(15, 30, 54, 65); + return date.getTime(); + }); + expect(PlanningEventManager.getCurrentDateString()).toBe('2020-01-14 15:30'); +});