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