mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-16 06:07:21 +01:00
Improve repeating events handling
This commit is contained in:
40
test/content/events.test.js
Normal file
40
test/content/events.test.js
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
import { getRepeatingEvents } from '../../website/common/script/content/constants/events';
|
||||||
|
|
||||||
|
describe.only('events', () => {
|
||||||
|
let clock;
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
if (clock) {
|
||||||
|
clock.restore();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns empty array when no events are active', () => {
|
||||||
|
clock = sinon.useFakeTimers(new Date('2024-01-06'));
|
||||||
|
const events = getRepeatingEvents();
|
||||||
|
expect(events).to.be.empty;
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns events when active', () => {
|
||||||
|
clock = sinon.useFakeTimers(new Date('2024-01-31'));
|
||||||
|
const events = getRepeatingEvents();
|
||||||
|
expect(events).to.have.length(1);
|
||||||
|
expect(events[0].key).to.equal('birthday');
|
||||||
|
expect(events[0].end).to.be.greaterThan(new Date());
|
||||||
|
expect(events[0].start).to.be.lessThan(new Date());
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns nye event at beginning of the year', () => {
|
||||||
|
clock = sinon.useFakeTimers(new Date('2025-01-01'));
|
||||||
|
const events = getRepeatingEvents();
|
||||||
|
expect(events).to.have.length(1);
|
||||||
|
expect(events[0].key).to.equal('nye');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns nye event at end of the year', () => {
|
||||||
|
clock = sinon.useFakeTimers(new Date('2024-12-30'));
|
||||||
|
const events = getRepeatingEvents();
|
||||||
|
expect(events).to.have.length(1);
|
||||||
|
expect(events[0].key).to.equal('nye');
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -12,8 +12,8 @@ const gemsPromo = {
|
|||||||
|
|
||||||
export const REPEATING_EVENTS = {
|
export const REPEATING_EVENTS = {
|
||||||
nye: {
|
nye: {
|
||||||
start: '1970-12-28T08:00-05:00',
|
start: new Date('1970-12-28T08:00-05:00'),
|
||||||
end: '1970-01-04T23:59-05:00',
|
end: new Date('1970-01-04T23:59-05:00'),
|
||||||
season: 'nye',
|
season: 'nye',
|
||||||
npcImageSuffix: '_nye',
|
npcImageSuffix: '_nye',
|
||||||
content: [
|
content: [
|
||||||
@@ -26,15 +26,15 @@ export const REPEATING_EVENTS = {
|
|||||||
],
|
],
|
||||||
},
|
},
|
||||||
birthday: {
|
birthday: {
|
||||||
start: '1970-01-30T08:00-05:00',
|
start: new Date('1970-01-30T08:00-05:00'),
|
||||||
end: '1970-02-08T23:59-05:00',
|
end: new Date('1970-02-08T23:59-05:00'),
|
||||||
season: 'birthday',
|
season: 'birthday',
|
||||||
npcImageSuffix: '_birthday',
|
npcImageSuffix: '_birthday',
|
||||||
foodSeason: 'Cake',
|
foodSeason: 'Cake',
|
||||||
},
|
},
|
||||||
valentines: {
|
valentines: {
|
||||||
start: '1970-02-13T08:00-05:00',
|
start: new Date('1970-02-13T08:00-05:00'),
|
||||||
end: '1970-02-17T23:59-05:00',
|
end: new Date('1970-02-17T23:59-05:00'),
|
||||||
season: 'valentines',
|
season: 'valentines',
|
||||||
npcImageSuffix: '_valentines',
|
npcImageSuffix: '_valentines',
|
||||||
content: [
|
content: [
|
||||||
@@ -47,23 +47,23 @@ export const REPEATING_EVENTS = {
|
|||||||
],
|
],
|
||||||
},
|
},
|
||||||
piDay: {
|
piDay: {
|
||||||
start: '1970-03-13T08:00-05:00',
|
start: new Date('1970-03-13T08:00-05:00'),
|
||||||
end: '1970-03-15T23:59-05:00',
|
end: new Date('1970-03-15T23:59-05:00'),
|
||||||
foodSeason: 'Pie',
|
foodSeason: 'Pie',
|
||||||
},
|
},
|
||||||
namingDay: {
|
namingDay: {
|
||||||
start: '1970-07-30T08:00-05:00',
|
start: new Date('1970-07-30T08:00-05:00'),
|
||||||
end: '1970-08-01T23:59-05:00',
|
end: new Date('1970-08-01T23:59-05:00'),
|
||||||
foodSeason: 'Cake',
|
foodSeason: 'Cake',
|
||||||
},
|
},
|
||||||
habitoween: {
|
habitoween: {
|
||||||
start: '1970-10-30T08:00-05:00',
|
start: new Date('1970-10-30T08:00-05:00'),
|
||||||
end: '1970-11-01T23:59-05:00',
|
end: new Date('1970-11-01T23:59-05:00'),
|
||||||
foodSeason: 'Candy',
|
foodSeason: 'Candy',
|
||||||
},
|
},
|
||||||
harvestFeast: {
|
harvestFeast: {
|
||||||
start: '1970-11-22T08:00-05:00',
|
start: new Date('1970-11-22T08:00-05:00'),
|
||||||
end: '1970-11-27T20:00-05:00',
|
end: new Date('1970-11-27T20:00-05:00'),
|
||||||
season: 'thanksgiving',
|
season: 'thanksgiving',
|
||||||
npcImageSuffix: '_thanksgiving',
|
npcImageSuffix: '_thanksgiving',
|
||||||
foodSeason: 'Pie',
|
foodSeason: 'Pie',
|
||||||
@@ -72,13 +72,18 @@ export const REPEATING_EVENTS = {
|
|||||||
|
|
||||||
export function getRepeatingEvents (date) {
|
export function getRepeatingEvents (date) {
|
||||||
const momentDate = date instanceof moment ? date : moment(date);
|
const momentDate = date instanceof moment ? date : moment(date);
|
||||||
return filter(Object.keys(REPEATING_EVENTS), eventKey => {
|
return Object.keys(REPEATING_EVENTS).map(eventKey => {
|
||||||
const eventData = REPEATING_EVENTS[eventKey];
|
const event = REPEATING_EVENTS[eventKey];
|
||||||
const startDate = eventData.start.replace('1970', momentDate.year());
|
event.key = eventKey;
|
||||||
const endDate = eventData.end.replace('1970', momentDate.year());
|
event.start.setYear(momentDate.year());
|
||||||
|
event.end.setYear(momentDate.year());
|
||||||
return momentDate.isBetween(startDate, endDate);
|
if (event.end < event.start && momentDate < event.start) {
|
||||||
}).map(eventKey => REPEATING_EVENTS[eventKey]);
|
event.start.setYear(momentDate.year() - 1);
|
||||||
|
} else if (event.end < event.start && momentDate > event.end) {
|
||||||
|
event.end.setYear(momentDate.year() + 1);
|
||||||
|
}
|
||||||
|
return event;
|
||||||
|
}).filter(event => momentDate.isBetween(event.start, event.end));
|
||||||
}
|
}
|
||||||
|
|
||||||
export const EVENTS = {
|
export const EVENTS = {
|
||||||
|
|||||||
Reference in New Issue
Block a user