import { OrderRepositoryMemory } from '@cupcake/repository' ; import { EmailNotificationMock } from '@cupcake/notification' ; import { OrderRepository } from '../src' ; import { EmailNotification } from '../src/EmailNotification' ; import { createOrderPayloadFixture } from '../src/order/createOrderPayloadFixture' ; import { OrderService } from '../src/order/OrderService' ; let orderService ; let emailNotification ; let orderRepository ; describe ( `Create Order` , ( ) => { beforeEach ( ( ) => { emailNotification = new EmailNotificationMock ( ) ; orderRepository = new OrderRepositoryMemory ( ) ; orderService = new OrderService ( emailNotification , orderRepository ) ; } ) ; describe ( `Validate Order` , ( ) => { it ( `rejects empty order` , async ( ) => { const payload = createOrderPayloadFixture . build ( { items : [ ] } ) ; await expect ( orderService . create ( payload ) ) . rejects . toThrowErrorMatchingInlineSnapshot ( `"Order must contain at least 3 items but contains only 0"` ) ; } ) ; } ) ; it ( `sends email notification` , async ( ) => { jest . spyOn ( emailNotification , 'sendOrderNotification' ) ; const payload = createOrderPayloadFixture . build ( ) ; const order = await orderService . create ( payload ) ; expect ( emailNotification . sendOrderNotification ) . toHaveBeenCalledWith ( order ) ; } ) ; it ( `saves order` , async ( ) => { jest . spyOn ( orderRepository , 'save' ) ; const payload = createOrderPayloadFixture . build ( ) ; const order = await orderService . create ( payload ) ; expect ( orderRepository . save ) . toHaveBeenCalledWith ( order ) ; } ) ; } ) ;