UNPKG

core-state

Version:

179 lines (175 loc) 8.1 kB
teleport = require "../lib/teleport" portal = require "../lib/portal" Static = {} describe "basic tests",()-> it "create core state",(done)=> Static.coreState = new teleport.CoreState() done() it "cteate a property in default group and test it",(done)-> value = "testValue" coreState = Static.coreState Static.defaultName = coreState.createMaintainer("name",null,value) console.assert coreState.get("default.name").data is value console.assert coreState.get("default.name").path is "default.name" done() it "create a property in a custom group and test it",(done)-> name = "teleport" version = "0.0.0" coreState = Static.coreState Static.configName = coreState.createMaintainer("name","config",name) Static.configVersion = coreState.createMaintainer("version","config",version) console.assert (coreState.get "config.name").data is name console.assert (coreState.get "config.version").data is version done() it "create a property should emit init event",(done)-> testData = Math.random() Static.coreState.once "init",(info)-> console.assert info.path is "default.test" console.assert info.data is testData done() defaultTest = Static.coreState.createMaintainer "test","default",testData it "test get group",(done)-> configs = (Static.coreState.get "config") or [] console.assert configs.length is 2 done() it "unregister config.name by maintainer",(done)-> Static.coreState.unregister Static.configName console.assert not (Static.coreState.get "config.name") done() it "unregister config.version by path",(done)-> Static.coreState.unregister "config.version" console.assert not (Static.coreState.get "config.version") done() it "print not through",()-> Static.coreState.print() it "set value shoud trigger change",(done)-> newName = "scarlet" oldProperty = Static.coreState.get "default.name" Static.coreState.once "change",(info)-> console.assert info.data is newName,"name should equals new name" console.assert info.version is oldProperty.version + 1,"version should prompt" console.assert info.path is "default.name","path not match" done() Static.defaultName.setValue newName it "delete maintainer should trigger delete event",(done)-> Static.coreState.once "delete",(path)-> console.assert path is "default.name","should return correct path" done() Static.coreState.unregister Static.defaultName describe "test observe window",()-> name = "miku" version = "0.0.0" lastUpdate = Date.now() it "create core state and relative observe window should pass",(done)-> Static.coreState = new teleport.CoreState() Static.observeWindow = new teleport.ObserveWindow(Static.coreState) Static.configName = Static.coreState.createMaintainer "name","config","miku" Static.configVersion = Static.coreState.createMaintainer "version","config",version Static.configLastUpdate = Static.coreState.createMaintainer "lastUpdate","config",lastUpdate done() it "empty observe window should recieve nothing",(done)-> Static.observeWindow.on "delete",(done)-> done new Error "shouldnt recieve delete" Static.observeWindow.on "change",(done)-> done new Error "shouldnt recieve change" Static.observeWindow.on "init",(done)-> done new Error "shouldnt recieve init" testProperty = Static.coreState.createMaintainer "test","default","hehe" testProperty.setValue "change" testProperty.unregister() setTimeout ()=> Static.observeWindow.removeAllListeners() done() ,0 it "observe window should receive a property event if observe",(done)-> configNameProp = Static.coreState.get "config.name" ow = Static.observeWindow info = ow.observe "config.name" console.assert configNameProp.data is info.data ow.observe "test.value" initValue = "rin" newValue = "ren" ow.once "init",(prop)-> console.assert prop.data is initValue ow.once "change",(prop)-> console.assert prop.data is newValue ow.once "delete",(path)-> console.assert path is "test.value" done() valueProp = Static.coreState.createMaintainer "value","test",initValue valueProp.setValue newValue valueProp.unregister() it "stop observe should nolonger observe",(done)-> ow = Static.observeWindow ow.stopObserve "test.value" ow.once "init",()-> done new Error "shouldn't observe init event after stop observe" ow.once "change",()-> done new Error "shouldn't observe change event after stop observe" ow.once "delete",()-> done new Error "shouldn't observe delete event after stop observe" state = Static.coreState prop = state.createMaintainer "value","test","well just a value" prop.setValue "not important" prop.unregister() setTimeout ()=> ow.removeAllListeners() done() ,0 describe "test client side protal",()-> it "create facilities",(done)-> Static.coreState = new teleport.CoreState Static.observeWindow = new teleport.ObserveWindow(Static.coreState) Static.adapter = new portal.ExampleAdapter(Static.observeWindow) Static.portal = new portal.ObservePortal(Static.adapter) done() it "observe something",(done)-> master = {} value = "miku" Static.coreState.createMaintainer "name","config",value Static.portal.observeBy master,"config.name",(err)-> console.assert not err,"shouldnt' have error" console.assert Static.portal.get("config.name") is value,"value should match #{value} is #{Static.portal.get("config.name")}" done() it "observe all kind of changes",(done)-> master = {} Static.portal.observeBy master,"miku.name",()-> obj = {} Static.portal.on "change/miku.name",()-> obj.hasChange ?= 0 obj.hasChange += 1 Static.portal.on "init/miku.name",()-> obj.hasInit ?= 0 obj.hasInit += 1 Static.portal.on "delete/miku.name",()-> obj.hasDelete ?= 0 obj.hasDelete += 1 miku = Static.coreState.createMaintainer "name","miku","name" miku.setValue "831" miku.unregister() setTimeout ()-> console.assert obj.hasChange is 1,"change should be triggered once" console.assert obj.hasInit is 1,"init should be triggered once" console.assert obj.hasDelete is 1,"delete should be triggered once" done() ,0 it "stop observe should works",(done)-> Static.portal.removeAllListeners() master = {} obj = {} smile = Static.coreState.createMaintainer "smile","miku","hehe" dance = Static.coreState.createMaintainer "dance","miku","fly" Static.portal.observeBy master,"miku.dance",()-> Static.portal.on "change/miku.dance",()-> obj.dance ?= 0 obj.dance += 1 Static.portal.observeBy master,"miku.smile",()-> Static.portal.on "change/miku.smile",()-> done new Error "shoudn't get update if not observe" Static.portal.stopObserveBy master,"miku.smile" smile.setValue "haha" dance.setValue "swing" setTimeout ()-> console.assert obj.dance is 1,"stop observe one prop shouldn't influnce others" done() ,0