-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fb60b22
commit 8d29f2a
Showing
7 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
src/domain/event/customer/customer-change-address-event.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { EventDispatcher } from '../@shared/event-dispatcher' | ||
import { CustomerChangeAddressEvent } from './customer-change-address-event' | ||
import { SendConsoleLogHandler } from './handlers/send-console-log-handler' | ||
|
||
describe('Customer change address events tests', () => { | ||
it('should register an event handler', () => { | ||
const eventDispatcher = new EventDispatcher() | ||
const eventHandler = new SendConsoleLogHandler() | ||
eventDispatcher.register('CustomerChangeAddressEvent', eventHandler) | ||
expect(eventDispatcher.getEventHandlers.CustomerChangeAddressEvent).toBeDefined() | ||
expect(eventDispatcher.getEventHandlers.CustomerChangeAddressEvent.length).toBe(1) | ||
expect(eventDispatcher.getEventHandlers.CustomerChangeAddressEvent[0]).toMatchObject(eventHandler) | ||
}) | ||
|
||
it('should notify all event handlers', () => { | ||
const eventDispatcher = new EventDispatcher() | ||
const eventHandler = new SendConsoleLogHandler() | ||
const spyEventHandler = jest.spyOn(eventHandler, 'handler') | ||
|
||
eventDispatcher.register('CustomerChangeAddressEvent', eventHandler) | ||
expect(eventDispatcher.getEventHandlers.CustomerChangeAddressEvent[0]).toMatchObject(eventHandler) | ||
|
||
const customerCreatedEvent = new CustomerChangeAddressEvent({ | ||
id: '123', | ||
name: 'John', | ||
street: 'Street 1', | ||
number: 123, | ||
zip: '12345-678', | ||
city: 'São Paulo', | ||
state: 'SP' | ||
}) | ||
|
||
eventDispatcher.notify(customerCreatedEvent) | ||
expect(spyEventHandler).toHaveBeenCalled() | ||
}) | ||
}) |
11 changes: 11 additions & 0 deletions
11
src/domain/event/customer/customer-change-address-event.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { EventInterface } from '../@shared/event-interface' | ||
|
||
export class CustomerChangeAddressEvent implements EventInterface { | ||
readonly dataTimeOccurred: Date | ||
readonly eventData: any | ||
|
||
constructor (eventData: any) { | ||
this.dataTimeOccurred = new Date() | ||
this.eventData = eventData | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { EventDispatcher } from '../@shared/event-dispatcher' | ||
import { CustomerCreatedEvent } from './customer-created-event' | ||
import { SendConsoleLog1Handler } from './handlers/send-console-log1-handler' | ||
import { SendConsoleLog2Handler } from './handlers/send-console-log2-handler' | ||
|
||
describe('Customer created events tests', () => { | ||
it('should register an event handler', () => { | ||
const eventDispatcher = new EventDispatcher() | ||
const eventHandler = new SendConsoleLog1Handler() | ||
eventDispatcher.register('CustomerCreatedEvent', eventHandler) | ||
expect(eventDispatcher.getEventHandlers.CustomerCreatedEvent).toBeDefined() | ||
expect(eventDispatcher.getEventHandlers.CustomerCreatedEvent.length).toBe(1) | ||
expect(eventDispatcher.getEventHandlers.CustomerCreatedEvent[0]).toMatchObject(eventHandler) | ||
}) | ||
|
||
it('should notify all event handlers', () => { | ||
const eventDispatcher = new EventDispatcher() | ||
const eventHandler1 = new SendConsoleLog1Handler() | ||
const eventHandler2 = new SendConsoleLog2Handler() | ||
|
||
const spyEventHandler1 = jest.spyOn(eventHandler1, 'handler') | ||
const spyEventHandler2 = jest.spyOn(eventHandler2, 'handler') | ||
|
||
eventDispatcher.register('CustomerCreatedEvent', eventHandler1) | ||
eventDispatcher.register('CustomerCreatedEvent', eventHandler2) | ||
|
||
expect(eventDispatcher.getEventHandlers.CustomerCreatedEvent[0]).toMatchObject(eventHandler1) | ||
expect(eventDispatcher.getEventHandlers.CustomerCreatedEvent[1]).toMatchObject(eventHandler2) | ||
|
||
const customerCreatedEvent = new CustomerCreatedEvent({ | ||
name: 'John' | ||
}) | ||
|
||
eventDispatcher.notify(customerCreatedEvent) | ||
expect(spyEventHandler1).toHaveBeenCalled() | ||
expect(spyEventHandler2).toHaveBeenCalled() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { EventInterface } from '../@shared/event-interface' | ||
|
||
export class CustomerCreatedEvent implements EventInterface { | ||
readonly dataTimeOccurred: Date | ||
readonly eventData: any | ||
|
||
constructor (eventData: any) { | ||
this.dataTimeOccurred = new Date() | ||
this.eventData = eventData | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/domain/event/customer/handlers/send-console-log-handler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { EventHandlerInterface } from '../../@shared/event-handler-interface' | ||
import { EventInterface } from '../../@shared/event-interface' | ||
import { CustomerCreatedEvent } from '../customer-created-event' | ||
|
||
export class SendConsoleLogHandler implements EventHandlerInterface<CustomerCreatedEvent> { | ||
handler (event: EventInterface): void { | ||
console.log(`endereço do cliente: ${event.eventData.id as string}, ${event.eventData.name as string} alterado para: ${event.eventData.street as string}, ${event.eventData.number.toString() as string}`) | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/domain/event/customer/handlers/send-console-log1-handler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { EventHandlerInterface } from '../../@shared/event-handler-interface' | ||
import { EventInterface } from '../../@shared/event-interface' | ||
import { CustomerCreatedEvent } from '../customer-created-event' | ||
|
||
export class SendConsoleLog1Handler implements EventHandlerInterface<CustomerCreatedEvent> { | ||
handler (event: EventInterface): void { | ||
console.log('esse é o primeiro console.log do evento: CustomerCreated') | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/domain/event/customer/handlers/send-console-log2-handler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { EventHandlerInterface } from '../../@shared/event-handler-interface' | ||
import { EventInterface } from '../../@shared/event-interface' | ||
import { CustomerCreatedEvent } from '../customer-created-event' | ||
|
||
export class SendConsoleLog2Handler implements EventHandlerInterface<CustomerCreatedEvent> { | ||
handler (event: EventInterface): void { | ||
console.log('esse é o segundo console.log do evento: CustomerCreated') | ||
} | ||
} |