summaryrefslogtreecommitdiff
path: root/spec/javascripts/graphs/stat_graph_contributors_graph_spec.js
blob: 563d134ca816d60b6b3e0bd9e16183e56b6a0810 (plain)
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/* eslint-disable jasmine/no-suite-dupes, vars-on-top, no-var */

import { scaleLinear, scaleTime } from 'd3-scale';
import { timeParse } from 'd3-time-format';
import {
  ContributorsGraph,
  ContributorsMasterGraph,
} from '~/pages/projects/graphs/show/stat_graph_contributors_graph';

const d3 = { scaleLinear, scaleTime, timeParse };

describe('ContributorsGraph', function() {
  describe('#set_x_domain', function() {
    it('set the x_domain', function() {
      ContributorsGraph.set_x_domain(20);

      expect(ContributorsGraph.prototype.x_domain).toEqual(20);
    });
  });

  describe('#set_y_domain', function() {
    it('sets the y_domain', function() {
      ContributorsGraph.set_y_domain([{ commits: 30 }]);

      expect(ContributorsGraph.prototype.y_domain).toEqual([0, 30]);
    });
  });

  describe('#init_x_domain', function() {
    it('sets the initial x_domain', function() {
      ContributorsGraph.init_x_domain([{ date: '2013-01-31' }, { date: '2012-01-31' }]);

      expect(ContributorsGraph.prototype.x_domain).toEqual(['2012-01-31', '2013-01-31']);
    });
  });

  describe('#init_y_domain', function() {
    it('sets the initial y_domain', function() {
      ContributorsGraph.init_y_domain([{ commits: 30 }]);

      expect(ContributorsGraph.prototype.y_domain).toEqual([0, 30]);
    });
  });

  describe('#init_domain', function() {
    it('calls init_x_domain and init_y_domain', function() {
      spyOn(ContributorsGraph, 'init_x_domain');
      spyOn(ContributorsGraph, 'init_y_domain');
      ContributorsGraph.init_domain();

      expect(ContributorsGraph.init_x_domain).toHaveBeenCalled();
      expect(ContributorsGraph.init_y_domain).toHaveBeenCalled();
    });
  });

  describe('#set_dates', function() {
    it('sets the dates', function() {
      ContributorsGraph.set_dates('2013-12-01');

      expect(ContributorsGraph.prototype.dates).toEqual('2013-12-01');
    });
  });

  describe('#set_x_domain', function() {
    it("sets the instance's x domain using the prototype's x_domain", function() {
      ContributorsGraph.prototype.x_domain = 20;
      var instance = new ContributorsGraph();
      instance.x = d3
        .scaleTime()
        .range([0, 100])
        .clamp(true);
      spyOn(instance.x, 'domain');
      instance.set_x_domain();

      expect(instance.x.domain).toHaveBeenCalledWith(20);
    });
  });

  describe('#set_y_domain', function() {
    it("sets the instance's y domain using the prototype's y_domain", function() {
      ContributorsGraph.prototype.y_domain = 30;
      var instance = new ContributorsGraph();
      instance.y = d3
        .scaleLinear()
        .range([100, 0])
        .nice();
      spyOn(instance.y, 'domain');
      instance.set_y_domain();

      expect(instance.y.domain).toHaveBeenCalledWith(30);
    });
  });

  describe('#set_domain', function() {
    it('calls set_x_domain and set_y_domain', function() {
      var instance = new ContributorsGraph();
      spyOn(instance, 'set_x_domain');
      spyOn(instance, 'set_y_domain');
      instance.set_domain();

      expect(instance.set_x_domain).toHaveBeenCalled();
      expect(instance.set_y_domain).toHaveBeenCalled();
    });
  });

  describe('#set_data', function() {
    it('sets the data', function() {
      var instance = new ContributorsGraph();
      instance.set_data('20');

      expect(instance.data).toEqual('20');
    });
  });
});

describe('ContributorsMasterGraph', function() {
  // TODO: fix or remove
  // describe("#process_dates", function () {
  //   it("gets and parses dates", function () {
  //     var graph = new ContributorsMasterGraph();
  //     var data = 'random data here';
  //     spyOn(graph, 'parse_dates');
  //     spyOn(graph, 'get_dates').andReturn("get");
  //     spyOn(ContributorsGraph,'set_dates').andCallThrough();
  //     graph.process_dates(data);
  //     expect(graph.parse_dates).toHaveBeenCalledWith(data);
  //     expect(graph.get_dates).toHaveBeenCalledWith(data);
  //     expect(ContributorsGraph.set_dates).toHaveBeenCalledWith("get");
  //   });
  // });

  describe('#get_dates', function() {
    it('plucks the date field from data collection', function() {
      var graph = new ContributorsMasterGraph();
      var data = [{ date: '2013-01-01' }, { date: '2012-12-15' }];

      expect(graph.get_dates(data)).toEqual(['2013-01-01', '2012-12-15']);
    });
  });

  describe('#parse_dates', function() {
    it('parses the dates', function() {
      var graph = new ContributorsMasterGraph();
      var parseDate = d3.timeParse('%Y-%m-%d');
      var data = [{ date: '2013-01-01' }, { date: '2012-12-15' }];
      var correct = [{ date: parseDate(data[0].date) }, { date: parseDate(data[1].date) }];
      graph.parse_dates(data);

      expect(data).toEqual(correct);
    });
  });
});