@@ -52,134 +52,105 @@ describe('app', () => {
52
52
} ) ;
53
53
54
54
it ( 'list: shows an empty list of pledges with the list command' , ( ) => {
55
- return request ( app ) . post ( '/slackCommand' )
56
- . send ( 'user_name=luca' )
57
- . send ( 'text=list' )
58
- . expect ( 200 )
59
- . then ( ( res ) => {
60
- expect ( typeof res . text ) . toBe ( 'string' ) ;
61
- // headers for lists are presents
62
- expect ( res . text ) . toMatch ( 'My pledges' ) ;
63
- expect ( res . text ) . toMatch ( 'My requests' ) ;
64
- // no pledge is present (empty lists)
65
- expect ( res . text ) . not . toMatch ( 'pledged to' ) ;
66
- } ) ;
55
+ return getList ( ) . then ( ( res ) => {
56
+ expect ( typeof res . text ) . toBe ( 'string' ) ;
57
+ // headers for lists are presents
58
+ expect ( res . text ) . toMatch ( 'My pledges' ) ;
59
+ expect ( res . text ) . toMatch ( 'My requests' ) ;
60
+ // no pledge is present (empty lists)
61
+ expect ( res . text ) . not . toMatch ( 'pledged to' ) ;
62
+ } ) ;
67
63
} ) ;
68
64
69
65
it ( 'create: notifies both immediately, appears in list' , ( ) => {
70
66
MockDate . set ( 0 ) ;
71
- request ( app ) . post ( '/slackCommand' )
72
- . send ( 'user_name=requester' )
73
- . send ( 'text=@performer pledge content by tomorrow at 10am' )
74
- . expect ( 200 )
75
- . then ( ( res ) => {
76
- expect ( typeof res . text ) . toBe ( 'string' ) ;
77
- // notification for requester sent as response to slack command
78
- expect ( res . text ) . toMatch ( 'You asked @performer to \"pledge content\" by tomorrow at 10am' ) ;
79
- // notification for performer
80
- expect ( slack . postOnSlack . mock . calls [ 0 ] [ 0 ] . text )
81
- . toMatch ( '@requester asked you to \"pledge content\" by tomorrow at 10am' ) ;
82
- expect ( slack . postOnSlack . mock . calls [ 0 ] [ 0 ] . channel )
83
- . toMatch ( '@performer' ) ;
84
- } ) ;
85
- request ( app ) . post ( '/slackCommand' )
86
- . send ( 'user_name=requester' )
87
- . send ( 'text=list' )
88
- . expect ( 200 )
89
- . then ( ( res ) => {
67
+ return createPledge ( 'tomorrow at 10am' ) . then ( ( res ) => {
68
+ expect ( typeof res . text ) . toBe ( 'string' ) ;
69
+ // notification for requester sent as response to slack command
70
+ expect ( res . text ) . toMatch ( 'You asked @performer to \"content\" by tomorrow at 10am' ) ;
71
+ // notification for performer
72
+ expect ( slack . postOnSlack . mock . calls [ 0 ] [ 0 ] . text )
73
+ . toMatch ( '@requester asked you to \"content\" by tomorrow at 10am' ) ;
74
+ expect ( slack . postOnSlack . mock . calls [ 0 ] [ 0 ] . channel )
75
+ . toMatch ( '@performer' ) ;
76
+ return getList ( ) . then ( ( res ) => {
90
77
expect ( typeof res . text ) . toBe ( 'string' ) ;
91
78
// pledge is present
92
- expect ( res . text ) . toMatch ( '_@performer_ pledged to pledge content *by 2 January at 10:00*' ) ;
79
+ expect ( res . text ) . toMatch ( '_@performer_ pledged to content *by 2 January at 10:00*' ) ;
93
80
} ) ;
81
+ } ) ;
94
82
} ) ;
95
83
96
84
it ( 'expired: notifies both at the right time, stays in list' , ( ) => {
97
85
const timezoneOffset = new Date ( ) . getTimezoneOffset ( ) ;
98
86
const interval = 10 * 60 * 60 * 1000 ; // 10 hours
99
87
const adjInterval = interval + timezoneOffset * 60 * 1000 ;
100
88
MockDate . set ( 0 ) ;
101
- return request ( app ) . post ( '/slackCommand' )
102
- . send ( 'user_name=requester' )
103
- . send ( 'text=@performer content by today at 10am' )
104
- . expect ( 200 )
105
- . then ( async ( ) => {
106
- // number of notifications sent to both performer and requester
107
- const nExp = ( ) =>
108
- slack . postOnSlackMultipleChannels . mock . calls . filter ( ( c ) => {
109
- return c [ 0 ] . text . match ( / e x p i r e d / ) && c [ 1 ] . includes ( '@performer' )
110
- && c [ 1 ] . includes ( '@requester' ) && c [ 1 ] . length === 2 ;
111
- } ) . length ;
112
- // just before, no notifications
113
- MockDate . set ( adjInterval - 1 ) ;
114
- await findNewNotifications ( ) ;
115
- expect ( nExp ( ) ) . toBe ( 0 ) ;
116
- // right after, it notifies
117
- MockDate . set ( adjInterval + 1 ) ;
118
- await findNewNotifications ( ) ;
119
- expect ( nExp ( ) ) . toBe ( 1 ) ;
120
- // after some time, does not notify twice
121
- MockDate . set ( adjInterval + 999 ) ;
122
- await findNewNotifications ( ) ;
123
- expect ( nExp ( ) ) . toBe ( 1 ) ;
124
- // check that it's still present in list
125
- return request ( app ) . post ( '/slackCommand' )
126
- . send ( 'user_name=requester' )
127
- . send ( 'text=list' )
128
- . expect ( 200 )
129
- . then ( ( res ) => {
130
- expect ( typeof res . text ) . toBe ( 'string' ) ;
131
- // pledge is present
132
- expect ( res . text ) . toMatch ( '_@performer_ pledged to content *by 1 January at 10:00*' ) ;
133
- } ) ;
89
+ return createPledge ( 'today at 10am' ) . then ( async ( ) => {
90
+ // number of notifications sent to both performer and requester
91
+ const nExp = ( ) =>
92
+ slack . postOnSlackMultipleChannels . mock . calls . filter ( ( c ) => {
93
+ return c [ 0 ] . text . match ( / e x p i r e d / ) && c [ 1 ] . includes ( '@performer' )
94
+ && c [ 1 ] . includes ( '@requester' ) && c [ 1 ] . length === 2 ;
95
+ } ) . length ;
96
+ // just before, no notifications
97
+ MockDate . set ( adjInterval - 1 ) ;
98
+ await findNewNotifications ( ) ;
99
+ expect ( nExp ( ) ) . toBe ( 0 ) ;
100
+ // right after, it notifies
101
+ MockDate . set ( adjInterval + 1 ) ;
102
+ await findNewNotifications ( ) ;
103
+ expect ( nExp ( ) ) . toBe ( 1 ) ;
104
+ // after some time, does not notify twice
105
+ MockDate . set ( adjInterval + 999 ) ;
106
+ await findNewNotifications ( ) ;
107
+ expect ( nExp ( ) ) . toBe ( 1 ) ;
108
+ // check that it's still present in list
109
+ return getList ( ) . then ( ( res ) => {
110
+ // pledge is present
111
+ expect ( res . text ) . toMatch ( '_@performer_ pledged to content *by 1 January at 10:00*' ) ;
134
112
} ) ;
113
+ } ) ;
135
114
} ) ;
136
115
137
116
it ( 'expired: does not notify for completed pledges' , ( ) => {
138
117
const timezoneOffset = new Date ( ) . getTimezoneOffset ( ) ;
139
118
const interval = 10 * 60 * 60 * 1000 ; // 10 hours
140
119
const adjInterval = interval + timezoneOffset * 60 * 1000 ;
141
120
MockDate . set ( 0 ) ;
142
- return request ( app ) . post ( '/slackCommand' )
143
- . send ( 'user_name=requester' )
144
- . send ( 'text=@performer content by today at 10am' )
145
- . expect ( 200 )
146
- . then ( async ( ) => {
147
- return request ( app ) . get ( '/completePledge/1' ) . expect ( 200 ) . then ( async ( ) => {
148
- const nExp = ( ) =>
149
- slack . postOnSlackMultipleChannels . mock . calls . filter ( ( c ) => {
150
- return c [ 0 ] . text . match ( / e x p i r e d / ) && c [ 1 ] . includes ( '@performer' )
151
- && c [ 1 ] . includes ( '@requester' ) && c [ 1 ] . length === 2 ;
152
- } ) . length ;
153
- // after some time, no notification ever arrived
154
- MockDate . set ( adjInterval + 999 ) ;
155
- await findNewNotifications ( ) ;
156
- expect ( nExp ( ) ) . toBe ( 0 ) ;
157
- } ) ;
121
+ return createPledge ( 'today at 10am' ) . then ( async ( ) => {
122
+ return request ( app ) . get ( '/completePledge/1' ) . expect ( 200 ) . then ( async ( ) => {
123
+ const nExp = ( ) =>
124
+ slack . postOnSlackMultipleChannels . mock . calls . filter ( ( c ) => {
125
+ return c [ 0 ] . text . match ( / e x p i r e d / ) && c [ 1 ] . includes ( '@performer' )
126
+ && c [ 1 ] . includes ( '@requester' ) && c [ 1 ] . length === 2 ;
127
+ } ) . length ;
128
+ // after some time, no notification ever arrived
129
+ MockDate . set ( adjInterval + 999 ) ;
130
+ await findNewNotifications ( ) ;
131
+ expect ( nExp ( ) ) . toBe ( 0 ) ;
158
132
} ) ;
133
+ } ) ;
159
134
} ) ;
160
135
161
136
it ( 'expired: does not notify for deleted pledges' , ( ) => {
162
137
const timezoneOffset = new Date ( ) . getTimezoneOffset ( ) ;
163
138
const interval = 10 * 60 * 60 * 1000 ; // 10 hours
164
139
const adjInterval = interval + timezoneOffset * 60 * 1000 ;
165
140
MockDate . set ( 0 ) ;
166
- return request ( app ) . post ( '/slackCommand' )
167
- . send ( 'user_name=requester' )
168
- . send ( 'text=@performer content by today at 10am' )
169
- . expect ( 200 )
170
- . then ( async ( ) => {
171
- return request ( app ) . get ( '/deletePledge/1' ) . expect ( 200 ) . then ( async ( ) => {
172
- const nExp = ( ) =>
173
- slack . postOnSlackMultipleChannels . mock . calls . filter ( ( c ) => {
174
- return c [ 0 ] . text . match ( / e x p i r e d / ) && c [ 1 ] . includes ( '@performer' )
175
- && c [ 1 ] . includes ( '@requester' ) && c [ 1 ] . length === 2 ;
176
- } ) . length ;
177
- // after some time, no notification ever arrived
178
- MockDate . set ( adjInterval + 999 ) ;
179
- await findNewNotifications ( ) ;
180
- expect ( nExp ( ) ) . toBe ( 0 ) ;
181
- } ) ;
141
+ return createPledge ( 'today at 10am' ) . then ( async ( ) => {
142
+ return request ( app ) . get ( '/deletePledge/1' ) . expect ( 200 ) . then ( async ( ) => {
143
+ const nExp = ( ) =>
144
+ slack . postOnSlackMultipleChannels . mock . calls . filter ( ( c ) => {
145
+ return c [ 0 ] . text . match ( / e x p i r e d / ) && c [ 1 ] . includes ( '@performer' )
146
+ && c [ 1 ] . includes ( '@requester' ) && c [ 1 ] . length === 2 ;
147
+ } ) . length ;
148
+ // after some time, no notification ever arrived
149
+ MockDate . set ( adjInterval + 999 ) ;
150
+ await findNewNotifications ( ) ;
151
+ expect ( nExp ( ) ) . toBe ( 0 ) ;
182
152
} ) ;
153
+ } ) ;
183
154
} ) ;
184
155
185
156
it ( 'delete: notifies both immediately, disappears from list' , ( ) => {
@@ -193,10 +164,11 @@ describe('app', () => {
193
164
return request ( app ) . get ( '/deletePledge/1' ) . expect ( 200 ) . then ( ( res ) => {
194
165
expect ( res . text ) . toEqual ( 'Successfully deleted pledge #1' ) ;
195
166
// notifications are sent
196
- expect ( slack . postOnSlackMultipleChannels . mock . calls [ 0 ] ) . toEqual ( [
197
- { text : 'pledge "content" has been deleted' } ,
198
- [ '@requester' , '@performer' ]
199
- ] ) ;
167
+ expect ( slack . postOnSlackMultipleChannels . mock . calls
168
+ . filter ( ( x ) => x [ 0 ] . text . match ( 'has been deleted' ) ) [ 0 ] ) . toEqual ( [
169
+ { text : 'pledge "content" has been deleted' } ,
170
+ [ '@requester' , '@performer' ]
171
+ ] ) ;
200
172
return getList ( ) . then ( ( res ) => {
201
173
// pledge is not in list any more
202
174
expect ( res . text ) . not . toMatch ( '_@performer_ pledged to content *by 2 January at 10:00*' ) ;
@@ -217,10 +189,11 @@ describe('app', () => {
217
189
return request ( app ) . get ( '/completePledge/1' ) . expect ( 200 ) . then ( ( res ) => {
218
190
expect ( res . text ) . toEqual ( 'Successfully completed pledge #1' ) ;
219
191
// notifications are sent
220
- expect ( slack . postOnSlackMultipleChannels . mock . calls [ 0 ] ) . toEqual ( [
221
- { text : 'pledge "content" has been completed !!!' } ,
222
- [ '@requester' , '@performer' ]
223
- ] ) ;
192
+ expect ( slack . postOnSlackMultipleChannels . mock . calls
193
+ . filter ( ( x ) => x [ 0 ] . text . match ( 'has been completed' ) ) [ 0 ] ) . toEqual ( [
194
+ { text : 'pledge "content" has been completed !!!' } ,
195
+ [ '@requester' , '@performer' ]
196
+ ] ) ;
224
197
return getList ( ) . then ( ( res ) => {
225
198
// pledge is not in list any more
226
199
expect ( res . text ) . not . toMatch ( '_@performer_ pledged to content *by 2 January at 10:00*' ) ;
0 commit comments