]>
Commit | Line | Data |
---|---|---|
6527f429 DM |
1 | describe("Ext.picker.Month", function() {\r |
2 | var component, makeComponent;\r | |
3 | \r | |
4 | function getByElementsByClassName(dom, className) {\r | |
5 | var elements, length, result, i, el, testRe;\r | |
6 | if (document.getElementsByClassName) {\r | |
7 | return dom.getElementsByClassName(className);\r | |
8 | }\r | |
9 | testRe = new RegExp("(^|\\s)" + className + "(\\s|$)");\r | |
10 | elements = dom.getElementsByTagName("*");\r | |
11 | length = elements.length;\r | |
12 | result = [];\r | |
13 | for (i = 0; i < length; i++) {\r | |
14 | el = elements[i];\r | |
15 | if (testRe.test(el.className)) {\r | |
16 | result.push(el);\r | |
17 | }\r | |
18 | }\r | |
19 | return result;\r | |
20 | }\r | |
21 | \r | |
22 | beforeEach(function() {\r | |
23 | makeComponent = function(config) {\r | |
24 | config = config || {};\r | |
25 | config = Ext.applyIf({\r | |
26 | renderTo: Ext.getBody()\r | |
27 | }, config);\r | |
28 | component = new Ext.picker.Month(config);\r | |
29 | };\r | |
30 | });\r | |
31 | \r | |
32 | afterEach(function() {\r | |
33 | if (component) {\r | |
34 | component.destroy();\r | |
35 | }\r | |
36 | component = makeComponent = null;\r | |
37 | });\r | |
38 | \r | |
39 | describe("initial value", function(){\r | |
40 | \r | |
41 | it("should not default any value", function(){\r | |
42 | makeComponent();\r | |
43 | expect(component.getValue()).toEqual([null, null]); \r | |
44 | });\r | |
45 | \r | |
46 | it("should accept a date as the value config", function(){\r | |
47 | makeComponent({\r | |
48 | value: new Date(2009, 3, 3)\r | |
49 | });\r | |
50 | \r | |
51 | expect(component.getValue()).toEqual([3, 2009]);\r | |
52 | });\r | |
53 | \r | |
54 | it("should accept an array as the value", function(){\r | |
55 | makeComponent({\r | |
56 | value: [4, 1984]\r | |
57 | }); \r | |
58 | \r | |
59 | expect(component.getValue()).toEqual([4, 1984]);\r | |
60 | });\r | |
61 | \r | |
62 | });\r | |
63 | \r | |
64 | describe("setting value", function(){\r | |
65 | \r | |
66 | it("should accept a date when setting a value", function(){\r | |
67 | makeComponent();\r | |
68 | component.setValue(new Date(2004, 1, 12));\r | |
69 | expect(component.getValue()).toEqual([1, 2004]);\r | |
70 | });\r | |
71 | \r | |
72 | it("should accept an array when setting a value", function(){\r | |
73 | makeComponent();\r | |
74 | component.setValue([9, 2001]);\r | |
75 | expect(component.getValue()).toEqual([9, 2001]);\r | |
76 | });\r | |
77 | \r | |
78 | it("should be able to null out certain values", function(){\r | |
79 | makeComponent({\r | |
80 | value: [3, 2010]\r | |
81 | }); \r | |
82 | component.setValue([null, 2010]);\r | |
83 | expect(component.getValue()).toEqual([null, 2010]);\r | |
84 | });\r | |
85 | });\r | |
86 | \r | |
87 | describe("rendering", function(){\r | |
88 | it("should respect the padding config", function() {\r | |
89 | makeComponent({\r | |
90 | padding: 10\r | |
91 | });\r | |
92 | expect(component.getWidth()).toBe(197);\r | |
93 | });\r | |
94 | \r | |
95 | it("should not show buttons if showButtons is false", function(){\r | |
96 | makeComponent({\r | |
97 | showButtons: false\r | |
98 | }); \r | |
99 | \r | |
100 | expect(getByElementsByClassName(component.el.dom, 'x-monthpicker-buttons').length).toEqual(0);\r | |
101 | }); \r | |
102 | \r | |
103 | describe("year range", function(){\r | |
104 | var getYears, getYearText;\r | |
105 | beforeEach(function(){\r | |
106 | getYears = function(){\r | |
107 | return getByElementsByClassName(component.el.dom, 'x-monthpicker-year');\r | |
108 | };\r | |
109 | \r | |
110 | getYearText = function(years, index){\r | |
111 | return years[index].firstChild.innerHTML; \r | |
112 | };\r | |
113 | });\r | |
114 | \r | |
115 | afterEach(function(){\r | |
116 | getYears = null;\r | |
117 | });\r | |
118 | \r | |
119 | it("should use the current year if none is provided", function(){\r | |
120 | makeComponent();\r | |
121 | \r | |
122 | var years = getYears(),\r | |
123 | year = (new Date()).getFullYear();\r | |
124 | expect(getYearText(years, 0)).toEqual((year - 4).toString());\r | |
125 | expect(getYearText(years, years.length - 1)).toEqual((year + 5).toString()); \r | |
126 | }); \r | |
127 | \r | |
128 | it("should use the value year as the active year if passed", function(){\r | |
129 | makeComponent({\r | |
130 | value: [0, 1970]\r | |
131 | }); \r | |
132 | \r | |
133 | var years = getYears();\r | |
134 | expect(getYearText(years, 0)).toEqual('1966');\r | |
135 | expect(getYearText(years, years.length - 1, 0)).toEqual('1975');\r | |
136 | });\r | |
137 | \r | |
138 | it("should change the year range if a new value is set", function(){\r | |
139 | makeComponent();\r | |
140 | component.setValue([0, 1980]);\r | |
141 | \r | |
142 | var years = getYears();\r | |
143 | expect(getYearText(years, 0)).toEqual('1976');\r | |
144 | expect(getYearText(years, years.length - 1, 0)).toEqual('1985');\r | |
145 | });\r | |
146 | \r | |
147 | it("it should not change the range if the value is within the current range", function(){\r | |
148 | makeComponent();\r | |
149 | var year = new Date().getFullYear();\r | |
150 | component.setValue([0, year + 1]);\r | |
151 | var years = getYears();\r | |
152 | expect(getYearText(years, 0)).toEqual((year - 4).toString());\r | |
153 | expect(getYearText(years, years.length - 1, 0)).toEqual((year + 5).toString());\r | |
154 | });\r | |
155 | });\r | |
156 | });\r | |
157 | \r | |
158 | describe("selection", function(){\r | |
159 | \r | |
160 | var getSelection;\r | |
161 | \r | |
162 | beforeEach(function(){\r | |
163 | getSelection = function(isMonth){\r | |
164 | var items = getByElementsByClassName(component.el.dom, 'x-monthpicker-' + (isMonth ? 'month' : 'year')),\r | |
165 | len = items.length,\r | |
166 | i = 0,\r | |
167 | item;\r | |
168 | \r | |
169 | for(; i < len; ++i) {\r | |
170 | item = items[i];\r | |
171 | if (item.firstChild.className.indexOf('x-monthpicker-selected') > -1) {\r | |
172 | return item.firstChild;\r | |
173 | }\r | |
174 | }\r | |
175 | return null;\r | |
176 | };\r | |
177 | });\r | |
178 | \r | |
179 | afterEach(function(){\r | |
180 | getSelection = null;\r | |
181 | });\r | |
182 | \r | |
183 | it("should have no selections if no value is specified", function(){\r | |
184 | makeComponent();\r | |
185 | expect(getSelection()).toBeNull();\r | |
186 | expect(getSelection(true)).toBeNull();\r | |
187 | });\r | |
188 | \r | |
189 | it("should only have a month selection for a month-only value", function(){\r | |
190 | makeComponent({\r | |
191 | value: [3, null]\r | |
192 | });\r | |
193 | expect(getSelection()).toBeNull();\r | |
194 | expect(getSelection(true)).hasHTML('Apr');\r | |
195 | });\r | |
196 | \r | |
197 | it("should only have a year selection for a year-only value", function(){\r | |
198 | makeComponent({\r | |
199 | value: [null, 2000]\r | |
200 | });\r | |
201 | expect(getSelection()).hasHTML('2000');\r | |
202 | expect(getSelection(true)).toBeNull();\r | |
203 | });\r | |
204 | \r | |
205 | it("should select have selections when both items are selected", function(){\r | |
206 | makeComponent({\r | |
207 | value: [0, 2004]\r | |
208 | });\r | |
209 | expect(getSelection()).hasHTML('2004');\r | |
210 | expect(getSelection(true)).hasHTML('Jan');\r | |
211 | });\r | |
212 | \r | |
213 | it("should remove any selection if it's not valid for the range", function(){\r | |
214 | var d = new Date();\r | |
215 | makeComponent({\r | |
216 | value: d\r | |
217 | }); \r | |
218 | expect(getSelection()).hasHTML(d.getFullYear().toString());\r | |
219 | component.adjustYear(-10);\r | |
220 | expect(getSelection()).toBeNull();\r | |
221 | });\r | |
222 | });\r | |
223 | });\r |