]>
Commit | Line | Data |
---|---|---|
6527f429 DM |
1 | describe('Ext.mixin.Responsive', function () {\r |
2 | function stashProps (object, backup, props) {\r | |
3 | for (var i = props.length; i-- > 0; ) {\r | |
4 | var name = props[i];\r | |
5 | if (name in backup) {\r | |
6 | object[name] = backup[name];\r | |
7 | } else {\r | |
8 | delete object[name];\r | |
9 | }\r | |
10 | }\r | |
11 | }\r | |
12 | \r | |
13 | var Cls, instance, Responsive,\r | |
14 | oldGetOrientation, oldGetViewWidth, oldGetViewHeight,\r | |
15 | environments = {\r | |
16 | ipad: {\r | |
17 | landscape: {\r | |
18 | width: 1024,\r | |
19 | height: 768,\r | |
20 | orientation: 'landscape'\r | |
21 | },\r | |
22 | portrait: {\r | |
23 | width: 768,\r | |
24 | height: 1024,\r | |
25 | orientation: 'portrait'\r | |
26 | }\r | |
27 | },\r | |
28 | iphone: {\r | |
29 | landscape: {\r | |
30 | width: 480,\r | |
31 | height: 320,\r | |
32 | orientation: 'landscape'\r | |
33 | },\r | |
34 | portrait: {\r | |
35 | width: 320,\r | |
36 | height: 480,\r | |
37 | orientation: 'portrait'\r | |
38 | }\r | |
39 | }\r | |
40 | },\r | |
41 | env;\r | |
42 | \r | |
43 | beforeEach(function () {\r | |
44 | Responsive = Ext.mixin.Responsive;\r | |
45 | \r | |
46 | oldGetOrientation = Ext.dom.Element.getOrientation;\r | |
47 | oldGetViewWidth = Ext.dom.Element.getViewportWidth;\r | |
48 | oldGetViewHeight = Ext.dom.Element.getViewportHeight;\r | |
49 | \r | |
50 | Ext.dom.Element.getOrientation = function () {\r | |
51 | return env.orientation;\r | |
52 | };\r | |
53 | \r | |
54 | Ext.dom.Element.getViewportWidth = function () {\r | |
55 | return env.width;\r | |
56 | };\r | |
57 | \r | |
58 | Ext.dom.Element.getViewportHeight = function () {\r | |
59 | return env.height;\r | |
60 | };\r | |
61 | \r | |
62 | Cls = Ext.define(null, {\r | |
63 | mixins: [\r | |
64 | 'Ext.mixin.Responsive'\r | |
65 | ],\r | |
66 | \r | |
67 | config: {\r | |
68 | title: 'Hello',\r | |
69 | bar: null,\r | |
70 | foo: null\r | |
71 | },\r | |
72 | \r | |
73 | responsiveFormulas: {\r | |
74 | small: 'width < 600',\r | |
75 | medium: 'width >= 600 && width < 800',\r | |
76 | large: 'width >= 800'\r | |
77 | },\r | |
78 | \r | |
79 | responsiveConfig: {\r | |
80 | small: {\r | |
81 | bar: 'S'\r | |
82 | },\r | |
83 | medium: {\r | |
84 | bar: 'M'\r | |
85 | },\r | |
86 | large: {\r | |
87 | bar: 'L'\r | |
88 | },\r | |
89 | landscape: {\r | |
90 | title: 'Landscape'\r | |
91 | },\r | |
92 | portrait: {\r | |
93 | title: 'Portrait'\r | |
94 | }\r | |
95 | },\r | |
96 | \r | |
97 | constructor: function (config) {\r | |
98 | this.initConfig(config);\r | |
99 | }\r | |
100 | });\r | |
101 | });\r | |
102 | \r | |
103 | afterEach(function () {\r | |
104 | Ext.dom.Element.getOrientation = oldGetOrientation;\r | |
105 | Ext.dom.Element.getViewportWidth = oldGetViewWidth;\r | |
106 | Ext.dom.Element.getViewportHeight = oldGetViewHeight;\r | |
107 | \r | |
108 | Cls = null;\r | |
109 | instance = Ext.destroy(instance);\r | |
110 | \r | |
111 | expect(Responsive.active).toBe(false);\r | |
112 | expect(Responsive.count).toBe(0);\r | |
113 | });\r | |
114 | \r | |
115 | describe('initialization', function () {\r | |
116 | var backupProps = ['tablet', 'desktop'],\r | |
117 | backup;\r | |
118 | \r | |
119 | beforeEach(function () {\r | |
120 | env = environments.ipad.landscape;\r | |
121 | \r | |
122 | backup = {};\r | |
123 | stashProps(Ext.platformTags, backup, backupProps);\r | |
124 | \r | |
125 | Ext.platformTags.desktop = false;\r | |
126 | Ext.platformTags.tablet = true;\r | |
127 | });\r | |
128 | \r | |
129 | afterEach(function () {\r | |
130 | stashProps(backup, Ext.platformTags, backupProps);\r | |
131 | });\r | |
132 | \r | |
133 | it('should init with landscape from class', function () {\r | |
134 | instance = new Cls();\r | |
135 | \r | |
136 | var title = instance.getTitle();\r | |
137 | expect(title).toBe('Landscape');\r | |
138 | });\r | |
139 | \r | |
140 | it('should init with landscape from class over instanceConfig', function () {\r | |
141 | instance = new Cls({\r | |
142 | title: 'Foo' // the responsiveConfig will win\r | |
143 | });\r | |
144 | \r | |
145 | var title = instance.getTitle();\r | |
146 | expect(title).toBe('Landscape');\r | |
147 | });\r | |
148 | \r | |
149 | it('should init with portrait from class', function () {\r | |
150 | env = environments.ipad.portrait;\r | |
151 | instance = new Cls();\r | |
152 | \r | |
153 | var title = instance.getTitle();\r | |
154 | expect(title).toBe('Portrait');\r | |
155 | });\r | |
156 | \r | |
157 | it('should init with wide from instanceConfig', function () {\r | |
158 | instance = new Cls({\r | |
159 | responsiveConfig: {\r | |
160 | wide: {\r | |
161 | foo: 'Wide'\r | |
162 | },\r | |
163 | tall: {\r | |
164 | foo: 'Tall'\r | |
165 | }\r | |
166 | }\r | |
167 | });\r | |
168 | \r | |
169 | var foo = instance.getFoo();\r | |
170 | expect(foo).toBe('Wide');\r | |
171 | });\r | |
172 | \r | |
173 | it('should init with tall from instanceConfig', function () {\r | |
174 | env = environments.ipad.portrait;\r | |
175 | instance = new Cls({\r | |
176 | responsiveConfig: {\r | |
177 | wide: {\r | |
178 | foo: 'Wide'\r | |
179 | },\r | |
180 | tall: {\r | |
181 | foo: 'Tall'\r | |
182 | }\r | |
183 | }\r | |
184 | });\r | |
185 | \r | |
186 | var foo = instance.getFoo();\r | |
187 | expect(foo).toBe('Tall');\r | |
188 | });\r | |
189 | \r | |
190 | it('should init with landscape from instanceConfig', function () {\r | |
191 | instance = new Cls({\r | |
192 | responsiveConfig: {\r | |
193 | landscape: {\r | |
194 | title: 'Landscape 2'\r | |
195 | }\r | |
196 | }\r | |
197 | });\r | |
198 | \r | |
199 | var title = instance.getTitle();\r | |
200 | expect(title).toBe('Landscape 2'); // instanceConfig wins\r | |
201 | });\r | |
202 | \r | |
203 | it('should init with portrait not hidden by instanceConfig', function () {\r | |
204 | env = environments.ipad.portrait;\r | |
205 | instance = new Cls({\r | |
206 | responsiveConfig: {\r | |
207 | landscape: {\r | |
208 | title: 'Landscape 2'\r | |
209 | }\r | |
210 | }\r | |
211 | });\r | |
212 | \r | |
213 | var title = instance.getTitle();\r | |
214 | expect(title).toBe('Portrait'); // not replaced by instanceConfig\r | |
215 | });\r | |
216 | \r | |
217 | it('should init with platform.tablet from instanceConfig', function () {\r | |
218 | instance = new Cls({\r | |
219 | responsiveConfig: {\r | |
220 | 'platform.tablet': {\r | |
221 | foo: 'Tablet'\r | |
222 | }\r | |
223 | }\r | |
224 | });\r | |
225 | \r | |
226 | var foo = instance.getFoo();\r | |
227 | expect(foo).toBe('Tablet');\r | |
228 | });\r | |
229 | \r | |
230 | it('should init with tablet from instanceConfig', function () {\r | |
231 | instance = new Cls({\r | |
232 | responsiveConfig: {\r | |
233 | tablet: {\r | |
234 | foo: 'Tablet'\r | |
235 | }\r | |
236 | }\r | |
237 | });\r | |
238 | \r | |
239 | var foo = instance.getFoo();\r | |
240 | expect(foo).toBe('Tablet');\r | |
241 | });\r | |
242 | \r | |
243 | it('should preserve instanceConfig if responsiveConfig has no match', function () {\r | |
244 | instance = new Cls({\r | |
245 | foo: 'Foo',\r | |
246 | responsiveConfig: {\r | |
247 | 'platform.desktop': { // env is tablet so this is false\r | |
248 | foo: 'Desktop'\r | |
249 | }\r | |
250 | }\r | |
251 | });\r | |
252 | \r | |
253 | var foo = instance.getFoo();\r | |
254 | expect(foo).toBe('Foo');\r | |
255 | });\r | |
256 | \r | |
257 | it('should preserve instanceConfig if responsiveConfig has no match w/o prefix', function () {\r | |
258 | instance = new Cls({\r | |
259 | foo: 'Foo',\r | |
260 | responsiveConfig: {\r | |
261 | desktop: { // env is tablet so this is false\r | |
262 | foo: 'Desktop'\r | |
263 | }\r | |
264 | }\r | |
265 | });\r | |
266 | \r | |
267 | var foo = instance.getFoo();\r | |
268 | expect(foo).toBe('Foo');\r | |
269 | });\r | |
270 | \r | |
271 | it('should pick responsiveConfig over instanceConfig', function () {\r | |
272 | instance = new Cls({\r | |
273 | foo: 'Foo',\r | |
274 | responsiveConfig: {\r | |
275 | 'platform.tablet': {\r | |
276 | foo: 'Tablet'\r | |
277 | }\r | |
278 | }\r | |
279 | });\r | |
280 | \r | |
281 | var foo = instance.getFoo();\r | |
282 | expect(foo).toBe('Tablet');\r | |
283 | });\r | |
284 | \r | |
285 | it('should pick responsiveConfig over instanceConfig w/o prefix', function () {\r | |
286 | instance = new Cls({\r | |
287 | foo: 'Foo',\r | |
288 | responsiveConfig: {\r | |
289 | tablet: {\r | |
290 | foo: 'Tablet'\r | |
291 | }\r | |
292 | }\r | |
293 | });\r | |
294 | \r | |
295 | var foo = instance.getFoo();\r | |
296 | expect(foo).toBe('Tablet');\r | |
297 | });\r | |
298 | }); // initializing\r | |
299 | \r | |
300 | describe('formulas', function () {\r | |
301 | var backupProps = ['tablet', 'desktop'],\r | |
302 | backup;\r | |
303 | \r | |
304 | beforeEach(function () {\r | |
305 | env = environments.ipad.landscape;\r | |
306 | \r | |
307 | backup = {};\r | |
308 | stashProps(Ext.platformTags, backup, backupProps);\r | |
309 | \r | |
310 | Ext.platformTags.desktop = false;\r | |
311 | Ext.platformTags.tablet = true;\r | |
312 | });\r | |
313 | \r | |
314 | afterEach(function () {\r | |
315 | stashProps(backup, Ext.platformTags, backupProps);\r | |
316 | });\r | |
317 | \r | |
318 | it('should init on iPad Landscape using formulas from class', function () {\r | |
319 | instance = new Cls();\r | |
320 | \r | |
321 | var bar = instance.getBar();\r | |
322 | expect(bar).toBe('L');\r | |
323 | });\r | |
324 | \r | |
325 | it('should init on iPad Portrait using formulas from class', function () {\r | |
326 | env = environments.ipad.portrait;\r | |
327 | instance = new Cls();\r | |
328 | \r | |
329 | var bar = instance.getBar();\r | |
330 | expect(bar).toBe('M');\r | |
331 | });\r | |
332 | \r | |
333 | it('should init on iPhone Portrait using formulas from class', function () {\r | |
334 | env = environments.iphone.portrait;\r | |
335 | instance = new Cls();\r | |
336 | \r | |
337 | var bar = instance.getBar();\r | |
338 | expect(bar).toBe('S');\r | |
339 | });\r | |
340 | \r | |
341 | }); // formulas\r | |
342 | \r | |
343 | describe('dynamic', function () {\r | |
344 | var backupProps = ['tablet', 'desktop'],\r | |
345 | backup;\r | |
346 | \r | |
347 | beforeEach(function () {\r | |
348 | env = environments.ipad.landscape;\r | |
349 | \r | |
350 | backup = {};\r | |
351 | stashProps(Ext.platformTags, backup, backupProps);\r | |
352 | \r | |
353 | Ext.platformTags.desktop = false;\r | |
354 | Ext.platformTags.tablet = true;\r | |
355 | });\r | |
356 | \r | |
357 | afterEach(function () {\r | |
358 | stashProps(backup, Ext.platformTags, backupProps);\r | |
359 | });\r | |
360 | \r | |
361 | it('should update when responsive state changes', function () {\r | |
362 | instance = new Cls({\r | |
363 | responsiveConfig: {\r | |
364 | wide: {\r | |
365 | foo: 'Wide'\r | |
366 | },\r | |
367 | tall: {\r | |
368 | foo: 'Tall'\r | |
369 | }\r | |
370 | }\r | |
371 | });\r | |
372 | \r | |
373 | var foo = instance.getFoo();\r | |
374 | expect(foo).toBe('Wide');\r | |
375 | \r | |
376 | env = environments.ipad.portrait;\r | |
377 | Responsive.notify();\r | |
378 | \r | |
379 | foo = instance.getFoo();\r | |
380 | expect(foo).toBe('Tall');\r | |
381 | });\r | |
382 | \r | |
383 | it('should update formulas when responsive state changes', function () {\r | |
384 | instance = new Cls();\r | |
385 | \r | |
386 | var bar = instance.getBar();\r | |
387 | expect(bar).toBe('L');\r | |
388 | \r | |
389 | env = environments.ipad.portrait;\r | |
390 | Responsive.notify();\r | |
391 | \r | |
392 | bar = instance.getBar();\r | |
393 | expect(bar).toBe('M');\r | |
394 | });\r | |
395 | });\r | |
396 | });\r |