Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wakhub committed Sep 28, 2016
1 parent 26da6b2 commit 4128be6
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 2 deletions.
4 changes: 2 additions & 2 deletions js/konashi.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ class Konashi {
return new Promise((resolve, reject) => {
that._c12c.pioSetting.readValue()
.then((v) => {
var data = v.getUint8();
var data = v.getUint8(0);
if (flag == Konashi.OUTPUT) {
data |= 0x01 << pin;
} else {
Expand All @@ -296,7 +296,7 @@ class Konashi {
return new Promise((resolve, reject) => {
that._c12c.pioPullUp.readValue()
.then(v => {
var data = v.getUint8();
var data = v.getUint8(0);
if (mode == Konashi.PULLUP) {
data |= 0x01 << pin;
} else {
Expand Down
15 changes: 15 additions & 0 deletions tests/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html> <html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>konashi web bluetooth tests</title>
<link rel="stylesheet" href="https://code.jquery.com/qunit/qunit-2.0.1.css">
</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture"></div>
<script src="https://code.jquery.com/qunit/qunit-2.0.1.js"></script>
<script src="../js/konashi.js"></script>
<script src="mock.js"></script>
<script src="tests.js"></script>
</body>
59 changes: 59 additions & 0 deletions tests/mock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
(() => {

class MockDevice {
constructor() {
this.gatt = new MockGatt();
}
}

class MockGatt {
connect() {
return new Promise((resolve, reject) => {
resolve(this);
});
}

getPrimaryService(uuid) {
return new MockKonashiService();
}
}

class MockKonashiService {
constructor() {
}
getCharacteristic(uuid) {
return new Promise((resolve, reject) => {
return resolve(new MockC12c());
});
}
}

class MockC12c {
readValue() {
return new Promise((resolve, reject) => {
var buff = new ArrayBuffer(1);
buff[0] = 1;
resolve(new DataView(buff));
});
}

writeValue() {
return new Promise((resolve, reject) => {
var buff = new ArrayBuffer(1);
buff[0] = 1;
resolve(new DataView(buff));
});
}
}

var bluetooth = {};

bluetooth.requestDevice = function(options) {
return new Promise((resolve, reject) => {
resolve(new MockDevice());
});
};

navigator.bluetooth = bluetooth;

})();
44 changes: 44 additions & 0 deletions tests/tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
QUnit.test('.find', (assert) => {
return Konashi.find(true, {}).then((_k) => {
assert.ok(_k instanceof Konashi);
});
});

Konashi.find(true).then((k) => {

QUnit.module('.pinMode', () => {
QUnit.test('INPUT', (assert) => {
return k.pinMode(k.PIO0, k.INPUT)
.then(() => k.pinMode(k.PIO1, k.INPUT))
.then(() => k.pinMode(k.PIO2, k.INPUT))
.then(() => k.pinMode(k.PIO3, k.INPUT))
.then(() => k.pinMode(k.PIO4, k.INPUT))
.then(() => {
assert.ok(true);
});
});
QUnit.test('OUTPUT', (assert) => {
return k.pinMode(k.PIO0, k.OUTPUT)
.then(() => k.pinMode(k.PIO1, k.OUTPUT))
.then(() => k.pinMode(k.PIO2, k.OUTPUT))
.then(() => k.pinMode(k.PIO3, k.OUTPUT))
.then(() => k.pinMode(k.PIO4, k.OUTPUT))
.then(() => {
assert.ok(true);
});
});
QUnit.test('INPUT & OUTPUT', (assert) => {
return k.pinMode(k.PIO0, k.INPUT)
.then(() => k.pinMode(k.PIO1, k.OUTPUT))
.then(() => k.pinMode(k.PIO2, k.INPUT))
.then(() => k.pinMode(k.PIO3, k.OUTPUT))
.then(() => k.pinMode(k.PIO4, k.INPUT))
.then(() => {
assert.ok(true);
});
});
}); // .pinMode


});

0 comments on commit 4128be6

Please sign in to comment.