import mongoose from 'mongoose' ; import { Message } from 'node-nats-streaming' ; import { OrderStatus , ExpirationCompleteEvent } from '@sgtickets/common' ; import { ExpirationCompleteListener } from '../expiration-complete-listener' ; import { natsWrapper } from '../../../nats-wrapper' ; import { Order } from '../../../models/order' ; import { Ticket } from '../../../models/ticket' ; const setup = async ( ) => { const listener = new ExpirationCompleteListener ( natsWrapper . client ) ; const ticket = Ticket . build ( { id : mongoose . Types . ObjectId ( ) . toHexString ( ) , title : 'concert' , price : 20 , } ) ; await ticket . save ( ) ; const order = Order . build ( { status : OrderStatus . Created , userId : 'alskdfj' , expiresAt : new Date ( ) , ticket , } ) ; await order . save ( ) ; const data = { orderId : order . id , } ; const msg = { ack : jest . fn ( ) , } ; return { listener , order , ticket , data , msg } ; } ; it ( 'updates the order status to cancelled' , async ( ) => { const { listener , order , data , msg } = await setup ( ) ; await listener . onMessage ( data , msg ) ; const updatedOrder = await Order . findById ( order . id ) ; expect ( updatedOrder ! . status ) . toEqual ( OrderStatus . Cancelled ) ; } ) ; it ( 'emit an OrderCancelled event' , async ( ) => { const { listener , order , data , msg } = await setup ( ) ; await listener . onMessage ( data , msg ) ; expect ( natsWrapper . client . publish ) . toHaveBeenCalled ( ) ; const eventData = JSON . parse ( ( natsWrapper . client . publish as jest . Mock ) . mock . calls [ 0 ] [ 1 ] ) ; expect ( eventData . id ) . toEqual ( order . id ) ; } ) ; it ( 'ack the message' , async ( ) => { const { listener , data , msg } = await setup ( ) ; await listener . onMessage ( data , msg ) ; expect ( msg . ack ) . toHaveBeenCalled ( ) ; } ) ;