-
-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathdiscover.routes.test.js
149 lines (137 loc) · 5.7 KB
/
discover.routes.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import app from '../server/index';
import { expect } from 'chai';
import request from 'supertest-as-promised';
import * as utils from '../test/utils';
import models from '../server/models';
const collectiveData1 = utils.data('collective1'); // `open source` tag
const collectiveData2 = utils.data('collective2'); // `meetup` tag
const paymentMethodData1 = utils.data('paymentMethod1');
const transactionsData = utils.data('transactions1').transactions;
const application = utils.data('application');
describe('discover', () => {
let user, collective1, collective2, paymentMethod1;
beforeEach(() => utils.resetTestDB());
beforeEach(() => models.PaymentMethod.create(paymentMethodData1).tap(pm => paymentMethod1 = pm));
beforeEach(() => models.User.createUserWithCollective(utils.data('user1')).tap(u => user = u));
beforeEach(() => models.Collective.create(collectiveData1).tap(g => collective1 = g));
beforeEach(() => models.Collective.create(collectiveData2).tap(g => collective2 = g));
beforeEach((done) => {
transactionsData[8].CreatedByUserId = user.id;
transactionsData[8].HostCollectiveId = user.CollectiveId;
transactionsData[8].FromCollectiveId = user.CollectiveId;
transactionsData[8].CollectiveId = collective1.id;
transactionsData[8].PaymentMethodId = paymentMethod1.id;
models.Transaction.createDoubleEntry(transactionsData[8]).tap(() => done()).catch(done);
});
beforeEach((done) => {
transactionsData[8].CollectiveId = collective2.id;
transactionsData[8].PaymentMethodId = paymentMethod1.id;
models.Transaction.createDoubleEntry(transactionsData[8]).tap(() => done()).catch(done);
});
/**
* Get.
*/
describe('#get /discover', () => {
it('/discover - No params defaults to: /discover?show=all&sort=most%20popular', (done) => {
request(app)
.get(`/discover?api_key=${application.api_key}`)
.expect(200)
.end((err, res) => {
const { body } = res;
const ids = body.collectives.map(c => c.id);
expect(body.show).equal('all');
expect(body.sort).equal('most popular');
expect(body.collectives).to.be.Array;
expect(body.collectives.length).equal(2);
expect(ids.indexOf(collective1.id)).not.equal(-1);
expect(ids.indexOf(collective2.id)).not.equal(-1);
expect(ids[0]).equal(collective1.id); // Newest (created second)
expect(ids[1]).equal(collective2.id);
done();
})
});
it('/discover?show=open source', (done) => {
request(app)
.get(`/discover?api_key=${application.api_key}&show=open%20source`)
.expect(200)
.end((err, res) => {
const { body } = res;
const ids = body.collectives.map(c => c.id);
expect(body.show).equal('open source');
expect(body.sort).equal('most popular');
expect(body.collectives).to.be.Array;
expect(body.collectives.length).equal(1);
expect(ids.indexOf(collective1.id)).not.equal(-1);
expect(ids.indexOf(collective2.id)).equal(-1);
expect(ids[0]).equal(collective1.id);
done();
})
});
it('/discover?show=meetup', (done) => {
request(app)
.get(`/discover?api_key=${application.api_key}&show=meetup`)
.expect(200)
.end((err, res) => {
const { body } = res;
const ids = body.collectives.map(c => c.id);
expect(body.show).equal('meetup');
expect(body.sort).equal('most popular');
expect(body.collectives).to.be.Array;
expect(body.collectives.length).equal(1);
expect(ids.indexOf(collective1.id)).equal(-1);
expect(ids.indexOf(collective2.id)).not.equal(-1);
expect(ids[0]).equal(collective2.id);
done();
})
});
it('/discover?show=undefined', (done) => {
request(app)
.get(`/discover?api_key=${application.api_key}&show=undefined`)
.expect(200)
.end((err, res) => {
const { body } = res;
expect(body.show).equal('undefined');
expect(body.sort).equal('most popular');
expect(body.collectives).to.be.Array;
expect(body.collectives.length).equal(0);
done();
})
});
it('/discover?show=all&sort=newest', (done) => {
request(app)
.get(`/discover?api_key=${application.api_key}&show=all&sort=newest`)
.expect(200)
.end((err, res) => {
const { body } = res;
const ids = body.collectives.map(c => c.id);
expect(body.show).equal('all');
expect(body.sort).equal('newest');
expect(body.collectives).to.be.Array;
expect(body.collectives.length).equal(2);
expect(ids.indexOf(collective1.id)).not.equal(-1);
expect(ids.indexOf(collective2.id)).not.equal(-1);
expect(ids[0]).equal(collective2.id); // Newest (created last)
expect(ids[1]).equal(collective1.id);
done();
})
});
it('/discover?show=all&sort=undefined', (done) => {
request(app)
.get(`/discover?api_key=${application.api_key}&show=all&sort=undefined`)
.expect(200)
.end((err, res) => {
const { body } = res;
const ids = body.collectives.map(c => c.id);
expect(body.show).equal('all');
expect(body.sort).equal('most popular');
expect(body.collectives).to.be.Array;
expect(body.collectives.length).equal(2);
expect(ids.indexOf(collective1.id)).not.equal(-1);
expect(ids.indexOf(collective2.id)).not.equal(-1);
expect(ids[0]).equal(collective1.id); // Most popular
expect(ids[1]).equal(collective2.id);
done();
})
});
});
});