| 1 | define([ |
| 2 | "doh", |
| 3 | "dojo/Stateful", |
| 4 | "dojox/mvc/StatefulArray" |
| 5 | ], function(doh, Stateful, StatefulArray){ |
| 6 | doh.register("dojox.mvc.tests.doh.StatefulArray", [ |
| 7 | { |
| 8 | name: "splice", |
| 9 | runTest: function(){ |
| 10 | var dfd = new doh.Deferred(), |
| 11 | a = new StatefulArray([0, 1, 2, 3]); |
| 12 | |
| 13 | this.h = a.watchElements(function(idx, removals, adds){ |
| 14 | try{ |
| 15 | doh.is(1, idx, "The removal starts with index 1"); |
| 16 | doh.is([1, 2], removals, "Index 1 and index 2 should be removed"); |
| 17 | doh.is([100, 101], adds, "100 and 101 should be added"); |
| 18 | }catch(e){ |
| 19 | dfd.errback(e); |
| 20 | } |
| 21 | }); |
| 22 | |
| 23 | a.splice(-3, 2, 100, 101); |
| 24 | |
| 25 | doh.is([0, 100, 101, 3], a, "The array should end up with 0, 100, 101, 3"); |
| 26 | dfd.callback(1); |
| 27 | |
| 28 | return dfd; |
| 29 | }, |
| 30 | tearDown: function(){ |
| 31 | this.h && this.h.remove(); |
| 32 | } |
| 33 | }, |
| 34 | function slice(){ |
| 35 | var a = new StatefulArray([0, 1, 2, 3]); |
| 36 | doh.is([1, 2], a.slice(-3, 3), "Index 1 and index 2 should be returned"); |
| 37 | doh.is([1], a.slice(-3, -2), "Index 1 should be returned"); |
| 38 | }, |
| 39 | function concat(){ |
| 40 | var a = new StatefulArray([0, 1, 2, 3]); |
| 41 | doh.is([0, 1, 2, 3], a.concat(), "concat() with empty args should end up with the same array"); |
| 42 | doh.is([0, 1, 2, 3, 4, 5, 6, 7, 8], a.concat([4, 5], [6, 7, 8]), "concat() should support multiple args"); |
| 43 | doh.is([0, 1, 2, 3, 4, 5, 6, 7, 8], a.concat(4, 5, [6, 7, 8]), "concat() should non-array values"); |
| 44 | }, |
| 45 | function isInstanceOf(){ |
| 46 | var a = new StatefulArray(); |
| 47 | doh.t(a.isInstanceOf(Stateful), "StatefulArray should be a subclass of Stateful"); |
| 48 | doh.t(a.isInstanceOf(StatefulArray), "StatefulArray's instance should work with isInstanceOf() call"); |
| 49 | }, |
| 50 | { |
| 51 | name: "changeLength", |
| 52 | runTest: function(){ |
| 53 | var dfd = new doh.Deferred(), |
| 54 | a = new StatefulArray([0, 1, 2, 3]), |
| 55 | changes = []; |
| 56 | this.h = a.watchElements(dfd.getTestErrback(function(idx, removals, adds){ |
| 57 | changes.push({ |
| 58 | idx: idx, |
| 59 | removals: removals, |
| 60 | adds: adds |
| 61 | }); |
| 62 | if(changes.length >= 2){ |
| 63 | doh.is(2, changes.length, "Two changes should have been notified"); |
| 64 | doh.is({ |
| 65 | idx: 2, |
| 66 | removals: [2, 3], |
| 67 | adds: [] |
| 68 | }, changes[0], "The first change should remove two elements at index 2"); |
| 69 | doh.is({ |
| 70 | idx: 2, |
| 71 | removals: [], |
| 72 | adds: [void 0, void 0] |
| 73 | }, changes[1], "The second change should add empty two elements"); |
| 74 | dfd.callback(1); |
| 75 | } |
| 76 | })); |
| 77 | a.set("length", 2); |
| 78 | a.set("length", 4); |
| 79 | return dfd; |
| 80 | }, |
| 81 | tearDown: function(){ |
| 82 | this.h && this.h.remove(); |
| 83 | } |
| 84 | } |
| 85 | ]); |
| 86 | }); |