Skip to content

Instantly share code, notes, and snippets.

@ClementWalter
Last active May 4, 2018 09:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ClementWalter/e1342529a2f06f59cb217fad16bfbbce to your computer and use it in GitHub Desktop.
Save ClementWalter/e1342529a2f06f59cb217fad16bfbbce to your computer and use it in GitHub Desktop.
Tests of the utils for the UserStamp mixin
'use strict';
const sinon = require('sinon');
const chai = require('chai');
const sinonChai = require('sinon-chai');
const expect = chai.expect;
chai.use(sinonChai);
const utils = require('../../../server/mixins/utils');
describe('mixins utils', () => {
let next;
let context;
before(() => {
next = sinon.stub();
});
describe('addUserInfoToOptions', () => {
before(() => {
context = { args: { options: {} } };
});
it('should throw an error when context has no info', (done) => {
context.req = {};
const unused = null;
const badRequest = function() {
utils.addUserInfoToOptions(context, unused, next);
};
expect(badRequest).to.throw();
done();
});
it('should throw an error when context has a current user but no info', (done) => {
context.req = { currentUser: {} };
const unused = null;
const badRequest = function() {
utils.addUserInfoToOptions(context, unused, next);
};
expect(badRequest).to.throw();
done();
});
it('should add id to user', (done) => {
context.req = { currentUser: { id: 'id' } };
const unused = null;
utils.addUserInfoToOptions(context, unused, next);
expect(context.args.options.creatorId).to.equal('id');
expect(next.calledOnce).to.be.true;
done();
});
});
describe('addUserInfoToInstance', () => {
let options;
before(() => {
options = {};
options.creatorId = 'creator_id';
});
beforeEach(() => {
next = sinon.stub();
});
it('should throw an error when options has no key', (done) => {
const badRequest = function() {
utils.addUserInfoToInstance(options)(context, next);
};
expect(badRequest).to.throw();
done();
});
it('should return context.instance info into instance', (done) => {
context = {
instance: {
[options.creatorId]: 'id',
},
options: {
creatorId: 'options_id',
},
};
utils.addUserInfoToInstance(options)(context, next);
expect(context.instance.creator_id).to.equal('id');
done();
});
it('should return context.options info into instance', (done) => {
context = {
instance: {},
options: {
creatorId: 'options_id',
},
};
utils.addUserInfoToInstance(options)(context, next);
expect(context.instance.creator_id).to.equal('options_id');
expect(next.calledOnce).to.be.true;
done();
});
it('should return context.data info into data', (done) => {
context = {
data: {
[options.creatorId]: 'id',
},
options: {
creatorId: 'options_id',
},
};
utils.addUserInfoToInstance(options)(context, next);
expect(context.data.creator_id).to.equal('id');
expect(next.calledOnce).to.be.true;
done();
});
it('should return context.options info into data', (done) => {
context = {
data: {},
options: {
creatorId: 'options_id',
},
};
utils.addUserInfoToInstance(options)(context, next);
expect(context.data.creator_id).to.equal('options_id');
expect(next.calledOnce).to.be.true;
done();
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment