Skip to content
This repository was archived by the owner on Dec 13, 2021. It is now read-only.

Commit 5b8eb31

Browse files
committed
Refactor /lists tests with shared checks
1 parent e5a42b6 commit 5b8eb31

File tree

5 files changed

+160
-48
lines changed

5 files changed

+160
-48
lines changed

test/Routes/Lists.js

+13-46
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,30 @@
1-
const { describe, it, expect, request, db, locale, checks, auth, dom } = require('../base');
1+
const { describe, it, expect, request, db, locale, checks, auth, fetchPage } = require('../base');
22

33
describe('/lists', () => {
44
describe('GET', () => {
5-
let res, page;
6-
before('fetch the page', done => {
7-
request().get('/lists').end((_, r) => {
8-
res = r;
9-
page = dom(r);
10-
done();
11-
});
12-
});
13-
it('returns an OK status code', done => {
14-
expect(res).to.have.status(200);
15-
done();
16-
});
17-
it('has the correct page title', done => {
18-
checks.title(res, `All Bot Lists - ${locale('site_name')} - ${locale('short_desc')}`);
5+
const test = () => request().get('/lists');
6+
fetchPage(test);
7+
8+
it('returns an OK status code', function(done) {
9+
expect(this.res).to.have.status(200);
1910
done();
2011
});
2112

13+
checks.meta(`All Bot Lists - ${locale('site_name')} - ${locale('short_desc')}`);
14+
2215
describe('renders the expected content', () => {
23-
it('has the correct title', done => {
24-
expect(res.text).to.include('All Bot Lists');
16+
it('has the correct title', function(done) {
17+
expect(this.res.text).to.include('All Bot Lists');
2518
done();
2619
});
27-
it('has the stats footer', done => {
28-
const footer = page.querySelector(".hero.card .hero-body.hero-stats.card-body");
20+
it('has the stats footer', function(done) {
21+
const footer = this.page.querySelector(".hero.card .hero-body.hero-stats.card-body");
2922
expect(footer).to.exist;
3023
expect(footer.innerHTML).to.include(`${locale('site_name')} - Bot List Stats`);
3124
done();
3225
});
3326

34-
describe('contains the list cards', () => {
35-
let listCards;
36-
before('fetch list cards', done => {
37-
db.select('id', 'name', 'url').from('lists').where({ display: true, defunct: false }).then(lists => {
38-
listCards = lists;
39-
done();
40-
});
41-
});
42-
it('has the list names', done => {
43-
listCards.forEach(list => {
44-
expect(res.text).to.include(list.name);
45-
});
46-
done();
47-
});
48-
it('has the list urls', done => {
49-
listCards.forEach(list => {
50-
expect(res.text).to.include(list.url);
51-
});
52-
done();
53-
});
54-
it('has the list information button', done => {
55-
listCards.forEach(list => {
56-
expect(page.querySelector(`.card a.button[href="/lists/${list.id}"]`)).to.exist;
57-
});
58-
done();
59-
});
60-
});
27+
checks.listCards(test, db, { display: true, defunct: false });
6128
});
6229
});
6330
});

test/base.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ const auth = require('./helpers/auth');
66
const request = require('./helpers/request');
77
const { ratelimitBypass, resetRatelimits } = require('./helpers/ratelimits');
88
const dom = require('./helpers/dom');
9-
const { describe, it } = require('mocha');
9+
const fetchPage = require('./helpers/fetchPage');
10+
const { before, describe, it } = require('mocha');
1011
const chai = require('chai');
1112
const { expect } = require('chai');
1213
const chaiHttp = require('chai-http');
@@ -35,6 +36,7 @@ const compareObjects = (template, actual) => {
3536
};
3637

3738
module.exports = {
39+
before,
3840
describe,
3941
it,
4042
expect,
@@ -47,5 +49,6 @@ module.exports = {
4749
checks,
4850
auth,
4951
dom,
52+
fetchPage,
5053
compareObjects
5154
};

test/helpers/checks.js

+130-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
const { describe, it } = require('mocha');
12
const { expect } = require('chai');
23
const locale = require('../../src/Util/i18n').__;
34
const { resetRatelimits } = require('./ratelimits');
5+
const fetchPage = require('./fetchPage');
6+
const auth = require('./auth');
47

58
const ratelimit = (context, limit, test, done, status = 200) => {
69
context.retries(0);
@@ -44,4 +47,130 @@ const title = (res, expectedTitle) => {
4447
expect(res.text).to.include(`<meta name="twitter:description" content="${locale('full_desc')}">`);
4548
};
4649

47-
module.exports = { ratelimit, authRequired, title };
50+
const meta = expectedTitle => {
51+
describe('has the correct page metadata', () => {
52+
it('is a valid HTML page', function (done) {
53+
expect(this.res).to.be.html;
54+
done();
55+
});
56+
it('has the correct title', function (done) {
57+
const title = this.page.querySelector('title');
58+
expect(title).to.exist;
59+
expect(title.textContent).to.eq(expectedTitle);
60+
done();
61+
});
62+
it('has the correct description', function (done) {
63+
const description = this.page.querySelector('meta[name="description"]');
64+
expect(description).to.exist;
65+
expect(description.hasAttribute('content')).to.be.true;
66+
expect(description.getAttribute('content')).to.eq(`${locale('site_name')} - ${locale('full_desc')}`);
67+
done();
68+
});
69+
describe('OpenGraph', () => {
70+
it('has the correct title', function (done) {
71+
const title = this.page.querySelector('meta[property="og:title"]');
72+
expect(title).to.exist;
73+
expect(title.hasAttribute('content')).to.be.true;
74+
expect(title.getAttribute('content')).to.eq(expectedTitle);
75+
done();
76+
});
77+
it('has the correct site name', function (done) {
78+
const title = this.page.querySelector('meta[property="og:site_name"]');
79+
expect(title).to.exist;
80+
expect(title.hasAttribute('content')).to.be.true;
81+
expect(title.getAttribute('content')).to.eq(locale('site_name'));
82+
done();
83+
});
84+
it('has the correct description', function (done) {
85+
const description = this.page.querySelector('meta[property="og:description"]');
86+
expect(description).to.exist;
87+
expect(description.hasAttribute('content')).to.be.true;
88+
expect(description.getAttribute('content')).to.eq(locale('full_desc'));
89+
done();
90+
});
91+
});
92+
describe('Twitter', () => {
93+
it('has the correct title', function (done) {
94+
const title = this.page.querySelector('meta[name="twitter:title"]');
95+
expect(title).to.exist;
96+
expect(title.hasAttribute('content')).to.be.true;
97+
expect(title.getAttribute('content')).to.eq(expectedTitle);
98+
done();
99+
});
100+
it('has the correct description', function (done) {
101+
const description = this.page.querySelector('meta[name="twitter:description"]');
102+
expect(description).to.exist;
103+
expect(description.hasAttribute('content')).to.be.true;
104+
expect(description.getAttribute('content')).to.eq(locale('full_desc'));
105+
done();
106+
});
107+
});
108+
});
109+
};
110+
111+
const listCards = (test, db, where) => {
112+
describe('contains the list cards', () => {
113+
fetchPage(test);
114+
115+
before('fetch list cards', function(done) {
116+
db.select('id', 'name', 'url').from('lists').where(where).then(lists => {
117+
this.listCards = lists;
118+
done();
119+
});
120+
});
121+
122+
it('has the list names', function(done) {
123+
this.listCards.forEach(list => {
124+
expect(this.res.text).to.include(list.name);
125+
});
126+
done();
127+
});
128+
it('has the list urls', function(done) {
129+
this.listCards.forEach(list => {
130+
expect(this.res.text).to.include(list.url);
131+
});
132+
done();
133+
});
134+
135+
describe('card buttons', () => {
136+
it('has the list information button', function(done) {
137+
this.listCards.forEach(list => {
138+
expect(this.page.querySelector(`.card a.button[href="/lists/${list.id}"]`)).to.exist;
139+
});
140+
done();
141+
});
142+
143+
describe('as an anonymous user', () => {
144+
fetchPage(() => auth.asAnon(test()));
145+
it('does not have the edit button', function(done) {
146+
this.listCards.forEach(list => {
147+
expect(this.page.querySelector(`.card a.button[href="/lists/${list.id}/edit"]`)).to.not.exist;
148+
});
149+
done();
150+
});
151+
});
152+
153+
describe('as a logged in user', () => {
154+
fetchPage(() => auth.asUser(test()));
155+
it('does not have the edit button', function(done) {
156+
this.listCards.forEach(list => {
157+
expect(this.page.querySelector(`.card a.button[href="/lists/${list.id}/edit"]`)).to.not.exist;
158+
});
159+
done();
160+
});
161+
});
162+
163+
describe('as a moderator', () => {
164+
fetchPage(() => auth.asMod(test()));
165+
it('has the edit button', function(done) {
166+
this.listCards.forEach(list => {
167+
expect(this.page.querySelector(`.card a.button[href="/lists/${list.id}/edit"]`)).to.exist;
168+
});
169+
done();
170+
});
171+
});
172+
});
173+
});
174+
};
175+
176+
module.exports = { ratelimit, authRequired, title, meta, listCards };

test/helpers/fetchpage.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const { before } = require('mocha');
2+
const dom = require('./dom');
3+
4+
module.exports = test => {
5+
before('fetch the page', function(done) {
6+
test().end((_, r) => {
7+
this.res = r;
8+
this.page = dom(r);
9+
done();
10+
});
11+
});
12+
};

test/mocha.opts

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# mocha.opts
22
--recursive
33
--exclude base.js
4+
--exclude helpers
45
--slow 600
56
--timeout 12000
67
--retries 1

0 commit comments

Comments
 (0)