From f1e720e561dd9d9fa2809d45fface4beb500ecf4 Mon Sep 17 00:00:00 2001 From: Masayuki Tanaka Date: Sun, 21 Sep 2014 16:20:34 +0900 Subject: [PATCH] Add specs for legend - #515 --- Gruntfile.coffee | 4 +++- spec/c3-helper.js | 11 +++++++++++ spec/c3-spec.js | 19 +++++++------------ spec/core-spec.js | 42 ++++++++++++++++++++++++++++++++++++++++++ spec/legend-spec.js | 32 ++++++++++++++++++++++++++++++++ 5 files changed, 95 insertions(+), 13 deletions(-) create mode 100644 spec/c3-helper.js create mode 100644 spec/core-spec.js create mode 100644 spec/legend-spec.js diff --git a/Gruntfile.coffee b/Gruntfile.coffee index 17a0312..bdbd857 100644 --- a/Gruntfile.coffee +++ b/Gruntfile.coffee @@ -83,7 +83,9 @@ module.exports = (grunt) -> c3: src: 'c3.js' options: - specs: 'spec/*.js' + specs: 'spec/*-spec.js' + helpers: 'spec/*-helper.js' + vendor: 'http://d3js.org/d3.v3.min.js' uglify: c3: diff --git a/spec/c3-helper.js b/spec/c3-helper.js new file mode 100644 index 0000000..9cc91b2 --- /dev/null +++ b/spec/c3-helper.js @@ -0,0 +1,11 @@ +function initDom() { + 'use strict'; + + var div = document.createElement('div'); + div.id = 'chart'; + div.style.width = '640px'; + div.style.height = '480px'; + document.body.appendChild(div); + document.body.style.margin = '0px'; +} +typeof initDom !== 'undefined'; diff --git a/spec/c3-spec.js b/spec/c3-spec.js index 49735c2..c2455b0 100644 --- a/spec/c3-spec.js +++ b/spec/c3-spec.js @@ -1,20 +1,15 @@ - -var describe = window.describe; -var expect = window.expect; -var it = window.it; - -var c3 = window.c3; +var describe = window.describe, + expect = window.expect, + it = window.it; describe('c3', function () { 'use strict'; - it('exists', function () { + var c3 = window.c3; - expect(c3).not.toBe(null); + it('exists', function () { + expect(c3).not.toBeNull(); expect(typeof c3).toBe('object'); - }); - - // ...write other tests here - }); + diff --git a/spec/core-spec.js b/spec/core-spec.js new file mode 100644 index 0000000..596234e --- /dev/null +++ b/spec/core-spec.js @@ -0,0 +1,42 @@ +var describe = window.describe, + expect = window.expect, + it = window.it, + beforeEach = window.beforeEach; + +describe('c3 chart', function () { + 'use strict'; + + var chart, d3; + + beforeEach(function () { + window.initDom(); + + chart = window.c3.generate({ + data: { + columns: [ + ['data1', 30, 200, 100, 400, 150, 250], + ['data2', 50, 20, 10, 40, 15, 25], + ['data3', 150, 120, 110, 140, 115, 125] + ] + } + }); + + d3 = chart.internal.d3; + }); + + it('should be created', function () { + var svg = d3.select('#chart svg'); + expect(svg).not.toBeNull(); + }); + + it('should have same width', function () { + var svg = d3.select('#chart svg'); + expect(+svg.attr('width')).toBe(640); + }); + + it('should have same height', function () { + var svg = d3.select('#chart svg'); + expect(+svg.attr('height')).toBe(480); + }); + +}); diff --git a/spec/legend-spec.js b/spec/legend-spec.js new file mode 100644 index 0000000..a2cd80c --- /dev/null +++ b/spec/legend-spec.js @@ -0,0 +1,32 @@ +var describe = window.describe, + expect = window.expect, + it = window.it, + beforeEach = window.beforeEach; + +describe('c3 chart legend', function () { + 'use strict'; + + var chart, d3; + + beforeEach(function () { + window.initDom(); + + chart = window.c3.generate({ + data: { + columns: [ + ['data1', 30, 200, 100, 400, 150, 250], + ['data2', 50, 20, 10, 40, 15, 25], + ['data3', 150, 120, 110, 140, 115, 125] + ] + } + }); + + d3 = chart.internal.d3; + }); + + it('should be located on the center of chart', function () { + var box = chart.internal.legend.node().getBoundingClientRect(); + expect(box.left + box.right).toBe(640); + }); + +});