// (C) 2007-2019 GoodData Corporation import { format } from "date-fns"; import { datesBetween } from "../datesBetween"; describe("datesBetween", () => { const formatDate = (date: Date) => format(date, "MM-DD-YYYY"); it("should gen. sequence if dates are the same", () => { const startDate = new Date("01-01-2017"); const finalDate = new Date("01-01-2017"); const sequence = datesBetween(startDate, finalDate); expect(Array.from(sequence).map(formatDate)).toEqual(["01-01-2017"]); }); it("should gen. sequence of dates between the starting and final date", () => { const startDate = new Date("01-01-2017"); const finalDate = new Date("01-02-2017"); const sequence = datesBetween(startDate, finalDate); expect(Array.from(sequence).map(formatDate)).toEqual(["01-01-2017", "01-02-2017"]); }); it("should gen. sequence in such way, that the dates are not mutated", () => { const addOneDay = (date: Date) => date.setDate(date.getDate() + 1); const startDate = new Date("01-01-2017"); const finalDate = new Date("01-03-2017"); const sequence = datesBetween(startDate, finalDate); const firstDate = sequence.next().value; expect(formatDate(firstDate)).toEqual("01-01-2017"); // If yield date wouldn't be cloned before yielding, // adding one day would modify internal date as well addOneDay(firstDate); const secondDate = sequence.next().value; expect(secondDate).not.toBeUndefined(); expect(formatDate(secondDate)).toEqual("01-02-2017"); }); });