Skip to content

Commit

Permalink
Merge pull request #8288 from jrjohnson/5908-first-week-year
Browse files Browse the repository at this point in the history
Fix New Year Errors
  • Loading branch information
stopfstedt authored Dec 30, 2024
2 parents ef3bebd + 00bdbeb commit 966882b
Show file tree
Hide file tree
Showing 5 changed files with 200 additions and 103 deletions.
38 changes: 25 additions & 13 deletions packages/ilios-common/addon/components/dashboard/week.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,37 @@
import Component from '@glimmer/component';
import { DateTime } from 'luxon';

export default class DashboardWeekComponent extends Component {
get expanded() {
const now = DateTime.now();
const lastSunday = now.set({ weekday: 1 }).minus({ week: 1 }).toFormat('W');
const thisSunday = now.set({ weekday: 1 }).toFormat('W');
const nextSunday = now.set({ weekday: 1 }).plus({ week: 1 }).toFormat('W');
const lastSunday = this.thisThursday.minus({ week: 1 }).toFormat('W');
const thisSunday = this.thisThursday.toFormat('W');
const nextSunday = this.thisThursday.plus({ week: 1 }).toFormat('W');

return `${lastSunday}-${thisSunday}-${nextSunday}`;
}

get thisThursday() {
const thursday = DateTime.fromObject({
weekday: 4,
hour: 0,
minute: 0,
second: 0,
});

// In this component the week always starts on Sunday, but luxon's starts on Monday
// If today is sunday, we need to add a week to get the correct Thursday
if (DateTime.now().weekday === 7) {
return thursday.plus({ weeks: 1 });
}

return thursday;
}

get year() {
return DateTime.now().year;
return this.thisThursday.weekYear;
}

get week() {
const today = DateTime.now();
// In this component the week always starts on Sunday, but luxon's starts on Monday
// so we need to adjust the week number to account for that.
if (today.weekday === 7) {
return today.weekNumber + 1;
} else {
return today.weekNumber;
}
return this.thisThursday.weekNumber;
}
}
16 changes: 14 additions & 2 deletions packages/ilios-common/addon/components/week-glance.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,33 @@ export default class WeeklyGlance extends Component {
}

get thursdayOfTheWeek() {
return DateTime.fromObject({
const thursday = DateTime.fromObject({
weekYear: this.args.year,
weekNumber: this.args.week,
weekday: 4,
hour: 0,
minute: 0,
second: 0,
}).toJSDate();
});

if (!thursday.isValid) {
console.error('Invalid date', thursday.invalidReason, this.args.year, this.args.week);
return null;
}
return thursday.toJSDate();
}

get midnightAtTheStartOfTheWeekDateTime() {
if (!this.thursdayOfTheWeek) {
return null;
}
return DateTime.fromJSDate(this.localeDays.firstDayOfDateWeek(this.thursdayOfTheWeek));
}

get midnightAtTheEndOfTheWeekDateTime() {
if (!this.thursdayOfTheWeek) {
return null;
}
return DateTime.fromJSDate(this.localeDays.lastDayOfDateWeek(this.thursdayOfTheWeek));
}

Expand Down
4 changes: 2 additions & 2 deletions packages/ilios-common/addon/components/weekly-events.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { DateTime } from 'luxon';

export default class WeeklyEvents extends Component {
get weeksInYear() {
const weeksInTheYear = DateTime.now().set({ year: this.args.year }).weeksInWeekYear;
const { weeksInWeekYear } = DateTime.fromObject({ year: this.args.year });
const weeks = [];
for (let i = 1; i <= weeksInTheYear; i++) {
for (let i = 1; i <= weeksInWeekYear; i++) {
weeks.push(`${i}`);
}
return weeks;
Expand Down
199 changes: 113 additions & 86 deletions packages/test-app/tests/integration/components/dashboard/week-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,26 @@ module('Integration | Component | dashboard/week', function (hooks) {

return expectedTitle;
};

this.setupEvents = function (events) {
class UserEvents extends Service {
async getEvents() {
return events;
}
}
this.owner.register('service:user-events', UserEvents);
};

this.testTitleOnDate = async function (assert, obj, expectedTitle) {
const dt = DateTime.fromObject(obj);
freezeDateAt(dt.toJSDate());
await render(hbs`<Dashboard::Week />`);
assert.strictEqual(
component.weekGlance.title,
expectedTitle,
`correct title on ${dt.toISODate()}`,
);
};
});

hooks.afterEach(() => {
Expand Down Expand Up @@ -75,12 +95,7 @@ module('Integration | Component | dashboard/week', function (hooks) {
});

const { userevents } = this.server.db;
class UserEvents extends Service {
async getEvents() {
return userevents;
}
}
this.owner.register('service:user-events', UserEvents);
this.setupEvents(userevents);

await render(hbs`<Dashboard::Week />`);
const expectedTitle = this.getTitle();
Expand All @@ -92,14 +107,7 @@ module('Integration | Component | dashboard/week', function (hooks) {
});

test('it renders blank', async function (assert) {
class UserEvents extends Service {
async getEvents() {
return [];
}
}
this.owner.register('service:user-events', UserEvents);
this.userEvents = this.owner.lookup('service:user-events');

this.setupEvents([]);
await render(hbs`<Dashboard::Week />`);
const expectedTitle = this.getTitle();
assert.strictEqual(component.weeklyLink, 'All Weeks');
Expand All @@ -108,95 +116,114 @@ module('Integration | Component | dashboard/week', function (hooks) {
});

test('right week on sunday #5308', async function (assert) {
class UserEvents extends Service {
async getEvents() {
return [];
}
}
this.owner.register('service:user-events', UserEvents);
this.userEvents = this.owner.lookup('service:user-events');
freezeDateAt(DateTime.fromObject({ year: 2024, month: 3, day: 10 }).toJSDate());

await render(hbs`<Dashboard::Week />`);
assert.strictEqual(component.weekGlance.title, 'March 10-16 Week at a Glance');
assert.expect(1);
await this.testTitleOnDate(
assert,
{ year: 2024, month: 3, day: 10 },
'March 10-16 Week at a Glance',
);
});

test('right week on monday #5308', async function (assert) {
class UserEvents extends Service {
async getEvents() {
return [];
}
}
this.owner.register('service:user-events', UserEvents);
this.userEvents = this.owner.lookup('service:user-events');
freezeDateAt(DateTime.fromObject({ year: 2023, month: 8, day: 7 }).toJSDate());

await render(hbs`<Dashboard::Week />`);
assert.strictEqual(component.weekGlance.title, 'August 6-12 Week at a Glance');
assert.expect(1);
await this.testTitleOnDate(
assert,
{ year: 2023, month: 8, day: 7 },
'August 6-12 Week at a Glance',
);
});

test('right week on tuesday #5308', async function (assert) {
class UserEvents extends Service {
async getEvents() {
return [];
}
}
this.owner.register('service:user-events', UserEvents);
this.userEvents = this.owner.lookup('service:user-events');
freezeDateAt(DateTime.fromObject({ year: 2022, month: 12, day: 6 }).toJSDate());
await render(hbs`<Dashboard::Week />`);
assert.strictEqual(component.weekGlance.title, 'December 4-10 Week at a Glance');
assert.expect(1);
await this.testTitleOnDate(
assert,
{ year: 2022, month: 12, day: 6 },
'December 4-10 Week at a Glance',
);
});

test('right week on wednesday #5308', async function (assert) {
class UserEvents extends Service {
async getEvents() {
return [];
}
}
this.owner.register('service:user-events', UserEvents);
this.userEvents = this.owner.lookup('service:user-events');
freezeDateAt(DateTime.fromObject({ year: 2022, month: 7, day: 13 }).toJSDate());
await render(hbs`<Dashboard::Week />`);
assert.strictEqual(component.weekGlance.title, 'July 10-16 Week at a Glance');
assert.expect(1);
await this.testTitleOnDate(
assert,
{ year: 2022, month: 7, day: 13 },
'July 10-16 Week at a Glance',
);
});

test('right week on thursday #5308', async function (assert) {
class UserEvents extends Service {
async getEvents() {
return [];
}
}
this.owner.register('service:user-events', UserEvents);
this.userEvents = this.owner.lookup('service:user-events');
freezeDateAt(DateTime.fromObject({ year: 2021, month: 5, day: 13 }).toJSDate());
await render(hbs`<Dashboard::Week />`);
assert.strictEqual(component.weekGlance.title, 'May 9-15 Week at a Glance');
assert.expect(1);
await this.testTitleOnDate(
assert,
{ year: 2021, month: 5, day: 13 },
'May 9-15 Week at a Glance',
);
});

test('right week on friday #5308', async function (assert) {
class UserEvents extends Service {
async getEvents() {
return [];
}
}
this.owner.register('service:user-events', UserEvents);
this.userEvents = this.owner.lookup('service:user-events');
freezeDateAt(DateTime.fromObject({ year: 2021, month: 9, day: 24 }).toJSDate());
await render(hbs`<Dashboard::Week />`);
assert.strictEqual(component.weekGlance.title, 'September 19-25 Week at a Glance');
assert.expect(1);
await this.testTitleOnDate(
assert,
{ year: 2021, month: 9, day: 24 },
'September 19-25 Week at a Glance',
);
});

test('right week on saturday #5308', async function (assert) {
class UserEvents extends Service {
async getEvents() {
return [];
}
}
this.owner.register('service:user-events', UserEvents);
this.userEvents = this.owner.lookup('service:user-events');
freezeDateAt(DateTime.fromObject({ year: 2022, month: 7, day: 30 }).toJSDate());
await render(hbs`<Dashboard::Week />`);
assert.strictEqual(component.weekGlance.title, 'July 24-30 Week at a Glance');
assert.expect(1);
await this.testTitleOnDate(
assert,
{ year: 2022, month: 7, day: 30 },
'July 24-30 Week at a Glance',
);
});

test('correct at the end of 2023 and the start of 2024', async function (assert) {
assert.expect(7);
this.setupEvents([]);
const title = 'December 31 - January 6 Week at a Glance';
await this.testTitleOnDate(assert, { year: 2023, month: 12, day: 31 }, title);
await this.testTitleOnDate(assert, { year: 2024, month: 1, day: 1 }, title);
await this.testTitleOnDate(assert, { year: 2024, month: 1, day: 2 }, title);
await this.testTitleOnDate(assert, { year: 2024, month: 1, day: 3 }, title);
await this.testTitleOnDate(assert, { year: 2024, month: 1, day: 4 }, title);
await this.testTitleOnDate(assert, { year: 2024, month: 1, day: 5 }, title);
await this.testTitleOnDate(assert, { year: 2024, month: 1, day: 6 }, title);
});

test('correct at the end of 2024 and start of 2025', async function (assert) {
assert.expect(7);
this.setupEvents([]);
const title = 'December 29 - January 4 Week at a Glance';
await this.testTitleOnDate(assert, { year: 2024, month: 12, day: 29 }, title);
await this.testTitleOnDate(assert, { year: 2024, month: 12, day: 30 }, title);
await this.testTitleOnDate(assert, { year: 2024, month: 12, day: 31 }, title);
await this.testTitleOnDate(assert, { year: 2025, month: 1, day: 1 }, title);
await this.testTitleOnDate(assert, { year: 2025, month: 1, day: 2 }, title);
await this.testTitleOnDate(assert, { year: 2025, month: 1, day: 3 }, title);
await this.testTitleOnDate(assert, { year: 2025, month: 1, day: 4 }, title);
});

test('correct at the end of 2025 and start of 2026', async function (assert) {
assert.expect(7);
this.setupEvents([]);
const title = 'December 28 - January 3 Week at a Glance';
await this.testTitleOnDate(assert, { year: 2025, month: 12, day: 28 }, title);
await this.testTitleOnDate(assert, { year: 2025, month: 12, day: 29 }, title);
await this.testTitleOnDate(assert, { year: 2025, month: 12, day: 30 }, title);
await this.testTitleOnDate(assert, { year: 2025, month: 12, day: 31 }, title);
await this.testTitleOnDate(assert, { year: 2026, month: 1, day: 1 }, title);
await this.testTitleOnDate(assert, { year: 2026, month: 1, day: 2 }, title);
await this.testTitleOnDate(assert, { year: 2026, month: 1, day: 3 }, title);
});

test('correct on some random day', async function (assert) {
assert.expect(1);
this.setupEvents([]);
await this.testTitleOnDate(
assert,
{ year: 2005, month: 6, day: 24 },
'June 19-25 Week at a Glance',
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,50 @@ module('Integration | Component | weekly events', function (hooks) {
assert.strictEqual(component.topNavigation.title, '2016');
assert.strictEqual(component.bottomNavigation.title, '2016');
});

test('it renders 2024 ilios/ilios#5908', async function (assert) {
await render(
hbs`<WeeklyEvents
@year={{2024}}
@expandedWeeks={{(array)}}
@setYear={{(noop)}}
@toggleOpenWeek={{(noop)}}
/>`,
);
assert.strictEqual(component.topNavigation.title, '2024');
assert.strictEqual(component.weeks.length, 52);
assert.strictEqual(component.weeks[0].title, 'December 31 - January 6');
assert.strictEqual(component.weeks[51].title, 'December 22-28');
});

test('it renders 2025 ilios/ilios#5908', async function (assert) {
await render(
hbs`<WeeklyEvents
@year={{2025}}
@expandedWeeks={{(array)}}
@setYear={{(noop)}}
@toggleOpenWeek={{(noop)}}
/>`,
);
assert.strictEqual(component.topNavigation.title, '2025');
assert.strictEqual(component.weeks.length, 52);
assert.strictEqual(component.weeks[0].title, 'December 29 - January 4');
assert.strictEqual(component.weeks[51].title, 'December 21-27');
});

test('it renders 2026 ilios/ilios#5908', async function (assert) {
assert.expect(4);
await render(
hbs`<WeeklyEvents
@year={{2026}}
@expandedWeeks={{(array)}}
@setYear={{(noop)}}
@toggleOpenWeek={{(noop)}}
/>`,
);
assert.strictEqual(component.topNavigation.title, '2026');
assert.strictEqual(component.weeks.length, 53);
assert.strictEqual(component.weeks[0].title, 'December 28 - January 3');
assert.strictEqual(component.weeks[52].title, 'December 27 - January 2');
});
});

0 comments on commit 966882b

Please sign in to comment.