mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-17 22:57:21 +01:00
Fix setting end date
This commit is contained in:
@@ -3,7 +3,8 @@ import {
|
|||||||
expectValidTranslationString,
|
expectValidTranslationString,
|
||||||
} from '../helpers/content.helper'; */
|
} from '../helpers/content.helper'; */
|
||||||
// eslint-disable-next-line max-len
|
// 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) {
|
function validateMatcher (matcher, checkedDate) {
|
||||||
expect(matcher.end).to.be.a('date');
|
expect(matcher.end).to.be.a('date');
|
||||||
@@ -11,6 +12,10 @@ function validateMatcher (matcher, checkedDate) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
describe('Content Schedule', () => {
|
describe('Content Schedule', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
clearCachedMatchers();
|
||||||
|
});
|
||||||
|
|
||||||
it('assembles scheduled items on january 15th', () => {
|
it('assembles scheduled items on january 15th', () => {
|
||||||
const date = new Date('2024-01-15');
|
const date = new Date('2024-01-15');
|
||||||
const matchers = getAllScheduleMatchingGroups(date);
|
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', () => {
|
describe('backgrounds matcher', () => {
|
||||||
it('allows background matching the month for new backgrounds', () => {
|
it('allows background matching the month for new backgrounds', () => {
|
||||||
const date = new Date('2024-07-08');
|
const date = new Date('2024-07-08');
|
||||||
|
|||||||
@@ -873,21 +873,28 @@ function makeMatcherClass (date) {
|
|||||||
function makeEndDate (checkedDate, matcher) {
|
function makeEndDate (checkedDate, matcher) {
|
||||||
let end = moment(checkedDate);
|
let end = moment(checkedDate);
|
||||||
end.date(TYPE_SCHEDULE[matcher.type]);
|
end.date(TYPE_SCHEDULE[matcher.type]);
|
||||||
|
end.hour(0);
|
||||||
|
end.minute(0);
|
||||||
|
end.second(0);
|
||||||
if (matcher.endMonth !== undefined) {
|
if (matcher.endMonth !== undefined) {
|
||||||
end.month(matcher.endMonth);
|
end.month(matcher.endMonth);
|
||||||
} else if (end.date() <= checkedDate) {
|
} else if (end.date() <= checkedDate.getDate()) {
|
||||||
end = moment(end).add(1, 'months');
|
end = moment(end).add(1, 'months');
|
||||||
}
|
}
|
||||||
return end.toDate();
|
return end.toDate();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function clearCachedMatchers () {
|
||||||
|
cacheDate = null;
|
||||||
|
cachedScheduleMatchers = null;
|
||||||
|
}
|
||||||
|
|
||||||
export function getAllScheduleMatchingGroups (date) {
|
export function getAllScheduleMatchingGroups (date) {
|
||||||
const checkedDate = date || new Date();
|
const checkedDate = date || new Date();
|
||||||
if (cacheDate !== null && (getDay(checkedDate) !== getDay(cacheDate)
|
if (cacheDate !== null && (getDay(checkedDate) !== getDay(cacheDate)
|
||||||
|| getMonth(checkedDate) !== getMonth(cacheDate))) {
|
|| getMonth(checkedDate) !== getMonth(cacheDate))) {
|
||||||
// Clear cached matchers, since they are old
|
// Clear cached matchers, since they are old
|
||||||
cacheDate = null;
|
clearCachedMatchers();
|
||||||
cachedScheduleMatchers = null;
|
|
||||||
}
|
}
|
||||||
if (!cachedScheduleMatchers) {
|
if (!cachedScheduleMatchers) {
|
||||||
// No matchers exist, make new ones
|
// No matchers exist, make new ones
|
||||||
|
|||||||
Reference in New Issue
Block a user