You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
149 lines
5.0 KiB
149 lines
5.0 KiB
import { ExpirationDate } from '../expiration-date.vo'; |
|
|
|
describe('ExpirationDate', () => { |
|
const MOCKED_NOW = new Date('2023-01-01T12:00:00Z'); |
|
|
|
describe('constructor', () => { |
|
it('should create ExpirationDate with valid future date', () => { |
|
const futureDate = new Date(MOCKED_NOW); |
|
futureDate.setDate(futureDate.getDate() + 7); // 7 days in the future |
|
const expirationDate = new ExpirationDate(futureDate); |
|
|
|
expect(expirationDate.getValue()).toEqual(futureDate); |
|
}); |
|
|
|
it('should throw error when date is not a Date object', () => { |
|
expect(() => { |
|
new ExpirationDate('not-a-date' as any); |
|
}).toThrow('Expiration date must be a Date object'); |
|
}); |
|
|
|
it('should throw error when date is invalid', () => { |
|
expect(() => { |
|
new ExpirationDate(new Date('invalid-date')); |
|
}).toThrow('Expiration date must be a valid date'); |
|
}); |
|
|
|
}); |
|
|
|
describe('static create', () => { |
|
it('should create ExpirationDate from valid Date', () => { |
|
const futureDate = new Date(MOCKED_NOW); |
|
futureDate.setDate(futureDate.getDate() + 7); |
|
const expirationDate = ExpirationDate.create(futureDate); |
|
|
|
expect(expirationDate).toBeInstanceOf(ExpirationDate); |
|
expect(expirationDate.getValue()).toEqual(futureDate); |
|
}); |
|
}); |
|
|
|
describe('static fromString', () => { |
|
it('should create ExpirationDate from valid ISO date string', () => { |
|
const futureDate = new Date(MOCKED_NOW); |
|
futureDate.setDate(futureDate.getDate() + 7); |
|
const dateString = futureDate.toISOString(); |
|
|
|
const expirationDate = ExpirationDate.fromString(dateString); |
|
|
|
expect(expirationDate).toBeInstanceOf(ExpirationDate); |
|
expect(expirationDate.getValue()).toEqual(futureDate); |
|
}); |
|
|
|
it('should throw error for invalid date string', () => { |
|
expect(() => { |
|
ExpirationDate.fromString('invalid-date'); |
|
}).toThrow('Invalid date string format'); |
|
}); |
|
}); |
|
|
|
describe('getValue', () => { |
|
it('should return a copy of the date', () => { |
|
const futureDate = new Date(MOCKED_NOW); |
|
futureDate.setDate(futureDate.getDate() + 7); |
|
const expirationDate = new ExpirationDate(futureDate); |
|
|
|
const returnedDate = expirationDate.getValue(); |
|
|
|
expect(returnedDate).toEqual(futureDate); |
|
expect(returnedDate).not.toBe(futureDate); // Should be a different object |
|
}); |
|
|
|
it('should return immutable date', () => { |
|
const futureDate = new Date(MOCKED_NOW); |
|
futureDate.setDate(futureDate.getDate() + 7); |
|
const expirationDate = new ExpirationDate(futureDate); |
|
|
|
const returnedDate = expirationDate.getValue(); |
|
returnedDate.setDate(returnedDate.getDate() + 1); // Try to modify |
|
|
|
// Original should remain unchanged |
|
expect(expirationDate.getValue()).toEqual(futureDate); |
|
}); |
|
}); |
|
|
|
describe('format', () => { |
|
it('should return ISO string format', () => { |
|
const futureDate = new Date(MOCKED_NOW); |
|
futureDate.setDate(futureDate.getDate() + 7); |
|
const expirationDate = new ExpirationDate(futureDate); |
|
|
|
expect(expirationDate.format()).toBe(futureDate.toISOString()); |
|
}); |
|
}); |
|
|
|
describe('toISOString', () => { |
|
it('should return ISO string format', () => { |
|
const futureDate = new Date(MOCKED_NOW); |
|
futureDate.setDate(futureDate.getDate() + 7); |
|
const expirationDate = new ExpirationDate(futureDate); |
|
|
|
expect(expirationDate.toISOString()).toBe(futureDate.toISOString()); |
|
}); |
|
}); |
|
|
|
describe('toString', () => { |
|
it('should return string representation', () => { |
|
const futureDate = new Date(MOCKED_NOW); |
|
futureDate.setDate(futureDate.getDate() + 7); |
|
const expirationDate = new ExpirationDate(futureDate); |
|
|
|
expect(expirationDate.toString()).toBe(futureDate.toISOString()); |
|
}); |
|
}); |
|
|
|
describe('equals', () => { |
|
it('should return true for equal dates', () => { |
|
const futureDate = new Date(MOCKED_NOW); |
|
futureDate.setDate(futureDate.getDate() + 7); |
|
const expirationDate1 = new ExpirationDate(futureDate); |
|
const expirationDate2 = new ExpirationDate(futureDate); |
|
|
|
expect(expirationDate1.equals(expirationDate2)).toBe(true); |
|
}); |
|
|
|
it('should return false for different dates', () => { |
|
const futureDate1 = new Date(MOCKED_NOW); |
|
futureDate1.setDate(futureDate1.getDate() + 7); |
|
|
|
const futureDate2 = new Date(MOCKED_NOW); |
|
futureDate2.setDate(futureDate2.getDate() + 8); |
|
|
|
const expirationDate1 = new ExpirationDate(futureDate1); |
|
const expirationDate2 = new ExpirationDate(futureDate2); |
|
|
|
expect(expirationDate1.equals(expirationDate2)).toBe(false); |
|
}); |
|
|
|
it('should return true for dates with same timestamp', () => { |
|
const timestamp = MOCKED_NOW.getTime() + 86400000; // 1 day in the future |
|
const date1 = new Date(timestamp); |
|
const date2 = new Date(timestamp); |
|
|
|
const expirationDate1 = new ExpirationDate(date1); |
|
const expirationDate2 = new ExpirationDate(date2); |
|
|
|
expect(expirationDate1.equals(expirationDate2)).toBe(true); |
|
}); |
|
}); |
|
|
|
}); |