Skip to content

Commit 16ffab0

Browse files
committed
stateVariables tested
1 parent 55995a9 commit 16ffab0

File tree

5 files changed

+83
-24
lines changed

5 files changed

+83
-24
lines changed

build/stateElement.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export var stateBehaviour;
66
})(stateBehaviour || (stateBehaviour = {}));
77
var _isCallback_locked = false;
88
const _transitions_callbackMap = new Map();
9+
// _transitions_callbackMap.clear(); // is this needed?? FIXME
910
export class StateTransition {
1011
constructor(NAME) {
1112
this.name = NAME;
@@ -14,10 +15,9 @@ export class StateTransition {
1415
if (typeof (this.name) !== "string")
1516
throw Error("Variable name must be a string.");
1617
}
17-
lock_callbacks(event) {
18+
lock_callbacks() {
1819
if (_isCallback_locked) {
19-
console.log('The following target has dispatched a ' + this.name + ' event during a UI update callback:');
20-
console.log(event.target);
20+
this.unlock_callbacks();
2121
throw Error('Forbidden multiple-update during an update callback loop.');
2222
}
2323
else
@@ -28,7 +28,7 @@ export class StateTransition {
2828
}
2929
updateHandler(event) {
3030
console.log('Handling event UPDATE from stateTransition: ' + this.name);
31-
this.lock_callbacks(event);
31+
this.lock_callbacks();
3232
this.usrDefined_transition(event);
3333
// loop over watchers callbacks
3434
for (let update_callback of this.callbackMap.values()) {
@@ -99,7 +99,7 @@ export class StateVariable extends StateTransition {
9999
}
100100
updateHandler(event) {
101101
console.log('Handling event UPDATE from state variable: ' + this.name);
102-
this.lock_callbacks(event);
102+
this.lock_callbacks();
103103
this.value = event.detail.value;
104104
// loop over watchers callbacks
105105
for (let update_callback of this.callbackMap.values()) {

src/stateElement.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ export enum stateBehaviour{
66
}
77

88
var _isCallback_locked = false;
9-
const _transitions_callbackMap : Map<Map<object,Function>,any> = new Map();
10-
9+
const _transitions_callbackMap : Map<Map<EventTarget,Function>,any> = new Map();
10+
// _transitions_callbackMap.clear(); // is this needed?? FIXME
1111

1212
export class StateTransition {
1313
name : string;
14-
callbackMap : Map<object,Function> ;
14+
callbackMap : Map<EventTarget,Function> ;
1515
usrDefined_transition: Function;
1616

1717
constructor(NAME:string){
@@ -22,10 +22,9 @@ export class StateTransition {
2222
if(typeof(this.name) !== "string") throw Error("Variable name must be a string.");
2323
}
2424

25-
lock_callbacks(event:CustomEvent){
25+
lock_callbacks(){
2626
if(_isCallback_locked) {
27-
console.log('The following target has dispatched a '+ this.name +' event during a UI update callback:');
28-
console.log(event.target);
27+
this.unlock_callbacks();
2928
throw Error('Forbidden multiple-update during an update callback loop.');
3029
}
3130
else _isCallback_locked = true;
@@ -38,7 +37,7 @@ export class StateTransition {
3837
updateHandler( event:CustomEvent) :void {
3938

4039
console.log('Handling event UPDATE from stateTransition: '+this.name);
41-
this.lock_callbacks(event);
40+
this.lock_callbacks();
4241

4342
this.usrDefined_transition(event);
4443

@@ -129,7 +128,7 @@ export class StateVariable extends StateTransition{
129128
updateHandler( event:CustomEvent) :void {
130129

131130
console.log('Handling event UPDATE from state variable: '+this.name);
132-
this.lock_callbacks(event);
131+
this.lock_callbacks();
133132

134133
this.value = event.detail.value;
135134

test/mocha_setup.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ import "../node_modules/mocha/mocha.js";
33

44
// here do the asserts imports
55
import state_var_assert from './stateVariable.test.js';
6+
import transitions_assert from './stateTransitions.test.js';
67

78
mocha.setup('bdd');
89

910
// here run asserts
1011
state_var_assert();
12+
transitions_assert();
1113

1214
mocha.checkLeaks();
1315
mocha.run();

test/stateTransitions.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import {StateTransition} from '../build/stateElement.js';
2+
3+
export default function (){
4+
// this is a must ;)
5+
localStorage.clear();
6+
7+
// create single transition and show that can attach, update, detach event
8+
// watch handler
9+
10+
// detach handler
11+
12+
// update handler
13+
// calles the user defined transition
14+
// calls its own updates
15+
// calls the global updates (autovalue works)
16+
// global map is cleared, if run multiple times the result is predictable
17+
18+
19+
// lock and unlock was tested
20+
}

test/stateVariable.test.js

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -94,17 +94,55 @@ export default function (){
9494
});
9595

9696
});
97-
// Input output for bool, string, object and number
98-
// write/read (getter/setter test) proper value, write read proper type,
99-
// throws when corrupted
100-
101-
// Update_handler
102-
// it does lock the callback, it throws
103-
// it updates the variable, updates only once
104-
// it runs the callbacks, set ad hoc, passes the right modified value
105-
// that unlocks
106-
107-
// Set_auto value willbe tested in Transitions
97+
describe('Update Handler',()=>{
98+
it('It locks',()=>{
99+
let test_string = new StateVariable("test_string",'string',"ciao");
100+
let test_number = new StateVariable("test_number",'number', 7);
101+
let double_mod = ()=>{ let pippo = new CustomEvent("bordello",{ bubbles:true, detail:{'value':872}}); test_number.updateHandler(pippo) };
102+
test_string.callbackMap.set(document.body, double_mod);
103+
104+
let throw_lock = ()=>{ let pippo = new CustomEvent("bordello",{ bubbles:true, detail:{'value':"ggg"}}); test_string.updateHandler(pippo)};
105+
106+
chai.assert.Throw(throw_lock);
107+
});
108+
109+
it('It updates all values and only once and unlocks',()=>{
110+
let test_string = new StateVariable("test_string",'string',"ciao");
111+
let test_number = new StateVariable("test_number",'number', 7);
112+
let test_object = new StateVariable("test_object",'object',{ciao:"bella", hey:67, poz:["cool", 9]});
113+
let test_bool = new StateVariable("test_bool",'boolean',true);
114+
115+
test_string.value = "hey";
116+
test_number.value = 123;
117+
test_object.value = {bla:67, ca:"ca"};
118+
test_bool.value = false;
119+
120+
let counter = 0;
121+
let counter_func = ()=>{ counter++; };
122+
test_string.callbackMap.set(document.body, counter_func);
123+
test_number.callbackMap.set(document.body, counter_func);
124+
test_object.callbackMap.set(document.body, counter_func);
125+
test_bool.callbackMap.set(document.body, counter_func);
126+
127+
let ev_n = new CustomEvent("bordello",{ bubbles:true, detail:{'value':321}});
128+
let ev_s = new CustomEvent("bordello",{ bubbles:true, detail:{'value':"qwerty"}});
129+
let ev_o = new CustomEvent("bordello",{ bubbles:true, detail:{'value':{a:1, b:2}}});
130+
let ev_b = new CustomEvent("bordello",{ bubbles:true, detail:{'value':true}});
131+
132+
test_string.updateHandler(ev_s);
133+
chai.assert.equal(test_string.value,"qwerty", "String " );
134+
chai.assert.equal(counter,1,"Called once ");
135+
test_number.updateHandler(ev_n);
136+
chai.assert.equal(test_number.value,321, "number " );
137+
chai.assert.equal(counter,2,"Called once ");
138+
test_object.updateHandler(ev_o);
139+
chai.assert.deepEqual(test_object.value,{a:1, b:2} , "Object " );
140+
chai.assert.equal(counter,3,"Called once ");
141+
test_bool.updateHandler(ev_b);
142+
chai.assert.equal(test_bool.value,true, "bool " );
143+
chai.assert.equal(counter,4,"Called once ");
144+
});
145+
});
108146

109147
});
110148
}

0 commit comments

Comments
 (0)