1
1
import "jest-extended" ;
2
2
3
- import { Application } from "@packages/core-kernel/src/application" ;
4
3
import { 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" ;
5
7
import { Database } from "@packages/core-webhooks/src/database" ;
8
+ import { WebhookEvent } from "@packages/core-webhooks/src/events" ;
6
9
import { Identifiers } from "@packages/core-webhooks/src/identifiers" ;
7
- import { Listener } from "@packages/core-webhooks/src/listener" ;
8
10
import { Webhook } from "@packages/core-webhooks/src/interfaces" ;
11
+ import { Listener } from "@packages/core-webhooks/src/listener" ;
9
12
import { dirSync , setGracefulCleanup } from "tmp" ;
10
- import { HttpOptions , HttpResponse } from "@packages/core-kernel/src/utils" ;
13
+
11
14
import { dummyWebhook } from "./__fixtures__/assets" ;
12
- import * as coditions from "@packages/core-webhooks/src/conditions" ;
13
15
14
- let app : Application ;
16
+ let sandbox : Sandbox ;
15
17
let database : Database ;
16
18
let listener : Listener ;
17
19
let webhook : Webhook ;
@@ -21,20 +23,42 @@ const logger = {
21
23
error : jest . fn ( ) ,
22
24
} ;
23
25
26
+ const mockEventDispatcher = {
27
+ dispatch : jest . fn ( ) ,
28
+ } ;
29
+
24
30
let spyOnPost : jest . SpyInstance ;
25
31
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
+
26
49
beforeEach ( ( ) => {
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 ) ;
29
52
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 ( ) ;
31
55
32
- app . bind ( Container . Identifiers . LogService ) . toConstantValue ( logger ) ;
56
+ sandbox . app . bind ( Container . Identifiers . LogService ) . toConstantValue ( logger ) ;
33
57
34
- database = app . get < Database > ( Identifiers . Database ) ;
58
+ database = sandbox . app . get < Database > ( Identifiers . Database ) ;
35
59
database . boot ( ) ;
36
60
37
- listener = app . resolve < Listener > ( Listener ) ;
61
+ listener = sandbox . app . resolve < Listener > ( Listener ) ;
38
62
39
63
webhook = Object . assign ( { } , dummyWebhook ) ;
40
64
@@ -48,6 +72,7 @@ beforeEach(() => {
48
72
} ) ;
49
73
50
74
afterEach ( ( ) => {
75
+ jest . clearAllMocks ( ) ;
51
76
jest . resetAllMocks ( ) ;
52
77
} ) ;
53
78
@@ -62,6 +87,12 @@ describe("Listener", () => {
62
87
63
88
expect ( spyOnPost ) . toHaveBeenCalled ( ) ;
64
89
expect ( logger . debug ) . toHaveBeenCalled ( ) ;
90
+
91
+ expect ( mockEventDispatcher . dispatch ) . toHaveBeenCalledTimes ( 1 ) ;
92
+ expect ( mockEventDispatcher . dispatch ) . toHaveBeenCalledWith (
93
+ WebhookEvent . Broadcasted ,
94
+ expectFinishedEventData ( ) ,
95
+ ) ;
65
96
} ) ;
66
97
67
98
it ( "should log error if broadcast is not successful" , async ( ) => {
@@ -77,6 +108,9 @@ describe("Listener", () => {
77
108
78
109
expect ( spyOnPost ) . toHaveBeenCalled ( ) ;
79
110
expect ( logger . error ) . toHaveBeenCalled ( ) ;
111
+
112
+ expect ( mockEventDispatcher . dispatch ) . toHaveBeenCalledTimes ( 1 ) ;
113
+ expect ( mockEventDispatcher . dispatch ) . toHaveBeenCalledWith ( WebhookEvent . Failed , expectFailedEventData ( ) ) ;
80
114
} ) ;
81
115
} ) ;
82
116
@@ -90,6 +124,14 @@ describe("Listener", () => {
90
124
expect ( spyOnPost ) . toHaveBeenCalledTimes ( 0 ) ;
91
125
} ) ;
92
126
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
+
93
135
it ( "should broadcast if webhook condition is satisfied" , async ( ) => {
94
136
webhook . conditions = [
95
137
{
@@ -103,6 +145,12 @@ describe("Listener", () => {
103
145
await listener . handle ( { name : "event" , data : { test : 1 } } ) ;
104
146
105
147
expect ( spyOnPost ) . toHaveBeenCalledTimes ( 1 ) ;
148
+
149
+ expect ( mockEventDispatcher . dispatch ) . toHaveBeenCalledTimes ( 1 ) ;
150
+ expect ( mockEventDispatcher . dispatch ) . toHaveBeenCalledWith (
151
+ WebhookEvent . Broadcasted ,
152
+ expectFinishedEventData ( ) ,
153
+ ) ;
106
154
} ) ;
107
155
108
156
it ( "should not broadcast if webhook condition is not satisfied" , async ( ) => {
@@ -121,7 +169,7 @@ describe("Listener", () => {
121
169
} ) ;
122
170
123
171
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 ) => {
125
173
throw new Error ( ) ;
126
174
} ) ;
127
175
0 commit comments