Conditions | 11 |
Paths | 1 |
Total Lines | 67 |
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(ꞌ#run()ꞌ) 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 */ |
||
79 | describe('#run()', function () { |
||
80 | it('throws on second call', function () { |
||
81 | var transition = factory() |
||
82 | transition.run() |
||
83 | expect(transition.run).to.throw() |
||
84 | }) |
||
85 | |||
86 | it('ends with Executed status if everything went smoothly', function () { |
||
87 | var value = {x: 12} |
||
88 | var target = stateFactory(function () { |
||
89 | return value |
||
90 | }) |
||
91 | var transition = factory(target) |
||
92 | return transition |
||
93 | .run() |
||
94 | .then(function (result) { |
||
95 | expect(result.value).to.eq(value) |
||
96 | expect(result.status).to.eq(Transition.Status.Executed) |
||
97 | }) |
||
98 | }) |
||
99 | |||
100 | it('ends with Executed status if timeout handler has rescued situation', function () { |
||
101 | var value = {x: 12} |
||
102 | var target = stateFactory() |
||
103 | var promise = new Promise(function () {}) |
||
104 | target.transition.handler = Sinon.stub().returns(promise) |
||
105 | target.transition.timeout = 0 |
||
106 | target.transition.onTimeout.handler = Sinon.stub().returns(value) |
||
107 | var transition = factory(target) |
||
108 | return transition |
||
109 | .run() |
||
110 | .then(function (result) { |
||
111 | expect(target.transition.handler.callCount).to.eq(1) |
||
112 | expect(target.transition.onTimeout.handler.callCount).to.eq(1) |
||
113 | var token = target.transition.handler.getCall(0).args[2] |
||
114 | expect(token.isCancelled()).to.be.true |
||
|
|||
115 | expect(result.value).to.eq(value) |
||
116 | expect(result.status).to.eq(Status.Executed) |
||
117 | }) |
||
118 | }) |
||
119 | |||
120 | it('ends with ExecutionFailure status if everything has timed out', function () { |
||
121 | var target = stateFactory(function () { |
||
122 | return new Promise(function () { |
||
123 | }) |
||
124 | }, 0) |
||
125 | var transition = factory(target) |
||
126 | return transition |
||
127 | .run() |
||
128 | .then(function (result) { |
||
129 | expect(result.value).to.be.instanceOf(TimeoutException) |
||
130 | expect(result.status).to.eq(Status.ExecutionFailure) |
||
131 | }) |
||
132 | }) |
||
133 | |||
134 | it('passes hints to handler', function () { |
||
135 | var target = stateFactory() |
||
136 | var hints = {x: 12} |
||
137 | var transition = factory(target, hints) |
||
138 | return transition |
||
139 | .run() |
||
140 | .then(function () { |
||
141 | var arg = target.transition.handler.getCall(0).args[1] |
||
142 | expect(arg).to.eq(hints) |
||
143 | }) |
||
144 | }) |
||
145 | }) |
||
146 | |||
290 |