Skip to content

Commit e484422

Browse files
committed
added tests for the current modules.
1 parent 0930f14 commit e484422

7 files changed

Lines changed: 257 additions & 6 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.idea
22
node_modules
3-
lib
3+
lib
4+
/coverage

.npmignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
.idea
1+
.idea
2+
*.test.js
3+
/coverage

package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44
"description": "This is an ES6/ES2015 library of Electrical Engineering Equations",
55
"main": "lib/index.js",
66
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1",
7+
"test": "jest --coverage",
88
"build": "babel src --out-dir lib --source-maps",
99
"prepublish": "npm run build"
1010
},
11+
"jest": {
12+
"testEnvironment": "node"
13+
},
1114
"repository": {
1215
"type": "git",
1316
"url": "git+https://github.com/markhatchell/JS-Electrical-Engineering-Equations.git"
@@ -25,6 +28,7 @@
2528
"devDependencies": {
2629
"babel-cli": "^6.26.0",
2730
"babel-plugin-transform-es2015-block-scoping": "^6.26.0",
28-
"babel-preset-env": "^1.6.1"
31+
"babel-preset-env": "^1.6.1",
32+
"jest-cli": "^22.1.4"
2933
}
3034
}

src/Ohms-Law.test.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
const OhmsLaw = require('./Ohms-Law').default;
2+
3+
4+
describe('OhmsLaw', () => {
5+
6+
test('if current is .5 and volts are 10 then resistance should be 20 and wattage should be 5', () => {
7+
expect(OhmsLaw.calcWattsResistance(.5, 10)).toEqual({
8+
resistance: 20,
9+
watts: 5
10+
})
11+
});
12+
13+
test('if current is .5 and resistance is 20 then volts should be 10 and wattage should be 5', () => {
14+
expect(OhmsLaw.calcWattsVoltage(20, .5)).toEqual({
15+
voltage: 10,
16+
watts: 5
17+
})
18+
});
19+
20+
test('if current is .5 and wattage is 5 then resistance should be 20 and voltage should be 10', () => {
21+
expect(OhmsLaw.calcVoltageResistance(.5, 5)).toEqual({
22+
voltage: 10,
23+
resistance: 20
24+
})
25+
});
26+
27+
test('if wattage is 5 and volts are 10 then resistance should be 20 and current should be .5', () => {
28+
expect(OhmsLaw.calcCurrentResistance(5, 10)).toEqual({
29+
resistance: 20,
30+
current: .5
31+
})
32+
});
33+
34+
test('if wattage is 5 and resistance is 20 then voltage should be 10 and current should be .5', () => {
35+
expect(OhmsLaw.calcVoltageCurrent(20, 5)).toEqual({
36+
voltage: 10,
37+
current: .5
38+
})
39+
});
40+
41+
test('if volts are 10 and resistance is 20 then watts should be 5 and current should be .5', () => {
42+
expect(OhmsLaw.calcWattsCurrent(20, 10)).toEqual({
43+
watts: 5,
44+
current: .5
45+
})
46+
});
47+
48+
});

src/Tank-Circuit.test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const TankCircuit = require('./Tank-Circuit').default;
2+
3+
4+
describe('TankCircuit', () => {
5+
6+
test('if InductanceInNanoHenries is 50 and CapacitanceInPicoFarads is 50 then calculatedFrequencyInHertz should be 100658424.20897408', () => {
7+
const inductance = parseFloat(50) / 1000000000;
8+
const capacitance = parseFloat(50) / 1000000000000;
9+
10+
expect(TankCircuit.calcFrequency(inductance, capacitance).calculatedFrequencyInHertz).toEqual(100658424.20897408)
11+
});
12+
13+
test('if frequency in Hertz is 100658424.20897408 and CapacitanceInPicoFarads is 50 then calculatedInductanceInHenries should be 0.000000050', () => {
14+
const frequency = 100658424.20897408;
15+
const capacitance = parseFloat(50) / 1000000000000;
16+
17+
expect(TankCircuit.calcInductance(frequency, capacitance).calculatedInductanceInHenries).toEqual(0.000000050)
18+
});
19+
20+
test('if frequency in Hertz is 100658424.20897408 and InductanceInNanoHenries is 50 then calculatedCapacitanceInFarads should be 0.000000000050', () => {
21+
const frequency = 100658424.20897408;
22+
const inductance = parseFloat(50) / 1000000000;
23+
24+
expect(TankCircuit.calcCapacitance(frequency, inductance).calculatedCapacitanceInFarads).toEqual(0.000000000050)
25+
});
26+
27+
28+
29+
});

src/Wavelength.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function metersToInches(meters, precision = 4) {
2525
class Wavelength {
2626
constructor(frequencyInMegahertz, precision = 4) {
2727
if (typeof frequencyInMegahertz === 'undefined') {
28-
throw Error('Need a frequency to init a Wavelength');
28+
throw new Error('Need a frequency to init a Wavelength');
2929
}
3030
this.frequencyInMegahertz = parseFloat(frequencyInMegahertz);
3131
this.valueInMeters = 300 / this.frequencyInMegahertz;
@@ -36,6 +36,10 @@ class Wavelength {
3636
return this.valueInMeters.toPrecision(this.precision);
3737
}
3838

39+
toFloat() {
40+
return parseFloat(this.valueInMeters.toPrecision(this.precision));
41+
}
42+
3943
setPrecision(p) {
4044
this.precision = p;
4145
}
@@ -108,7 +112,7 @@ class Wavelength {
108112
return metersToInches(quaterWavelength, this.precision);
109113
}
110114
}
111-
115+
112116
}
113117

114118

src/Wavelength.test.js

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
const Wavelength = require('./Wavelength').default;
2+
3+
4+
describe('Wavelength', () => {
5+
let wavelength = new Wavelength('300');
6+
7+
test('should throw error when initialized with out a frequency', () => {
8+
expect(() => {
9+
new Wavelength()
10+
}).toThrowError('Need a frequency to init a Wavelength');
11+
});
12+
13+
14+
test('should initialize with a wavelength and output as string', () => {
15+
expect(wavelength.toString()).toEqual("1.000");
16+
});
17+
18+
test('should initialize with a wavelength and output as float', () => {
19+
expect(wavelength.toFloat()).toEqual(1.0);
20+
});
21+
22+
test('should set precision to 1', () => {
23+
wavelength.setPrecision(1);
24+
expect(wavelength.toString()).toEqual("1");
25+
wavelength.setPrecision(4);
26+
});
27+
28+
// meters tests
29+
test('should show full wavelength in meters as 1.000', () => {
30+
expect(wavelength.toString()).toEqual("1.000");
31+
expect(wavelength.toFloat()).toEqual(1.000);
32+
});
33+
34+
test('should show three quarters wavelength in meters as 0.7500', () => {
35+
wavelength.setPrecision(4);
36+
expect(wavelength.toThreeQuartersWavelength('m')).toEqual("0.7500");
37+
expect(parseFloat(wavelength.toThreeQuartersWavelength('m'))).toEqual(0.7500);
38+
});
39+
40+
test('should show five eighths half wavelength in meters as 0.6250', () => {
41+
wavelength.setPrecision(4);
42+
expect(wavelength.toFiveEighthsWavelength('m')).toEqual("0.6250");
43+
expect(parseFloat(wavelength.toFiveEighthsWavelength('m'))).toEqual(0.6250);
44+
});
45+
46+
test('should show one half wavelength in meters as 0.5000', () => {
47+
wavelength.setPrecision(4);
48+
expect(wavelength.toHalfWavelength('m')).toEqual("0.5000");
49+
expect(parseFloat(wavelength.toHalfWavelength('m'))).toEqual(0.5000);
50+
});
51+
52+
test('should show one quarter wavelength in meters as 0.2500', () => {
53+
wavelength.setPrecision(4);
54+
expect(wavelength.toQuarterWavelength('m')).toEqual("0.2500");
55+
expect(parseFloat(wavelength.toQuarterWavelength('m'))).toEqual(0.2500);
56+
});
57+
58+
// centimeters tests
59+
test('should show full wavelength in centimeters as 100.0', () => {
60+
expect(wavelength.toCentimeters()).toEqual("100.0");
61+
expect(parseFloat(wavelength.toCentimeters())).toEqual(100);
62+
});
63+
64+
test('should show three quarters wavelength in centimeters as 75.00', () => {
65+
wavelength.setPrecision(4);
66+
expect(wavelength.toThreeQuartersWavelength('cm')).toEqual("75.00");
67+
expect(parseFloat(wavelength.toThreeQuartersWavelength('cm'))).toEqual(75.00);
68+
});
69+
70+
test('should show five eighths half wavelength in centimeters as 62.50', () => {
71+
wavelength.setPrecision(4);
72+
expect(wavelength.toFiveEighthsWavelength('cm')).toEqual("62.50");
73+
expect(parseFloat(wavelength.toFiveEighthsWavelength('cm'))).toEqual(62.50);
74+
});
75+
76+
test('should show one half wavelength in centimeters as 50.00', () => {
77+
wavelength.setPrecision(4);
78+
expect(wavelength.toHalfWavelength('cm')).toEqual("50.00");
79+
expect(parseFloat(wavelength.toHalfWavelength('cm'))).toEqual(50.00);
80+
});
81+
82+
test('should show one quarter wavelength in centimeters as 25.00', () => {
83+
wavelength.setPrecision(4);
84+
expect(wavelength.toQuarterWavelength('cm')).toEqual("25.00");
85+
expect(parseFloat(wavelength.toQuarterWavelength('cm'))).toEqual(25.00);
86+
});
87+
88+
// feet tests
89+
90+
test('should show full wavelength in feet as 3.281', () => {
91+
wavelength.setPrecision(4);
92+
expect(wavelength.toFeet()).toEqual("3.281");
93+
expect(parseFloat(wavelength.toFeet())).toEqual(3.281);
94+
});
95+
96+
test('should show three quarters wavelength in feet as 2.4611', () => {
97+
wavelength.setPrecision(4);
98+
expect(wavelength.toThreeQuartersWavelength('f')).toEqual("2.461");
99+
expect(parseFloat(wavelength.toThreeQuartersWavelength('f'))).toEqual(2.461);
100+
});
101+
102+
test('should show five eighths half wavelength in feet as 2.051', () => {
103+
wavelength.setPrecision(4);
104+
expect(wavelength.toFiveEighthsWavelength('f')).toEqual("2.051");
105+
expect(parseFloat(wavelength.toFiveEighthsWavelength('f'))).toEqual(2.051);
106+
});
107+
108+
test('should show one half wavelength in feet as 1.640', () => {
109+
wavelength.setPrecision(4);
110+
expect(wavelength.toHalfWavelength('f')).toEqual("1.640");
111+
expect(parseFloat(wavelength.toHalfWavelength('f'))).toEqual(1.640);
112+
});
113+
114+
test('should show one quarter wavelength in feet as 0.8202', () => {
115+
wavelength.setPrecision(4);
116+
expect(wavelength.toQuarterWavelength('f')).toEqual("0.8202");
117+
expect(parseFloat(wavelength.toQuarterWavelength('f'))).toEqual(0.8202);
118+
});
119+
120+
121+
// inches tests
122+
123+
test('should show full wavelength in inches as 39.37', () => {
124+
wavelength.setPrecision(4);
125+
expect(wavelength.toInches()).toEqual("39.37");
126+
expect(parseFloat(wavelength.toInches())).toEqual(39.37);
127+
});
128+
129+
test('should show three quarters wavelength in feet as 29.53', () => {
130+
wavelength.setPrecision(4);
131+
expect(wavelength.toThreeQuartersWavelength('in')).toEqual("29.53");
132+
expect(parseFloat(wavelength.toThreeQuartersWavelength('in'))).toEqual(29.53);
133+
});
134+
135+
test('should show five eighths half wavelength in feet as 24.61', () => {
136+
wavelength.setPrecision(4);
137+
expect(wavelength.toFiveEighthsWavelength('in')).toEqual("24.61");
138+
expect(parseFloat(wavelength.toFiveEighthsWavelength('in'))).toEqual(24.61);
139+
});
140+
141+
test('should show one half wavelength in feet as 19.69', () => {
142+
wavelength.setPrecision(4);
143+
expect(wavelength.toHalfWavelength('in')).toEqual("19.69");
144+
expect(parseFloat(wavelength.toHalfWavelength('in'))).toEqual(19.69);
145+
});
146+
147+
test('should show one quarter wavelength in feet as 9.843', () => {
148+
wavelength.setPrecision(4);
149+
expect(wavelength.toQuarterWavelength('in')).toEqual("9.843");
150+
expect(parseFloat(wavelength.toQuarterWavelength('in'))).toEqual(9.843);
151+
});
152+
153+
154+
155+
156+
// test('if current is .5 and volts are 10 then resistance should be 20 and wattage should be 5', () => {
157+
// expect(OhmsLaw.calcWattsResistance(.5, 10)).toEqual({
158+
// resistance: 20,
159+
// watts: 5
160+
// })
161+
// });
162+
163+
});

0 commit comments

Comments
 (0)