You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
156 lines
4.0 KiB
156 lines
4.0 KiB
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" |
|
"http://www.w3.org/TR/html4/strict.dtd"> |
|
<html> |
|
<head> |
|
|
|
<title>_WidgetBase lifecycle unit test</title> |
|
|
|
<script src="boilerplate.js"></script> |
|
|
|
<script type="text/javascript"> |
|
require([ |
|
"doh/runner", |
|
"dojo/_base/declare", "dojo/dom", |
|
"dijit/registry", "dijit/_WidgetBase", |
|
"dojo/domReady!" |
|
], function(doh, declare, dom, registry, _WidgetBase){ |
|
|
|
var obj = { |
|
foo: function(){ |
|
// summary: empty function that we connect to |
|
} |
|
}; |
|
|
|
// Number of times foo was called while TestWidget existed |
|
var calls = 0; |
|
|
|
declare("TestWidget", _WidgetBase, { |
|
postMixInProperties: function(){ |
|
// make sure we can call set for parameters that don't touch the DOM |
|
this.set("foo", "bar"); |
|
}, |
|
postCreate: function(){ |
|
// Rather odd call to this.connect() For testing the connections are dropped on destroy() |
|
this.connect(obj, "foo", function(){ |
|
calls++; |
|
}); |
|
} |
|
}); |
|
|
|
var w; |
|
|
|
doh.register("create and destroy", [ |
|
{ |
|
name: "create", |
|
runTest: function(t){ |
|
w = new TestWidget({id: "w1"}, "w1"); |
|
|
|
doh.t(registry.byId("w1"), "widget in registry"); |
|
|
|
// since there's no template, the widget just points to the srcNodeRef |
|
doh.is(w.domNode, dom.byId("w1"), "srcNodeRef read in"); |
|
|
|
// test the connection |
|
doh.is(0, calls, "foo() not called yet"); |
|
obj.foo(); |
|
doh.is(1, calls, "foo() called"); |
|
} |
|
}, |
|
{ |
|
name: "destroy", |
|
runTest: function(t){ |
|
w.destroy(); |
|
|
|
doh.f(registry.byId("w1"), "widget no longer in registry"); |
|
|
|
// test the connection was destroyed |
|
calls = 0; |
|
obj.foo(); |
|
doh.is(0, calls, "connection was deleted"); |
|
|
|
// test the DOM node was removed |
|
doh.f(dom.byId("w1"), "DOM Node removed"); |
|
} |
|
}, |
|
{ |
|
name: "destroy(true) (preserving DOM node)", |
|
runTest: function(t){ |
|
w = new TestWidget({id: "w2"}, "w2"); |
|
|
|
doh.t(registry.byId("w2"), "widget in registry"); |
|
w.destroy(true); |
|
|
|
doh.f(registry.byId("w2"), "widget no longer in registry"); |
|
|
|
// test the DOM node *wasn't* removed |
|
doh.t(dom.byId("w2"), "DOM Node left"); |
|
} |
|
}, |
|
{ |
|
name: "create with undefined id", |
|
runTest: function(t){ |
|
// If id is "specified" as undefined, generate a new one |
|
w = new TestWidget({id: undefined}); |
|
|
|
doh.isNot(undefined, w.id) |
|
} |
|
}, |
|
{ |
|
name: "create where id specified in prototype", |
|
runTest: function(t){ |
|
declare("TestWidgetWithId", _WidgetBase, { |
|
id: "id-in-prototype" |
|
}); |
|
|
|
// Creating without an id should use the id in the prototype |
|
w = new TestWidgetWithId(); |
|
w.placeAt(document.body); |
|
doh.t(registry.byId("id-in-prototype"), "widget in registry"); |
|
doh.is(w.domNode, dom.byId("id-in-prototype"), "id on domnode"); |
|
w.destroy(); |
|
|
|
// Then try overriding id |
|
w = new TestWidgetWithId({id: "override"}); |
|
w.placeAt(document.body); |
|
doh.t(registry.byId("override"), "widget in registry"); |
|
doh.is(w.domNode, dom.byId("override"), "id on domnode"); |
|
w.destroy(); |
|
} |
|
}, |
|
|
|
]); |
|
|
|
doh.register("setter calls on creation", function(){ |
|
// Make sure setters are called even for anonymous classes (#12122), |
|
// and even when there's no value explicitly specified in the parameters |
|
var fooSetterCalled, |
|
MyWidget = declare(_WidgetBase, { |
|
foo: 345, |
|
_setFooAttr: function(val){ |
|
fooSetterCalled = val; |
|
this._set("foo", val); |
|
} |
|
}); |
|
|
|
new MyWidget(); |
|
|
|
doh.is(345, fooSetterCalled, "fooSetterCalled"); |
|
}); |
|
|
|
doh.register("tweaking params in postMixInProperties", function(){ |
|
// Tests that property changes in postMixInProperties() are not lost (#16080) |
|
var Test = declare(_WidgetBase, { foo: "bar", postMixInProperties: function(){ this.foo = "baz"; } }); |
|
var t = new Test({ foo: "blah" }); |
|
doh.is("baz", t.foo); |
|
}); |
|
|
|
doh.run(); |
|
}); |
|
|
|
</script> |
|
</head> |
|
<body> |
|
<div id="w1"></div> |
|
<div id="w2"></div> |
|
</body> |
|
</html>
|
|
|