Skip to content

Instantly share code, notes, and snippets.

@r01010010
Created February 7, 2016 23:35
Show Gist options
  • Save r01010010/f34fd567eaad50f061c3 to your computer and use it in GitHub Desktop.
Save r01010010/f34fd567eaad50f061c3 to your computer and use it in GitHub Desktop.
/**
* This module defines the tests for the faqEditor controller.
*/
// Test dependencies
var mockery = require('mockery');
var buster = require('buster');
var assert = buster.referee.assert;
var expect = buster.referee.expect;
var sinon = require('sinon');
var tree = require('zappy').tree;
buster.spec.expose();
var CommonEditor = function(config){
if(!config.name) throw new Error('config.name not provided');
// Config globals
this.name = config.name;
this.brand = config.brand || 'testBrand';
this.req = config.req || {};
this.res = config.res || {};
this.data = config.data || {};
this.dataBridge = this.data[this.name];
this.noop = config.noop || function () {};
this.middleware = config.middleware || undefined;
this.testTags = config.testTags || undefined;
this.editor = undefined;
this.searchFields = config.searchFields || [];
this.sortFields = config.sortFields || [];
// Test Globals
this.sorter = {
sorter: undefined,
sorterArg: undefined,
Sorter: undefined
};
this.filter = {
filter: undefined,
filterArg: undefined,
Filter: undefined
};
this.gridHandler = {
gridHandlerArg: undefined,
gridHandlerFunc: {},
GridHandler: undefined,
};
};
CommonEditor.prototype.runTests = function() {
var _self = this;
var noop = _self.noop,
brand = _self.brand,
req = _self.req,
res = _self.res,
editor = _self.editor,
data = _self.data,
sorter = _self.sorter.sorter,
sorterArg = _self.sorter.sorterArg,
filterArg = _self.filter.filterArg,
filter = _self.filter.filter,
gridHandlerArg = _self.gridHandler.gridHandlerArg,
gridHandlerFunc = _self.gridHandler.gridHandlerFunc,
GridHandler = _self.gridHandler.GridHandler
middleware = _self.middleware,
sandboxSinon = undefined;
describe('Running tests for ' + _self.name + 'Editor', function(){
beforeEach(function(){
mockery.enable({
useCleanCache: true,
warnOnUnregistered: false
});
sandboxSinon = sinon.sandbox.create();
var Sorter = function (fields) {
_self.sorter.sorter = this;
_self.sorter.sorterArg = fields;
};
//var Sorter = _self.sorter.Sorter;
var Filter = function (config) {
_self.filter.filter = this;
_self.filter.filterArg = config;
};
//var Filter = _self.filter.Filter;
var GridHandler = function (config) {
_self.gridHandler.gridHandlerArg = config;
this.on = function (name, func) {
_self.gridHandler.gridHandlerFunc[name] = func;
};
this.middleware = function () {
return _self.gridHandler.middleware;
};
};
_self.dataBridge.delete = sandboxSinon.stub(_self.dataBridge, "delete");
mockery.registerMock('./sorter', Sorter);
mockery.registerMock('./filter', Filter);
mockery.registerMock('./gridHandler', GridHandler);
mockery.registerMock('../../../data', _self.data);
_self.editor = require(dirs.admin.ctrls + 'grids/' + _self.name + 'Editor.js');
_self.editor(_self.req, _self.res, _self.noop);
});
afterEach(function(){
mockery.deregisterAll();
mockery.disable();
sandboxSinon.restore();
});
describe('Request handler', function () {
it('should return the result of middleware on the grid handler', function () {
expect(_self.editor(req, res, noop)).toBe(middleware);
});
it('should create a sorter, passing the sortable fields', function () {
var sorterArg = _self.sorter.sorterArg;
var sortFields = _self.sortFields;
var sortTermsLength = _self.sortFields.length;
expect(sorterArg).toBeArray();
expect(sorterArg.length).toBe(1);
for(var i = 0; i < sortTermsLength; i++){
assert(sorterArg.indexOf(sortFields[i]) > -1);
}
});
it('should create a filter, passing the site var, the searchable fields and the brand of the request', function () {
var filterArg = _self.filter.filterArg;
var searchFields = _self.searchFields;
var searchTermsLength = _self.searchFields.length;
expect(filterArg).toBeObject();
expect(filterArg.siteVar).toBe('primarySite');
expect(filterArg.site).toBe(brand);
expect(filterArg.searchFields).toBeArray();
expect(filterArg.searchFields.length).toBe(searchTermsLength);
for(var i = 0; i < searchTermsLength; i++){
assert(filterArg.searchFields.indexOf(searchFields[i]) > -1);
}
});
});
describe('Grid configuration', function () {
var gridHandlerArg = _self.gridHandler.gridHandlerArg;
it('=>should be an object', function () {
expect(gridHandlerArg).toBeObject();
});
it('should contain a pre-render function', function () {
expect(gridHandlerArg.preRenderData).toBeFunction();
});
it('should contain a "table" property that points to ' + _self.name + 'Data', function () {
expect(gridHandlerArg.table).toBe(data);
});
it('should contain a "filter" property that points to filter', function () {
expect(gridHandlerArg.filter).toBe(filter);
});
it('should contain a "sorter" property that points to sorter', function () {
expect(gridHandlerArg.sorter).toBe(sorter);
});
});
describe('remove', function () {
it('should call ' + _self.name + 'delete supplying the id from the body data', function(){
var req = tree('body.data._id', 9999);
_self.gridHandler.gridHandlerFunc.remove(req, _self.res, _self.noop);
expect(_self.dataBridge.delete.calledOnce).toBe(true);
// expect(dataMock.faq.delete.calledWith(req.body.data._id)).toBe(true);
});
});
});
}
module.exports = CommonEditor;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment