Skip to content

Commit

Permalink
Merge pull request #16 from QXIP/issue-15
Browse files Browse the repository at this point in the history
Add tests, add meaningful ack message on .delete
  • Loading branch information
sergibondarenko authored Feb 15, 2018
2 parents cc07908 + 69d05fe commit fd6be40
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 7 deletions.
11 changes: 11 additions & 0 deletions .nycrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"include": [
"src/**/*.js"
],
"exclude": [
"src/**/__test__/*.js"
],
"cache": true,
"all": true
}

14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ It is a lib to run [Gun](http://gun.js.org) host in Node.js

1. [Install](#install)
2. [Usage](#usage)
3. [Development](#development)

# Install
```
Expand Down Expand Up @@ -132,3 +133,16 @@ root: {
}
}
```
# Development
## Install libs
```
npm install -g eslint eslint-config-google nyc
```
## Test
```
npm run rest
```
## Run examples
```
node examples/usage.js
```
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"scripts": {
"start": "node bootstrap.js",
"test": "echo \"Error: no test specified\" && exit 1"
"test": "nyc ./node_modules/mocha/bin/mocha"
},
"repository": {
"type": "git",
Expand All @@ -27,5 +27,10 @@
"hapi": "16.6.2",
"lodash": "4.17.5",
"pem": "1.12.3"
},
"devDependencies": {
"expect.js": "0.3.1",
"mocha": "5.0.1",
"nyc": "11.4.1"
}
}
69 changes: 69 additions & 0 deletions src/__test__/gun-host.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
const {describe, it} = require('mocha');
const expect = require('expect.js');

const GunHost = require('../gun-host');

describe('Gun', function() {
const cluster = {
enabled: true,
name: '_test_gun',
priority_for_master: 0,
absent_time_for_delete: 86400,
absent_time: 15,
loop_delay: 5,
cert: {
selfsigned: true,
valid: 10,
key: null,
cert: null,
},
gun: {
port: 9000,
host: '0.0.0.0',
cache: 'data.json',
peers: ['https://localhost:9000/gun'],
},
host: {
id: '123',
name: 'trex',
priority: 0,
node: 'hosts',
},
};

const node = new GunHost({
peers: cluster.gun.peers,
rootNodeName: cluster.name,
});

it('start server', function() {
return node.start({
host: cluster.gun.host,
port: cluster.gun.port,
cache: cluster.gun.cache,
cert: cluster.cert,
}).then(function(resp) {
expect(resp).to.eql('gun server started successfully');
});
});

it('add node', function() {
return node.add(`${cluster.host.node}.${cluster.host.id}`, cluster.host).then(function(resp) {
expect(resp.err).to.be(null);
expect(resp.ok).to.be(1);
});
});

it('get node', function() {
return node.get(`${cluster.host.node}.${cluster.host.id}`).then(function(resp) {
expect(resp.id).to.be(cluster.host.id);
});
});

it('delete node', function() {
return node.delete(`${cluster.host.node}.${cluster.host.id}`).then(function(resp) {
expect(resp.err).to.be(null);
expect(resp.ok).to.be(1);
});
});
});
1 change: 1 addition & 0 deletions src/__test__/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require('./gun-host');
2 changes: 1 addition & 1 deletion src/gun-host.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class GunHost {
*
* @param {string} path for node
* @param {object} node to add
* @return {object} added node
* @return {object} ack
*/
add(path, node) {
return this.host.put(path, node);
Expand Down
10 changes: 5 additions & 5 deletions src/gun-promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class GunPromise {
*
* @param {string} pathway - a.b.c or a
* @param {object} value
* @return {object} new Gun node which contains value properties
* @return {object} ack
*/
put(pathway, value) {
return new Promise((resolve, reject) => {
Expand All @@ -72,14 +72,14 @@ class GunPromise {
* Check if value exists
*
* @param {string} pathway - a.b.c or a
* @return {boolean}
* @return {boolean} exist
*/
exists(pathway) {
return new Promise((resolve, reject) => {
this.node.path(pathway).val(function(value) {
resolve(value ? true : false);
});
});
});
});
}

/**
Expand Down Expand Up @@ -108,7 +108,7 @@ class GunPromise {
// 'null' put here to facilitate filtering deleted values
this.node.path(pathway).put(null);
// Gun bug, .put(cb) callback is not resolved if value is null: https://github.com/amark/gun/issues/453
resolve(null);
resolve({err: null, ok: 1});
});
}
}
Expand Down
1 change: 1 addition & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require('../src/__test__');

0 comments on commit fd6be40

Please sign in to comment.