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

170 lines
5.1 KiB
JavaScript

import assert, { doesNotMatch } 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' } }
}
const foundation = { planets: ['Trantor', 'Helcion'], persons: { giskard: { alias: 'daneel' }, hari: { name: 'seldon', planet: 'Helcion' } } }
class Test extends RxClass {
constructor (opts = {}) {
super(opts)
this.obj = obj
}
}
const opts = {
[rxopts]: {
// handler: () => {}
handler: (value) => { console.log('default subscription handler', value) }
}
}
const test = new Test(opts)
opts[rxopts].namespace = 'rx2'
opts[rxopts].getSetPrefix = '_'
opts[rxopts].getSetNamespace = '$'
const test2 = 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('can make a property reactive', function () {
// test.rxAdd('obj.today.bing')
// assert(test.isRx('obj.today.bing'))
// })
it('can make a property containing object reactive', function () {
// test.foundation = foundation
test.set('foundation.test', foundation, { traverse: false })
test2.$._set('foundation.test', foundation, { traverse: false })
// console.log(test.foundation)
// test.rx.set('foundation.test.persons.jimbo', { name: 'jim', plant: 'remulak' })
// console.log(test.foundation.test)
assert(test.rx.isRx('foundation.test'))
assert(test2.rx2.isRx('foundation.test'))
// assert(test.rx.isRx('foundation.test.persons'))
// assert(test.rx.isRx('foundation.test.persons.giskard.alias'))
// assert(!test.rx.isRx('foundation.test.persons.bogus'))
})
})
// 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
// describe('TODO: Misc', function () {
// it('get should return values of object nested props', function (done) {
// done()
// })
// it('set should make all nested leaves reactive if traverse is requested', function (done) {
// done()
// })
// it('should remove rx for property', function (done) {
// done()
// })
// }) // end hooks