]> git.proxmox.com Git - extjs.git/blob - extjs/packages/core/test/specs/AbstractManager.js
add extjs 6.0.1 sources
[extjs.git] / extjs / packages / core / test / specs / AbstractManager.js
1 describe("AbstractManager", function(){
2
3 var manager;
4
5 beforeEach(function(){
6 manager = new Ext.AbstractManager();
7 });
8
9 afterEach(function(){
10 manager = null;
11 });
12
13 describe("get/register/unregister", function(){
14 it("should return undefined for an item not in the collection", function(){
15 expect(manager.get('notthere')).toBeUndefined();
16 });
17
18 it("should return the object if it exists in the collection", function(){
19 var o = {id: 'item'};
20 manager.register(o);
21 expect(manager.get('item')).toBe(o);
22 });
23
24 it("should register multiple items", function(){
25 var o1 = {id: 'item1'},
26 o2 = {id: 'item2'};
27
28 manager.register(o1);
29 manager.register(o2);
30
31 expect(manager.get('item1')).toBe(o1);
32 expect(manager.get('item2')).toBe(o2);
33 });
34
35 it("should remove items when unregistered", function(){
36 var o1 = {id: 'item1'},
37 o2 = {id: 'item2'};
38
39 manager.register(o1);
40 manager.register(o2);
41
42 manager.unregister(o2);
43 expect(manager.get('item1')).toBe(o1);
44 expect(manager.get('item2')).toBeUndefined();
45 });
46 });
47
48 describe("registerType/isRegistered/create", function(){
49
50 afterEach(function(){
51 delete Ext.util.Filter.type;
52 });
53
54 it("should copy the type name onto the prototype", function(){
55 manager.registerType('filter', Ext.util.Filter);
56 expect(Ext.util.Filter.type).toEqual('filter');
57 });
58
59 it("should return true when a type is registered", function(){
60 manager.registerType('filter', Ext.util.Filter);
61 expect(manager.isRegistered('filter')).toBe(true);
62 });
63
64 it("should return false when a type is not registered", function(){
65 expect(manager.isRegistered('notRegged')).toBe(false);
66 });
67
68 it("should thrown an exception when trying to create a type that doesn't exist", function(){
69 expect(function(){
70 manager.create('filter');
71 }).toRaiseExtError();
72 });
73
74 it("should return an instance of the type", function(){
75 manager.registerType('filter', Ext.util.Filter);
76 expect(manager.create({
77 type: 'filter',
78 filterFn: Ext.emptyFn
79 }) instanceof Ext.util.Filter).toBe(true);
80 });
81
82 it("should fallback to the default type", function(){
83 manager.registerType('filter', Ext.util.Filter);
84 expect(manager.create({
85 filterFn: Ext.emptyFn
86 }, 'filter') instanceof Ext.util.Filter).toBe(true);
87 });
88
89 it("should pass the config to the constructor", function(){
90 manager.registerType('filter', Ext.util.Filter);
91 var filter = manager.create({
92 type: 'filter',
93 property: 'name',
94 value: 'x'
95 });
96
97 expect(filter.getProperty()).toBe('name');
98 });
99 });
100
101 describe("onAvailable", function(){
102 it("should never fire if no items are added", function(){
103 var spy = jasmine.createSpy('spy');
104 manager.onAvailable('item', spy);
105 expect(spy.callCount).toBe(0);
106 });
107
108 it("should never fire if items with no matching id are added", function(){
109 var spy = jasmine.createSpy('spy');
110 manager.onAvailable('item', spy);
111 manager.register({
112 id: 'other'
113 });
114 expect(spy.callCount).toBe(0);
115 });
116
117 it("should fire the function if an item is added with a matching id", function(){
118 var spy = jasmine.createSpy('spy');
119 manager.onAvailable('item', spy);
120 manager.register({
121 id: 'item'
122 });
123 expect(spy.callCount).toBe(1);
124 });
125
126 it("should fire the function if the onAvailable is bound when the item already exists", function(){
127 var spy = jasmine.createSpy('spy');
128 manager.register({
129 id: 'item'
130 });
131 manager.onAvailable('item', spy);
132 expect(spy.callCount).toBe(1);
133 });
134
135 it("should pass the item as a parameter", function(){
136 var o = {id: 'item'},
137 actual,
138 fn = function(item){
139 actual = item;
140 };
141
142 manager.onAvailable('item', fn);
143 manager.register(o);
144 expect(actual).toBe(o);
145 });
146
147 it("should default the scope to the item if not specified", function(){
148 var o = {id: 'item'},
149 actual,
150 fn = function(){
151 actual = this;
152 };
153
154 manager.onAvailable('item', fn);
155 manager.register(o);
156 expect(actual).toBe(o);
157 });
158
159 it("should use the passed scope", function(){
160 var o = {id: 'item'},
161 actual,
162 scope = {},
163 fn = function(){
164 actual = this;
165 };
166
167 manager.onAvailable('item', fn, scope);
168 manager.register(o);
169 expect(actual).toBe(scope);
170 });
171
172 it("should remove the listener once the component is created", function(){
173 var fn1 = function(){
174 ++first;
175 }, fn2 = function(){
176 ++second;
177 }, first = 0,
178 second = 0,
179 o = {
180 id: 'item'
181 };
182
183 manager.onAvailable('item', fn1);
184 manager.register(o);
185 manager.unregister(o);
186 manager.onAvailable('item', fn2);
187 manager.register(o);
188
189 expect(first).toBe(1);
190 expect(second).toBe(1);
191 })
192 });
193
194 describe("each", function(){
195 it("should not iterate if there are no items", function(){
196 var spy = jasmine.createSpy('spy');
197 manager.each(spy);
198 expect(spy.callCount).toBe(0);
199 });
200
201 it("should loop over each item", function(){
202 var spy = jasmine.createSpy('spy'),
203 i = 0;
204
205 for (; i < 5; ++i) {
206 manager.register({
207 id: 'id' + i
208 });
209 }
210 manager.each(spy);
211 expect(spy.callCount).toBe(5);
212 });
213
214 it("should default the scope to the manager", function(){
215 var o = {id: 'item'},
216 scope,
217 fn = function(){
218 scope = this;
219 };
220
221 manager.register(o);
222 manager.each(fn);
223 expect(scope).toBe(manager);
224 });
225
226 it("should use the passed scope", function(){
227 var o = {id: 'item'},
228 scope = {},
229 actual,
230 fn = function(){
231 actual = this;
232 };
233
234 manager.register(o);
235 manager.each(fn, scope);
236 expect(actual).toBe(scope);
237 });
238
239 });
240
241 describe("getCount", function(){
242 it("should return 0 by default", function(){
243 expect(manager.getCount()).toBe(0);
244 });
245
246 it("should return the correct count after adding items", function(){
247 manager.register({
248 id: 'a'
249 });
250 expect(manager.getCount()).toBe(1);
251
252 manager.register({
253 id: 'b'
254 });
255 expect(manager.getCount()).toBe(2);
256 });
257
258 it("should return the correct count after removing items", function(){
259 var o = {id: 'item'};
260 manager.register(o);
261 manager.unregister(o);
262 expect(manager.getCount()).toBe(0);
263 });
264 })
265 });