tobowers / mamoo

A javascript model, observer framework built on top of the Motionbox EventHandler

This URL has Read+Write access

tobowers (author)
Sun Mar 15 10:18:27 -0700 2009
commit  2803cd4f50c0ff6e7f4d44705af7da46df0724b2
tree    5f421422e1600f48360e1cb91d19ece356c1040f
parent  c7d4d2c03557182bff6973b99dbc96af03d695ec
mamoo / specs / specs / js_controller_spec.js
100644 143 lines (118 sloc) 5.925 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
Screw.Unit(function() {
    describe('MBX.JsController', function() {
        var MyController;
        
        after(function () {
           MBX.JsController.destroyController('MyController');
        });
        
        it("should allow the creation of a controller", function () {
            expect(typeof MBX.JsController.create('MyController')).to(equal, "object");
        });
        
        it("should fire a new controller event when creating a controller", function () {
            TH.countEvent(MBX.JsController.Event.newController);
            MyController = MBX.JsController.create("MyController");
            expect(TH.eventCountFor(MBX.JsController.Event.newController)).to(equal, 1);
        });
        
        it("should allow extentions of all controllers", function () {
            TH.Mock.obj("MBX.JsController");
            PrototypeTestController = MBX.JsController.create('PrototypeTestController');
            MyController = MBX.JsController.create('MyController');
            
            MBX.JsController.extend({ newAttr: "cool" });
            
            expect(PrototypeTestController.newAttr).to(equal, "cool");
            expect(MyController.newAttr).to(equal, "cool");
            
            MBX.JsController.destroyController('PrototypeTestController');
        });
        
        it("should call initialize if it exists", function () {
            var thisController = MBX.JsController.create("ATestController", {
                initialize: function () {
                    this.anAfterCreateAttr = "cool";
                }
            });
            expect(thisController.anAfterCreateAttr).to(equal, "cool");
            MBX.JsController.destroyController("ATestController");
        });
        
        describe("loosely coupled controllers", function () {
            var MyModel;
            before(function() {
                Screw.MBXlooselyCoupledFired = false;
                MyModel = MBX.JsModel.create("MyModel");
                MyController = MBX.JsController.create("MyModelController", {
                    model: MyModel,
                    looselyCoupled: true,
                    onInstanceCreate: function () {
                        Screw.MBXlooselyCoupledFired = true;
                    }
                });
            });
            
            after(function () {
                MBX.JsModel.destroyModel("MyModel");
            });
            
            it("should defer the subscription when loosely coupled", function (me) {
                MyModel.create();
                expect(Screw.MBXlooselyCoupledFired).to(be_false);
                using(me).wait(2).and_then(function () {
                    expect(Screw.MBXlooselyCoupledFired).to(be_true);
                });
            });
            
            
        });
        
        describe("a new controller with a model", function () {
           var MyModel;
           before(function () {
               MyModel = MBX.JsModel.create("MyModel");
           });
           
           after(function () {
               MBX.JsModel.destroyModel('MyModel');
           });
           
           it('should subscribe to model events', function () {
               var eventSubscriptions = [];
               var mockedHandler = TH.Mock.obj("MBX.EventHandler");
               mockedHandler.subscribe = function (specifiers, evtTypes, funcs) {
                   eventSubscriptions.push([specifiers, evtTypes]);
               };
               MyController = MBX.JsController.create('MyController', { model: MyModel });
               
               expect(eventSubscriptions[0]).to(equal, [MBX, MyModel.Event.changeInstance]);
               expect(eventSubscriptions[1]).to(equal, [MBX, MyModel.Event.newInstance]);
               expect(eventSubscriptions[2]).to(equal, [MBX, MyModel.Event.destroyInstance]);
           });
           
           describe("callbacks", function () {
              var lastCallback, instance;
              before(function () {
                  lastCallback = null;
                  MyController = MBX.JsController.create('MyController', {
                      model: MyModel,
                      onInstanceCreate: function (instance) {
                          lastCallback = instance;
                      },
                      onInstanceChange: function (instance, key) {
                          lastCallback = [instance, key];
                      },
                      onInstanceDestroy: function (instance) {
                          lastCallback = instance;
                      },
                      onAttributeChange: function (key) {
                          lastCallback = key;
                      }
                  });
                  instance = MyModel.create();
              });
              
              it("should call onInstanceCreate when a new instance of Model is created", function () {
                  expect(lastCallback).to(equal, instance);
              });
              
              it("should call onInstanceChange when an instance is changed", function () {
                  lastCallback = null;
                  instance.set('AChange', 'IsDifferent');
                  expect(lastCallback).to(equal, [instance, 'AChange']);
              });
              
              it("should call onInstanceDestroy when an instance is destroyed", function () {
                  lastCallback = null;
                  instance.destroy();
                  expect(lastCallback).to(equal, instance);
              });
              
              it("should call onInstanceDestroy when an instance is destroyed", function () {
                  lastCallback = null;
                  MyModel.set("hi", "bye");
                  expect(lastCallback).to(equal, "hi");
              });
              
           });
           
        });
    });
});