diff --git a/spec/lib/grid/set_data_spec.coffee b/spec/lib/grid/set_data_spec.coffee new file mode 100644 index 0000000..c7e0a1e --- /dev/null +++ b/spec/lib/grid/set_data_spec.coffee @@ -0,0 +1,193 @@ +describe 'Morris.Grid#setData', -> + + it 'should not alter user-supplied data', -> + my_data = [{x: 1, y: 1}, {x: 2, y: 2}] + expected_data = [{x: 1, y: 1}, {x: 2, y: 2}] + Morris.Line + element: 'graph' + data: my_data + xkey: 'x' + ykeys: ['y'] + labels: ['dontcare'] + my_data.should.deep.equal expected_data + + describe 'ymin/ymax', -> + + it 'should use a user-specified minimum and maximum value', -> + line = Morris.Line + element: 'graph' + data: [{x: 1, y: 1}] + xkey: 'x' + ykeys: ['y', 'z'] + labels: ['y', 'z'] + ymin: 10 + ymax: 20 + line.ymin.should.equal 10 + line.ymax.should.equal 20 + + describe 'auto', -> + + it 'should automatically calculate the minimum and maximum value', -> + line = Morris.Line + element: 'graph' + data: [{x: 1, y: 10}, {x: 2, y: 15}, {x: 3, y: null}, {x: 4}] + xkey: 'x' + ykeys: ['y', 'z'] + labels: ['y', 'z'] + ymin: 'auto' + ymax: 'auto' + line.ymin.should.equal 10 + line.ymax.should.equal 15 + line = Morris.Line + element: 'graph' + data: [{x: 1}, {x: 2}, {x: 3}, {x: 4}] + xkey: 'x' + ykeys: ['y', 'z'] + labels: ['y', 'z'] + ymin: 'auto' + ymax: 'auto' + line.ymin.should.equal 0 + line.ymax.should.equal 1 + + describe 'auto [n]', -> + + it 'should automatically calculate the minimum and maximum value', -> + line = Morris.Line + element: 'graph' + data: [{x: 1, y: 10}, {x: 2, y: 15}, {x: 3, y: null}, {x: 4}] + xkey: 'x' + ykeys: ['y', 'z'] + labels: ['y', 'z'] + ymin: 'auto 11' + ymax: 'auto 13' + line.ymin.should.equal 10 + line.ymax.should.equal 15 + line = Morris.Line + element: 'graph' + data: [{x: 1}, {x: 2}, {x: 3}, {x: 4}] + xkey: 'x' + ykeys: ['y', 'z'] + labels: ['y', 'z'] + ymin: 'auto 11' + ymax: 'auto 13' + line.ymin.should.equal 11 + line.ymax.should.equal 13 + + it 'should use a user-specified minimum and maximum value', -> + line = Morris.Line + element: 'graph' + data: [{x: 1, y: 10}, {x: 2, y: 15}, {x: 3, y: null}, {x: 4}] + xkey: 'x' + ykeys: ['y', 'z'] + labels: ['y', 'z'] + ymin: 'auto 5' + ymax: 'auto 20' + line.ymin.should.equal 5 + line.ymax.should.equal 20 + line = Morris.Line + element: 'graph' + data: [{x: 1}, {x: 2}, {x: 3}, {x: 4}] + xkey: 'x' + ykeys: ['y', 'z'] + labels: ['y', 'z'] + ymin: 'auto 5' + ymax: 'auto 20' + line.ymin.should.equal 5 + line.ymax.should.equal 20 + + describe 'xmin/xmax', -> + + it 'should calculate the horizontal range', -> + line = Morris.Line + element: 'graph' + data: [{x: 2, y: 2}, {x: 1, y: 1}, {x: 4, y: 4}, {x: 3, y: 3}] + xkey: 'x' + ykeys: ['y'] + labels: ['y'] + line.xmin.should == 1 + line.xmax.should == 4 + + it "should pad the range if there's only one data point", -> + line = Morris.Line + element: 'graph' + data: [{x: 2, y: 2}] + xkey: 'x' + ykeys: ['y'] + labels: ['y'] + line.xmin.should == 1 + line.xmax.should == 3 + + describe 'sorting', -> + + it 'should sort data when parseTime is true', -> + line = Morris.Line + element: 'graph' + data: [ + {x: '2012 Q1', y: 2}, + {x: '2012 Q3', y: 1}, + {x: '2012 Q4', y: 4}, + {x: '2012 Q2', y: 3}] + xkey: 'x' + ykeys: ['y'] + labels: ['y'] + line.data.map((row) -> row.label).should.deep.equal ['2012 Q1', '2012 Q2', '2012 Q3', '2012 Q4'] + + it 'should not sort data when parseTime is false', -> + line = Morris.Line + element: 'graph' + data: [{x: 1, y: 2}, {x: 4, y: 1}, {x: 3, y: 4}, {x: 2, y: 3}] + xkey: 'x' + ykeys: ['y'] + labels: ['y'] + parseTime: false + line.data.map((row) -> row.label).should.deep.equal [1, 4, 3, 2] + + describe 'timestamp data', -> + + it 'should generate default labels for timestamp x-values', -> + d = [ + new Date 2012, 0, 1 + new Date 2012, 0, 2 + new Date 2012, 0, 3 + new Date 2012, 0, 4 + ] + line = Morris.Line + element: 'graph' + data: [ + {x: d[0].getTime(), y: 2}, + {x: d[1].getTime(), y: 1}, + {x: d[2].getTime(), y: 4}, + {x: d[3].getTime(), y: 3}] + xkey: 'x' + ykeys: ['y'] + labels: ['y'] + line.data.map((row) -> row.label).should.deep.equal d.map((t) -> t.toString()) + + it 'should use a user-supplied formatter for labels', -> + line = Morris.Line + element: 'graph' + data: [ + {x: new Date(2012, 0, 1).getTime(), y: 2}, + {x: new Date(2012, 0, 2).getTime(), y: 1}, + {x: new Date(2012, 0, 3).getTime(), y: 4}, + {x: new Date(2012, 0, 4).getTime(), y: 3}] + xkey: 'x' + ykeys: ['y'] + labels: ['y'] + dateFormat: (ts) -> + date = new Date(ts) + "#{date.getFullYear()}-#{date.getMonth()+1}-#{date.getDate()}" + line.data.map((row) -> row.label).should.deep.equal ['2012-1-1', '2012-1-2', '2012-1-3', '2012-1-4'] + + it 'should parse y-values in strings', -> + line = Morris.Line + element: 'graph' + data: [{x: 2, y: '12'}, {x: 1, y: '13.5'}, {x: 4, y: '14'}, {x: 3, y: '16'}] + xkey: 'x' + ykeys: ['y'] + labels: ['y'] + line.ymin.should == 12 + line.ymax.should == 16 + line.data.map((row) -> row.y).should.deep.equal [[13.5], [12], [16], [14]] + + diff --git a/spec/lib/line_spec.coffee b/spec/lib/line/line_spec.coffee similarity index 100% rename from spec/lib/line_spec.coffee rename to spec/lib/line/line_spec.coffee diff --git a/spec/lib/line/set_data_spec.coffee b/spec/lib/line/set_data_spec.coffee deleted file mode 100644 index 840e3d4..0000000 --- a/spec/lib/line/set_data_spec.coffee +++ /dev/null @@ -1,97 +0,0 @@ -describe 'Morris.Line data', -> - - it 'should not alter user-supplied data', -> - my_data = [{x: 1, y: 1}, {x: 2, y: 2}] - expected_data = [{x: 1, y: 1}, {x: 2, y: 2}] - Morris.Line - element: 'graph' - data: my_data - xkey: 'x' - ykeys: ['y'] - labels: ['dontcare'] - my_data.should.deep.equal expected_data - - describe 'ymin/ymax', -> - - it 'should use a user-specified minimum and maximum value', -> - line = Morris.Line - element: 'graph' - data: [{x: 1, y: 1}] - xkey: 'x' - ykeys: ['y', 'z'] - labels: ['y', 'z'] - ymin: 10 - ymax: 20 - line.ymin.should.equal 10 - line.ymax.should.equal 20 - - describe 'auto', -> - - it 'should automatically calculate the minimum and maximum value', -> - line = Morris.Line - element: 'graph' - data: [{x: 1, y: 10}, {x: 2, y: 15}, {x: 3, y: null}, {x: 4}] - xkey: 'x' - ykeys: ['y', 'z'] - labels: ['y', 'z'] - ymin: 'auto' - ymax: 'auto' - line.ymin.should.equal 10 - line.ymax.should.equal 15 - line = Morris.Line - element: 'graph' - data: [{x: 1}, {x: 2}, {x: 3}, {x: 4}] - xkey: 'x' - ykeys: ['y', 'z'] - labels: ['y', 'z'] - ymin: 'auto' - ymax: 'auto' - line.ymin.should.equal 0 - line.ymax.should.equal 1 - - describe 'auto [n]', -> - - it 'should automatically calculate the minimum and maximum value', -> - line = Morris.Line - element: 'graph' - data: [{x: 1, y: 10}, {x: 2, y: 15}, {x: 3, y: null}, {x: 4}] - xkey: 'x' - ykeys: ['y', 'z'] - labels: ['y', 'z'] - ymin: 'auto 11' - ymax: 'auto 13' - line.ymin.should.equal 10 - line.ymax.should.equal 15 - line = Morris.Line - element: 'graph' - data: [{x: 1}, {x: 2}, {x: 3}, {x: 4}] - xkey: 'x' - ykeys: ['y', 'z'] - labels: ['y', 'z'] - ymin: 'auto 11' - ymax: 'auto 13' - line.ymin.should.equal 11 - line.ymax.should.equal 13 - - it 'should use a user-specified minimum and maximum value', -> - line = Morris.Line - element: 'graph' - data: [{x: 1, y: 10}, {x: 2, y: 15}, {x: 3, y: null}, {x: 4}] - xkey: 'x' - ykeys: ['y', 'z'] - labels: ['y', 'z'] - ymin: 'auto 5' - ymax: 'auto 20' - line.ymin.should.equal 5 - line.ymax.should.equal 20 - line = Morris.Line - element: 'graph' - data: [{x: 1}, {x: 2}, {x: 3}, {x: 4}] - xkey: 'x' - ykeys: ['y', 'z'] - labels: ['y', 'z'] - ymin: 'auto 5' - ymax: 'auto 20' - line.ymin.should.equal 5 - line.ymax.should.equal 20 -