From 82c6a80ebb4b581337b1d6254eca339f2e986f46 Mon Sep 17 00:00:00 2001 From: BrainDoctor Date: Mon, 24 Jul 2017 23:38:55 +0200 Subject: added more karma unit tests for easypiechart --- tests/web/easypiechart.chart.spec.js | 39 ++++++++++++ tests/web/easypiechart.percentage.spec.js | 99 +++++++++++++++++++++++++++++++ tests/web/easypiechart.spec.js | 60 ------------------- 3 files changed, 138 insertions(+), 60 deletions(-) create mode 100644 tests/web/easypiechart.chart.spec.js create mode 100644 tests/web/easypiechart.percentage.spec.js delete mode 100644 tests/web/easypiechart.spec.js (limited to 'tests') diff --git a/tests/web/easypiechart.chart.spec.js b/tests/web/easypiechart.chart.spec.js new file mode 100644 index 0000000000..0c82580aac --- /dev/null +++ b/tests/web/easypiechart.chart.spec.js @@ -0,0 +1,39 @@ +"use strict"; + + +// with xdescribe, this is skipped. +describe("creation of easy pie charts", function () { + + beforeAll(function () { + // karma stores the loaded files relative to "base/". + // This command is needed to load HTML fixtures + jasmine.getFixtures().fixturesPath = "base/tests/web/fixtures"; + }); + + it("should create new chart, but it's failure is expected for demonstration purpose", function () { + // arrange + // Theoretically we can load some html. What about jquery? could this work? + // https://stackoverflow.com/questions/5337481/spying-on-jquery-selectors-in-jasmine + loadFixtures("easypiechart.creation.fixture1.html"); + + // for easy pie chart, we can fake the data result: + var data = { + result: [5] + }; + // act + var result = NETDATA.easypiechartChartCreate(createState(), data); + // assert + expect(result).toBe(true); + }); + + function createState(min, max) { + // create a fake state with only needed properties. + return { + tmp: { + easyPieChartMin: min, + easyPieChartMax: max + } + }; + } + +}); \ No newline at end of file diff --git a/tests/web/easypiechart.percentage.spec.js b/tests/web/easypiechart.percentage.spec.js new file mode 100644 index 0000000000..9a48390991 --- /dev/null +++ b/tests/web/easypiechart.percentage.spec.js @@ -0,0 +1,99 @@ +"use strict"; + + +describe("percentage calculations for easy pie charts with dynamic range", function () { + + it("should return positive value, if value greater than dynamic max", function () { + var state = createState(null, null); + + var result = NETDATA.easypiechartPercentFromValueMinMax(state, 6, 2, 10); + + expect(result).toBe(60); + }); + + it("should return negative value, if value lesser than dynamic min", function () { + var state = createState(null, null); + + var result = NETDATA.easypiechartPercentFromValueMinMax(state, -6, -10, 10); + + expect(result).toBe(-60); + }); + + it("should return 0.1 if value is zero and min/max undefined", function () { + var state = createState(null, null); + + var result = NETDATA.easypiechartPercentFromValueMinMax(state, 0, -1, 2); + + expect(result).toBe(0.1); + }); + + it("should return positive value, if max is user-defined", function () { + var state = createState(null, 50); + + var result = NETDATA.easypiechartPercentFromValueMinMax(state, 46, -40, 50); + + expect(result).toBe(92); + }); + + it("should return negative value, if min is user-defined", function () { + var state = createState(-50, null); + + var result = NETDATA.easypiechartPercentFromValueMinMax(state, -46, -50, 40); + + expect(result).toBe(-92); + }); + +}); + +describe("percentage calculations for easy pie charts with fixed range", function () { + + it("should return positive value, if min and max are user-defined", function () { + var state = createState(40, 50); + + var result = NETDATA.easypiechartPercentFromValueMinMax(state, 46, 40, 50); + + expect(result).toBe(60); + }); + + it("should return 100 if positive min and max are user-defined, but value is greater than max", function () { + var state = createState(40, 50); + + var result = NETDATA.easypiechartPercentFromValueMinMax(state, 60, 40, 50); + + expect(result).toBe(100); + }); + + it("should return 0.1 if positive min and max are user-defined, but value is smaller than min", function () { + var state = createState(40, 50); + + var result = NETDATA.easypiechartPercentFromValueMinMax(state, 39.9, 42, 48); + + expect(result).toBe(0.1); + }); + + it("should return -100 if negative min and max are user-defined, but value is smaller than min", function () { + var state = createState(-40, -50); + + var result = NETDATA.easypiechartPercentFromValueMinMax(state, -50.1, -40, -50); + + expect(result).toBe(-100); + }); + + it("should return 0.1 if negative min and max are user-defined, but value is smaller than min", function () { + var state = createState(-40, -50); + + var result = NETDATA.easypiechartPercentFromValueMinMax(state, -50.1, -20, -45); + + expect(result).toBe(-100); + }); +}); + +function createState(min, max) { + // create a fake state with only the needed properties. + return { + tmp: { + easyPieChartMin: min, + easyPieChartMax: max + } + }; +} \ No newline at end of file diff --git a/tests/web/easypiechart.spec.js b/tests/web/easypiechart.spec.js deleted file mode 100644 index 53eaefcfe8..0000000000 --- a/tests/web/easypiechart.spec.js +++ /dev/null @@ -1,60 +0,0 @@ -// delete these comments if not needed anymore. - -function createState(min, max) { - // create a fake state with only needed properties? Spying? not figured it out yet... - return { - tmp: { - easyPieChartMin: min, - easyPieChartMax: max - } - }; -} - - -describe("percentage calculations for easy pie charts", function () { - - // some easy functions to test. incomplete yet. - it('should return 50 if positive value between min and max', function () { - // act - var result = NETDATA.easypiechartPercentFromValueMinMax(createState(0, 0), 1, 0, 2); - // assert - expect(result).toBe(50); - }); - - it('should return 0.1 if value is zero', function () { - // act - var result = NETDATA.easypiechartPercentFromValueMinMax(createState(0, 0), 0, 0, 2); - // assert - expect(result).toBe(0.1); - }); - -}); - - -// with xdescribe, this is skipped. -// Delete the x to enable again and let it fail to test the build -describe('creation of easy pie charts', function () { - - beforeAll(function () { - // karma stores the loaded files relative to "base/". - // This command is needed to load HTML fixtures - jasmine.getFixtures().fixturesPath = "base/tests/web/fixtures"; - }); - - it('should create new chart', function () { - // arrange - // Theoretically we can load some html. What about jquery? could this work? - // https://stackoverflow.com/questions/5337481/spying-on-jquery-selectors-in-jasmine - loadFixtures("easypiechart.creation.fixture1.html"); - - // for easy pie chart, we can fake the data result: - var data = { - result: [5] - }; - // act - var result = NETDATA.easypiechartChartCreate(createState(), data); - // assert - expect(result).toBe(true); - }); - -}); \ No newline at end of file -- cgit v1.2.3