Conditions | 24 |
Paths | 1 |
Total Lines | 101 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like Transition.spec.js ➔ ... ➔ describe(ꞌ#abort()ꞌ) often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | /* eslint-env mocha */ |
||
147 | describe('#abort()', function () { |
||
148 | it('throws if transition is not started', function () { |
||
149 | var transition = factory() |
||
150 | expect(transition.abort).to.throw() |
||
151 | }) |
||
152 | |||
153 | it('throws if transition has already finished', function () { |
||
154 | var transition = factory() |
||
155 | return transition |
||
156 | .run() |
||
157 | .then(function () { |
||
158 | expect(transition.abort).to.throw() |
||
159 | }) |
||
160 | }) |
||
161 | |||
162 | it('ends with Aborted status if has been aborted before completion', function () { |
||
163 | var value = {x: 12} |
||
164 | var target = stateFactory() |
||
165 | target.transition.handler = function () { |
||
166 | return new Promise(function () {}) |
||
167 | } |
||
168 | target.abort.handler = function () { |
||
169 | return value |
||
170 | } |
||
171 | var transition = factory(target) |
||
172 | transition.run() |
||
173 | var promise = transition.abort() |
||
174 | return promise |
||
175 | .then(function (result) { |
||
176 | expect(result.value).to.eq(value) |
||
177 | expect(result.status).to.eq(Status.Aborted) |
||
178 | }) |
||
179 | }) |
||
180 | |||
181 | it('ends with AbortFailure status if has been aborted before completion and failed it', function () { |
||
182 | var error = new Error() |
||
183 | var target = stateFactory() |
||
184 | var transition = factory(target) |
||
185 | target.transition.handler = function () { |
||
186 | return new Promise(function () {}) |
||
187 | } |
||
188 | target.abort.handler = function () { |
||
189 | throw error |
||
190 | } |
||
191 | transition.run() |
||
192 | var promise = transition.abort() |
||
193 | return promise |
||
194 | .then(function (result) { |
||
195 | expect(result.value).to.eq(error) |
||
196 | expect(result.status).to.eq(Status.AbortFailure) |
||
197 | }) |
||
198 | }) |
||
199 | |||
200 | it('ends with AbortFailure if abort handlers timed out', function () { |
||
201 | var handler = function () { |
||
202 | return new Promise(function () {}) |
||
203 | } |
||
204 | var target = stateFactory(handler, 0) |
||
205 | target.transition.timeout = null |
||
206 | var transition = factory(target) |
||
207 | transition.run() |
||
208 | var promise = transition.abort() |
||
209 | return promise |
||
210 | .then(function (result) { |
||
211 | expect(result.value).to.be.instanceOf(TimeoutException) |
||
212 | expect(result.status).to.eq(Status.AbortFailure) |
||
213 | }) |
||
214 | }) |
||
215 | |||
216 | it('prevents transition from completing', function () { |
||
217 | var abortValue = {result: 'aborted'} |
||
218 | var completionValue = {result: 'completed'} |
||
219 | var abortBarrier = new Future() |
||
220 | var completionBarrier = new Future() |
||
221 | var completionHandler = function () { |
||
222 | return completionBarrier |
||
223 | } |
||
224 | var abortHandler = function () { |
||
225 | return abortBarrier |
||
226 | } |
||
227 | var target = stateFactory() |
||
228 | target.transition.handler = completionHandler |
||
229 | target.abort.handler = abortHandler |
||
230 | var transition = factory(target) |
||
231 | var promise = transition.run() |
||
232 | expect(transition.getStatus()).to.eq(Status.Executing) |
||
233 | transition.abort() |
||
234 | expect(transition.getStatus()).to.eq(Status.Aborting) |
||
235 | return completionBarrier |
||
236 | .resolve(completionValue) |
||
237 | .then(function () { |
||
238 | abortBarrier.resolve(abortValue) |
||
239 | }) |
||
240 | .then(function () { |
||
241 | return promise |
||
242 | }) |
||
243 | .then(function (result) { |
||
244 | expect(result.status).to.eq(Status.Aborted) |
||
245 | }) |
||
246 | }) |
||
247 | }) |
||
248 | |||
290 |