added more karma unit tests for easypiechart
BrainDoctor committed
Jul 24, 2017 at 23:38 UTC
82c6a80ebb4b581337b1d6254eca339f2e986f46
3 files changed
+138
-60
tests/web/easypiechart.chart.spec.js
new
+39
@@ -0,0 +1,39 @@
1
+"use strict";
2
+
3
+
4
+// with xdescribe, this is skipped.
5
+describe("creation of easy pie charts", function () {
6
+
7
+ beforeAll(function () {
8
+ // karma stores the loaded files relative to "base/".
9
+ // This command is needed to load HTML fixtures
10
+ jasmine.getFixtures().fixturesPath = "base/tests/web/fixtures";
11
+ });
12
+
13
+ it("should create new chart, but it's failure is expected for demonstration purpose", function () {
14
+ // arrange
15
+ // Theoretically we can load some html. What about jquery? could this work?
16
+ // https://stackoverflow.com/questions/5337481/spying-on-jquery-selectors-in-jasmine
17
+ loadFixtures("easypiechart.creation.fixture1.html");
18
+
19
+ // for easy pie chart, we can fake the data result:
20
+ var data = {
21
+ result: [5]
22
+ };
23
+ // act
24
+ var result = NETDATA.easypiechartChartCreate(createState(), data);
25
+ // assert
26
+ expect(result).toBe(true);
27
+ });
28
+
29
+ function createState(min, max) {
30
+ // create a fake state with only needed properties.
31
+ return {
32
+ tmp: {
33
+ easyPieChartMin: min,
34
+ easyPieChartMax: max
35
+ }
36
+ };
37
+ }
38
+
39
+});
\ No newline at end of file
tests/web/easypiechart.percentage.spec.js
new
+99
@@ -0,0 +1,99 @@
1
+"use strict";
2
+
3
+
4
+describe("percentage calculations for easy pie charts with dynamic range", function () {
5
+
6
+ it("should return positive value, if value greater than dynamic max", function () {
7
+ var state = createState(null, null);
8
+
9
+ var result = NETDATA.easypiechartPercentFromValueMinMax(state, 6, 2, 10);
10
+
11
+ expect(result).toBe(60);
12
+ });
13
+
14
+ it("should return negative value, if value lesser than dynamic min", function () {
15
+ var state = createState(null, null);
16
+
17
+ var result = NETDATA.easypiechartPercentFromValueMinMax(state, -6, -10, 10);
18
+
19
+ expect(result).toBe(-60);
20
+ });
21
+
22
+ it("should return 0.1 if value is zero and min/max undefined", function () {
23
+ var state = createState(null, null);
24
+
25
+ var result = NETDATA.easypiechartPercentFromValueMinMax(state, 0, -1, 2);
26
+
27
+ expect(result).toBe(0.1);
28
+ });
29
+
30
+ it("should return positive value, if max is user-defined", function () {
31
+ var state = createState(null, 50);
32
+
33
+ var result = NETDATA.easypiechartPercentFromValueMinMax(state, 46, -40, 50);
34
+
35
+ expect(result).toBe(92);
36
+ });
37
+
38
+ it("should return negative value, if min is user-defined", function () {
39
+ var state = createState(-50, null);
40
+
41
+ var result = NETDATA.easypiechartPercentFromValueMinMax(state, -46, -50, 40);
42
+
43
+ expect(result).toBe(-92);
44
+ });
45
+
46
+});
47
+
48
+describe("percentage calculations for easy pie charts with fixed range", function () {
49
+
50
+ it("should return positive value, if min and max are user-defined", function () {
51
+ var state = createState(40, 50);
52
+
53
+ var result = NETDATA.easypiechartPercentFromValueMinMax(state, 46, 40, 50);
54
+
55
+ expect(result).toBe(60);
56
+ });
57
+
58
+ it("should return 100 if positive min and max are user-defined, but value is greater than max", function () {
59
+ var state = createState(40, 50);
60
+
61
+ var result = NETDATA.easypiechartPercentFromValueMinMax(state, 60, 40, 50);
62
+
63
+ expect(result).toBe(100);
64
+ });
65
+
66
+ it("should return 0.1 if positive min and max are user-defined, but value is smaller than min", function () {
67
+ var state = createState(40, 50);
68
+
69
+ var result = NETDATA.easypiechartPercentFromValueMinMax(state, 39.9, 42, 48);
70
+
71
+ expect(result).toBe(0.1);
72
+ });
73
+
74
+ it("should return -100 if negative min and max are user-defined, but value is smaller than min", function () {
75
+ var state = createState(-40, -50);
76
+
77
+ var result = NETDATA.easypiechartPercentFromValueMinMax(state, -50.1, -40, -50);
78
+
79
+ expect(result).toBe(-100);
80
+ });
81
+
82
+ it("should return 0.1 if negative min and max are user-defined, but value is smaller than min", function () {
83
+ var state = createState(-40, -50);
84
+
85
+ var result = NETDATA.easypiechartPercentFromValueMinMax(state, -50.1, -20, -45);
86
+
87
+ expect(result).toBe(-100);
88
+ });
89
+});
90
+
91
+function createState(min, max) {
92
+ // create a fake state with only the needed properties.
93
+ return {
94
+ tmp: {
95
+ easyPieChartMin: min,
96
+ easyPieChartMax: max
97
+ }
98
+ };
99
+}
\ No newline at end of file
tests/web/easypiechart.spec.js
deleted
-60
@@ -1,60 +0,0 @@
1
-// delete these comments if not needed anymore.
2
-
3
-function createState(min, max) {
4
- // create a fake state with only needed properties? Spying? not figured it out yet...
5
- return {
6
- tmp: {
7
- easyPieChartMin: min,
8
- easyPieChartMax: max
9
- }
10
- };
11
-}
12
-
13
-
14
-describe("percentage calculations for easy pie charts", function () {
15
-
16
- // some easy functions to test. incomplete yet.
17
- it('should return 50 if positive value between min and max', function () {
18
- // act
19
- var result = NETDATA.easypiechartPercentFromValueMinMax(createState(0, 0), 1, 0, 2);
20
- // assert
21
- expect(result).toBe(50);
22
- });
23
-
24
- it('should return 0.1 if value is zero', function () {
25
- // act
26
- var result = NETDATA.easypiechartPercentFromValueMinMax(createState(0, 0), 0, 0, 2);
27
- // assert
28
- expect(result).toBe(0.1);
29
- });
30
-
31
-});
32
-
33
-
34
-// with xdescribe, this is skipped.
35
-// Delete the x to enable again and let it fail to test the build
36
-describe('creation of easy pie charts', function () {
37
-
38
- beforeAll(function () {
39
- // karma stores the loaded files relative to "base/".
40
- // This command is needed to load HTML fixtures
41
- jasmine.getFixtures().fixturesPath = "base/tests/web/fixtures";
42
- });
43
-
44
- it('should create new chart', function () {
45
- // arrange
46
- // Theoretically we can load some html. What about jquery? could this work?
47
- // https://stackoverflow.com/questions/5337481/spying-on-jquery-selectors-in-jasmine
48
- loadFixtures("easypiechart.creation.fixture1.html");
49
-
50
- // for easy pie chart, we can fake the data result:
51
- var data = {
52
- result: [5]
53
- };
54
- // act
55
- var result = NETDATA.easypiechartChartCreate(createState(), data);
56
- // assert
57
- expect(result).toBe(true);
58
- });
59
-
60
-});
\ No newline at end of file