|
1 | | -import {StateVariable, stateBehaviour} from '../build/stateElement.js'; |
| 1 | +import {StateVariable} from '../build/stateElement.js'; |
2 | 2 |
|
3 | 3 | export default function (){ |
4 | | - describe("doesn't crash",()=>{ |
5 | | - it("bella",()=>{ |
6 | | - let test_state = new StateVariable("test",'string',stateBehaviour['NORMAL'],); |
7 | | - chai.assert.equal(localStorage.getItem('test'), '100', "var is in local storage" ); |
| 4 | + // this is a must ;) |
| 5 | + localStorage.clear(); |
| 6 | + |
| 7 | + describe("StateVariable Test:",()=>{ |
| 8 | + describe("Instantiation",()=>{ |
| 9 | + |
| 10 | + it("Instantiated to the store,proper name and initial value",()=>{ |
| 11 | + let test_string = new StateVariable("test_string",'string',"ciao"); |
| 12 | + let test_number = new StateVariable("test_number",'number', 7); |
| 13 | + let test_object = new StateVariable("test_object",'object',{ciao:"bella", hey:67, poz:["cool", 9]}); |
| 14 | + let test_bool = new StateVariable("test_bool",'boolean',true); |
| 15 | + |
| 16 | + chai.assert.equal(localStorage.getItem('test_string'), 'ciao', "string not in local storage" ); |
| 17 | + chai.assert.equal(localStorage.getItem('test_number'), '7', "number not in local storage" ); |
| 18 | + chai.assert.equal(localStorage.getItem('test_object'), JSON.stringify({ciao:"bella", hey:67, poz:["cool", 9]}), "object not in local storage" ); |
| 19 | + chai.assert.equal(localStorage.getItem('test_bool'), 'true', "bool not in local storage" ); |
| 20 | + }); |
| 21 | + |
| 22 | + it("If exist do not overide",()=>{ |
| 23 | + let test_string2 = new StateVariable("test_string",'string',"ciao2"); |
| 24 | + let test_number2 = new StateVariable("test_number",'number', 14); |
| 25 | + let test_object2 = new StateVariable("test_object",'object',{ciao:"bella2", hey:67, poz:["cool", 9]}); |
| 26 | + let test_bool2 = new StateVariable("test_bool",'boolean',false); |
| 27 | + chai.assert.equal(localStorage.getItem('test_string'), 'ciao', "string overridden" ); |
| 28 | + chai.assert.equal(localStorage.getItem('test_number'), '7', "number overridden" ); |
| 29 | + chai.assert.equal(localStorage.getItem('test_object'), JSON.stringify({ciao:"bella", hey:67, poz:["cool", 9]}), "object overridden" ); |
| 30 | + chai.assert.equal(localStorage.getItem('test_bool'), 'true', "bool overridden" ); |
| 31 | + }); |
| 32 | + |
| 33 | + it("throws for wrong init type",()=>{ |
| 34 | + let pollo; |
| 35 | + let test_function = () =>{let a = new StateVariable("test_function",'function',"ciao"); }; |
| 36 | + let test_function2 = () =>{let a = new StateVariable("test_function",'string',test_function); }; |
| 37 | + let test_function3 = () =>{let a = new StateVariable("test_function",'bool',true); }; |
| 38 | + let test_function6 = () =>{let a = new StateVariable("test_function",'boolean',5); }; |
| 39 | + let test_function5 = () =>{let a = new StateVariable("test_function",'number',true); }; |
| 40 | + let test_function4 = () =>{let a = new StateVariable("test_function",'string',8); }; |
| 41 | + let test_function7 = () =>{let a = new StateVariable("test_function",'string',pollo); }; |
| 42 | + |
| 43 | + chai.assert.Throw(test_function); |
| 44 | + chai.assert.Throw(test_function2); |
| 45 | + chai.assert.Throw(test_function3); |
| 46 | + chai.assert.Throw(test_function4); |
| 47 | + chai.assert.Throw(test_function5); |
| 48 | + chai.assert.Throw(test_function6); |
| 49 | + chai.assert.Throw(test_function7); |
| 50 | + }); |
8 | 51 | }); |
| 52 | + describe('Input Output',()=>{ |
| 53 | + |
| 54 | + it("Getter and Setters return proper value and type.",()=>{ |
| 55 | + let test_string = new StateVariable("test_string",'string',"ciao"); |
| 56 | + let test_number = new StateVariable("test_number",'number', 7); |
| 57 | + let test_object = new StateVariable("test_object",'object',{ciao:"bella", hey:67, poz:["cool", 9]}); |
| 58 | + let test_bool = new StateVariable("test_bool",'boolean',true); |
| 59 | + test_string.value = "pelam123"; |
| 60 | + test_number.value = 9; |
| 61 | + test_object.value = {bim:"bum",bam:8}; |
| 62 | + test_bool.value = false; |
| 63 | + |
| 64 | + chai.assert.equal(test_string.value, "pelam123", "String " ); |
| 65 | + chai.assert.equal(test_number.value, 9 , "Number " ); |
| 66 | + chai.assert.deepEqual(test_object.value,{bim:"bum",bam:8} , "object " ); |
| 67 | + chai.assert.equal(test_bool.value,false , "boolean " ); |
| 68 | + }); |
| 69 | + it("Throws when corrupted, also additional throw test of setter",()=>{ |
| 70 | + // only number bool and object can be corrupted, strings cant because of performance cut on JSON parse |
| 71 | + // Also the throw of set function has been tested already in the init (few more here) |
| 72 | + let test_object = new StateVariable("test_object",'object',{ciao:"bella", hey:67, poz:["cool", 9]}); |
| 73 | + let test_bool = new StateVariable("test_bool",'boolean',true); |
| 74 | + let test_number = new StateVariable("test_number",'number',7); |
| 75 | + |
| 76 | + let test_function = () =>{ test_object.value = "fuck";}; |
| 77 | + let test_function2 = () =>{ test_bool.value = 89;}; |
| 78 | + let test_function3 = () =>{ test_number.value = undefined;}; |
| 79 | + |
| 80 | + chai.assert.Throw(test_function); |
| 81 | + chai.assert.Throw(test_function2); |
| 82 | + chai.assert.Throw(test_function3); |
| 83 | + |
| 84 | + localStorage.setItem("test_object",'\"ciao\"'); |
| 85 | + localStorage.setItem("test_bool",'\"ciao\"'); |
| 86 | + localStorage.setItem("test_number",'\"ciao\"'); |
| 87 | + let test_function4 = () =>{ let ciao = test_object.value;}; |
| 88 | + let test_function5 = () =>{ let ciao = test_bool.value;}; |
| 89 | + let test_function6 = () =>{ let ciao = test_number.value;}; |
| 90 | + |
| 91 | + chai.assert.Throw(test_function4); |
| 92 | + chai.assert.Throw(test_function5); |
| 93 | + chai.assert.Throw(test_function6); |
| 94 | + }); |
| 95 | + |
| 96 | + }); |
| 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 |
| 108 | + |
9 | 109 | }); |
10 | 110 | } |
0 commit comments