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

Update sub-route when route could be matched #77

Closed
Closed
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
10 changes: 7 additions & 3 deletions carbon-route.html
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@
__resetProperties: function() {
this._setActive(false);
this._matched = null;
//this.tail = { path: null, prefix: null, queryParams: null };
//this.tail = { path: null, prefix: null, __queryParams: null };
//this.data = {};
},

Expand Down Expand Up @@ -275,7 +275,6 @@
return;
}
}

this._matched = matched.join('/');

// Properties that must be updated atomically.
Expand All @@ -289,6 +288,9 @@
var tailPath = remainingPieces.join('/');
if (remainingPieces.length > 0) {
tailPath = '/' + tailPath;
// To prevent a sub-route to become active but the last match was only empty
} else if (matched[matched.length - 1] !== '') {
tailPath = '/';
}
if (!this.tail ||
this.tail.prefix !== tailPrefix ||
Expand Down Expand Up @@ -325,7 +327,9 @@
}
newPath += tailPath;
}
this.set('route.path', newPath);
if (tailPath !== '/') {
this.set('route.path', newPath);
}
},

/**
Expand Down
64 changes: 55 additions & 9 deletions test/carbon-route.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@
route="{{fooRoute}}"
data="{{bazData}}">
</carbon-route>

<carbon-route
pattern="/:page"
route="{{fooRoute}}"
data="{{pageData}}">
</carbon-route>
</template>
</test-fixture>

Expand All @@ -71,7 +77,8 @@
return {
foo: routes[0],
bar: routes[1],
baz: routes[2]
baz: routes[2],
page: routes[3]
};
}

Expand Down Expand Up @@ -112,7 +119,7 @@
route.set('route.path', '/user/toriel');
expect(route.tail).to.be.deep.equal({
prefix: '/user/toriel',
path: '',
path: '/',
__queryParams: {}
});
});
Expand Down Expand Up @@ -215,6 +222,45 @@
routes.bar.set('data.bar', 'cba');
expect(routes.foo.route.path).to.be.eql('/foo/123/baz/zyx');
});

suite('updates when path could be matched', function() {
test('with literal part', function() {
var routes = fixtureChainedRoutes({ path: '/foo/123/bar/' });

expect(routes.bar.active).to.be.eql(true);
routes.bar.set('data.bar', 'zyx');
expect(routes.foo.route.path).to.be.eql('/foo/123/bar/zyx');
});

test('with only match', function() {
var routes = fixtureChainedRoutes({ path: '/foo/123' });

expect(routes.page.active).to.be.eql(true);
routes.page.set('data.page', 'page1');
expect(routes.foo.route.path).to.be.eql('/foo/123/page1');
});

test('correctly sets tail', function() {
var routes = fixtureChainedRoutes({ path: '/foo/' });
routes.foo.set('data.foo', '123');
expect(routes.page.route.path).to.be.eql('/');
});

test('with only match and trailing slash', function() {
var routes = fixtureChainedRoutes({ path: '/foo/123/' });

expect(routes.page.active).to.be.eql(true);
routes.page.set('data.page', 'page1');
expect(routes.foo.route.path).to.be.eql('/foo/123/page1');
});

test('nested does not become active when empty path', function() {
var routes = fixtureChainedRoutes({ path: '/foo/' });

expect(routes.page.active).to.be.eql(false);
expect(routes.foo.data.foo).to.be.eql('');
});
});
});
});

Expand Down Expand Up @@ -311,7 +357,7 @@
expect(window.location.pathname).to.be.equal('/bar');
expect(r.data).to.be.deep.equal({page: 'bar'});
expect(r.route.path).to.be.equal('/bar');
expect(r.tail.path).to.be.equal('');
expect(r.tail.path).to.be.equal('/');
});

test('changing a data piece in response to path changing', function() {
Expand All @@ -324,7 +370,7 @@
expect(window.location.pathname).to.be.equal('/bar');
expect(r.data).to.be.deep.equal({page: 'bar'});
expect(r.route.path).to.be.equal('/bar');
expect(r.tail.path).to.be.equal('');
expect(r.tail.path).to.be.equal('/');
});

test('changing the tail in response to path changing', function() {
Expand Down Expand Up @@ -354,7 +400,7 @@
expect(window.location.pathname).to.be.equal('/bar');
expect(r.data).to.be.deep.equal({page: 'bar'});
expect(r.route.path).to.be.equal('/bar');
expect(r.tail.path).to.be.equal('');
expect(r.tail.path).to.be.equal('/');
});

test('changing data in response to data changing', function() {
Expand All @@ -366,7 +412,7 @@
expect(window.location.pathname).to.be.equal('/bar');
expect(r.data).to.be.deep.equal({page: 'bar'});
expect(r.route.path).to.be.equal('/bar');
expect(r.tail.path).to.be.equal('');
expect(r.tail.path).to.be.equal('/');
});

test('changing the data object wholesale in response to data changing', function() {
Expand All @@ -380,7 +426,7 @@
expect(window.location.pathname).to.be.equal('/bar');
expect(r.data).to.be.deep.equal({page: 'bar'});
expect(r.route.path).to.be.equal('/bar');
expect(r.tail.path).to.be.equal('');
expect(r.tail.path).to.be.equal('/');
});

test('changing the tail in response to data changing', function() {
Expand Down Expand Up @@ -418,7 +464,7 @@
expect(window.location.pathname).to.be.equal('/baz');
expect(r.data).to.be.deep.equal({page: 'baz'});
expect(r.route.path).to.be.equal('/baz');
expect(r.tail.path).to.be.equal('');
expect(r.tail.path).to.be.equal('/');
});

test('changing the data object wholesale in response to tail changing', function() {
Expand All @@ -431,7 +477,7 @@
expect(window.location.pathname).to.be.equal('/baz');
expect(r.data).to.be.deep.equal({page: 'baz'});
expect(r.route.path).to.be.equal('/baz');
expect(r.tail.path).to.be.equal('');
expect(r.tail.path).to.be.equal('/');
});

test('changing the tail in response to tail changing', function() {
Expand Down