Skip to content

Commit e02b0b4

Browse files
committed
Refactor SupportCode.Library spec
1 parent dc0d2c0 commit e02b0b4

File tree

1 file changed

+54
-54
lines changed

1 file changed

+54
-54
lines changed

spec/cucumber/support_code/library_spec.js

+54-54
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,24 @@ describe("Cucumber.SupportCode.Library", function() {
1111

1212
beforeEach(function() {
1313
rawSupportCode = createSpy("Raw support code");
14-
afterHookCollection = Cucumber.Type.Collection();
15-
beforeHookCollection = Cucumber.Type.Collection();
14+
beforeHookCollection = createSpy("before hook collection");
15+
afterHookCollection = createSpy("after hook collection");
1616
stepDefinitionCollection = [
1717
createSpyWithStubs("First step definition", {matchesStepName:false}),
1818
createSpyWithStubs("Second step definition", {matchesStepName:false}),
1919
createSpyWithStubs("Third step definition", {matchesStepName:false})
2020
];
2121
worldConstructor = createSpy("world constructor");
2222
spyOnStub(stepDefinitionCollection, 'syncForEach').andCallFake(function(cb) { stepDefinitionCollection.forEach(cb); });
23-
spyOn(Cucumber.Type, 'Collection').andCallFake(function() {
24-
if (this.Collection.callCount == 1) {
25-
return beforeHookCollection;
26-
} else if (this.Collection.callCount == 2) {
27-
return afterHookCollection;
28-
} else {
29-
return stepDefinitionCollection;
30-
}
31-
});
23+
spyOn(Cucumber.Type, 'Collection').andReturnSeveral([beforeHookCollection, afterHookCollection, stepDefinitionCollection]);
3224
spyOn(Cucumber.SupportCode, 'WorldConstructor').andReturn(worldConstructor);
3325
library = Cucumber.SupportCode.Library(rawSupportCode);
3426
});
3527

3628
describe("constructor", function() {
37-
it("creates a collection of step definitions", function() {
29+
it("creates collecitons of before hooks, after hooks and step definitions", function() {
3830
expect(Cucumber.Type.Collection).toHaveBeenCalled();
31+
expect(Cucumber.Type.Collection.callCount).toBe(3);
3932
});
4033

4134
it("executes the raw support code", function() {
@@ -57,12 +50,12 @@ describe("Cucumber.SupportCode.Library", function() {
5750
supportCodeHelper = rawSupportCode.mostRecentCall.object;
5851
});
5952

60-
it("exposes a method to define Before methods", function() {
53+
it("exposes a method to define Before hooks", function() {
6154
expect(supportCodeHelper.Before).toBeAFunction();
6255
expect(supportCodeHelper.Before).toBe(library.defineBeforeHook);
6356
});
6457

65-
it("exposes a method to define After methods", function() {
58+
it("exposes a method to define After hooks", function() {
6659
expect(supportCodeHelper.After).toBeAFunction();
6760
expect(supportCodeHelper.After).toBe(library.defineAfterHook);
6861
});
@@ -97,7 +90,6 @@ describe("Cucumber.SupportCode.Library", function() {
9790
var stepName;
9891

9992
beforeEach(function() {
100-
library = Cucumber.SupportCode.Library(rawSupportCode);
10193
stepName = createSpy("Step name");
10294
});
10395

@@ -174,36 +166,40 @@ describe("Cucumber.SupportCode.Library", function() {
174166
});
175167

176168
describe("triggerBeforeHooks", function() {
177-
var beforeHook, callback, code, invokeSpy, world;
169+
var world, callback;
178170

179171
beforeEach(function() {
180-
code = createSpy("before code");
181-
world = library.instantiateNewWorld();
172+
world = createSpy("world");
182173
callback = createSpy("callback");
183-
beforeHook = createSpy("before hook");
184-
invokeSpy = spyOnStub(beforeHook, "invoke");
185-
spyOn(Cucumber.SupportCode, "Hook").andReturn(beforeHook);
186-
library.defineBeforeHook(code);
174+
spyOnStub(beforeHookCollection, 'forEach');
187175
});
188176

189-
it("triggers each before hook", function() {
190-
library.triggerBeforeHooks(world, function() {
191-
expect(beforeHook, "invoke").
192-
toHaveBeenCalledWithValueAsNthParameter(world, 1);
193-
expect(beforeHook, "invoke").
194-
toHaveBeenCalledWithAFunctionAsNthParameter(2);
195-
});
177+
it("iterates over the before hooks", function() {
178+
library.triggerBeforeHooks(world, callback);
179+
expect(beforeHookCollection.forEach).toHaveBeenCalled();
180+
expect(beforeHookCollection.forEach).toHaveBeenCalledWithAFunctionAsNthParameter(1);
181+
expect(beforeHookCollection.forEach).toHaveBeenCalledWithValueAsNthParameter(callback, 2);
196182
});
197183

198-
it("calls the callback when finished", function() {
199-
invokeSpy.andCallFake(function(world, callback) { callback(); });
200-
library.triggerBeforeHooks(world, callback);
201-
expect(callback).toHaveBeenCalled();
184+
describe("for each before hook", function() {
185+
var beforeHook, forEachBeforeHookFunction, forEachBeforeHookFunctionCallback;
186+
187+
beforeEach(function() {
188+
library.triggerBeforeHooks(world, callback);
189+
forEachBeforeHookFunction = beforeHookCollection.forEach.mostRecentCall.args[0];
190+
forEachBeforeHookFunctionCallback = createSpy("for each before hook iteration callback");
191+
beforeHook = createSpyWithStubs("before hook", {invoke: null});
192+
});
193+
194+
it("invokes the hook", function() {
195+
forEachBeforeHookFunction(beforeHook, forEachBeforeHookFunctionCallback);
196+
expect(beforeHook.invoke).toHaveBeenCalledWith(world, forEachBeforeHookFunctionCallback);
197+
});
202198
});
203199
});
204200

205201
describe("defineAfterHook", function() {
206-
var code, afterHook;
202+
var afterHook, code;
207203

208204
beforeEach(function() {
209205
code = createSpy("after code");
@@ -217,38 +213,42 @@ describe("Cucumber.SupportCode.Library", function() {
217213
expect(Cucumber.SupportCode.Hook).toHaveBeenCalledWith(code);
218214
});
219215

220-
it("unshifts the after hook to the after hooks collection", function() {
216+
it("adds the after hook to the after hooks collection", function() {
221217
library.defineAfterHook(code);
222218
expect(afterHookCollection.unshift).toHaveBeenCalledWith(afterHook);
223219
});
224220
});
225221

226222
describe("triggerAfterHooks", function() {
227-
var afterHook, callback, code, invokeSpy, world;
223+
var world, callback;
228224

229225
beforeEach(function() {
230-
code = createSpy("after code");
231-
world = library.instantiateNewWorld();
232-
callback = createSpy("callback");
233-
afterHook = createSpy("after hook");
234-
invokeSpy = spyOnStub(afterHook, "invoke");
235-
spyOn(Cucumber.SupportCode, "Hook").andReturn(afterHook);
236-
library.defineAfterHook(code);
226+
world = createSpy("world");
227+
callback = createSpy("callback");
228+
spyOnStub(afterHookCollection, 'forEach');
237229
});
238230

239-
it("triggers each after hook", function() {
240-
library.triggerAfterHooks(world, function() {
241-
expect(afterHook, "invoke").
242-
toHaveBeenCalledWithValueAsNthParameter(world, 1);
243-
expect(afterHook, "invoke").
244-
toHaveBeenCalledWithAFunctionAsNthParameter(2);
245-
});
231+
it("iterates over the after hooks", function() {
232+
library.triggerAfterHooks(world, callback);
233+
expect(afterHookCollection.forEach).toHaveBeenCalled();
234+
expect(afterHookCollection.forEach).toHaveBeenCalledWithAFunctionAsNthParameter(1);
235+
expect(afterHookCollection.forEach).toHaveBeenCalledWithValueAsNthParameter(callback, 2);
246236
});
247237

248-
it("calls the callback when finished", function() {
249-
invokeSpy.andCallFake(function(world, callback) { callback(); });
250-
library.triggerAfterHooks(world, callback);
251-
expect(callback).toHaveBeenCalled();
238+
describe("for each after hook", function() {
239+
var afterHook, forEachAfterHookFunction, forEachAfterHookFunctionCallback;
240+
241+
beforeEach(function() {
242+
library.triggerAfterHooks(world, callback);
243+
forEachAfterHookFunction = afterHookCollection.forEach.mostRecentCall.args[0];
244+
forEachAfterHookFunctionCallback = createSpy("for each after hook iteration callback");
245+
afterHook = createSpyWithStubs("after hook", {invoke: null});
246+
});
247+
248+
it("invokes the hook", function() {
249+
forEachAfterHookFunction(afterHook, forEachAfterHookFunctionCallback);
250+
expect(afterHook.invoke).toHaveBeenCalledWith(world, forEachAfterHookFunctionCallback);
251+
});
252252
});
253253
});
254254

0 commit comments

Comments
 (0)