Update gunsmith test
This commit is contained in:
parent
51bf5904db
commit
93533660bd
1 changed files with 87 additions and 85 deletions
|
|
@ -6,6 +6,7 @@ const Gun = require('./GunSmith')
|
||||||
const words = require('random-words')
|
const words = require('random-words')
|
||||||
const fs = require('fs')
|
const fs = require('fs')
|
||||||
const debounce = require('lodash/debounce')
|
const debounce = require('lodash/debounce')
|
||||||
|
const once = require('lodash/once')
|
||||||
const expect = require('expect')
|
const expect = require('expect')
|
||||||
|
|
||||||
const logger = require('../../config/log')
|
const logger = require('../../config/log')
|
||||||
|
|
@ -36,6 +37,78 @@ describe('gun smith', () => {
|
||||||
Gun.kill()
|
Gun.kill()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// **************************************************************************
|
||||||
|
// These tests are long but we run them first to detect if the re-forging
|
||||||
|
// logic is flawed and affecting functionality.
|
||||||
|
// **************************************************************************
|
||||||
|
|
||||||
|
it('writes object items into sets and correctly populates item._.get with the newly created id', done => {
|
||||||
|
const node = instance.get(words()).get(words())
|
||||||
|
|
||||||
|
const obj = {
|
||||||
|
a: 1,
|
||||||
|
b: 'hello'
|
||||||
|
}
|
||||||
|
|
||||||
|
const item = node.set(obj)
|
||||||
|
|
||||||
|
node.get(item._.get).once(data => {
|
||||||
|
expect(removeBuiltInGunProps(data)).toEqual(obj)
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('provides an special once() that restarts gun until a value is fetched', done => {
|
||||||
|
const a = words()
|
||||||
|
const b = words()
|
||||||
|
const node = instance.get(a).get(b)
|
||||||
|
const value = words()
|
||||||
|
|
||||||
|
node.specialOnce(data => {
|
||||||
|
expect(data).toEqual(value)
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
|
||||||
|
setTimeout(() => {
|
||||||
|
node.put(value)
|
||||||
|
}, 30000)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('provides an special then() that restarts gun until a value is fetched', async () => {
|
||||||
|
const a = words()
|
||||||
|
const b = words()
|
||||||
|
const node = instance.get(a).get(b)
|
||||||
|
const value = words()
|
||||||
|
|
||||||
|
setTimeout(() => {
|
||||||
|
node.put(value)
|
||||||
|
}, 30000)
|
||||||
|
|
||||||
|
const res = await node.specialThen()
|
||||||
|
|
||||||
|
expect(res).toBe(value)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('provides an special on() that restarts gun when a value has not been obtained in a determinate amount of time', done => {
|
||||||
|
const node = instance.get(words()).get(words())
|
||||||
|
|
||||||
|
const secondValue = words()
|
||||||
|
|
||||||
|
const onceDone = once(done)
|
||||||
|
|
||||||
|
node.specialOn(
|
||||||
|
debounce(data => {
|
||||||
|
if (data === secondValue) {
|
||||||
|
onceDone()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
)
|
||||||
|
|
||||||
|
setTimeout(() => {
|
||||||
|
node.put(secondValue)
|
||||||
|
}, 32000)
|
||||||
|
})
|
||||||
|
|
||||||
it('puts a true and reads it with once()', done => {
|
it('puts a true and reads it with once()', done => {
|
||||||
logger.info('puts a true and reads it with once()')
|
logger.info('puts a true and reads it with once()')
|
||||||
const a = words()
|
const a = words()
|
||||||
|
|
@ -313,7 +386,8 @@ describe('gun smith', () => {
|
||||||
|
|
||||||
let checked = 0
|
let checked = 0
|
||||||
|
|
||||||
node.on(data => {
|
node.on(
|
||||||
|
debounce(data => {
|
||||||
checked++
|
checked++
|
||||||
if (checked === 1) {
|
if (checked === 1) {
|
||||||
expect(removeBuiltInGunProps(data)).toEqual(a)
|
expect(removeBuiltInGunProps(data)).toEqual(a)
|
||||||
|
|
@ -324,86 +398,14 @@ describe('gun smith', () => {
|
||||||
done()
|
done()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
)
|
||||||
|
|
||||||
node.put(a)
|
node.put(a)
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
node.put(b)
|
node.put(b)
|
||||||
}, 400)
|
}, 800)
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
node.put(c)
|
node.put(c)
|
||||||
}, 800)
|
}, 1200)
|
||||||
})
|
|
||||||
|
|
||||||
// **************************************************************************
|
|
||||||
// ** Long tests below
|
|
||||||
// **************************************************************************
|
|
||||||
|
|
||||||
it('writes object items into sets and correctly populates item._.get with the newly created id', done => {
|
|
||||||
const node = instance.get(words()).get(words())
|
|
||||||
|
|
||||||
const obj = {
|
|
||||||
a: 1,
|
|
||||||
b: 'hello'
|
|
||||||
}
|
|
||||||
|
|
||||||
const item = node.set(obj)
|
|
||||||
|
|
||||||
node.get(item._.get).once(data => {
|
|
||||||
expect(removeBuiltInGunProps(data)).toEqual(obj)
|
|
||||||
done()
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
it('provides an special once() that restarts gun until a value is fetched', done => {
|
|
||||||
const a = words()
|
|
||||||
const b = words()
|
|
||||||
const node = instance.get(a).get(b)
|
|
||||||
const value = words()
|
|
||||||
|
|
||||||
node.specialOnce(data => {
|
|
||||||
expect(data).toEqual(value)
|
|
||||||
done()
|
|
||||||
})
|
|
||||||
|
|
||||||
setTimeout(() => {
|
|
||||||
node.put(value)
|
|
||||||
}, 30000)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('provides an special then() that restarts gun until a value is fetched', async () => {
|
|
||||||
const a = words()
|
|
||||||
const b = words()
|
|
||||||
const node = instance.get(a).get(b)
|
|
||||||
const value = words()
|
|
||||||
|
|
||||||
setTimeout(() => {
|
|
||||||
node.put(value)
|
|
||||||
}, 30000)
|
|
||||||
|
|
||||||
const res = await node.specialThen()
|
|
||||||
|
|
||||||
expect(res).toBe(value)
|
|
||||||
})
|
|
||||||
|
|
||||||
it('provides an special on() that restarts gun when a value has not been obtained in a determinate amount of time', done => {
|
|
||||||
// Kinda crappy test, this should be easier to test in real usage.
|
|
||||||
const initialProcCounter = Gun._getProcCounter()
|
|
||||||
|
|
||||||
const node = instance.get(words()).get(words())
|
|
||||||
|
|
||||||
const secondValue = words()
|
|
||||||
|
|
||||||
node.specialOn(
|
|
||||||
debounce(data => {
|
|
||||||
if (data === secondValue) {
|
|
||||||
expect(Gun._getProcCounter()).toEqual(initialProcCounter + 1)
|
|
||||||
done()
|
|
||||||
}
|
|
||||||
})
|
|
||||||
)
|
|
||||||
|
|
||||||
setTimeout(() => {
|
|
||||||
node.put(secondValue)
|
|
||||||
}, 32000)
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue