1- import { describe , it , expect , beforeAll , afterAll , vi } from 'vitest'
1+ import { describe , it , expect , beforeAll , afterAll } from 'vitest'
22import * as when from '../steps/when.mjs'
33import * as given from '../steps/given.mjs'
44import * as teardown from '../steps/teardown.mjs'
5- import { EventBridgeClient } from '@aws-sdk/client-eventbridge'
6-
7- const mockSend = vi . fn ( )
8- EventBridgeClient . prototype . send = mockSend
5+ import { startListening } from '../messages.mjs'
96
107describe ( 'Given an authenticated user' , ( ) => {
11- let user
8+ let user , listener
129
1310 beforeAll ( async ( ) => {
1411 user = await given . an_authenticated_user ( )
12+ listener = startListening ( )
1513 } )
1614
1715 afterAll ( async ( ) => {
1816 await teardown . an_authenticated_user ( user )
17+ await listener . stop ( )
1918 } )
2019
2120 describe ( `When we invoke the POST /orders endpoint` , ( ) => {
2221 let resp
2322
2423 beforeAll ( async ( ) => {
25- mockSend . mockClear ( )
26- mockSend . mockReturnValue ( { } )
27-
2824 resp = await when . we_invoke_place_order ( user , 'Fangtasia' )
2925 } )
3026
3127 it ( `Should return 200` , async ( ) => {
3228 expect ( resp . statusCode ) . toEqual ( 200 )
3329 } )
3430
35- if ( process . env . TEST_MODE === 'handler' ) {
36- it ( `Should publish a message to EventBridge bus` , async ( ) => {
37- expect ( mockSend ) . toHaveBeenCalledTimes ( 1 )
38- const [ putEventsCmd ] = mockSend . mock . calls [ 0 ]
39- expect ( putEventsCmd . input ) . toEqual ( {
40- Entries : [
41- expect . objectContaining ( {
42- Source : 'big-mouth' ,
43- DetailType : 'order_placed' ,
44- Detail : expect . stringContaining ( `"restaurantName":"Fangtasia"` ) ,
45- EventBusName : process . env . bus_name
46- } )
47- ]
48- } )
31+ it ( `Should publish a message to EventBridge bus` , async ( ) => {
32+ const { orderId } = resp . body
33+ const expectedMsg = JSON . stringify ( {
34+ source : 'big-mouth' ,
35+ 'detail-type' : 'order_placed' ,
36+ detail : {
37+ orderId,
38+ restaurantName : 'Fangtasia'
39+ }
4940 } )
50- }
41+
42+ await listener . waitForMessage ( x =>
43+ x . sourceType === 'eventbridge' &&
44+ x . source === process . env . bus_name &&
45+ x . message === expectedMsg
46+ )
47+ } , 10000 )
5148 } )
52- } )
49+ } )
0 commit comments