From 72c7f717a8f221c5de834cb4d062166b4e238d7d Mon Sep 17 00:00:00 2001 From: haad Date: Sun, 29 Jan 2017 19:21:23 +0200 Subject: [PATCH] Add test for sync() returning a hash --- src/OrbitDB.js | 2 +- test/client.test.js | 42 +++++++++++++++++++++++++++++++++--------- 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/src/OrbitDB.js b/src/OrbitDB.js index 99e9d11..bbb9816 100644 --- a/src/OrbitDB.js +++ b/src/OrbitDB.js @@ -92,7 +92,7 @@ class OrbitDB { // console.log(".MESSAGE", dbname, hash) const store = this.stores[dbname] store.sync(hash) - .then((res) => Cache.set(dbname, hash)) + .then((res) => Cache.set(dbname, res)) .then(() => this.events.emit('synced', dbname, hash)) .catch((e) => console.error(e.stack)) } diff --git a/test/client.test.js b/test/client.test.js index de14875..0c8fce9 100644 --- a/test/client.test.js +++ b/test/client.test.js @@ -532,15 +532,39 @@ const hasIpfsApiWithPubsub = (ipfs) => { assert.equal(_.isEqual(v1, val), true) })) - it('syncs databases', async(() => { - const db2 = await(client2.kvstore(channel, { subscribe: false })) - db2.delete() - db2.events.on('write', async((dbname, hash) => { - await(db.sync(hash)) - const value = db.get('key1') - assert.equal(value, 'hello2') - })) - })) + it('syncs databases', (done) => { + const db2 = client2.kvstore(channel, { subscribe: false, maxHistory: 0 }) + db2.events.on('write', (dbname, hash) => { + assert.equal(db.get('key1', null)) + + db.sync(hash).then((hash) => { + const value = db.get('key1') + assert.equal(value, 'hello2') + done() + }) + }) + db2.put('key1', 'hello2') + }) + + it('sync returns the updated log\'s hash', (done) => { + let firstHash, secondHash + const db2 = client2.kvstore(channel, { subscribe: false, maxHistory: 0 }) + db2.events.on('write', (dbname, hash) => { + db.sync(hash).then((hash) => { + const value1 = db.get('key1') + const value2 = db.get('key2') + assert.equal(value1, 'hello1') + assert.equal(value2, 'hello2') + assert.notEqual(firstHash, hash) + done() + }) + }) + db.events.on('write', (dbname, hash) => { + firstHash = hash + db2.put('key2', 'hello2') + }) + db.put('key1', 'hello1') + }) }) describe('Document Store - default index \'_id\'', function() {