Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for dynamic segments in visitable urls #17

Merged
merged 1 commit into from
May 31, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions test-support/page-object/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,32 @@

import Attribute from './attribute';

function visitable() {
this.page.lastPromise = visit(this.path);
function fillInDynamicSegments(path, params) {
return path.split('/').map(function(segment) {
let match;

if (match = segment.match(/^:(.+)$/)) {
let key = match[1];

if (!params[key]) {
throw new Error(`Missing parameter for '${key}'`);
}

return params[key];
}

return segment;
}).join('/');
}

function visitable(params = {}) {
let path = this.path;

if (path.indexOf(':') !== -1) {
path = fillInDynamicSegments(path, params);
}

this.page.lastPromise = visit(path);

return this.page;
}
Expand Down
23 changes: 23 additions & 0 deletions tests/unit/actions/visitable-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,26 @@ it('calls Ember\'s visit helper', function(assert) {

buildAttribute(visitableAttribute, expectedRoute)();
});

it('fills in dynamic segments', function(assert) {
assert.expect(1);

window.visit = function(actualRoute) {
assert.equal(actualRoute, '/users/5/comments/1');
};

buildAttribute(visitableAttribute, '/users/:user_id/comments/:comment_id')({
user_id: 5,
comment_id: 1
});
});

it("raises an exception if params aren't given for all dynamic segments", function(assert) {
assert.expect(1);

try {
buildAttribute(visitableAttribute, '/users/:user_id')();
} catch(e) {
assert.equal(e.message, "Missing parameter for 'user_id'");
}
});