state.html revision 4dde8ac845fcdfa95d8ef2662b10e98f7619a48e
<html>
<head>
<title>State Tests</title>
#console .yui3-console-entry {
padding:2px;
margin:0px;
min-height:0;
}
#console .yui3-console-entry-fail .yui3-console-entry-cat {
background-color:red;
}
#console .yui3-console-entry-pass .yui3-console-entry-cat {
background-color:green;
}
#console .yui3-console-entry-perf .yui3-console-entry-cat {
background-color:blue;
}
#console {
position:static;
}
html, body {
height:100%;
}
</style>
<script type="text/javascript">
YUI({
useBrowserConsole:false,
filter: (window.location.search.match(/[?&]filter=([^&]+)/) || [])[1] || 'min'
}).use("dump", "attribute-core", "console", "test", function(Y) {
var basicTemplate = {
name: "State Tests",
testInstantiation : function() {
var s = new Y.State();
},
testAdd : function() {
var s = new Y.State();
s.add("A", "keyOne", "valueAOne");
Y.Assert.areEqual("valueAOne", s.get("A", "keyOne"), "Initial add failed");
s.add("A", "keyTwo", "valueATwo");
Y.Assert.areEqual("valueAOne", s.get("A", "keyOne"), "Second add broke initial add");
Y.Assert.areEqual("valueATwo", s.get("A", "keyTwo"), "Second add failed");
s.add("A", "keyOne", "valueAOne-updated");
Y.Assert.areEqual("valueAOne-updated", s.get("A", "keyOne"), "Update add failed");
s.add("B", "keyOne", "valueBOne");
Y.Assert.areEqual("valueBOne", s.get("B", "keyOne"), "Second name add failed");
},
testGet : function() {
var s = new Y.State();
s.add("A", "keyOne", "valueAOne");
s.add("B", "keyOne", "valueBOne");
Y.Assert.areEqual("valueAOne", s.get("A", "keyOne"));
Y.Assert.areEqual("valueBOne", s.get("B", "keyOne"));
Y.Assert.isUndefined(s.get("A", "keyTwo"));
Y.Assert.isUndefined(s.get("B", "keyTwo"));
Y.Assert.isUndefined(s.get("C", "keyOne"));
Y.Assert.isUndefined(s.get("C", "keyTwo"));
},
testAddAll : function() {
var s = new Y.State();
a = {
keyOne: "valueAOne",
keyTwo: "valueATwo",
keyThree: "valueAThree"
};
s.addAll("A", a);
Y.Assert.areNotSame(s.data["A"], a);
Y.Assert.areEqual("valueAOne", s.get("A", "keyOne"));
Y.Assert.areEqual("valueATwo", s.get("A", "keyTwo"));
Y.Assert.areEqual("valueAThree", s.get("A", "keyThree"));
Y.Assert.isUndefined(s.get("A", "keyFour"));
s.addAll("B", {
keyOne: "valueBOne",
keyTwo: "valueBTwo"
});
Y.Assert.areEqual("valueBOne", s.get("B", "keyOne"));
Y.Assert.areEqual("valueBTwo", s.get("B", "keyTwo"));
},
testGetAll : function() {
var s = new Y.State();
s.addAll("A", {
keyOne: "valueAOne",
keyTwo: "valueATwo",
keyThree: "valueAThree"
});
var shallow = s.getAll("A");
Y.Assert.areNotSame(shallow, s.data["A"]);
keyOne: "valueAOne",
keyTwo: "valueATwo",
keyThree: "valueAThree"
}, shallow);
var ref = s.getAll("A", true);
Y.Assert.areSame(ref, s.data["A"]);
keyOne: "valueAOne",
keyTwo: "valueATwo",
keyThree: "valueAThree"
}, ref);
Y.Assert.isUndefined(s.getAll("B"));
},
testRemove : function() {
var s = new Y.State();
s.addAll("A", {
keyOne: "valueAOne",
keyTwo: "valueATwo",
keyThree: "valueAThree"
});
s.add("B", "keyOne", "valueBOne");
s.remove("A", "keyOne");
Y.Assert.isUndefined(s.get("A", "keyOne"));
Y.Assert.areEqual("valueATwo", s.get("A", "keyTwo"));
Y.Assert.areEqual("valueAThree", s.get("A", "keyThree"));
Y.Assert.areEqual("valueBOne", s.get("B", "keyOne"));
},
testRemoveAll : function() {
var s = new Y.State();
s.add("B", "keyOne", "valueBOne");
s.addAll("A", {
keyOne: "valueAOne",
keyTwo: "valueATwo",
keyThree: "valueAThree"
});
s.removeAll("A");
Y.Assert.isUndefined(s.get("A", "keyOne"));
Y.Assert.isUndefined(s.get("A", "keyTwo"));
Y.Assert.isUndefined(s.get("A", "keyThree"));
Y.Assert.isUndefined(s.getAll("A"));
Y.Assert.areEqual("valueBOne", s.get("B", "keyOne"));
},
testRemoveAllArray : function() {
var s = new Y.State();
s.add("B", "keyOne", "valueBOne");
s.addAll("A", {
keyOne: "valueAOne",
keyTwo: "valueATwo",
keyThree: "valueAThree"
});
s.removeAll("A", ["keyTwo", "keyThree"]);
Y.Assert.isUndefined(s.get("A", "keyTwo"));
Y.Assert.isUndefined(s.get("A", "keyThree"));
Y.Assert.isObject(s.getAll("A"));
Y.Assert.areEqual("valueAOne", s.get("A", "keyOne"));
Y.Assert.areEqual("valueBOne", s.get("B", "keyOne"));
},
testRemoveAllObject : function() {
var s = new Y.State();
s.add("B", "keyOne", "valueBOne");
s.addAll("A", {
keyOne: "valueAOne",
keyTwo: "valueATwo",
keyThree: "valueAThree"
});
s.removeAll("A", {
"keyTwo" : true,
"keyThree" : ""
});
Y.Assert.isUndefined(s.get("A", "keyTwo"));
Y.Assert.isUndefined(s.get("A", "keyThree"));
Y.Assert.isObject(s.getAll("A"));
Y.Assert.areEqual("valueAOne", s.get("A", "keyOne"));
Y.Assert.areEqual("valueBOne", s.get("B", "keyOne"));
}
};
var suite = new Y.Test.Suite({name:"State Unit Tests"});
suite.add(new Y.Test.Case(basicTemplate));
Y.Test.Runner.add(suite);
Y.Test.Runner.setName("State Tests");
var console;
Y.one("#btnRun").set("disabled", false).on("click", function() {
if (!console) {
//create the console
console = new Y.Console({
width:"100%",
height:"95%",
id:"console",
verbose : false,
printTimeout: 0,
newestOnTop : false,
entryTemplate: '<pre class="{entry_class} {cat_class} {src_class}">'+
'<span class="{entry_cat_class}">{label}</span>'+
'<span class="{entry_content_class}">{message}</span>'+
'</pre>',
render:true
});
}
});
});
</script>
</head>
<body class="yui3-skin-sam">
<p><input type="button" value="Run Tests" id="btnRun" disabled=true></p>
</body>
</html>