Skip to content

Commit

Permalink
Merge pull request #55 from dtex/buttonTests
Browse files Browse the repository at this point in the history
Button tests
  • Loading branch information
dtex authored Jul 4, 2020
2 parents 2993420 + 644556b commit 182da27
Show file tree
Hide file tree
Showing 4 changed files with 299 additions and 12 deletions.
9 changes: 4 additions & 5 deletions docs/button_index.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,10 @@ <h1><span class="name">button/index.js</span></h1>
const Provider = await getProvider(options, "builtin/digital");

let mode = Provider.Input;
if (!options.isPullup) {
if (typeof options.isPullup !== "undefined") {
mode = Provider.InputPullUp;
}
if (!options.isPulldown) {
if (typeof options.isPulldown !== "undefined") {
mode = Provider.InputPullDown;
}

Expand All @@ -195,12 +195,12 @@ <h1><span class="name">button/index.js</span></h1>
},
downValue: {
get: () => {
return 1 ^ this.isPullup ^ this.invert;
return 1 ^ this.#state.isPullup ^ this.invert;
}
},
upValue: {
get: () => {
return 0 ^ this.isPullup ^ this.invert;
return 0 ^ this.#state.isPullup ^ this.invert;
}
},
holdtime: {
Expand Down Expand Up @@ -241,7 +241,6 @@ <h1><span class="name">button/index.js</span></h1>
intialize(options, callback) { }

processRead() {

if (this.isOpen) {
this.emit("open");
timer.clearTimeout(this.#state.interval);
Expand Down
9 changes: 4 additions & 5 deletions lib/button/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ class Button extends Emitter {
const Provider = await getProvider(options, "builtin/digital");

let mode = Provider.Input;
if (!options.isPullup) {
if (typeof options.isPullup !== "undefined") {
mode = Provider.InputPullUp;
}
if (!options.isPulldown) {
if (typeof options.isPulldown !== "undefined") {
mode = Provider.InputPullDown;
}

Expand All @@ -99,12 +99,12 @@ class Button extends Emitter {
},
downValue: {
get: () => {
return 1 ^ this.isPullup ^ this.invert;
return 1 ^ this.#state.isPullup ^ this.invert;
}
},
upValue: {
get: () => {
return 0 ^ this.isPullup ^ this.invert;
return 0 ^ this.#state.isPullup ^ this.invert;
}
},
holdtime: {
Expand Down Expand Up @@ -145,7 +145,6 @@ class Button extends Emitter {
intialize(options, callback) { }

processRead() {

if (this.isOpen) {
this.emit("open");
timer.clearTimeout(this.#state.interval);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "j5e",
"version": "0.4.2",
"version": "0.4.3",
"description": "j5e is a device framework built for ECMA TC-53's IO pattern",
"main": "index.js",
"exports": {
Expand Down
291 changes: 290 additions & 1 deletion test/button.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import assert from "assert";
import sinon from "sinon";
import chai from "chai";

import { Digital } from "@dtex/mock-io";
import Button from "j5e/button";
Expand All @@ -16,6 +17,103 @@ describe("Button", function() {
assert.equal(button instanceof Button, true);
assert.equal(button.io instanceof Digital, true);
});

describe("Options", function() {

describe("invert", function() {

it("should invert state when the invert option is true", async function() {
const button = await new Button({
pin: 13,
io: Digital,
invert: true
});

button.io.write(0);
assert.equal(button.isClosed, true);
assert.equal(button.isOpen, false);

button.io.write(1);
assert.equal(button.isClosed, false);
assert.equal(button.isOpen, true);

});
});

describe("isPullup", function() {

it("should invert state when the isPullup option is true", async function() {
const button = await new Button({
pin: 13,
io: Digital,
isPullup: true
});

button.io.write(0);
assert.equal(button.isClosed, true);
assert.equal(button.isOpen, false);

button.io.write(1);
assert.equal(button.isClosed, false);
assert.equal(button.isOpen, true);

});

it("should not invert state when the isPullup and invert options are true", async function() {
const button = await new Button({
pin: 13,
io: Digital,
isPullup: true,
invert: true
});

button.io.write(1);
assert.equal(button.isClosed, true);
assert.equal(button.isOpen, false);

button.io.write(0);
assert.equal(button.isClosed, false);
assert.equal(button.isOpen, true);

});

});

describe("isPulldown", function() {

it("should not invert state when the isPulldown option is true", async function() {
const button = await new Button({
pin: 13,
io: Digital,
isPulldown: true
});

button.io.write(1);
assert.equal(button.isClosed, true);
assert.equal(button.isOpen, false);

button.io.write(0);
assert.equal(button.isClosed, false);
assert.equal(button.isOpen, true);

});
});

describe("holdtime", function() {

it("should set the holdtime state when passed a value in options", async function() {
const button = await new Button({
pin: 13,
io: Digital,
holdtime: 2000
});

assert.equal(button.holdtime, 2000);

});
});

});
});

describe("Properties", function() {
Expand Down Expand Up @@ -70,6 +168,198 @@ describe("Button", function() {
assert.equal(button.isOpen, true);
});
});

describe("holdtime", function() {

it("should be settable and getable", async function() {
const button = await new Button({
pin: 13,
io: Digital
});

assert.equal(button.holdtime, 500);
button.holdtime = 1000;
assert.equal(button.holdtime, 1000);

});

it("should emit hold event after 500ms", async function() {

const clock = sinon.useFakeTimers();
const holdSpy = sinon.spy();
const button = await new Button({
pin: 13,
io: Digital
});

button.on("hold", holdSpy);

clock.tick(1);
button.io.write(1);
assert.equal(holdSpy.callCount, 0);
clock.tick(499);
assert.equal(holdSpy.callCount, 0);
clock.tick(10);
assert.equal(holdSpy.callCount, 1);

clock.restore();

});

it("should emit hold event after 2000ms", async function() {

const clock = sinon.useFakeTimers();
const holdSpy = sinon.spy();
const button = await new Button({
pin: 13,
io: Digital,
holdtime: 2000
});

button.on("hold", holdSpy);

clock.tick(1);
button.io.write(1);
assert.equal(holdSpy.callCount, 0);
clock.tick(1999);
assert.equal(holdSpy.callCount, 0);
clock.tick(10);
assert.equal(holdSpy.callCount, 1);

clock.restore();

});



});

describe("downValue", function() {

it("should have the correct default value", async function() {

const button = await new Button({
pin: 13,
io: Digital
});

assert.equal(button.downValue, 1);

});

it("should have the correct value when invert is true", async function() {

const button = await new Button({
pin: 13,
io: Digital,
invert: true
});

assert.equal(button.downValue, 0);

});

it("should have the correct value when isPullup is true", async function() {

const button = await new Button({
pin: 13,
io: Digital,
isPullup: true
});

assert.equal(button.downValue, 0);

});

it("should have the correct value when isPullup and invert are true", async function() {

const button = await new Button({
pin: 13,
io: Digital,
isPullup: true,
invert: true
});

assert.equal(button.downValue, 1);

});

it("should not be settable", async function() {
const button = await new Button({
pin: 13,
io: Digital
});

chai.expect(() => {
button.downValue = 1;
}).to.throw(TypeError);
});

});

describe("upValue", function() {

it("should have the correct default value", async function() {

const button = await new Button({
pin: 13,
io: Digital
});

assert.equal(button.upValue, 0);

});

it("should have the correct value when invert is true", async function() {

const button = await new Button({
pin: 13,
io: Digital,
invert: true
});

assert.equal(button.upValue, 1);

});

it("should have the correct value when isPullup is true", async function() {

const button = await new Button({
pin: 13,
io: Digital,
isPullup: true
});

assert.equal(button.upValue, 1);

});

it("should have the correct value when isPullup and invert are true", async function() {

const button = await new Button({
pin: 13,
io: Digital,
isPullup: true,
invert: true
});

assert.equal(button.upValue, 0);

});

it("should not be settable", async function() {
const button = await new Button({
pin: 13,
io: Digital
});

chai.expect(() => {
button.upValue = 1;
}).to.throw(TypeError);
});

});

});

describe("Events", function() {
Expand Down Expand Up @@ -167,5 +457,4 @@ describe("Button", function() {
});
});


});

0 comments on commit 182da27

Please sign in to comment.