Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add tests to settings models/endpoints #21

Merged
merged 4 commits into from
Nov 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions tests/models/setting.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* eslint-env node, mocha */

require('../utils/mongoose');

const Setting = require('../../models/setting');
const expect = require('chai').expect;

describe('Setting: model', () => {

beforeEach(() => {
const defaults = {id: 1, moderation: 'pre'};
return Setting.update({id: '1'}, {$setOnInsert: defaults}, {upsert: true});
});

describe('#getSettings()', () => {
it('should have a moderation field defined', () => {
return Setting.getSettings().then(settings => {
expect(settings).to.have.property('moderation').and.to.equal('pre');
});
});
});

describe('#updateSettings()', () => {
it('should update the settings with a passed object', () => {
const mockSettings = {moderation: 'post'};
return Setting.updateSettings(mockSettings).then(updatedSettings => {
expect(updatedSettings).to.have.property('moderation').and.to.equal('post');
});
});
});

});
62 changes: 62 additions & 0 deletions tests/routes/api/settings/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
process.env.NODE_ENV = 'test';

require('../../../utils/mongoose');

const app = require('../../../../app');
const chai = require('chai');
const chaiHttp = require('chai-http');
chai.use(chaiHttp);
const expect = chai.expect;

const Setting = require('../../../../models/setting');
const defaults = {id: '1', moderation: 'pre'};

describe('GET /settings', () => {

beforeEach(() => {
return Setting.update({id: '1'}, {$setOnInsert: defaults}, {upsert: true});
});

it('should return a settings object', done => {
chai.request(app)
.get('/api/v1/settings')
.end((err, res) => {
expect(err).to.be.null;
expect(res).to.have.status(200);
expect(res).to.be.json;
expect(res.body).to.have.property('moderation');
expect(res.body.moderation).to.equal('pre');
done(err);
});
});
});

// update the settings.
describe('update settings', () => {

before(() => {
return Setting.update({id: '1'}, {$setOnInsert: defaults}, {upsert: true});
});

it('should respond to a PUT with new settings', () => {
chai.request(app)
.put('/api/v1/settings')
.send({moderation: 'post'}, (err, res) => {
expect(err).to.be.null;
expect(res).to.have.status(204);
Copy link

@jde jde Nov 7, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be safer to return 200 with the settings object in the body? This way a fe could update it's state and ensure that what the user sees exactly reflects what's in the db.

This is a larger style question for our apis. Approving PR.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's what I had before, but @wyattjoh pointed out that it's more inline with REST to just return something along the lines of "it worked" instead of returning stuff from a PUT.

done(err);
});
});

it('should have updates settings', () => {
chai.request(app)
.get('/api/v1/settings')
.end((err, res) => {
expect(err).to.be.null;
expect(res).to.have.status(200);
expect(res).to.be.json;
expect(res.body).to.have.property('moderation');
expect(res.body.moderation).to.equal('post');
});
});
});