-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathtest.battery.js
113 lines (89 loc) · 3.85 KB
/
test.battery.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
describe("battery", function () {
var battery = require("cordova/plugin/battery"),
exec = require("cordova/exec");
describe("when handling events", function () {
it("calls exec when subscribing", function () {
var cb = jasmine.createSpy("cb");
window.addEventListener("batterystatus", cb);
expect(exec).toHaveBeenCalledWith(battery._status, battery._error, "Battery", "start", []);
window.removeEventListener("batterystatus", cb);
});
it("only calls exec with start once", function () {
var cb1 = jasmine.createSpy("cb1"),
cb2 = jasmine.createSpy("cb2");
exec.reset();
window.addEventListener("batterystatus", cb1);
window.addEventListener("batterystatus", cb2);
expect(exec.callCount).toBe(1);
window.removeEventListener("batterystatus", cb1);
window.removeEventListener("batterystatus", cb2);
});
it("calls stop when all handlers are removed", function () {
var cb1 = jasmine.createSpy("cb1"),
cb2 = jasmine.createSpy("cb2");
window.addEventListener("batterystatus", cb1);
exec.reset();
window.removeEventListener("batterystatus", cb1);
expect(exec).toHaveBeenCalledWith(null, null, "Battery", "stop", []);
});
it("only calls exec with stop once", function () {
var cb1 = jasmine.createSpy("cb1"),
cb2 = jasmine.createSpy("cb2");
window.addEventListener("batterystatus", cb1);
window.addEventListener("batterystatus", cb2);
exec.reset();
window.removeEventListener("batterystatus", cb1);
window.removeEventListener("batterystatus", cb2);
expect(exec.callCount).toBe(1);
});
});
describe("when monitoring the status", function () {
var cordova = require('cordova');
beforeEach(function () {
spyOn(cordova, "fireWindowEvent");
});
//HACK: This is suspect that we can do this but
// it being public for now makes it very easy to
// test ;)
it("fires the battery status event", function () {
var info = {
level: 100,
isPlugged: false
};
battery._status(info);
expect(cordova.fireWindowEvent).toHaveBeenCalledWith("batterystatus", info);
});
it("doesn't fire the battery status event if the values haven't changed", function () {
var info = {
level: 95,
isPlugged: false
};
battery._status(info);
battery._status(info);
expect(cordova.fireWindowEvent.callCount).toBe(1);
});
it("fires the battery low event when the level is 20", function () {
var info = {
level: 20,
isPlugged: false
};
battery._status(info);
expect(cordova.fireWindowEvent).toHaveBeenCalledWith("batterylow", info);
});
it("fires the battery critical event when the level is 5", function () {
var info = {
level: 5,
isPlugged: false
};
battery._status(info);
expect(cordova.fireWindowEvent).toHaveBeenCalledWith("batterycritical", info);
});
});
it("logs errors to the console", function () {
//HACK: shouldn't be able to test like this but public is public ;)
var e = "We are traveling at warp speed. How did you manage to beam aboard this ship?";
spyOn(console, "log");
battery._error(e);
expect(console.log).toHaveBeenCalledWith("Error initializing Battery: " + e);
});
});