Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.