view data tests
This commit is contained in:
@@ -1,7 +1,182 @@
|
||||
import * as mod from '@core/media/domain/entities/Avatar';
|
||||
import { Avatar } from './Avatar';
|
||||
import { MediaUrl } from '../value-objects/MediaUrl';
|
||||
|
||||
describe('media/domain/entities/Avatar.ts', () => {
|
||||
it('imports', () => {
|
||||
expect(mod).toBeTruthy();
|
||||
describe('Avatar', () => {
|
||||
describe('create', () => {
|
||||
it('creates a new avatar with required properties', () => {
|
||||
const avatar = Avatar.create({
|
||||
id: 'avatar-1',
|
||||
driverId: 'driver-1',
|
||||
mediaUrl: 'https://example.com/avatar.png',
|
||||
});
|
||||
|
||||
expect(avatar.id).toBe('avatar-1');
|
||||
expect(avatar.driverId).toBe('driver-1');
|
||||
expect(avatar.mediaUrl).toBeInstanceOf(MediaUrl);
|
||||
expect(avatar.mediaUrl.value).toBe('https://example.com/avatar.png');
|
||||
expect(avatar.isActive).toBe(true);
|
||||
expect(avatar.selectedAt).toBeInstanceOf(Date);
|
||||
});
|
||||
|
||||
it('throws error when driverId is missing', () => {
|
||||
expect(() =>
|
||||
Avatar.create({
|
||||
id: 'avatar-1',
|
||||
driverId: '',
|
||||
mediaUrl: 'https://example.com/avatar.png',
|
||||
})
|
||||
).toThrow('Driver ID is required');
|
||||
});
|
||||
|
||||
it('throws error when mediaUrl is missing', () => {
|
||||
expect(() =>
|
||||
Avatar.create({
|
||||
id: 'avatar-1',
|
||||
driverId: 'driver-1',
|
||||
mediaUrl: '',
|
||||
})
|
||||
).toThrow('Media URL is required');
|
||||
});
|
||||
|
||||
it('throws error when mediaUrl is invalid', () => {
|
||||
expect(() =>
|
||||
Avatar.create({
|
||||
id: 'avatar-1',
|
||||
driverId: 'driver-1',
|
||||
mediaUrl: 'invalid-url',
|
||||
})
|
||||
).toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
describe('reconstitute', () => {
|
||||
it('reconstitutes an avatar from props', () => {
|
||||
const selectedAt = new Date('2024-01-01T00:00:00.000Z');
|
||||
const avatar = Avatar.reconstitute({
|
||||
id: 'avatar-1',
|
||||
driverId: 'driver-1',
|
||||
mediaUrl: 'https://example.com/avatar.png',
|
||||
selectedAt,
|
||||
isActive: true,
|
||||
});
|
||||
|
||||
expect(avatar.id).toBe('avatar-1');
|
||||
expect(avatar.driverId).toBe('driver-1');
|
||||
expect(avatar.mediaUrl.value).toBe('https://example.com/avatar.png');
|
||||
expect(avatar.selectedAt).toEqual(selectedAt);
|
||||
expect(avatar.isActive).toBe(true);
|
||||
});
|
||||
|
||||
it('reconstitutes an inactive avatar', () => {
|
||||
const avatar = Avatar.reconstitute({
|
||||
id: 'avatar-1',
|
||||
driverId: 'driver-1',
|
||||
mediaUrl: 'https://example.com/avatar.png',
|
||||
selectedAt: new Date(),
|
||||
isActive: false,
|
||||
});
|
||||
|
||||
expect(avatar.isActive).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('deactivate', () => {
|
||||
it('deactivates an active avatar', () => {
|
||||
const avatar = Avatar.create({
|
||||
id: 'avatar-1',
|
||||
driverId: 'driver-1',
|
||||
mediaUrl: 'https://example.com/avatar.png',
|
||||
});
|
||||
|
||||
expect(avatar.isActive).toBe(true);
|
||||
|
||||
avatar.deactivate();
|
||||
|
||||
expect(avatar.isActive).toBe(false);
|
||||
});
|
||||
|
||||
it('can deactivate an already inactive avatar', () => {
|
||||
const avatar = Avatar.reconstitute({
|
||||
id: 'avatar-1',
|
||||
driverId: 'driver-1',
|
||||
mediaUrl: 'https://example.com/avatar.png',
|
||||
selectedAt: new Date(),
|
||||
isActive: false,
|
||||
});
|
||||
|
||||
avatar.deactivate();
|
||||
|
||||
expect(avatar.isActive).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('toProps', () => {
|
||||
it('returns correct props for a new avatar', () => {
|
||||
const avatar = Avatar.create({
|
||||
id: 'avatar-1',
|
||||
driverId: 'driver-1',
|
||||
mediaUrl: 'https://example.com/avatar.png',
|
||||
});
|
||||
|
||||
const props = avatar.toProps();
|
||||
|
||||
expect(props.id).toBe('avatar-1');
|
||||
expect(props.driverId).toBe('driver-1');
|
||||
expect(props.mediaUrl).toBe('https://example.com/avatar.png');
|
||||
expect(props.selectedAt).toBeInstanceOf(Date);
|
||||
expect(props.isActive).toBe(true);
|
||||
});
|
||||
|
||||
it('returns correct props for an inactive avatar', () => {
|
||||
const selectedAt = new Date('2024-01-01T00:00:00.000Z');
|
||||
const avatar = Avatar.reconstitute({
|
||||
id: 'avatar-1',
|
||||
driverId: 'driver-1',
|
||||
mediaUrl: 'https://example.com/avatar.png',
|
||||
selectedAt,
|
||||
isActive: false,
|
||||
});
|
||||
|
||||
const props = avatar.toProps();
|
||||
|
||||
expect(props.id).toBe('avatar-1');
|
||||
expect(props.driverId).toBe('driver-1');
|
||||
expect(props.mediaUrl).toBe('https://example.com/avatar.png');
|
||||
expect(props.selectedAt).toEqual(selectedAt);
|
||||
expect(props.isActive).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('value object validation', () => {
|
||||
it('validates mediaUrl as MediaUrl value object', () => {
|
||||
const avatar = Avatar.create({
|
||||
id: 'avatar-1',
|
||||
driverId: 'driver-1',
|
||||
mediaUrl: 'https://example.com/avatar.png',
|
||||
});
|
||||
|
||||
expect(avatar.mediaUrl).toBeInstanceOf(MediaUrl);
|
||||
expect(avatar.mediaUrl.value).toBe('https://example.com/avatar.png');
|
||||
});
|
||||
|
||||
it('accepts data URI for mediaUrl', () => {
|
||||
const avatar = Avatar.create({
|
||||
id: 'avatar-1',
|
||||
driverId: 'driver-1',
|
||||
mediaUrl: 'data:image/png;base64,abc',
|
||||
});
|
||||
|
||||
expect(avatar.mediaUrl.value).toBe('data:image/png;base64,abc');
|
||||
});
|
||||
|
||||
it('accepts root-relative path for mediaUrl', () => {
|
||||
const avatar = Avatar.create({
|
||||
id: 'avatar-1',
|
||||
driverId: 'driver-1',
|
||||
mediaUrl: '/images/avatar.png',
|
||||
});
|
||||
|
||||
expect(avatar.mediaUrl.value).toBe('/images/avatar.png');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user