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

Commit

Permalink
Refactor /lists tests with shared checks
Browse files Browse the repository at this point in the history
  • Loading branch information
MattIPv4 committed Jan 18, 2020
1 parent e5a42b6 commit 5b8eb31
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 48 deletions.
59 changes: 13 additions & 46 deletions test/Routes/Lists.js
Original file line number Diff line number Diff line change
@@ -1,63 +1,30 @@
const { describe, it, expect, request, db, locale, checks, auth, dom } = require('../base');
const { describe, it, expect, request, db, locale, checks, auth, fetchPage } = require('../base');

describe('/lists', () => {
describe('GET', () => {
let res, page;
before('fetch the page', done => {
request().get('/lists').end((_, r) => {
res = r;
page = dom(r);
done();
});
});
it('returns an OK status code', done => {
expect(res).to.have.status(200);
done();
});
it('has the correct page title', done => {
checks.title(res, `All Bot Lists - ${locale('site_name')} - ${locale('short_desc')}`);
const test = () => request().get('/lists');
fetchPage(test);

it('returns an OK status code', function(done) {
expect(this.res).to.have.status(200);
done();
});

checks.meta(`All Bot Lists - ${locale('site_name')} - ${locale('short_desc')}`);

describe('renders the expected content', () => {
it('has the correct title', done => {
expect(res.text).to.include('All Bot Lists');
it('has the correct title', function(done) {
expect(this.res.text).to.include('All Bot Lists');
done();
});
it('has the stats footer', done => {
const footer = page.querySelector(".hero.card .hero-body.hero-stats.card-body");
it('has the stats footer', function(done) {
const footer = this.page.querySelector(".hero.card .hero-body.hero-stats.card-body");
expect(footer).to.exist;
expect(footer.innerHTML).to.include(`${locale('site_name')} - Bot List Stats`);
done();
});

describe('contains the list cards', () => {
let listCards;
before('fetch list cards', done => {
db.select('id', 'name', 'url').from('lists').where({ display: true, defunct: false }).then(lists => {
listCards = lists;
done();
});
});
it('has the list names', done => {
listCards.forEach(list => {
expect(res.text).to.include(list.name);
});
done();
});
it('has the list urls', done => {
listCards.forEach(list => {
expect(res.text).to.include(list.url);
});
done();
});
it('has the list information button', done => {
listCards.forEach(list => {
expect(page.querySelector(`.card a.button[href="/lists/${list.id}"]`)).to.exist;
});
done();
});
});
checks.listCards(test, db, { display: true, defunct: false });
});
});
});
Expand Down
5 changes: 4 additions & 1 deletion test/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ const auth = require('./helpers/auth');
const request = require('./helpers/request');
const { ratelimitBypass, resetRatelimits } = require('./helpers/ratelimits');
const dom = require('./helpers/dom');
const { describe, it } = require('mocha');
const fetchPage = require('./helpers/fetchPage');
const { before, describe, it } = require('mocha');
const chai = require('chai');
const { expect } = require('chai');
const chaiHttp = require('chai-http');
Expand Down Expand Up @@ -35,6 +36,7 @@ const compareObjects = (template, actual) => {
};

module.exports = {
before,
describe,
it,
expect,
Expand All @@ -47,5 +49,6 @@ module.exports = {
checks,
auth,
dom,
fetchPage,
compareObjects
};
131 changes: 130 additions & 1 deletion test/helpers/checks.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
const { describe, it } = require('mocha');
const { expect } = require('chai');
const locale = require('../../src/Util/i18n').__;
const { resetRatelimits } = require('./ratelimits');
const fetchPage = require('./fetchPage');
const auth = require('./auth');

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

module.exports = { ratelimit, authRequired, title };
const meta = expectedTitle => {
describe('has the correct page metadata', () => {
it('is a valid HTML page', function (done) {
expect(this.res).to.be.html;
done();
});
it('has the correct title', function (done) {
const title = this.page.querySelector('title');
expect(title).to.exist;
expect(title.textContent).to.eq(expectedTitle);
done();
});
it('has the correct description', function (done) {
const description = this.page.querySelector('meta[name="description"]');
expect(description).to.exist;
expect(description.hasAttribute('content')).to.be.true;
expect(description.getAttribute('content')).to.eq(`${locale('site_name')} - ${locale('full_desc')}`);
done();
});
describe('OpenGraph', () => {
it('has the correct title', function (done) {
const title = this.page.querySelector('meta[property="og:title"]');
expect(title).to.exist;
expect(title.hasAttribute('content')).to.be.true;
expect(title.getAttribute('content')).to.eq(expectedTitle);
done();
});
it('has the correct site name', function (done) {
const title = this.page.querySelector('meta[property="og:site_name"]');
expect(title).to.exist;
expect(title.hasAttribute('content')).to.be.true;
expect(title.getAttribute('content')).to.eq(locale('site_name'));
done();
});
it('has the correct description', function (done) {
const description = this.page.querySelector('meta[property="og:description"]');
expect(description).to.exist;
expect(description.hasAttribute('content')).to.be.true;
expect(description.getAttribute('content')).to.eq(locale('full_desc'));
done();
});
});
describe('Twitter', () => {
it('has the correct title', function (done) {
const title = this.page.querySelector('meta[name="twitter:title"]');
expect(title).to.exist;
expect(title.hasAttribute('content')).to.be.true;
expect(title.getAttribute('content')).to.eq(expectedTitle);
done();
});
it('has the correct description', function (done) {
const description = this.page.querySelector('meta[name="twitter:description"]');
expect(description).to.exist;
expect(description.hasAttribute('content')).to.be.true;
expect(description.getAttribute('content')).to.eq(locale('full_desc'));
done();
});
});
});
};

const listCards = (test, db, where) => {
describe('contains the list cards', () => {
fetchPage(test);

before('fetch list cards', function(done) {
db.select('id', 'name', 'url').from('lists').where(where).then(lists => {
this.listCards = lists;
done();
});
});

it('has the list names', function(done) {
this.listCards.forEach(list => {
expect(this.res.text).to.include(list.name);
});
done();
});
it('has the list urls', function(done) {
this.listCards.forEach(list => {
expect(this.res.text).to.include(list.url);
});
done();
});

describe('card buttons', () => {
it('has the list information button', function(done) {
this.listCards.forEach(list => {
expect(this.page.querySelector(`.card a.button[href="/lists/${list.id}"]`)).to.exist;
});
done();
});

describe('as an anonymous user', () => {
fetchPage(() => auth.asAnon(test()));
it('does not have the edit button', function(done) {
this.listCards.forEach(list => {
expect(this.page.querySelector(`.card a.button[href="/lists/${list.id}/edit"]`)).to.not.exist;
});
done();
});
});

describe('as a logged in user', () => {
fetchPage(() => auth.asUser(test()));
it('does not have the edit button', function(done) {
this.listCards.forEach(list => {
expect(this.page.querySelector(`.card a.button[href="/lists/${list.id}/edit"]`)).to.not.exist;
});
done();
});
});

describe('as a moderator', () => {
fetchPage(() => auth.asMod(test()));
it('has the edit button', function(done) {
this.listCards.forEach(list => {
expect(this.page.querySelector(`.card a.button[href="/lists/${list.id}/edit"]`)).to.exist;
});
done();
});
});
});
});
};

module.exports = { ratelimit, authRequired, title, meta, listCards };
12 changes: 12 additions & 0 deletions test/helpers/fetchpage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const { before } = require('mocha');
const dom = require('./dom');

module.exports = test => {
before('fetch the page', function(done) {
test().end((_, r) => {
this.res = r;
this.page = dom(r);
done();
});
});
};
1 change: 1 addition & 0 deletions test/mocha.opts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# mocha.opts
--recursive
--exclude base.js
--exclude helpers
--slow 600
--timeout 12000
--retries 1
Expand Down

0 comments on commit 5b8eb31

Please sign in to comment.