I did have to test if that stack overflow code would worked. I made some changes to follow the recommendations of the moment js documentation
function weekDays(month, year) {
const endDate = moment.utc().year(year).month(month).endOf('month');
return Array(endDate.date()).fill(0).map((_, i) => moment.utc().year(year).month(month).date(i + 1))
.map((day) => ({day, week: day.week()}))
.filter(({week}, i, arr) => arr.findIndex((info) => info.week === week) === i)
.map(({day, week}) => ({
week,
days: Array(7).fill(0).map((_, i) => moment.utc(day).week(week).startOf('week').add(i, 'day'))
}));
}
Test with Jest Framework
let testAll = (month, year) => {
const monthYear = moment().year(year).month(month)
const firstDay = monthYear.startOf('month');
// console.log(firstDay)
const endDay = moment().year(year).month(month).endOf("month");
const calendar = weekDays(month, year);
describe("testing calendar", () => {
it('check if first date is in first week / should be true', () => {
expect(calendar[0]["days"].filter((day) => day.date() === firstDay.date())[0].format("YYYY-MM-DD") === firstDay.format("YYYY-MM-DD")).toBe(true);
});
it('check if end date is in end week / should be true', () => {
expect(calendar[calendar.length - 1]["days"].filter((day) => day.date() === endDay.date())[0].format("YYYY-MM-DD") === endDay.format("YYYY-MM-DD")).toBe(true);
});
});
}
Once modified I had to know if it would work, and so the need arise to test the code until 80 years
let locale = ["en", "es", "fr", "de"]
for (let i in locale){
moment.locale(`${locale[i]}`)
for (let y = 2000; y < 2100; y++) {
for (let m = 0; m <= 11; m++) {
testAll(m, y)
console.log(m)
}
}
}
P.S: I'm not an expert in javascript and it's my first unit test
Top comments (0)