Skip to content

Commit 182da27

Browse files
authored
Merge pull request #55 from dtex/buttonTests
Button tests
2 parents 2993420 + 644556b commit 182da27

File tree

4 files changed

+299
-12
lines changed

4 files changed

+299
-12
lines changed

docs/button_index.js.html

+4-5
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,10 @@ <h1><span class="name">button/index.js</span></h1>
166166
const Provider = await getProvider(options, "builtin/digital");
167167

168168
let mode = Provider.Input;
169-
if (!options.isPullup) {
169+
if (typeof options.isPullup !== "undefined") {
170170
mode = Provider.InputPullUp;
171171
}
172-
if (!options.isPulldown) {
172+
if (typeof options.isPulldown !== "undefined") {
173173
mode = Provider.InputPullDown;
174174
}
175175

@@ -195,12 +195,12 @@ <h1><span class="name">button/index.js</span></h1>
195195
},
196196
downValue: {
197197
get: () => {
198-
return 1 ^ this.isPullup ^ this.invert;
198+
return 1 ^ this.#state.isPullup ^ this.invert;
199199
}
200200
},
201201
upValue: {
202202
get: () => {
203-
return 0 ^ this.isPullup ^ this.invert;
203+
return 0 ^ this.#state.isPullup ^ this.invert;
204204
}
205205
},
206206
holdtime: {
@@ -241,7 +241,6 @@ <h1><span class="name">button/index.js</span></h1>
241241
intialize(options, callback) { }
242242

243243
processRead() {
244-
245244
if (this.isOpen) {
246245
this.emit("open");
247246
timer.clearTimeout(this.#state.interval);

lib/button/index.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ class Button extends Emitter {
7070
const Provider = await getProvider(options, "builtin/digital");
7171

7272
let mode = Provider.Input;
73-
if (!options.isPullup) {
73+
if (typeof options.isPullup !== "undefined") {
7474
mode = Provider.InputPullUp;
7575
}
76-
if (!options.isPulldown) {
76+
if (typeof options.isPulldown !== "undefined") {
7777
mode = Provider.InputPullDown;
7878
}
7979

@@ -99,12 +99,12 @@ class Button extends Emitter {
9999
},
100100
downValue: {
101101
get: () => {
102-
return 1 ^ this.isPullup ^ this.invert;
102+
return 1 ^ this.#state.isPullup ^ this.invert;
103103
}
104104
},
105105
upValue: {
106106
get: () => {
107-
return 0 ^ this.isPullup ^ this.invert;
107+
return 0 ^ this.#state.isPullup ^ this.invert;
108108
}
109109
},
110110
holdtime: {
@@ -145,7 +145,6 @@ class Button extends Emitter {
145145
intialize(options, callback) { }
146146

147147
processRead() {
148-
149148
if (this.isOpen) {
150149
this.emit("open");
151150
timer.clearTimeout(this.#state.interval);

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "j5e",
3-
"version": "0.4.2",
3+
"version": "0.4.3",
44
"description": "j5e is a device framework built for ECMA TC-53's IO pattern",
55
"main": "index.js",
66
"exports": {

test/button.js

+290-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import assert from "assert";
22
import sinon from "sinon";
3+
import chai from "chai";
34

45
import { Digital } from "@dtex/mock-io";
56
import Button from "j5e/button";
@@ -16,6 +17,103 @@ describe("Button", function() {
1617
assert.equal(button instanceof Button, true);
1718
assert.equal(button.io instanceof Digital, true);
1819
});
20+
21+
describe("Options", function() {
22+
23+
describe("invert", function() {
24+
25+
it("should invert state when the invert option is true", async function() {
26+
const button = await new Button({
27+
pin: 13,
28+
io: Digital,
29+
invert: true
30+
});
31+
32+
button.io.write(0);
33+
assert.equal(button.isClosed, true);
34+
assert.equal(button.isOpen, false);
35+
36+
button.io.write(1);
37+
assert.equal(button.isClosed, false);
38+
assert.equal(button.isOpen, true);
39+
40+
});
41+
});
42+
43+
describe("isPullup", function() {
44+
45+
it("should invert state when the isPullup option is true", async function() {
46+
const button = await new Button({
47+
pin: 13,
48+
io: Digital,
49+
isPullup: true
50+
});
51+
52+
button.io.write(0);
53+
assert.equal(button.isClosed, true);
54+
assert.equal(button.isOpen, false);
55+
56+
button.io.write(1);
57+
assert.equal(button.isClosed, false);
58+
assert.equal(button.isOpen, true);
59+
60+
});
61+
62+
it("should not invert state when the isPullup and invert options are true", async function() {
63+
const button = await new Button({
64+
pin: 13,
65+
io: Digital,
66+
isPullup: true,
67+
invert: true
68+
});
69+
70+
button.io.write(1);
71+
assert.equal(button.isClosed, true);
72+
assert.equal(button.isOpen, false);
73+
74+
button.io.write(0);
75+
assert.equal(button.isClosed, false);
76+
assert.equal(button.isOpen, true);
77+
78+
});
79+
80+
});
81+
82+
describe("isPulldown", function() {
83+
84+
it("should not invert state when the isPulldown option is true", async function() {
85+
const button = await new Button({
86+
pin: 13,
87+
io: Digital,
88+
isPulldown: true
89+
});
90+
91+
button.io.write(1);
92+
assert.equal(button.isClosed, true);
93+
assert.equal(button.isOpen, false);
94+
95+
button.io.write(0);
96+
assert.equal(button.isClosed, false);
97+
assert.equal(button.isOpen, true);
98+
99+
});
100+
});
101+
102+
describe("holdtime", function() {
103+
104+
it("should set the holdtime state when passed a value in options", async function() {
105+
const button = await new Button({
106+
pin: 13,
107+
io: Digital,
108+
holdtime: 2000
109+
});
110+
111+
assert.equal(button.holdtime, 2000);
112+
113+
});
114+
});
115+
116+
});
19117
});
20118

21119
describe("Properties", function() {
@@ -70,6 +168,198 @@ describe("Button", function() {
70168
assert.equal(button.isOpen, true);
71169
});
72170
});
171+
172+
describe("holdtime", function() {
173+
174+
it("should be settable and getable", async function() {
175+
const button = await new Button({
176+
pin: 13,
177+
io: Digital
178+
});
179+
180+
assert.equal(button.holdtime, 500);
181+
button.holdtime = 1000;
182+
assert.equal(button.holdtime, 1000);
183+
184+
});
185+
186+
it("should emit hold event after 500ms", async function() {
187+
188+
const clock = sinon.useFakeTimers();
189+
const holdSpy = sinon.spy();
190+
const button = await new Button({
191+
pin: 13,
192+
io: Digital
193+
});
194+
195+
button.on("hold", holdSpy);
196+
197+
clock.tick(1);
198+
button.io.write(1);
199+
assert.equal(holdSpy.callCount, 0);
200+
clock.tick(499);
201+
assert.equal(holdSpy.callCount, 0);
202+
clock.tick(10);
203+
assert.equal(holdSpy.callCount, 1);
204+
205+
clock.restore();
206+
207+
});
208+
209+
it("should emit hold event after 2000ms", async function() {
210+
211+
const clock = sinon.useFakeTimers();
212+
const holdSpy = sinon.spy();
213+
const button = await new Button({
214+
pin: 13,
215+
io: Digital,
216+
holdtime: 2000
217+
});
218+
219+
button.on("hold", holdSpy);
220+
221+
clock.tick(1);
222+
button.io.write(1);
223+
assert.equal(holdSpy.callCount, 0);
224+
clock.tick(1999);
225+
assert.equal(holdSpy.callCount, 0);
226+
clock.tick(10);
227+
assert.equal(holdSpy.callCount, 1);
228+
229+
clock.restore();
230+
231+
});
232+
233+
234+
235+
});
236+
237+
describe("downValue", function() {
238+
239+
it("should have the correct default value", async function() {
240+
241+
const button = await new Button({
242+
pin: 13,
243+
io: Digital
244+
});
245+
246+
assert.equal(button.downValue, 1);
247+
248+
});
249+
250+
it("should have the correct value when invert is true", async function() {
251+
252+
const button = await new Button({
253+
pin: 13,
254+
io: Digital,
255+
invert: true
256+
});
257+
258+
assert.equal(button.downValue, 0);
259+
260+
});
261+
262+
it("should have the correct value when isPullup is true", async function() {
263+
264+
const button = await new Button({
265+
pin: 13,
266+
io: Digital,
267+
isPullup: true
268+
});
269+
270+
assert.equal(button.downValue, 0);
271+
272+
});
273+
274+
it("should have the correct value when isPullup and invert are true", async function() {
275+
276+
const button = await new Button({
277+
pin: 13,
278+
io: Digital,
279+
isPullup: true,
280+
invert: true
281+
});
282+
283+
assert.equal(button.downValue, 1);
284+
285+
});
286+
287+
it("should not be settable", async function() {
288+
const button = await new Button({
289+
pin: 13,
290+
io: Digital
291+
});
292+
293+
chai.expect(() => {
294+
button.downValue = 1;
295+
}).to.throw(TypeError);
296+
});
297+
298+
});
299+
300+
describe("upValue", function() {
301+
302+
it("should have the correct default value", async function() {
303+
304+
const button = await new Button({
305+
pin: 13,
306+
io: Digital
307+
});
308+
309+
assert.equal(button.upValue, 0);
310+
311+
});
312+
313+
it("should have the correct value when invert is true", async function() {
314+
315+
const button = await new Button({
316+
pin: 13,
317+
io: Digital,
318+
invert: true
319+
});
320+
321+
assert.equal(button.upValue, 1);
322+
323+
});
324+
325+
it("should have the correct value when isPullup is true", async function() {
326+
327+
const button = await new Button({
328+
pin: 13,
329+
io: Digital,
330+
isPullup: true
331+
});
332+
333+
assert.equal(button.upValue, 1);
334+
335+
});
336+
337+
it("should have the correct value when isPullup and invert are true", async function() {
338+
339+
const button = await new Button({
340+
pin: 13,
341+
io: Digital,
342+
isPullup: true,
343+
invert: true
344+
});
345+
346+
assert.equal(button.upValue, 0);
347+
348+
});
349+
350+
it("should not be settable", async function() {
351+
const button = await new Button({
352+
pin: 13,
353+
io: Digital
354+
});
355+
356+
chai.expect(() => {
357+
button.upValue = 1;
358+
}).to.throw(TypeError);
359+
});
360+
361+
});
362+
73363
});
74364

75365
describe("Events", function() {
@@ -167,5 +457,4 @@ describe("Button", function() {
167457
});
168458
});
169459

170-
171460
});

0 commit comments

Comments
 (0)