Skip to content

Commit

Permalink
added final tests for complete coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
dmill-bz committed Apr 4, 2016
1 parent 3fd0176 commit fc7d31e
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -5,3 +5,4 @@ node_modules/*
api/*
coverage/*
.coveralls.yml
npm-debug*
7 changes: 7 additions & 0 deletions karma.conf.js
Expand Up @@ -7,6 +7,13 @@ module.exports = function (config) {
frameworks: ['mocha', 'chai', 'sinon'],
singleRun: true, //just run once by default

client: {
//mocha configuration
mocha: {
timeout: 3000 // adding 1s to default timeout of 2000ms
}
},

files: [
'tests.webpack.js', //just load this file
{pattern: 'test/index.html', watched: false, served:true} // load html file
Expand Down
8 changes: 4 additions & 4 deletions src/Console.js
Expand Up @@ -55,8 +55,8 @@ class Console extends EventEmitter {
constructor(windowElement, inputElement, options = {}) {
super();

this.windowElement = this._getElement(windowElement);
this.inputElement = this._getElement(inputElement);
this.windowElement = Console._getElement(windowElement);
this.inputElement = Console._getElement(inputElement);

//set window params
this.windowElement.css({overflowY: "auto"});
Expand Down Expand Up @@ -198,7 +198,7 @@ class Console extends EventEmitter {
* @param {String|DOM} element the elementwe want to get/set
* @return {DOM} the dom element (translated from string if necessary)
*/
_getElement(element) {
static _getElement(element) {
if(typeof element == "string") {
return $(element);
} else {
Expand Down Expand Up @@ -247,7 +247,7 @@ class Console extends EventEmitter {
}
} else {
console.log("Your initializing script produced an error : \n" + result.getError());
this.options.onError("Your initializing script produced an error : \n" + Html._htmlEncode(result.getError()));
this.emit('error', new Error( "Your initializing script produced an error : \n" + Html.htmlEncode(result.getError())));
}
});
}
Expand Down
78 changes: 76 additions & 2 deletions test/ConsoleTest.js
@@ -1,4 +1,5 @@
import Console from '../src/Console.js';
import Result from '../src/Result.js';
import $ from 'jquery';

beforeEach(() => {
Expand Down Expand Up @@ -73,6 +74,30 @@ describe('Console', () => {
gc.historyPointer.should.eql(2);
});

it('should create a console and throw error if history is eronous', (done) => {
const spy = sinon.spy();
const gc = new Console("#window", "#input", {history: [
{query:"g = doesnotexist()", results: ["meep"], error:null},
{query:"g.V()", results: ["moop"], error:null}
]});

gc.on('error', (err) => {console.log(err);done();}); //catch error
});

it('should create a console and populate window if history provided', (done) => {
const window = $("#window");
const response = '<div class="port-section"><div class="port-query">gremlin&gt; g = TinkerFactory.createModern().traversal()</div><div class="port-response json">meep<br></div></div><div class="port-section"><div class="port-query">gremlin&gt; g.V().has(\'name\', \'marko\')</div><div class="port-response json">{<span class="key">"id":</span> <span class="number">1</span>,<span class="key">"label":</span> <span class="string">"person"</span>,<span class="key">"type":</span> <span class="string">"vertex"</span>,<span class="key">"properties":</span> {<span class="key">"name":</span> [{<span class="key">"id":</span> <span class="number">0</span>,<span class="key">"value":</span> <span class="string">"marko"</span>}],<span class="key">"age":</span> [{<span class="key">"id":</span> <span class="number">1</span>,<span class="key">"value":</span> <span class="number">29</span>}]}}<br></div></div>';
const spy = sinon.spy();
const gc = new Console(window, "#input", {history: [
{query:"g = TinkerFactory.createModern().traversal()", results: ["meep"], error:null},
{query:"g.V().has('name', 'marko')", results: ["moop"], error:null}
]});
gc.on('results', (query, result) => {
window.html().replace(/\n */g, '').should.eql(response);
done();
});
});

it('should create from jquery DOM elements', () => {
const gc = new Console($("#window"), $("#input"));
});
Expand Down Expand Up @@ -129,8 +154,6 @@ describe('Console', () => {
e.which = 38; //up
input.trigger(e);
input.val().should.eql('g.V()');


});
});

Expand Down Expand Up @@ -174,4 +197,55 @@ describe('Console', () => {

});
});

describe('.handleResults()', () => {
it('should populate the window with results', () => {
const window = $("#window");
const gc = new Console(window, "#input");
gc.handleResults('5+5', new Result(null, [10]));
window.html().should.eql('<div class="port-section"><div class="port-query">gremlin&gt; 5+5</div><div class="port-response json"><span class="number">10</span><br></div></div>');
//check appending behavior by issuing another result
gc.handleResults('5+15', new Result(null, [20]));
window.html().should.eql('<div class="port-section"><div class="port-query">gremlin&gt; 5+5</div><div class="port-response json"><span class="number">10</span><br></div></div><div class="port-section"><div class="port-query">gremlin&gt; 5+15</div><div class="port-response json"><span class="number">20</span><br></div></div>');
});

it('should populate the window with error', () => {
const window = $("#window");
const gc = new Console(window, "#input");
gc.handleResults('somethingCrazy', new Result({message: "an error occured"}, null));
window.html().should.eql('<div class="port-section"><div class="port-query">gremlin&gt; somethingCrazy</div><div class="port-error">Could not complete query =&gt; an error occured</div></div>');
//check appending behavior by issuing another result
gc.handleResults('somethingCrazyAgain', new Result({message: "an other error occured"}, null));
window.html().should.eql('<div class="port-section"><div class="port-query">gremlin&gt; somethingCrazy</div><div class="port-error">Could not complete query =&gt; an error occured</div></div><div class="port-section"><div class="port-query">gremlin&gt; somethingCrazyAgain</div><div class="port-error">Could not complete query =&gt; an other error occured</div></div>');
});
});

describe('._getElement()', () => {
it('should return a DOM object when given a selector', () => {
const window = new Console._getElement("#window");
window[0].constructor.name.should.eql('HTMLDivElement');
window.attr('id').should.eql('window');
});

it('should return a DOM object when given a jquery object', () => {
const window = new Console._getElement($("#window"));
window[0].constructor.name.should.eql('HTMLDivElement');
window.attr('id').should.eql('window');
});
});

describe('.register()', () => {
it('should run the plugin\'s .load()', () => {
const spy = sinon.spy();
class StubPlugin {
load(a) {
a.constructor.name.should.eql('Console');
spy();
}
}
const gc = new Console("#window", "#input");
gc.register(new StubPlugin());
assert.isOk(spy.called, "spy wasn't called");
});
});
});
59 changes: 59 additions & 0 deletions test/ResultTest.js
@@ -0,0 +1,59 @@
import Result from '../src/Result';

describe('Result', () => {
describe('.constructor()', () => {
it('should populate properties correctly', () => {
const result = new Result({message:"error message"}, ["result"]);
expect(result._rawError).to.eql({message:"error message"});
expect(result._rawResults).to.eql(["result"]);
});
});

describe('.getRawError()', () => {
it('should return the proper data', () => {
const result = new Result({message:"error message"}, ["result"]);
expect(result.getRawError()).to.eql({message:"error message"});
});
});

describe('.getError()', () => {
it('should return the proper data', () => {
const result = new Result({message:"error message"}, ["result"]);
expect(result.getError()).to.eql({message:"error message"});
});
});

describe('.getHtmlError()', () => {
it('should return the error wrapped in a jquery element', () => {
const result = new Result({message:"error message"}, ["result"]);
const htmlError = result.getHtmlError();
htmlError[0].constructor.name.should.eql('HTMLDivElement');
htmlError.attr('class').should.eql('port-error');
htmlError.html().should.eql('Could not complete query =&gt; error message');
});
});

describe('.getRawResults()', () => {
it('should return the proper data', () => {
const result = new Result({message:"error message"}, ["result"]);
expect(result.getRawResults()).to.eql(["result"]);
});
});

describe('.getResults()', () => {
it('should return the proper data', () => {
const result = new Result({message:"error message"}, ["result"]);
expect(result.getResults()).to.eql(["result"]);
});
});

describe('.getHtmlResults()', () => {
it('should return the error wrapped in a jquery element', () => {
const result = new Result({message:"error message"}, [{"key" : "result>"}]);
const htmlResults = result.getHtmlResults();
htmlResults[0].constructor.name.should.eql('HTMLDivElement');
htmlResults.attr('class').should.eql('port-response json');
htmlResults.html().replace(/\n */g,'').should.eql('{<span class="key">"key":</span> <span class="string">"result&gt;"</span>}<br>');
});
});
});

0 comments on commit fc7d31e

Please sign in to comment.