Skip to content

Commit

Permalink
[#2458] Add unit tests for the plot module
Browse files Browse the repository at this point in the history
The suite has several stubbed tests for the less important methods.
  • Loading branch information
aron committed Jul 31, 2012
1 parent f991345 commit d12ff01
Show file tree
Hide file tree
Showing 3 changed files with 223 additions and 0 deletions.
30 changes: 30 additions & 0 deletions ckanext/stats/public/ckanext/stats/test/fixtures/table.html
@@ -0,0 +1,30 @@
<table data-module="plot">
<thead>
<tr>
<th>X Axis</th>
<th>Series A Legend</th>
<th>Series B Legend</th>
</tr>
</thead>
<tbody>
<tr>
<!-- This is the x value for each series -->
<th data-type="date" data-value="1176073200">Apr 09, 2007</th>
<!-- This is the y value for series a -->
<td>20</td>
<!-- This is the y value for series b -->
<td>7</td>
</tr>
<tr>
<th data-type="date" data-value="1176678000">Apr 16, 2007</th>
<td>12</td>
<td>6</td>
</tr>
<tr>
<th data-type="date" data-value="1177282800">Apr 23, 2007</th>
<td>27</td>
<td>12</td>
</tr>
</tbody>
</table>

57 changes: 57 additions & 0 deletions ckanext/stats/public/ckanext/stats/test/index.html
@@ -0,0 +1,57 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Mocha Tests</title>
<link rel="stylesheet" href="../../../base/test/vendor/mocha.css" />
</head>
<body>
<div id="mocha"></div>
<div id="fixture" style="position: absolute; top: -9999px; left: -9999px"></div>

<!-- Test Runner -->
<script src="../../../base/test/vendor/sinon.js"></script>
<script src="../../../base/test/vendor/mocha.js"></script>
<script src="../../../base/test/vendor/chai.js"></script>
<script>
mocha.setup('bdd');
var assert = chai.assert;

// Export sinon.assert methods onto assert.
sinon.assert.expose(assert, {prefix: ''});

var ckan = {ENV: 'testing'};
</script>

<!-- Source -->
<script src="../../../base/vendor/jed.js"></script>
<script src="../../../base/vendor/jquery.js"></script>
<script src="../../../base/vendor/bootstrap/js/bootstrap-transition.js"></script>
<script src="../../../base/vendor/bootstrap/js/bootstrap-alert.js"></script>
<script src="../../../base/javascript/plugins/jquery.inherit.js"></script>
<script src="../../../base/javascript/plugins/jquery.proxy-all.js"></script>
<script src="../../../base/javascript/sandbox.js"></script>
<script src="../../../base/javascript/module.js"></script>
<script src="../../../base/javascript/pubsub.js"></script>
<script src="../../../base/javascript/client.js"></script>
<script src="../../../base/javascript/notify.js"></script>
<script src="../../../base/javascript/i18n.js"></script>
<script src="../../../base/javascript/main.js"></script>
<script src="../javascript/modules/plot.js"></script>

<!-- Suite -->
<script src="./spec/modules/plot.spec.js"></script>

<script>
beforeEach(function () {
this.fixture = jQuery('#fixture').empty();
});

afterEach(function () {
this.fixture.empty();
});

mocha.run().globals(['ckan', 'setTimeout', 'setInterval', 'clearTimeout', 'clearInterval']);
</script>
</body>
</html>
136 changes: 136 additions & 0 deletions ckanext/stats/public/ckanext/stats/test/spec/modules/plot.spec.js
@@ -0,0 +1,136 @@
/*globals describe before beforeEach afterEach it assert sinon ckan jQuery */
describe('ckan.module.PlotModule()', function () {
var PlotModule = ckan.module.registry['plot'];

before(function (done) {
var _this = this;

jQuery.get('./fixtures/table.html', function (html) {
_this.template = html;
done();
});
});

beforeEach(function () {
this.el = jQuery(this.template).appendTo(this.fixture);
this.sandbox = ckan.sandbox();
this.sandbox.body = this.fixture;
this.module = new PlotModule(this.el, {}, this.sandbox);
});

afterEach(function () {
this.module.teardown();
});

describe('.initialize()', function () {
it('should setup the canvas element', function () {
var target = sinon.stub(this.module, 'setupCanvas', this.module.setupCanvas);

this.module.initialize();
assert.called(target);
});

it('should draw the graph', function () {
var target = sinon.stub(this.module, 'draw');

this.module.initialize();
assert.called(target);
});

it('should listen for "shown" events on the body', function () {
var target = sinon.stub(this.sandbox.body, 'on');

this.module.initialize();
assert.called(target);
assert.calledWith(target, "shown", this.module._onShown);
});
});

describe('.teardown()', function () {
it('should remove "shown" listeners from the body', function () {
var target = sinon.stub(this.sandbox.body, 'off');

this.module.teardown();
assert.called(target);
assert.calledWith(target, "shown", this.module._onShown);
});
});

describe('.setupCanvas()', function () {
it('should create the .canvas element', function () {
this.module.setupCanvas();

assert.isDefined(this.module.canvas);
assert.isDefined(this.module.canvas.is('div'));
});

it('should replace the .el with the .canvas', function () {
this.module.setupCanvas();
assert.ok(jQuery.contains(this.sandbox.body[0], this.module.canvas[0]));
});
});

describe('.draw()', function () {
beforeEach(function () {
this.plot = {};
this.module.canvas = jQuery('<div />').appendTo(this.fixture);
jQuery.plot = sinon.stub().returns(this.plot);
});

it('should call jQuery.plot() if the canvas is visible', function () {
this.module.draw();

assert.called(jQuery.plot);
assert.calledWith(jQuery.plot, this.module.canvas, this.module.data, this.module.options);
});

it('should assign the .graph property', function () {
this.module.draw();
assert.strictEqual(this.module.graph, this.plot);
});

it('should not call jQuery.plot() if the canvas is not visible', function () {
this.module.canvas.hide();
this.module.draw();

assert.notCalled(jQuery.plot);
});
});

describe('.parseTable(table)', function () {
it('should parse the contents of the provided table', function () {
var target = this.module.parseTable(this.module.el);

assert.deepEqual(target, [{
label: 'Series A Legend',
data: [
[new Date(1176073200000), "20"],
[new Date(1176678000000), "12"],
[new Date(1177282800000), "27"]
]
}, {
label: 'Series B Legend',
data: [
[new Date(1176073200000), "7"],
[new Date(1176678000000), "6"],
[new Date(1177282800000), "12"]
]
}]);
});
});

describe('.getValue(cell)', function () {
it('should extract the value from a table cell');
it('should use the data-value attribute if present');
it('should parse the value using the data-type');
});

describe('.parseValue(value, type)', function () {
it('should create a date object if type == "date"');
it('should return the value if the type is not recognised');
});

describe('._onShown(event)', function () {
it('should call .draw() if the event.target contains the canvas');
});
});

0 comments on commit d12ff01

Please sign in to comment.