Skip to content
This repository was archived by the owner on Mar 25, 2020. It is now read-only.

Commit 9787034

Browse files
committed
refactor
1 parent d5081de commit 9787034

File tree

1 file changed

+78
-105
lines changed

1 file changed

+78
-105
lines changed

tests/index.test.js

+78-105
Original file line numberDiff line numberDiff line change
@@ -52,134 +52,105 @@ describe('app', () => {
5252
});
5353

5454
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+
});
6763
});
6864

6965
it('create: notifies both immediately, appears in list', () => {
7066
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) => {
9077
expect(typeof res.text).toBe('string');
9178
// 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*');
9380
});
81+
});
9482
});
9583

9684
it('expired: notifies both at the right time, stays in list', () => {
9785
const timezoneOffset = new Date().getTimezoneOffset();
9886
const interval = 10 * 60 * 60 * 1000; // 10 hours
9987
const adjInterval = interval + timezoneOffset * 60 * 1000;
10088
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(/expired/) && 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(/expired/) && 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*');
134112
});
113+
});
135114
});
136115

137116
it('expired: does not notify for completed pledges', () => {
138117
const timezoneOffset = new Date().getTimezoneOffset();
139118
const interval = 10 * 60 * 60 * 1000; // 10 hours
140119
const adjInterval = interval + timezoneOffset * 60 * 1000;
141120
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(/expired/) && 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(/expired/) && 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);
158132
});
133+
});
159134
});
160135

161136
it('expired: does not notify for deleted pledges', () => {
162137
const timezoneOffset = new Date().getTimezoneOffset();
163138
const interval = 10 * 60 * 60 * 1000; // 10 hours
164139
const adjInterval = interval + timezoneOffset * 60 * 1000;
165140
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(/expired/) && 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(/expired/) && 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);
182152
});
153+
});
183154
});
184155

185156
it('delete: notifies both immediately, disappears from list', () => {
@@ -193,10 +164,11 @@ describe('app', () => {
193164
return request(app).get('/deletePledge/1').expect(200).then((res) => {
194165
expect(res.text).toEqual('Successfully deleted pledge #1');
195166
// 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+
]);
200172
return getList().then((res) => {
201173
// pledge is not in list any more
202174
expect(res.text).not.toMatch('_@performer_ pledged to content *by 2 January at 10:00*');
@@ -217,10 +189,11 @@ describe('app', () => {
217189
return request(app).get('/completePledge/1').expect(200).then((res) => {
218190
expect(res.text).toEqual('Successfully completed pledge #1');
219191
// 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+
]);
224197
return getList().then((res) => {
225198
// pledge is not in list any more
226199
expect(res.text).not.toMatch('_@performer_ pledged to content *by 2 January at 10:00*');

0 commit comments

Comments
 (0)