Skip to content

Commit

Permalink
feat: create customer events
Browse files Browse the repository at this point in the history
  • Loading branch information
LeonardoCamargo31 committed Oct 31, 2023
1 parent fb60b22 commit 8d29f2a
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/domain/event/customer/customer-change-address-event.spec.ts
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 src/domain/event/customer/customer-change-address-event.ts
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
}
}
38 changes: 38 additions & 0 deletions src/domain/event/customer/customer-created-event.spec.ts
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()
})
})
11 changes: 11 additions & 0 deletions src/domain/event/customer/customer-created-event.ts
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
}
}
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}`)
}
}
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')
}
}
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')
}
}

0 comments on commit 8d29f2a

Please sign in to comment.