Skip to content

Commit

Permalink
Merge pull request emberjs#2724 from teddyzeenny/chained-helpers
Browse files Browse the repository at this point in the history
Added the ability to chain test helpers
  • Loading branch information
stefanpenner committed May 23, 2013
2 parents bb4b5e1 + 89b5ea6 commit 00b4437
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
49 changes: 48 additions & 1 deletion packages/ember-testing/lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ function find(app, selector, context) {
}

function wait(app, value) {
return Ember.Test.promise(function(resolve) {
var promise, obj = {}, helperName;

promise = Ember.Test.promise(function(resolve) {
Ember.Test.adapter.asyncStart();
var watcher = setInterval(function() {
var routerIsLoading = app.__container__.lookup('router:main').router.isLoading;
Expand All @@ -68,6 +70,51 @@ function wait(app, value) {
});
}, 10);
});

return buildChainObject(app, promise);
}

/**
Builds an object that contains
all helper methods. This object will be
returned by helpers and then-promises.
This allows us to chain helpers:
```javascript
visit('posts/new')
.click('.add-btn')
.fillIn('.title', 'Post')
.click('.submit')
.then(function() {
equal('.post-title', 'Post');
})
.visit('comments')
.then(function() {
equal(find('.comments'),length, 0);
});
```
*/
function buildChainObject(app, promise) {
var helperName, obj = {};
for(helperName in app.testHelpers) {
obj[helperName] = chain(app, promise, app.testHelpers[helperName]);
}
obj.then = function(fn) {
var thenPromise = promise.then(fn);
return buildChainObject(app, thenPromise);
};
return obj;
}

function chain(app, promise, fn) {
return function() {
var args = arguments, chainedPromise;
chainedPromise = promise.then(function() {
return fn.apply(null, args);
});
return buildChainObject(app, chainedPromise);
};
}

// expose these methods as test helpers
Expand Down
21 changes: 20 additions & 1 deletion packages/ember-testing/tests/acceptance_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ module("ember-testing Acceptance", {
}
});

test("helpers can be chained", function() {
test("helpers can be chained with then", function() {
expect(5);
Ember.Test.adapter = Ember.Test.QUnitAdapter.create({
exception: function(error) {
Expand Down Expand Up @@ -91,3 +91,22 @@ test("helpers can be chained", function() {
});

});



test("helpers can be chained to each other", function() {
expect(3);

currentRoute = 'index';

visit('/posts').click('a:contains("Comments")')
.fillIn('.ember-text-field', "hello")
.then(function() {
equal(currentRoute, 'comments', "Successfully visited posts route");
equal(Ember.$('.ember-text-field').val(), 'hello', "Fillin successfully works");
})
.visit('/posts')
.then(function() {
equal(currentRoute, 'posts', "Thens can also be chained to helpers");
});
});

0 comments on commit 00b4437

Please sign in to comment.