import { Describe, expect, ExpectException, Suite, Test, } from '@hg-ts/tests'; import { AGGREGATE_EVENTS_SYMBOL } from '../base.aggregate.js'; import { CreatedEventNotFoundException } from '../created-event.not-found.exception.js'; import { STORED_PROPERTY } from '../events/domain.event.js'; import { ExampleCreatedEvent } from './example-created.event.js'; import { ExampleUpdatedEvent } from './example-updated.event.js'; import { ExampleAggregate } from './example.aggregate.js'; @Describe() export class AggregateTest extends Suite { @Test() public simpleCreate(): void { const aggregate = ExampleAggregate.create('original name'); const events = aggregate[AGGREGATE_EVENTS_SYMBOL]; expect(aggregate).toBeInstanceOf(ExampleAggregate); expect(events).toBeInstanceOf(Array); expect(events).toHaveLength(1); expect(events[0]).toBeInstanceOf(ExampleCreatedEvent); expect(events[0]![STORED_PROPERTY]).toBe(false); } @Test() public equalsSuccess(): void { const baseAggregate = ExampleAggregate.create('original name'); const cloneAggregate1 = new ExampleAggregate(baseAggregate.toDto()); const cloneAggregate2 = new ExampleAggregate(baseAggregate.toDto()); expect(baseAggregate.equals(baseAggregate)).toBeTruthy(); expect(cloneAggregate1.equals(cloneAggregate2)).toBeTruthy(); } @Test() public equalsFails(): void { const aggregate = ExampleAggregate.create('original name'); const cloneAggregate = ExampleAggregate.create('original name'); expect(aggregate.equals(cloneAggregate)).toBeFalsy(); } @Test() public updateName(): void { const aggregate = ExampleAggregate.create('original name'); aggregate.update('new name'); const events = aggregate[AGGREGATE_EVENTS_SYMBOL]; expect(events).toBeInstanceOf(Array); expect(events).toHaveLength(2); expect(events[0]).toBeInstanceOf(ExampleCreatedEvent); expect(events[1]).toBeInstanceOf(ExampleUpdatedEvent); expect(aggregate.toDto().name).toEqual('new name'); } @Test() public restoreCreated(): void { const aggregate = ExampleAggregate.create('original name'); const originalEvents = aggregate[AGGREGATE_EVENTS_SYMBOL]; originalEvents.forEach(event => { event[STORED_PROPERTY] = true; }); const restored = ExampleAggregate.restoreFromEvents(ExampleAggregate, ExampleCreatedEvent, originalEvents); expect(restored.toDto()).toMatchObject(aggregate.toDto()); expect(restored.getNewEvets()).toHaveLength(0); } @Test() public restoreUpdated(): void { const aggregate = ExampleAggregate.create('original name'); aggregate.update('new name'); const originalEvents = aggregate[AGGREGATE_EVENTS_SYMBOL]; originalEvents.forEach(event => { event[STORED_PROPERTY] = true; }); const restored = ExampleAggregate.restoreFromEvents(ExampleAggregate, ExampleCreatedEvent, originalEvents); expect(restored.getEvents()).toHaveLength(originalEvents.length); expect(restored.getEvents()[0]?.toDto().name).toBe(ExampleCreatedEvent.name); expect(restored.toDto()).toMatchObject(aggregate.toDto()); } @Test() @ExpectException(CreatedEventNotFoundException) public noCreatedEvent(): void { ExampleAggregate.restoreFromEvents(ExampleAggregate, ExampleCreatedEvent, []); } }