File size: 5,373 Bytes
cbc76af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
define([
  'client/views/graph/line-set',
  'client/views/graph/line',
  'client/views/graph/component',
  'common/collections/grouped_timeseries'
], function (LineSet, Line, Component, Collection) {

  describe('Line Set', function () {

    var lineset, graph, collection;

    beforeEach(function () {
      collection = new Collection([
        { foo: 1, bar: 2 },
        { foo: 2, bar: 4 },
        { foo: 3, bar: 6 },
        { foo: 4, bar: 8 }
      ]);
      graph = {
        getXPos: _.identity,
        getYPos: function (index, attr) {
          return collection.at(index).get(attr);
        },
        getLines: function () {
          return [
            {
              key: 'foo',
              x: _.identity,
              y: _.identity
            },
            {
              key: 'bar',
              x: _.identity,
              y: _.identity
            }
          ];
        },
        getDefaultComponentOptions: function () {
          return {
            graph: graph,
            collection: collection,
            scales: {
              x: _.identity,
              y: _.identity
            }
          };
        }
      };
    });

    describe('initialize', function () {

      it('calls up to component initialize', function () {
        spyOn(Component.prototype, 'initialize').and.callThrough();
        lineset = new LineSet({
          graph: graph,
          collection: collection
        });
        /*
        Note:
        because of variable hoisting, we have to include the `el: undefined`
        argument in the expectation.
        The base Backbone.View object adds an "el" key to the passed-in 'options'
        argument at the very end of the View::initialize method, which jasmine
        then sees as part of the initial argument passed to the View object.

        So we have to "expect" that an `el: undefined` argument was passed,
        whether it was or not
        */
        expect(Component.prototype.initialize).toHaveBeenCalledWith({
          graph: graph,
          collection: collection,
          el: undefined
        });
      });

      it('creates a line component for each line', function () {
        lineset = new LineSet({
          graph: graph,
          collection: collection
        });
        expect(lineset.lines.length).toEqual(2);
        _.each(lineset.lines, function (line) {
          expect(line instanceof Line).toBe(true);
        });
      });

      it('sets classNames and valueAttrs on lines', function () {
        lineset = new LineSet({
          graph: graph,
          collection: collection
        });
        var lines = graph.getLines();
        _.each(lineset.lines, function (line, i) {
          expect(line.valueAttr).toEqual(lines[i].key);
          expect(line.className).toEqual('group' + i);
        });
      });

      it('passes a "grouped" property to lines', function () {
        lineset = new LineSet({
          graph: graph,
          collection: collection
        });
        _.each(lineset.lines, function (line) {
          expect(line.grouped).toEqual(true);
        });
      });

    });

    describe('onHover', function () {

      var e;

      beforeEach(function () {
        spyOn(Line.prototype, 'x').and.callFake(_.identity);
        e = {
          slice: 4,
          x: 1,
          y: 1
        };
        lineset = new LineSet({
          graph: graph,
          collection: collection
        });
        spyOn(collection, 'selectItem');
      });

      it('calls collection selectItem method with the closest model index', function () {
        lineset.onHover(e);
        expect(collection.selectItem).toHaveBeenCalledWith(1, jasmine.any(Object));

        e.x = 3;
        lineset.onHover(e);
        expect(collection.selectItem).toHaveBeenCalledWith(3, jasmine.any(Object));
      });

      it('does not select item if cursor is over linelabel area', function () {
        e.slice = 5; // 5 represents line labels section
        lineset.onHover(e);
        expect(collection.selectItem).not.toHaveBeenCalled();
      });

      it('passes the valueAttr of the closest line as an option', function () {
        e.y = 1;
        lineset.onHover(e);
        expect(collection.selectItem).toHaveBeenCalledWith(1, { valueAttr: 'foo', force: true });

        e.y = 5;
        lineset.onHover(e);
        expect(collection.selectItem).toHaveBeenCalledWith(1, { valueAttr: 'bar', force: true });
      });

      it('passes a null valueAttr if no line has a value at the closest index', function () {
        collection.at(3).set({ foo: null, bar: null });

        e.x = 3;
        lineset.onHover(e);
        expect(collection.selectItem).toHaveBeenCalledWith(3, { valueAttr: null, force: true });
      });

      it('interpolates between lines if x value is between two points', function () {
        collection.at(3).set('bar', 34);

        e.x = 2.4;
        e.y = 10;
        lineset.onHover(e);
        /* if we used purely "closest point" then this would return the
         * "bar" line at (2, 6) but because it slopes up so sharply then
         * the closest line at x=2.4 is the "foo" line
         * the interpolated mid-point between the lines at x=2.4 is y=15
         */
        expect(collection.selectItem).toHaveBeenCalledWith(2, { valueAttr: 'foo', force: true });
      });

    });

  });

});