uci-utils-rx-class/test/rxclass.test.js

138 lines
3.6 KiB
JavaScript

import assert from 'assert'
import { RxClass } from '../src/rx-class.js'
const rxopts = Symbol.for('rx-class-opts')
const obj = {
tomorrow: { bar: 'fight', foo: 'fighters' },
today: { bing: 'bing', bong: { who: 'first', what: 'second' } }
}
class Test extends RxClass {
constructor (opts = {}) {
super(opts)
this.obj = obj
}
}
const opts = {
[rxopts]: {
skip: 1,
handler: () => {}
}
}
const test = new Test(opts)
describe('Reactive Class', function () {
it('Can be extended', function () {
assert.deepStrictEqual(test.obj, obj)
})
it('Can get non rx deep value', function () {
assert.strictEqual(test.obj.today.bing, 'bing')
assert.strictEqual(test.get('obj.today.bing'), 'bing')
assert(!test.isRx('obj.today.bing'))
})
it('rx added to value', function () {
test.rxAdd('obj.today.bing')
assert.strictEqual(test.obj.today.bing, 'bing')
assert.strictEqual(test.get('obj.today.bing'), 'bing')
assert(test.isRx('obj.today.bing'))
})
})
describe('Emitted Events', function () {
function etest (event, value, path, done) {
test.once(event, (v, p) => {
// console.log('fired:', event, value, path)
assert.strictEqual(v, value)
assert.strictEqual(p, path)
clearTimeout(timeout)
done()
})
const timeout = setTimeout(() => {
assert(false, `Event ${event} did not fire in 1000 ms.`)
done()
}, 1000)
test.set(path, value)
}
const path = 'obj.today.bing'
it('should emit global default event', function (done) {
const event = 'changed'
etest(event, event, path, done)
})
it('should emit path if stringable', function (done) {
const event = path
etest(event, event, path, done)
})
it('should emit the key name of value being changed', function (done) {
const event = path.split('.').pop()
etest(event, event, path, done)
})
it('should emit a custom set event for key/value change after force rxAdd', function (done) {
const event = 'testing'
test.rxAdd('obj.today.bing', { force: true, event: 'testing' })
etest(event, event, path, done)
})
}) // end emits
describe('Subscriptions', function () {
const subsHandler = (value, timeout, name, done, v) => {
assert.strictEqual(v, value)
clearTimeout(timeout)
test.rxRemoveSubs(name)
done()
}
function subs (name, path, value, done, handler) {
const timeout = setTimeout(() => {
assert(false, 'subscription handler did not react in 1000 ms.')
done()
}, 1000)
const subscribe = handler ? { [handler]: subsHandler.bind(null, value, timeout, name, done) } : {}
test.rxAdd(path, { force: true, subscribe: subscribe })
if (!handler) {
test.rxSubscribe('obj.today.bing',
subsHandler.bind(null, value, timeout, name, done)
, name)
}
test.set(path, value)
}
const path = 'obj.today.bing'
it('should react to default subscription', function (done) {
const name = '_default_'
subs(name, path, name, done, name)
})
it('should react to property/key subscription', function (done) {
const name = 'keytest'
subs(name, path, name, done, name)
})
it('should react to new subscription', function (done) {
const name = 'atest'
subs(name, path, name, done)
})
}) // end subscriptions
describe('TODO: Amend,Hooks,Operators', function () {
it('should run a default hook', function (done) {
done()
})
it('should run a custom hook', function (done) {
done()
})
it('should support custom rxjs operators', function (done) {
done()
})
it('should amend a value that is set', function (done) {
done()
})
}) // end hooks