Fix setting end date

This commit is contained in:
Phillip Thelen
2024-05-07 15:17:22 +02:00
parent fbce5aae32
commit 0089506165
2 changed files with 71 additions and 4 deletions

View File

@@ -3,7 +3,8 @@ import {
expectValidTranslationString,
} from '../helpers/content.helper'; */
// eslint-disable-next-line max-len
import { getAllScheduleMatchingGroups } from '../../website/common/script/content/constants/schedule';
import moment from 'moment';
import { getAllScheduleMatchingGroups, clearCachedMatchers } from '../../website/common/script/content/constants/schedule';
function validateMatcher (matcher, checkedDate) {
expect(matcher.end).to.be.a('date');
@@ -11,6 +12,10 @@ function validateMatcher (matcher, checkedDate) {
}
describe('Content Schedule', () => {
beforeEach(() => {
clearCachedMatchers();
});
it('assembles scheduled items on january 15th', () => {
const date = new Date('2024-01-15');
const matchers = getAllScheduleMatchingGroups(date);
@@ -80,6 +85,61 @@ describe('Content Schedule', () => {
}
});
it('sets the end date if its in the same month', () => {
const date = new Date('2024-04-03');
const matchers = getAllScheduleMatchingGroups(date);
for (const key in matchers) {
if (matchers[key]) {
validateMatcher(matchers[key], date);
}
}
expect(matchers.backgrounds.end).to.eql(moment('2024-04-07').toDate());
});
it('sets the end date if its in the next day', () => {
const date = new Date('2024-05-06T14:00:00.000Z');
const matchers = getAllScheduleMatchingGroups(date);
for (const key in matchers) {
if (matchers[key]) {
validateMatcher(matchers[key], date);
}
}
expect(matchers.backgrounds.end).to.eql(moment('2024-05-07').toDate());
});
it('sets the end date if its on the release day', () => {
const date = new Date('2024-05-07');
const matchers = getAllScheduleMatchingGroups(date);
for (const key in matchers) {
if (matchers[key]) {
validateMatcher(matchers[key], date);
}
}
expect(matchers.backgrounds.end).to.eql(moment('2024-06-07').toDate());
});
it('sets the end date if its next month', () => {
const date = new Date('2024-05-20T01:00:00.000Z');
const matchers = getAllScheduleMatchingGroups(date);
for (const key in matchers) {
if (matchers[key]) {
validateMatcher(matchers[key], date);
}
}
expect(matchers.backgrounds.end).to.eql(moment('2024-06-07').toDate());
});
it('sets the end date for a gala', () => {
const date = new Date('2024-05-20');
const matchers = getAllScheduleMatchingGroups(date);
for (const key in matchers) {
if (matchers[key]) {
validateMatcher(matchers[key], date);
}
}
expect(matchers.seasonalGear.end).to.eql(moment('2024-06-21').toDate());
});
describe('backgrounds matcher', () => {
it('allows background matching the month for new backgrounds', () => {
const date = new Date('2024-07-08');

View File

@@ -873,21 +873,28 @@ function makeMatcherClass (date) {
function makeEndDate (checkedDate, matcher) {
let end = moment(checkedDate);
end.date(TYPE_SCHEDULE[matcher.type]);
end.hour(0);
end.minute(0);
end.second(0);
if (matcher.endMonth !== undefined) {
end.month(matcher.endMonth);
} else if (end.date() <= checkedDate) {
} else if (end.date() <= checkedDate.getDate()) {
end = moment(end).add(1, 'months');
}
return end.toDate();
}
export function clearCachedMatchers () {
cacheDate = null;
cachedScheduleMatchers = null;
}
export function getAllScheduleMatchingGroups (date) {
const checkedDate = date || new Date();
if (cacheDate !== null && (getDay(checkedDate) !== getDay(cacheDate)
|| getMonth(checkedDate) !== getMonth(cacheDate))) {
// Clear cached matchers, since they are old
cacheDate = null;
cachedScheduleMatchers = null;
clearCachedMatchers();
}
if (!cachedScheduleMatchers) {
// No matchers exist, make new ones