Merge branch 'master' of git+ssh://maxious.lambdacomplex.org/git/ckanext-dga-stats
[ckanext-dga-stats.git] / ckanext / plot.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*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');
  });
});