@@ -2,6 +2,7 @@ import { initServer } from '../../../src/server';
22import { getConfig } from '../../../src/config' ;
33import { StreamLabs } from '../../../src/services/StreamLabs' ;
44import { TwitchChat } from '../../../src/services/TwitchChat' ;
5+ import { PullRequestPayloadBuilder } from '../../builders/github/pull-request-payload-builder' ;
56
67describe ( 'POST /github' , ( ) => {
78 describe ( "GitHub 'pull_request' event" , ( ) => {
@@ -16,189 +17,72 @@ describe('POST /github', () => {
1617 spyTwitchChat . mockImplementationOnce ( jest . fn ( ) ) ;
1718 } ) ;
1819
19- describe ( 'StreamLabs alerts' , ( ) => {
20- it ( 'sends the notification to StreamLabs when a pull request was opened' , async ( ) => {
21- const subject = await initServer ( getConfig ( ) ) ;
22- const repositoryFullName = 'streamdevs/webhook' ;
23- const pullRequestLogin = 'SantiMA10' ;
20+ it ( 'handles the pull request opened event' , async ( ) => {
21+ const subject = await initServer ( getConfig ( ) ) ;
22+ const payload = new PullRequestPayloadBuilder ( ) . with ( { action : 'opened' } ) . getInstance ( ) ;
2423
25- await subject . inject ( {
26- method : 'POST' ,
27- url : '/github' ,
28- payload : {
29- action : 'opened' ,
30- repository : { full_name : repositoryFullName } ,
31- pull_request : { user : { login : pullRequestLogin } } ,
32- sender : { login : 'pepe' } ,
33- } ,
34- headers : { 'x-github-event' : 'pull_request' } ,
35- } ) ;
36-
37- const expectedPayload = {
38- message : `*${ pullRequestLogin } * just opened a pull request in *${ repositoryFullName } *` ,
39- } ;
40- expect ( spyStreamLabs ) . toHaveBeenCalledWith ( expectedPayload ) ;
41- } ) ;
42-
43- it ( "ignores other 'pull_request' event" , async ( ) => {
44- const subject = await initServer ( getConfig ( ) ) ;
45- const repositoryFullName = 'streamdevs/webhook' ;
46- const pullRequestLogin = 'SantiMA10' ;
47-
48- const { statusCode } = await subject . inject ( {
49- method : 'POST' ,
50- url : '/github' ,
51- payload : {
52- action : 'assigned' ,
53- repository : { full_name : repositoryFullName } ,
54- pull_request : { user : { login : pullRequestLogin } } ,
55- sender : { login : 'pepe' } ,
56- } ,
57- headers : { 'x-github-event' : 'pull_request' } ,
58- } ) ;
59-
60- expect ( spyStreamLabs ) . not . toHaveBeenCalled ( ) ;
61- expect ( statusCode ) . toBe ( 200 ) ;
62- } ) ;
63-
64- it ( "ignores other 'closed' event when it is not merged" , async ( ) => {
65- const subject = await initServer ( getConfig ( ) ) ;
66- const repositoryFullName = 'streamdevs/webhook' ;
67- const pullRequestLogin = 'SantiMA10' ;
68-
69- const { statusCode } = await subject . inject ( {
70- method : 'POST' ,
71- url : '/github' ,
72- payload : {
73- action : 'closed' ,
74- repository : { full_name : repositoryFullName } ,
75- pull_request : { user : { login : pullRequestLogin } } ,
76- sender : { login : 'pepe' } ,
77- } ,
78- headers : { 'x-github-event' : 'pull_request' } ,
79- } ) ;
80-
81- expect ( spyStreamLabs ) . not . toHaveBeenCalled ( ) ;
82- expect ( statusCode ) . toEqual ( 200 ) ;
24+ const { result } = await subject . inject ( {
25+ method : 'POST' ,
26+ url : '/github' ,
27+ payload,
28+ headers : { 'x-github-event' : 'pull_request' } ,
8329 } ) ;
8430
85- it ( 'sends a notification to StreamLabs when a pull request was merged' , async ( ) => {
86- const subject = await initServer ( getConfig ( ) ) ;
87- const repositoryFullName = 'streamdevs/webhook' ;
88- const pullRequestLogin = 'SantiMA10' ;
89-
90- await subject . inject ( {
91- method : 'POST' ,
92- url : '/github' ,
93- payload : {
94- action : 'closed' ,
95- repository : { full_name : repositoryFullName } ,
96- pull_request : { user : { login : pullRequestLogin } , merged : true } ,
97- sender : { login : 'pepe' } ,
98- } ,
99- headers : { 'x-github-event' : 'pull_request' } ,
100- } ) ;
101-
102- const expectedPayload = {
103- message : `The pull request from *${ pullRequestLogin } * has been merged into *${ repositoryFullName } *` ,
104- } ;
105-
106- expect ( spyStreamLabs ) . toHaveBeenCalledWith ( expectedPayload ) ;
107- } ) ;
31+ expect ( result ) . toEqual (
32+ expect . objectContaining ( {
33+ messages : [
34+ expect . objectContaining ( {
35+ twitchChat : expect . anything ( ) ,
36+ streamlabs : expect . anything ( ) ,
37+ } ) ,
38+ ] ,
39+ } ) ,
40+ ) ;
10841 } ) ;
10942
110- describe ( 'TwitchChat send' , ( ) => {
111- it ( 'sends the message to the Twitch chat when a pull request was opened' , async ( ) => {
112- const subject = await initServer ( getConfig ( ) ) ;
113- const repositoryUrl = 'https://github.com/streamdevs/webhook' ;
114- const pullRequestLogin = 'SantiMA10' ;
115-
116- await subject . inject ( {
117- method : 'POST' ,
118- url : '/github' ,
119- payload : {
120- action : 'opened' ,
121- repository : {
122- full_name : 'streamdevs/webhook' ,
123- html_url : repositoryUrl ,
124- } ,
125- pull_request : { user : { login : pullRequestLogin } } ,
126- sender : { login : 'pepe' } ,
127- } ,
128- headers : { 'x-github-event' : 'pull_request' } ,
129- } ) ;
130-
131- expect ( spyTwitchChat ) . toHaveBeenCalledWith (
132- `${ pullRequestLogin } just opened a pull request in ${ repositoryUrl } ` ,
133- ) ;
134- } ) ;
135-
136- it ( 'sends the message to the Twitch chat when a pull request was merged' , async ( ) => {
137- const subject = await initServer ( getConfig ( ) ) ;
138- const repositoryUrl = 'https://github.com/streamdevs/webhook' ;
139- const pullRequestLogin = 'SantiMA10' ;
140-
141- await subject . inject ( {
142- method : 'POST' ,
143- url : '/github' ,
144- payload : {
145- action : 'closed' ,
146- repository : {
147- full_name : 'streamdevs/webhook' ,
148- html_url : repositoryUrl ,
149- } ,
150- pull_request : { user : { login : pullRequestLogin } , merged : true } ,
151- sender : { login : 'pepe' } ,
152- } ,
153- headers : { 'x-github-event' : 'pull_request' } ,
154- } ) ;
155-
156- expect ( spyTwitchChat ) . toHaveBeenCalledWith (
157- `The pull request from ${ pullRequestLogin } has been merged into ${ repositoryUrl } ` ,
158- ) ;
43+ it ( 'handles the pull request merged event' , async ( ) => {
44+ const subject = await initServer ( getConfig ( ) ) ;
45+ const payload = new PullRequestPayloadBuilder ( )
46+ . with ( { action : 'closed' , pull_request : { merged : true } } )
47+ . getInstance ( ) ;
48+
49+ const { result } = await subject . inject ( {
50+ method : 'POST' ,
51+ url : '/github' ,
52+ payload,
53+ headers : { 'x-github-event' : 'pull_request' } ,
15954 } ) ;
16055
161- it ( "ignores other 'pull_request' event" , async ( ) => {
162- const subject = await initServer ( getConfig ( ) ) ;
163- const repositoryFullName = 'streamdevs/webhook' ;
164- const pullRequestLogin = 'SantiMA10' ;
165-
166- const { statusCode } = await subject . inject ( {
167- method : 'POST' ,
168- url : '/github' ,
169- payload : {
170- action : 'assigned' ,
171- repository : { full_name : repositoryFullName } ,
172- pull_request : { user : { login : pullRequestLogin } } ,
173- sender : { login : 'pepe' } ,
174- } ,
175- headers : { 'x-github-event' : 'pull_request' } ,
176- } ) ;
56+ expect ( result ) . toEqual (
57+ expect . objectContaining ( {
58+ messages : [
59+ expect . objectContaining ( {
60+ twitchChat : expect . anything ( ) ,
61+ streamlabs : expect . anything ( ) ,
62+ } ) ,
63+ ] ,
64+ } ) ,
65+ ) ;
66+ } ) ;
17767
178- expect ( spyTwitchChat ) . not . toHaveBeenCalled ( ) ;
179- expect ( statusCode ) . toBe ( 200 ) ;
68+ it ( 'ignores other pull request events' , async ( ) => {
69+ const subject = await initServer ( getConfig ( ) ) ;
70+ const payload = new PullRequestPayloadBuilder ( )
71+ . with ( { action : 'edited' , pull_request : { merged : true } } )
72+ . getInstance ( ) ;
73+
74+ const { result } = await subject . inject ( {
75+ method : 'POST' ,
76+ url : '/github' ,
77+ payload,
78+ headers : { 'x-github-event' : 'pull_request' } ,
18079 } ) ;
18180
182- it ( "ignores other 'closed' event when it is not merged" , async ( ) => {
183- const subject = await initServer ( getConfig ( ) ) ;
184- const repositoryFullName = 'streamdevs/webhook' ;
185- const pullRequestLogin = 'SantiMA10' ;
186-
187- const { statusCode } = await subject . inject ( {
188- method : 'POST' ,
189- url : '/github' ,
190- payload : {
191- action : 'closed' ,
192- repository : { full_name : repositoryFullName } ,
193- pull_request : { user : { login : pullRequestLogin } } ,
194- sender : { login : 'pepe' } ,
195- } ,
196- headers : { 'x-github-event' : 'pull_request' } ,
197- } ) ;
198-
199- expect ( spyTwitchChat ) . not . toHaveBeenCalled ( ) ;
200- expect ( statusCode ) . toEqual ( 200 ) ;
201- } ) ;
81+ expect ( result ) . toEqual (
82+ expect . objectContaining ( {
83+ message : expect . anything ( ) ,
84+ } ) ,
85+ ) ;
20286 } ) ;
20387 } ) ;
20488} ) ;
0 commit comments