| 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" |
| 2 | "http://www.w3.org/TR/html4/strict.dtd"> |
| 3 | <html> |
| 4 | <head> |
| 5 | |
| 6 | <title>_WidgetBase lifecycle unit test</title> |
| 7 | |
| 8 | <script src="boilerplate.js"></script> |
| 9 | |
| 10 | <script type="text/javascript"> |
| 11 | require([ |
| 12 | "doh/runner", |
| 13 | "dojo/_base/declare", "dojo/dom", |
| 14 | "dijit/registry", "dijit/_WidgetBase", |
| 15 | "dojo/domReady!" |
| 16 | ], function(doh, declare, dom, registry, _WidgetBase){ |
| 17 | |
| 18 | var obj = { |
| 19 | foo: function(){ |
| 20 | // summary: empty function that we connect to |
| 21 | } |
| 22 | }; |
| 23 | |
| 24 | // Number of times foo was called while TestWidget existed |
| 25 | var calls = 0; |
| 26 | |
| 27 | declare("TestWidget", _WidgetBase, { |
| 28 | postMixInProperties: function(){ |
| 29 | // make sure we can call set for parameters that don't touch the DOM |
| 30 | this.set("foo", "bar"); |
| 31 | }, |
| 32 | postCreate: function(){ |
| 33 | // Rather odd call to this.connect() For testing the connections are dropped on destroy() |
| 34 | this.connect(obj, "foo", function(){ |
| 35 | calls++; |
| 36 | }); |
| 37 | } |
| 38 | }); |
| 39 | |
| 40 | var w; |
| 41 | |
| 42 | doh.register("create and destroy", [ |
| 43 | { |
| 44 | name: "create", |
| 45 | runTest: function(t){ |
| 46 | w = new TestWidget({id: "w1"}, "w1"); |
| 47 | |
| 48 | doh.t(registry.byId("w1"), "widget in registry"); |
| 49 | |
| 50 | // since there's no template, the widget just points to the srcNodeRef |
| 51 | doh.is(w.domNode, dom.byId("w1"), "srcNodeRef read in"); |
| 52 | |
| 53 | // test the connection |
| 54 | doh.is(0, calls, "foo() not called yet"); |
| 55 | obj.foo(); |
| 56 | doh.is(1, calls, "foo() called"); |
| 57 | } |
| 58 | }, |
| 59 | { |
| 60 | name: "destroy", |
| 61 | runTest: function(t){ |
| 62 | w.destroy(); |
| 63 | |
| 64 | doh.f(registry.byId("w1"), "widget no longer in registry"); |
| 65 | |
| 66 | // test the connection was destroyed |
| 67 | calls = 0; |
| 68 | obj.foo(); |
| 69 | doh.is(0, calls, "connection was deleted"); |
| 70 | |
| 71 | // test the DOM node was removed |
| 72 | doh.f(dom.byId("w1"), "DOM Node removed"); |
| 73 | } |
| 74 | }, |
| 75 | { |
| 76 | name: "destroy(true) (preserving DOM node)", |
| 77 | runTest: function(t){ |
| 78 | w = new TestWidget({id: "w2"}, "w2"); |
| 79 | |
| 80 | doh.t(registry.byId("w2"), "widget in registry"); |
| 81 | w.destroy(true); |
| 82 | |
| 83 | doh.f(registry.byId("w2"), "widget no longer in registry"); |
| 84 | |
| 85 | // test the DOM node *wasn't* removed |
| 86 | doh.t(dom.byId("w2"), "DOM Node left"); |
| 87 | } |
| 88 | }, |
| 89 | { |
| 90 | name: "create with undefined id", |
| 91 | runTest: function(t){ |
| 92 | // If id is "specified" as undefined, generate a new one |
| 93 | w = new TestWidget({id: undefined}); |
| 94 | |
| 95 | doh.isNot(undefined, w.id) |
| 96 | } |
| 97 | }, |
| 98 | { |
| 99 | name: "create where id specified in prototype", |
| 100 | runTest: function(t){ |
| 101 | declare("TestWidgetWithId", _WidgetBase, { |
| 102 | id: "id-in-prototype" |
| 103 | }); |
| 104 | |
| 105 | // Creating without an id should use the id in the prototype |
| 106 | w = new TestWidgetWithId(); |
| 107 | w.placeAt(document.body); |
| 108 | doh.t(registry.byId("id-in-prototype"), "widget in registry"); |
| 109 | doh.is(w.domNode, dom.byId("id-in-prototype"), "id on domnode"); |
| 110 | w.destroy(); |
| 111 | |
| 112 | // Then try overriding id |
| 113 | w = new TestWidgetWithId({id: "override"}); |
| 114 | w.placeAt(document.body); |
| 115 | doh.t(registry.byId("override"), "widget in registry"); |
| 116 | doh.is(w.domNode, dom.byId("override"), "id on domnode"); |
| 117 | w.destroy(); |
| 118 | } |
| 119 | }, |
| 120 | |
| 121 | ]); |
| 122 | |
| 123 | doh.register("setter calls on creation", function(){ |
| 124 | // Make sure setters are called even for anonymous classes (#12122), |
| 125 | // and even when there's no value explicitly specified in the parameters |
| 126 | var fooSetterCalled, |
| 127 | MyWidget = declare(_WidgetBase, { |
| 128 | foo: 345, |
| 129 | _setFooAttr: function(val){ |
| 130 | fooSetterCalled = val; |
| 131 | this._set("foo", val); |
| 132 | } |
| 133 | }); |
| 134 | |
| 135 | new MyWidget(); |
| 136 | |
| 137 | doh.is(345, fooSetterCalled, "fooSetterCalled"); |
| 138 | }); |
| 139 | |
| 140 | doh.register("tweaking params in postMixInProperties", function(){ |
| 141 | // Tests that property changes in postMixInProperties() are not lost (#16080) |
| 142 | var Test = declare(_WidgetBase, { foo: "bar", postMixInProperties: function(){ this.foo = "baz"; } }); |
| 143 | var t = new Test({ foo: "blah" }); |
| 144 | doh.is("baz", t.foo); |
| 145 | }); |
| 146 | |
| 147 | doh.run(); |
| 148 | }); |
| 149 | |
| 150 | </script> |
| 151 | </head> |
| 152 | <body> |
| 153 | <div id="w1"></div> |
| 154 | <div id="w2"></div> |
| 155 | </body> |
| 156 | </html> |