Skip to content

Commit 196e461

Browse files
authored
Merge pull request #128 from dolittle/update
Update
2 parents a3a7e40 + 6a22ba6 commit 196e461

File tree

7 files changed

+149
-1
lines changed

7 files changed

+149
-1
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
# [23.2.3] - 2022-3-23 [PR: #127](https://github.com/dolittle/JavaScript.SDK/pull/127)
2+
## Summary
3+
4+
Fixes a bug in the AggregateRoot where an on-method would be called (while applying events) with the value of a property named `content` on the event (usually `undefined`) - not the actual event.
5+
6+
### Fixed
7+
8+
- When on-methods in Aggregate Roots where called while applying events, the wrong object was passed in as the event. Which in most cases would mean they were called with `undefined`.
9+
10+
111
# [23.2.2] - 2022-3-9 [PR: #125](https://github.com/dolittle/JavaScript.SDK/pull/125)
212
## Summary
313

Source/aggregates/AggregateRoot.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ export class AggregateRoot {
133133
}
134134
const onMethod = this.getOnMethodFor(event, eventType);
135135
if (onMethod) {
136-
onMethod.method.call(this, event.content);
136+
onMethod.method.call(this, event);
137137
}
138138
}
139139

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (c) Dolittle. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
import { EventSourceId } from '@dolittle/sdk.events';
5+
6+
import { AggregateRoot } from '../../AggregateRoot';
7+
import { on } from '../../onDecorator';
8+
9+
import { EventWithOnMethod, EventWithoutOnMethod } from './events';
10+
11+
export class AnAggregateRoot extends AggregateRoot {
12+
readonly onMethodEventsCalled: any[] = [];
13+
14+
constructor(eventSourceId: EventSourceId) {
15+
super(eventSourceId);
16+
}
17+
18+
applyWithOnMethod(event: EventWithOnMethod) {
19+
this.apply(event);
20+
}
21+
22+
applyWithoutOnMethod(event: EventWithoutOnMethod) {
23+
this.apply(event);
24+
}
25+
26+
applyBoth(first: EventWithOnMethod, second: EventWithoutOnMethod) {
27+
this.apply(first);
28+
this.apply(second);
29+
}
30+
31+
@on(EventWithOnMethod)
32+
onEvent(event: EventWithOnMethod) {
33+
this.onMethodEventsCalled.push(event);
34+
}
35+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright (c) Dolittle. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
import { EventType, EventTypeId, EventTypes, IEventTypes } from '@dolittle/sdk.events';
5+
6+
export class EventWithOnMethod {
7+
constructor(readonly property: number) {}
8+
}
9+
10+
export class EventWithoutOnMethod {
11+
constructor(readonly property: string) {}
12+
}
13+
14+
export const event_types = (): IEventTypes => {
15+
const types = new EventTypes();
16+
types.associate(EventWithOnMethod, new EventType(EventTypeId.from('a8881f3f-a34e-4289-9ba9-41f6a79c8d72')));
17+
types.associate(EventWithoutOnMethod, new EventType(EventTypeId.from('334b6259-05e6-420d-856e-5f45e3e740f3')));
18+
return types;
19+
};

Source/aggregates/for_AggregateRoot/when_applying_public_events.ts renamed to Source/aggregates/for_AggregateRoot/when_applying_a_public_event.ts

File renamed without changes.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright (c) Dolittle. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
import { describeThis } from '@dolittle/typescript.testing';
5+
6+
import { EventSourceId } from '@dolittle/sdk.events';
7+
8+
import { AnAggregateRoot } from './given/an_aggregate_root';
9+
import { event_types, EventWithOnMethod, EventWithoutOnMethod } from './given/events';
10+
11+
describeThis(__filename, () => {
12+
let event_source: EventSourceId;
13+
let aggregate_root: AnAggregateRoot;
14+
15+
beforeEach(() => {
16+
event_source = EventSourceId.from('f77f4484-e683-4971-9b90-4cbfafbd21f2');
17+
aggregate_root = new AnAggregateRoot(event_source);
18+
aggregate_root.eventTypes = event_types();
19+
});
20+
21+
describe('that does not have an on-method', () => {
22+
let event: EventWithoutOnMethod;
23+
24+
beforeEach(() => {
25+
event = new EventWithoutOnMethod('property');
26+
aggregate_root.applyWithoutOnMethod(event);
27+
});
28+
29+
it('should have applied one event', () => aggregate_root.appliedEvents.should.have.length(1));
30+
it('should have applied the correct event', () => aggregate_root.appliedEvents[0].event.should.equal(event));
31+
it('should not apply the event as public', () => aggregate_root.appliedEvents[0].isPublic.should.be.false);
32+
it('should not have called an on-method', () => aggregate_root.onMethodEventsCalled.should.have.length(0));
33+
});
34+
35+
describe('that has an on-method', () => {
36+
let event: EventWithOnMethod;
37+
38+
beforeEach(() => {
39+
event = new EventWithOnMethod(42);
40+
aggregate_root.applyWithOnMethod(event);
41+
});
42+
43+
it('should have applied one event', () => aggregate_root.appliedEvents.should.have.length(1));
44+
it('should have applied the correct event', () => aggregate_root.appliedEvents[0].event.should.equal(event));
45+
it('should not apply the event as public', () => aggregate_root.appliedEvents[0].isPublic.should.be.false);
46+
it('should not have called an on-method', () => aggregate_root.onMethodEventsCalled.should.have.length(1));
47+
it('should call the on-method with the correct event', () => aggregate_root.onMethodEventsCalled[0].should.equal(event));
48+
});
49+
});
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (c) Dolittle. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
import { describeThis } from '@dolittle/typescript.testing';
5+
6+
import { EventSourceId } from '@dolittle/sdk.events';
7+
8+
import { AnAggregateRoot } from './given/an_aggregate_root';
9+
import { event_types, EventWithOnMethod, EventWithoutOnMethod } from './given/events';
10+
11+
describeThis(__filename, () => {
12+
let event_source: EventSourceId;
13+
let aggregate_root: AnAggregateRoot;
14+
let first_event: EventWithOnMethod;
15+
let second_event: EventWithoutOnMethod;
16+
17+
beforeEach(() => {
18+
event_source = EventSourceId.from('f77f4484-e683-4971-9b90-4cbfafbd21f2');
19+
aggregate_root = new AnAggregateRoot(event_source);
20+
aggregate_root.eventTypes = event_types();
21+
22+
first_event = new EventWithOnMethod(1337);
23+
second_event = new EventWithoutOnMethod('hello world');
24+
});
25+
26+
beforeEach(() => aggregate_root.applyBoth(first_event, second_event));
27+
28+
it('should have applied two events', () => aggregate_root.appliedEvents.should.have.length(2));
29+
it('should have applied the correct first event', () => aggregate_root.appliedEvents[0].event.should.equal(first_event));
30+
it('should have applied the correct second event', () => aggregate_root.appliedEvents[1].event.should.equal(second_event));
31+
it('should not apply the first event as public', () => aggregate_root.appliedEvents[0].isPublic.should.be.false);
32+
it('should not apply the second event as public', () => aggregate_root.appliedEvents[1].isPublic.should.be.false);
33+
it('should have called an on-method', () => aggregate_root.onMethodEventsCalled.should.have.length(1));
34+
it('should call the on-method with the correct event', () => aggregate_root.onMethodEventsCalled[0].should.equal(first_event));
35+
});

0 commit comments

Comments
 (0)