11import "jest-extended" ;
22
3- import { Application } from "@packages/core-kernel/src/application" ;
43import { Container , Utils } from "@packages/core-kernel" ;
4+ import { HttpOptions , HttpResponse } from "@packages/core-kernel/src/utils" ;
5+ import { Sandbox } from "@packages/core-test-framework" ;
6+ import * as coditions from "@packages/core-webhooks/src/conditions" ;
57import { Database } from "@packages/core-webhooks/src/database" ;
8+ import { WebhookEvent } from "@packages/core-webhooks/src/events" ;
69import { Identifiers } from "@packages/core-webhooks/src/identifiers" ;
7- import { Listener } from "@packages/core-webhooks/src/listener" ;
810import { Webhook } from "@packages/core-webhooks/src/interfaces" ;
11+ import { Listener } from "@packages/core-webhooks/src/listener" ;
912import { dirSync , setGracefulCleanup } from "tmp" ;
10- import { HttpOptions , HttpResponse } from "@packages/core-kernel/src/utils" ;
13+
1114import { dummyWebhook } from "./__fixtures__/assets" ;
12- import * as coditions from "@packages/core-webhooks/src/conditions" ;
1315
14- let app : Application ;
16+ let sandbox : Sandbox ;
1517let database : Database ;
1618let listener : Listener ;
1719let webhook : Webhook ;
@@ -21,20 +23,42 @@ const logger = {
2123 error : jest . fn ( ) ,
2224} ;
2325
26+ const mockEventDispatcher = {
27+ dispatch : jest . fn ( ) ,
28+ } ;
29+
2430let spyOnPost : jest . SpyInstance ;
2531
32+ const expectFinishedEventData = ( ) => {
33+ return expect . objectContaining ( {
34+ executionTime : expect . toBeNumber ( ) ,
35+ webhook : expect . toBeObject ( ) ,
36+ payload : expect . anything ( ) ,
37+ } ) ;
38+ } ;
39+
40+ const expectFailedEventData = ( ) => {
41+ return expect . objectContaining ( {
42+ executionTime : expect . toBeNumber ( ) ,
43+ webhook : expect . toBeObject ( ) ,
44+ payload : expect . anything ( ) ,
45+ error : expect . toBeObject ( ) ,
46+ } ) ;
47+ } ;
48+
2649beforeEach ( ( ) => {
27- app = new Application ( new Container . Container ( ) ) ;
28- app . bind ( "path.cache" ) . toConstantValue ( dirSync ( ) . name ) ;
50+ sandbox = new Sandbox ( ) ;
51+ sandbox . app . bind ( "path.cache" ) . toConstantValue ( dirSync ( ) . name ) ;
2952
30- app . bind < Database > ( Identifiers . Database ) . to ( Database ) . inSingletonScope ( ) ;
53+ sandbox . app . bind ( Container . Identifiers . EventDispatcherService ) . toConstantValue ( mockEventDispatcher ) ;
54+ sandbox . app . bind < Database > ( Identifiers . Database ) . to ( Database ) . inSingletonScope ( ) ;
3155
32- app . bind ( Container . Identifiers . LogService ) . toConstantValue ( logger ) ;
56+ sandbox . app . bind ( Container . Identifiers . LogService ) . toConstantValue ( logger ) ;
3357
34- database = app . get < Database > ( Identifiers . Database ) ;
58+ database = sandbox . app . get < Database > ( Identifiers . Database ) ;
3559 database . boot ( ) ;
3660
37- listener = app . resolve < Listener > ( Listener ) ;
61+ listener = sandbox . app . resolve < Listener > ( Listener ) ;
3862
3963 webhook = Object . assign ( { } , dummyWebhook ) ;
4064
@@ -48,6 +72,7 @@ beforeEach(() => {
4872} ) ;
4973
5074afterEach ( ( ) => {
75+ jest . clearAllMocks ( ) ;
5176 jest . resetAllMocks ( ) ;
5277} ) ;
5378
@@ -62,6 +87,12 @@ describe("Listener", () => {
6287
6388 expect ( spyOnPost ) . toHaveBeenCalled ( ) ;
6489 expect ( logger . debug ) . toHaveBeenCalled ( ) ;
90+
91+ expect ( mockEventDispatcher . dispatch ) . toHaveBeenCalledTimes ( 1 ) ;
92+ expect ( mockEventDispatcher . dispatch ) . toHaveBeenCalledWith (
93+ WebhookEvent . Broadcasted ,
94+ expectFinishedEventData ( ) ,
95+ ) ;
6596 } ) ;
6697
6798 it ( "should log error if broadcast is not successful" , async ( ) => {
@@ -77,6 +108,9 @@ describe("Listener", () => {
77108
78109 expect ( spyOnPost ) . toHaveBeenCalled ( ) ;
79110 expect ( logger . error ) . toHaveBeenCalled ( ) ;
111+
112+ expect ( mockEventDispatcher . dispatch ) . toHaveBeenCalledTimes ( 1 ) ;
113+ expect ( mockEventDispatcher . dispatch ) . toHaveBeenCalledWith ( WebhookEvent . Failed , expectFailedEventData ( ) ) ;
80114 } ) ;
81115 } ) ;
82116
@@ -90,6 +124,14 @@ describe("Listener", () => {
90124 expect ( spyOnPost ) . toHaveBeenCalledTimes ( 0 ) ;
91125 } ) ;
92126
127+ it ( "should not broadcast if event is webhook event" , async ( ) => {
128+ database . create ( webhook ) ;
129+
130+ await listener . handle ( { name : WebhookEvent . Broadcasted , data : "dummy_data" } ) ;
131+
132+ expect ( spyOnPost ) . toHaveBeenCalledTimes ( 0 ) ;
133+ } ) ;
134+
93135 it ( "should broadcast if webhook condition is satisfied" , async ( ) => {
94136 webhook . conditions = [
95137 {
@@ -103,6 +145,12 @@ describe("Listener", () => {
103145 await listener . handle ( { name : "event" , data : { test : 1 } } ) ;
104146
105147 expect ( spyOnPost ) . toHaveBeenCalledTimes ( 1 ) ;
148+
149+ expect ( mockEventDispatcher . dispatch ) . toHaveBeenCalledTimes ( 1 ) ;
150+ expect ( mockEventDispatcher . dispatch ) . toHaveBeenCalledWith (
151+ WebhookEvent . Broadcasted ,
152+ expectFinishedEventData ( ) ,
153+ ) ;
106154 } ) ;
107155
108156 it ( "should not broadcast if webhook condition is not satisfied" , async ( ) => {
@@ -121,7 +169,7 @@ describe("Listener", () => {
121169 } ) ;
122170
123171 it ( "should not broadcast if webhook condition throws error" , async ( ) => {
124- let spyOnEq = jest . spyOn ( coditions , "eq" ) . mockImplementation ( ( actual , expected ) => {
172+ const spyOnEq = jest . spyOn ( coditions , "eq" ) . mockImplementation ( ( actual , expected ) => {
125173 throw new Error ( ) ;
126174 } ) ;
127175
0 commit comments