[483] | 1 | define(["doh/main", "../Stateful", "../_base/declare", "../Deferred", "../json"], |
---|
| 2 | function(doh, Stateful, declare, Deferred, JSON){ |
---|
| 3 | |
---|
| 4 | doh.register("tests.Stateful", [ |
---|
| 5 | function getSetWatch(t){ |
---|
| 6 | var s = new Stateful({ |
---|
| 7 | foo: 3 |
---|
| 8 | }); |
---|
| 9 | doh.is(3, s.get("foo")); |
---|
| 10 | var watching = s.watch("foo", function(name, oldValue, value){ |
---|
| 11 | doh.is("foo", name); |
---|
| 12 | doh.is(3, oldValue); |
---|
| 13 | doh.is(4, value); |
---|
| 14 | doh.is(4, s.get("foo")); |
---|
| 15 | }); |
---|
| 16 | s.set("foo", 4); |
---|
| 17 | doh.is(4, s.get("foo")); |
---|
| 18 | watching.unwatch(); |
---|
| 19 | s.set("foo", 5); |
---|
| 20 | doh.is(5, s.get("foo")); |
---|
| 21 | }, |
---|
| 22 | function removeWatchHandle(t){ |
---|
| 23 | var s = new Stateful({ |
---|
| 24 | foo: 3 |
---|
| 25 | }), |
---|
| 26 | watched = false; |
---|
| 27 | |
---|
| 28 | var watching = s.watch("foo", function(){ |
---|
| 29 | t.f(watched); |
---|
| 30 | watched = true; |
---|
| 31 | }); |
---|
| 32 | s.set("foo", 4); |
---|
| 33 | watching.remove(); |
---|
| 34 | s.set("foo", 5); |
---|
| 35 | }, |
---|
| 36 | function removeWatchHandleTwice(t){ |
---|
| 37 | var s = new Stateful({ |
---|
| 38 | foo: 3 |
---|
| 39 | }), |
---|
| 40 | assertions = 0; |
---|
| 41 | |
---|
| 42 | var watching = s.watch("foo", function(){ |
---|
| 43 | assertions++; |
---|
| 44 | }); |
---|
| 45 | var watching2 = s.watch("foo", function(){ |
---|
| 46 | assertions++; |
---|
| 47 | }); |
---|
| 48 | s.set("foo", 4); |
---|
| 49 | watching.remove(); |
---|
| 50 | watching.remove(); |
---|
| 51 | s.set("foo", 5); |
---|
| 52 | |
---|
| 53 | t.is(3, assertions, "assertions"); |
---|
| 54 | }, |
---|
| 55 | function setHash(t){ |
---|
| 56 | var s = new Stateful(), |
---|
| 57 | fooCount = 0, |
---|
| 58 | handle = s.watch('foo', function () { |
---|
| 59 | fooCount++; |
---|
| 60 | }); |
---|
| 61 | s.set({ |
---|
| 62 | foo:3, |
---|
| 63 | bar: 5 |
---|
| 64 | }); |
---|
| 65 | doh.is(3, s.get("foo")); |
---|
| 66 | doh.is(5, s.get("bar")); |
---|
| 67 | doh.is(1, fooCount); |
---|
| 68 | var s2 = new Stateful(); |
---|
| 69 | s2.set(s); |
---|
| 70 | doh.is(3, s2.get("foo")); |
---|
| 71 | doh.is(5, s2.get("bar")); |
---|
| 72 | // s watchers should not be copied to s2 |
---|
| 73 | doh.is(1, fooCount); |
---|
| 74 | handle.unwatch(); |
---|
| 75 | }, |
---|
| 76 | function wildcard(t){ |
---|
| 77 | var s = new Stateful(); |
---|
| 78 | s.set({ |
---|
| 79 | foo: 3, |
---|
| 80 | bar: 5 |
---|
| 81 | }); |
---|
| 82 | var wildcard = 0; |
---|
| 83 | var foo = 0; |
---|
| 84 | s.watch(function(){ |
---|
| 85 | wildcard++; |
---|
| 86 | }); |
---|
| 87 | s.watch("foo", function(){ |
---|
| 88 | foo++; |
---|
| 89 | }); |
---|
| 90 | s.set("foo", 4); |
---|
| 91 | s.set("bar", 6); |
---|
| 92 | doh.is(2, wildcard); |
---|
| 93 | doh.is(1, foo); |
---|
| 94 | }, |
---|
| 95 | function accessors(t){ |
---|
| 96 | var StatefulClass1 = declare([Stateful],{ |
---|
| 97 | foo: "", |
---|
| 98 | bar: 0, |
---|
| 99 | baz: "", |
---|
| 100 | |
---|
| 101 | _fooSetter: function(value){ |
---|
| 102 | this.foo = value; |
---|
| 103 | }, |
---|
| 104 | _fooGetter: function(){ |
---|
| 105 | return "bar"; |
---|
| 106 | }, |
---|
| 107 | |
---|
| 108 | _barSetter: function(value){ |
---|
| 109 | this.bar = value; |
---|
| 110 | } |
---|
| 111 | }); |
---|
| 112 | |
---|
| 113 | var attr1 = new StatefulClass1(); |
---|
| 114 | attr1.set("foo", "nothing"); |
---|
| 115 | attr1.set("bar", 2); |
---|
| 116 | attr1.set("baz", "bar"); |
---|
| 117 | |
---|
| 118 | t.is("nothing", attr1.foo, "attribute set properly"); |
---|
| 119 | t.is("bar", attr1.get("foo"), "getter working properly"); |
---|
| 120 | t.is(2, attr1.bar, "attribute set properly"); |
---|
| 121 | t.is( 2, attr1.get("bar"), "getter working properly"); |
---|
| 122 | t.is("bar", attr1.get("baz"), "getter working properly"); |
---|
| 123 | t.is("bar", attr1.baz, "properly set properly"); |
---|
| 124 | }, |
---|
| 125 | function paramHandling(t){ |
---|
| 126 | var StatefulClass2 = declare([Stateful], { |
---|
| 127 | foo: null, |
---|
| 128 | bar: 5, |
---|
| 129 | |
---|
| 130 | _fooSetter: function(value){ |
---|
| 131 | this.foo = value; |
---|
| 132 | }, |
---|
| 133 | _barSetter: function(value){ |
---|
| 134 | this.bar = value; |
---|
| 135 | } |
---|
| 136 | }); |
---|
| 137 | |
---|
| 138 | var attr2 = new StatefulClass2({ |
---|
| 139 | foo: function(){ |
---|
| 140 | return "baz"; |
---|
| 141 | }, |
---|
| 142 | bar: 4 |
---|
| 143 | }); |
---|
| 144 | |
---|
| 145 | t.is("function", typeof attr2.foo, "function attribute set"); |
---|
| 146 | t.is("baz", attr2.foo(), "function has proper return value"); |
---|
| 147 | t.is(4, attr2.get("bar"), "attribute has proper value"); |
---|
| 148 | }, |
---|
| 149 | function deferredSetting(t){ |
---|
| 150 | var td = new doh.Deferred(); |
---|
| 151 | var StatefulClass3 = declare([Stateful], { |
---|
| 152 | foo: null, |
---|
| 153 | |
---|
| 154 | _fooSetter: function(value){ |
---|
| 155 | var d = new Deferred(); |
---|
| 156 | var self = this; |
---|
| 157 | setTimeout(function(){ |
---|
| 158 | self.foo = value; |
---|
| 159 | d.resolve(value); |
---|
| 160 | }, 50); |
---|
| 161 | return d; |
---|
| 162 | } |
---|
| 163 | }); |
---|
| 164 | |
---|
| 165 | var attr3 = new StatefulClass3(); |
---|
| 166 | attr3.watch("foo", function(name, oldValue, value){ |
---|
| 167 | t.is("foo", name, "right attribute"); |
---|
| 168 | t.f(oldValue, "no value previously"); |
---|
| 169 | t.is(3, value, "new value set"); |
---|
| 170 | td.callback(true); |
---|
| 171 | }); |
---|
| 172 | attr3.set("foo", 3); |
---|
| 173 | |
---|
| 174 | return td; |
---|
| 175 | }, |
---|
| 176 | function changeAttrValue(t){ |
---|
| 177 | var output = []; |
---|
| 178 | var StatefulClass4 = declare([Stateful], { |
---|
| 179 | foo: null, |
---|
| 180 | bar: null, |
---|
| 181 | |
---|
| 182 | _fooSetter: function(value){ |
---|
| 183 | this._changeAttrValue("bar", value); |
---|
| 184 | this.foo = value; |
---|
| 185 | }, |
---|
| 186 | _barSetter: function(value){ |
---|
| 187 | this._changeAttrValue("foo", value); |
---|
| 188 | this.bar = value; |
---|
| 189 | } |
---|
| 190 | }); |
---|
| 191 | |
---|
| 192 | var attr4 = new StatefulClass4(); |
---|
| 193 | attr4.watch("foo", function(name, oldValue, value){ |
---|
| 194 | output.push(name, oldValue, value); |
---|
| 195 | }); |
---|
| 196 | attr4.watch("bar", function(name, oldValue, value){ |
---|
| 197 | output.push(name, oldValue, value); |
---|
| 198 | }); |
---|
| 199 | attr4.set("foo", 3); |
---|
| 200 | t.is(3, attr4.get("bar"), "value set properly"); |
---|
| 201 | attr4.set("bar", 4); |
---|
| 202 | t.is(4, attr4.get("foo"), "value set properly"); |
---|
| 203 | t.is(["bar", null, 3, "foo", null, 3, "foo", 3, 4, "bar", 3, 4], output); |
---|
| 204 | }, |
---|
| 205 | function serialize(t){ |
---|
| 206 | var StatefulClass5 = declare([Stateful], { |
---|
| 207 | foo: null, |
---|
| 208 | _fooSetter: function(value){ |
---|
| 209 | this.foo = value + "baz"; |
---|
| 210 | } |
---|
| 211 | }); |
---|
| 212 | |
---|
| 213 | var obj = new StatefulClass5({ |
---|
| 214 | foo: "bar" |
---|
| 215 | }); |
---|
| 216 | |
---|
| 217 | t.is('{"foo":"barbaz"}', JSON.stringify(obj), "object serializes properly"); |
---|
| 218 | } |
---|
| 219 | ]); |
---|
| 220 | |
---|
| 221 | }); |
---|