rwadurian/backend/services/leaderboard-service/test/domain/value-objects/leaderboard-period.vo.spec.ts

98 lines
3.4 KiB
TypeScript

import { LeaderboardPeriod } from '../../../src/domain/value-objects/leaderboard-period.vo';
import { LeaderboardType } from '../../../src/domain/value-objects/leaderboard-type.enum';
describe('LeaderboardPeriod', () => {
describe('currentDaily', () => {
it('应该创建当前日榜周期', () => {
const period = LeaderboardPeriod.currentDaily();
expect(period.type).toBe(LeaderboardType.DAILY);
expect(period.key).toMatch(/^\d{4}-\d{2}-\d{2}$/);
expect(period.startAt.getHours()).toBe(0);
expect(period.startAt.getMinutes()).toBe(0);
expect(period.endAt.getHours()).toBe(23);
expect(period.endAt.getMinutes()).toBe(59);
});
});
describe('currentWeekly', () => {
it('应该创建当前周榜周期', () => {
const period = LeaderboardPeriod.currentWeekly();
expect(period.type).toBe(LeaderboardType.WEEKLY);
expect(period.key).toMatch(/^\d{4}-W\d{2}$/);
expect(period.startAt.getDay()).toBe(1); // 周一
expect(period.endAt.getDay()).toBe(0); // 周日
});
});
describe('currentMonthly', () => {
it('应该创建当前月榜周期', () => {
const period = LeaderboardPeriod.currentMonthly();
expect(period.type).toBe(LeaderboardType.MONTHLY);
expect(period.key).toMatch(/^\d{4}-\d{2}$/);
expect(period.startAt.getDate()).toBe(1);
});
});
describe('current', () => {
it('应该根据类型创建当前周期', () => {
const daily = LeaderboardPeriod.current(LeaderboardType.DAILY);
const weekly = LeaderboardPeriod.current(LeaderboardType.WEEKLY);
const monthly = LeaderboardPeriod.current(LeaderboardType.MONTHLY);
expect(daily.type).toBe(LeaderboardType.DAILY);
expect(weekly.type).toBe(LeaderboardType.WEEKLY);
expect(monthly.type).toBe(LeaderboardType.MONTHLY);
});
});
describe('isCurrentPeriod', () => {
it('当前时间应该在当前周期内', () => {
const period = LeaderboardPeriod.currentDaily();
expect(period.isCurrentPeriod()).toBe(true);
});
});
describe('getPreviousPeriod', () => {
it('应该获取上一个日榜周期', () => {
const current = LeaderboardPeriod.currentDaily();
const previous = current.getPreviousPeriod();
expect(previous.type).toBe(LeaderboardType.DAILY);
expect(previous.endAt.getTime()).toBeLessThan(current.startAt.getTime());
});
it('应该获取上一个周榜周期', () => {
const current = LeaderboardPeriod.currentWeekly();
const previous = current.getPreviousPeriod();
expect(previous.type).toBe(LeaderboardType.WEEKLY);
});
it('应该获取上一个月榜周期', () => {
const current = LeaderboardPeriod.currentMonthly();
const previous = current.getPreviousPeriod();
expect(previous.type).toBe(LeaderboardType.MONTHLY);
});
});
describe('equals', () => {
it('相同类型和key的周期应该相等', () => {
const period1 = LeaderboardPeriod.currentDaily();
const period2 = LeaderboardPeriod.currentDaily();
expect(period1.equals(period2)).toBe(true);
});
it('不同类型的周期应该不相等', () => {
const daily = LeaderboardPeriod.currentDaily();
const weekly = LeaderboardPeriod.currentWeekly();
expect(daily.equals(weekly)).toBe(false);
});
});
});