Skip to content

Commit

Permalink
add a globalBeforeConfig to match globalTargetConfig standard
Browse files Browse the repository at this point in the history
  • Loading branch information
julio-saito-linx committed Sep 10, 2014
1 parent 56185ef commit 3e3ad05
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 69 deletions.
1 change: 1 addition & 0 deletions src/default-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module.exports = {
showArguments : false,
ignoreRegexPattern : null,
globalInterceptors : null,
globalBeforeConfig : null,
globalTargetConfig : null,
waitForPause : 100,
pauseCallBack : null
Expand Down
55 changes: 39 additions & 16 deletions src/reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ Reporter.prototype.onCall = function(info) {
Reporter.prototype.renderLogs = function(info) {
var mainMessage = info.method;

this._addTitle();
this._addBefore();

mainMessage = this._applyInterceptors(info);

Expand All @@ -79,13 +79,29 @@ Reporter.prototype.renderLogs = function(info) {
this._renderToConsole(info, mainMessage);
};

Reporter.prototype._addTitle = function() {
Reporter.prototype._addBefore = function() {
var selectedBeforeConfig, beforeLog;

if(!this.before){
return;
}

selectedBeforeConfig = this._getOrMergeLogConfiguration(
this.globalBeforeConfig,
this.localBeforeConfig
);

/*
title (first column / namespace)
target (function)
*/
if(this.before){
this._logs.push(this.before);
if(selectedBeforeConfig){
beforeLog = selectedBeforeConfig;
beforeLog.message = this.before.message;
}
else{
beforeLog = this.before;
}
this._logs.push(beforeLog);
};

Reporter.prototype._applyInterceptors = function(info) {
Expand All @@ -102,18 +118,11 @@ Reporter.prototype._applyInterceptors = function(info) {

Reporter.prototype._addMainLog = function(mainMessage) {
var selectedTargetConfig, targetLog;
/*
selectedTargetConfig local or global
*/
var hasLocal = this.localTargetConfig !== null;
var hasGlobal = this.globalTargetConfig !== null;

selectedTargetConfig = this.localTargetConfig || this.globalTargetConfig;

// merge both
if(hasLocal && hasGlobal){
selectedTargetConfig = helpers.merge(this.globalTargetConfig, this.localTargetConfig);
}
selectedTargetConfig = this._getOrMergeLogConfiguration(
this.globalTargetConfig,
this.localTargetConfig
);

/*
target (function)
Expand All @@ -130,6 +139,20 @@ Reporter.prototype._addMainLog = function(mainMessage) {
this._logs.push(targetLog);
};

Reporter.prototype._getOrMergeLogConfiguration = function(globalConfig, localConfig) {

var hasGlobal = globalConfig !== null;
var hasLocal = localConfig !== null;

var selectedConfig = localConfig || globalConfig;

// merge both
if(hasLocal && hasGlobal){
selectedConfig = helpers.merge(globalConfig, localConfig);
}

return selectedConfig;
};

Reporter.prototype._renderToConsole = function(info, mainMessage) {
/*
Expand Down
135 changes: 82 additions & 53 deletions test/reporter.renderLogs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ describe('Reporter RenderLogs:', function(){
afterEach(function(){
});

describe('_addTitle()', function () {
describe('_addBefore()', function () {
it('do not add nothing if no before config is passed', function() {
reporter = new Reporter({
globalConfig: {}
});

reporter._addTitle();
reporter._addBefore();

assert.deepEqual(0, reporter._logs.length);
});
Expand All @@ -66,10 +66,34 @@ describe('Reporter RenderLogs:', function(){
}
});

reporter._addTitle();
reporter._addBefore();

assert.deepEqual(beforeConfig, reporter._logs[0]);
});

describe('before configs', function () {

it('global target configuration only', function () {

var SOME_BEFORE_MESSAGE = 'SOME BEFORE MESSAGE';
var BEFORE_CONFIGURATION = {
css: 'color: red',
size: 15
};

reporter.globalBeforeConfig = BEFORE_CONFIGURATION;
reporter.before = {};
reporter.before.message = 'SOME BEFORE MESSAGE';

reporter._addBefore();

assert.equal(1, reporter._logs.length);
assert.equal(SOME_BEFORE_MESSAGE, reporter._logs[0].message);
assert.equal('color: red', reporter._logs[0].css);
assert.equal(15, reporter._logs[0].size);
});
});

});


Expand Down Expand Up @@ -108,74 +132,79 @@ describe('Reporter RenderLogs:', function(){
});

describe('_addMainLog()', function () {
it('if does not have a target configuration creates a simple message log', function () {

var LOG_MESSAGE = 'LOG_MESSAGE';
describe('target configs', function () {

reporter._addMainLog(LOG_MESSAGE);
it('if does not have a target configuration creates a simple message log', function () {

assert.equal(1, reporter._logs.length);
assert.equal(LOG_MESSAGE, reporter._logs[0].message);
});
var LOG_MESSAGE = 'LOG_MESSAGE';

it('global target configuration only', function () {
reporter._addMainLog(LOG_MESSAGE);

var LOG_MESSAGE = 'LOG_MESSAGE';
var TARGET_CONFIGURATION = {
css: 'color: red',
size: 15
};
assert.equal(1, reporter._logs.length);
assert.equal(LOG_MESSAGE, reporter._logs[0].message);
});

reporter.globalTargetConfig = TARGET_CONFIGURATION;
it('global target configuration only', function () {

reporter._addMainLog(LOG_MESSAGE);
var LOG_MESSAGE = 'LOG_MESSAGE';
var TARGET_CONFIGURATION = {
css: 'color: red',
size: 15
};

assert.equal(1, reporter._logs.length);
assert.equal(LOG_MESSAGE, reporter._logs[0].message);
assert.equal('color: red', reporter._logs[0].css);
assert.equal(15, reporter._logs[0].size);
});
reporter.globalTargetConfig = TARGET_CONFIGURATION;

it('local target configuration only', function () {
reporter._addMainLog(LOG_MESSAGE);

var LOG_MESSAGE = 'LOG_MESSAGE';
var TARGET_CONFIGURATION = {
css: 'color: red',
size: 15
};
assert.equal(1, reporter._logs.length);
assert.equal(LOG_MESSAGE, reporter._logs[0].message);
assert.equal('color: red', reporter._logs[0].css);
assert.equal(15, reporter._logs[0].size);
});

reporter.localTargetConfig = TARGET_CONFIGURATION;
it('local target configuration only', function () {

reporter._addMainLog(LOG_MESSAGE);
var LOG_MESSAGE = 'LOG_MESSAGE';
var TARGET_CONFIGURATION = {
css: 'color: red',
size: 15
};

assert.equal(1, reporter._logs.length);
assert.equal(LOG_MESSAGE, reporter._logs[0].message);
assert.equal('color: red', reporter._logs[0].css);
assert.equal(15, reporter._logs[0].size);
});
reporter.localTargetConfig = TARGET_CONFIGURATION;

it('global and local target configuration, local must win', function () {
reporter._addMainLog(LOG_MESSAGE);

var LOG_MESSAGE = 'LOG_MESSAGE';
assert.equal(1, reporter._logs.length);
assert.equal(LOG_MESSAGE, reporter._logs[0].message);
assert.equal('color: red', reporter._logs[0].css);
assert.equal(15, reporter._logs[0].size);
});

reporter.globalTargetConfig = {
css: 'color: blue',
size: 10
};
it('global and local target configuration, local must win', function () {

reporter.localTargetConfig = {
css: 'color: red',
size: 15
};
var LOG_MESSAGE = 'LOG_MESSAGE';

reporter.globalTargetConfig = {
css: 'color: blue',
size: 10
};

reporter._addMainLog(LOG_MESSAGE);
reporter.localTargetConfig = {
css: 'color: red',
size: 15
};

assert.equal(1, reporter._logs.length);
assert.equal(LOG_MESSAGE, reporter._logs[0].message);
reporter._addMainLog(LOG_MESSAGE);

assert.equal(1, reporter._logs.length);
assert.equal(LOG_MESSAGE, reporter._logs[0].message);

// locals wins
assert.equal('color: red', reporter._logs[0].css);
assert.equal(15, reporter._logs[0].size);
});

// locals wins
assert.equal('color: red', reporter._logs[0].css);
assert.equal(15, reporter._logs[0].size);
});
});

Expand Down Expand Up @@ -220,14 +249,14 @@ describe('Reporter RenderLogs:', function(){
describe('renderLogs()', function () {
it('should be calling all "internal" methods', function () {

reporter._addTitle = sinon.spy();
reporter._addBefore = sinon.spy();
reporter._applyInterceptors = sinon.spy();
reporter._addMainLog = sinon.spy();
reporter._renderToConsole = sinon.spy();

reporter.renderLogs(fakeInfo);

assert.equal(true, reporter._addTitle.called);
assert.equal(true, reporter._addBefore.called);
assert.equal(true, reporter._applyInterceptors.called);
assert.equal(true, reporter._addMainLog.called);
assert.equal(true, reporter._renderToConsole.called);
Expand Down

0 comments on commit 3e3ad05

Please sign in to comment.