Skip to content

Commit c179ed3

Browse files
committed
Created complete failing test for queryParams
The basic idea is that when a link doesn’t specify the value for a query param, the current value is preserved. So, given the hierarchy ``` qps // defines a QP named “string” qps.details // defines a QP named “nestedString” qps.details.more // defines a QP named “doubleNestedString” ``` Links inside `qps.details.more` that point to `qps.details` will contain “nestedString=bar&string=foo”, but never “doubleNestedString”. Links inside `qps.details` that point to `qps` will contain “string=foo”, but never “nestedString” or “doubleNestedString”. Links inside `qps.details` that point to `qps.details` with a different will model will contain “string=foo” but never “nestedString” or “doubleNestedString”. Links from a route to any descendant route will always include all the query params defined in the path.
1 parent 1c3231c commit c179ed3

File tree

7 files changed

+81
-27
lines changed

7 files changed

+81
-27
lines changed

addon/helpers/href-to.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ export default Em.Helper.extend({
1515
queryParams = params.pop().values;
1616
}
1717
let routing = this.get('_routing');
18-
let currentQueryParams = routing.get('currentState.routerJsState.queryParams');
19-
queryParams = Em.merge(Em.merge({}, currentQueryParams), queryParams);
2018
let targetRouteName = params.shift();
19+
let currentQueryParams = routing.get('currentState.routerJsState.fullQueryParams');
20+
queryParams = Em.merge(Em.merge({}, currentQueryParams), queryParams);
2121

2222
return routing.generateURL(targetRouteName, params, queryParams);
2323
}
24-
});
24+
});

tests/dummy/app/templates/about.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
[<a href="{{href-to 'about' (query-params section='two')}}" class="js__href-to">Two</a>]
1414
[<a href="{{href-to 'about' (query-params section=dynamic)}}" class="js__href-to">Three</a>]
1515

16-
dynamic QP value for Three link: {{input value=dynamic}}
16+
dynamic QP value for Three link: {{input id="section-attr-input" value=dynamic}}
1717
</div>
1818

1919
<hr />

tests/dummy/app/templates/application.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
[<a href="{{href-to 'pages.first'}}">First Page</a>]
1919
[<a href="{{href-to 'pages.second'}}">Second Page</a>]
2020
[<a href="{{href-to 'pages.second'}}"><span id="inner-span">Second Page (with inner span)</span></a>]
21-
[<a href="{{href-to 'qps.index'}}">QPS index</a>]
21+
[<a href="{{href-to 'qps.index'}}" id="qps-href-to">QPS index</a>]
2222
[<a>An anchor with no href</a>]
2323
[<a href="http://localhost:4200/about">An anchor with an absolute href</a>]
2424
[<a href="/about" download>An anchor with a download attribute</a>]

tests/dummy/app/templates/qps.hbs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
<td>bool: {{bool}}</td>
1010
</tr>
1111
<tr>
12-
<td><input type="text" value={{string}} oninput={{action (mut string) value="target.value"}}></td>
13-
<td><input type="number" value={{number}} oninput={{action (mut number) value="target.value"}}></td>
14-
<td><input type="checkbox" checked={{bool}} onchange={{action (mut bool) value="target.checked"}}></td>
12+
<td><input type="text" id="qps-input-text" value={{string}} oninput={{action (mut string) value="target.value"}}></td>
13+
<td><input type="number" id="qps-input-number" value={{number}} oninput={{action (mut number) value="target.value"}}></td>
14+
<td><input type="checkbox" id="qps-input-bool" checked={{bool}} onchange={{action (mut bool) value="target.checked"}}></td>
1515
</tr>
1616
</tbody>
1717
</table>

tests/dummy/app/templates/qps/details.hbs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
<td>nestedBool: {{nestedBool}}</td>
1010
</tr>
1111
<tr>
12-
<td><input type="text" value={{nestedString}} oninput={{action (mut nestedString) value="target.value"}}></td>
13-
<td><input type="number" value={{nestedNumber}} oninput={{action (mut nestedNumber) value="target.value"}}></td>
14-
<td><input type="checkbox" checked={{nestedBool}} onchange={{action (mut nestedBool) value="target.checked"}}></td>
12+
<td><input type="text" id="qps-input-nested-text" value={{nestedString}} oninput={{action (mut nestedString) value="target.value"}}></td>
13+
<td><input type="number" id="qps-input-nested-number" value={{nestedNumber}} oninput={{action (mut nestedNumber) value="target.value"}}></td>
14+
<td><input type="checkbox" id="qps-input-nested-bool" checked={{nestedBool}} onchange={{action (mut nestedBool) value="target.checked"}}></td>
1515
</tr>
1616
</tbody>
1717
</table>
1818

19-
<a href="{{href-to 'qps.details.more'}}">href-to for more info</a>
20-
{{#link-to 'qps.details.more'}}link-to for more info{{/link-to}}
19+
<a href="{{href-to 'qps.details.more'}}" id="qps-details-more-href-to">href-to for more info</a>
20+
{{#link-to 'qps.details.more' id="qps-details-more-link-to"}}link-to for more info{{/link-to}}
2121
{{outlet}}
2222
</section>

tests/dummy/app/templates/qps/details/more.hbs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
<td>dobleNestedBool: {{doubleNestedBool}}</td>
1111
</tr>
1212
<tr>
13-
<td><input type="text" value={{doubleNestedString}} oninput={{action (mut doubleNestedString) value="target.value"}}></td>
14-
<td><input type="number" value={{doubleNestedNumber}} oninput={{action (mut doubleNestedNumber) value="target.value"}}></td>
15-
<td><input type="checkbox" checked={{doubleNestedBool}} onchange={{action (mut doubleNestedBool) value="target.checked"}}></td>
13+
<td><input type="text" id="qps-input-double-nested-text" value={{doubleNestedString}} oninput={{action (mut doubleNestedString) value="target.value"}}></td>
14+
<td><input type="number" id="qps-input-double-nested-number" value={{doubleNestedNumber}} oninput={{action (mut doubleNestedNumber) value="target.value"}}></td>
15+
<td><input type="checkbox" id="qps-input-double-nested-bool" checked={{doubleNestedBool}} onchange={{action (mut doubleNestedBool) value="target.checked"}}></td>
1616
</tr>
1717
</tbody>
1818
</table>

tests/integration/href-to-test.js

Lines changed: 65 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,6 @@ test('clicking a href-to to a nested route', function(assert) {
6464
});
6565
});
6666

67-
test('clicking a href-to with query params', function(assert) {
68-
visit('/');
69-
leftClick('#href-to-links a:contains(About)');
70-
leftClick('#about-href-to-links a:contains(Two)');
71-
andThen(function() {
72-
assert.equal(currentURL(), '/about?section=two');
73-
assertAnchorIsActive('#link-to-links a:contains(About)', assert);
74-
assertAnchorIsActive('#about-link-to-links a:contains(Two)', assert);
75-
});
76-
});
77-
7867
test('clicking an action works', function(assert) {
7968
visit('/about');
8069
leftClick('a:contains(Increment)');
@@ -93,3 +82,68 @@ test('clicking a href-to to should propagate events and prevent default ', funct
9382
assert.equal(event.isPropagationStopped(), false, 'should not stop propagation');
9483
});
9584
});
85+
86+
// Query params
87+
test('clicking a link with explicit query params ({{href-to "route.name" foo=bar}})', function(assert) {
88+
visit('/');
89+
leftClick('#href-to-links a:contains(About)');
90+
leftClick('#about-href-to-links a:contains(Two)');
91+
andThen(function() {
92+
assert.equal(currentURL(), '/about?section=two');
93+
assertAnchorIsActive('#link-to-links a:contains(About)', assert);
94+
assertAnchorIsActive('#about-link-to-links a:contains(Two)', assert);
95+
});
96+
});
97+
98+
test('updating a param passed to href-to ({{href-to "route.name" foo=bar}}) updates the url of the anchor', function(assert) {
99+
visit('/about');
100+
andThen(function() {
101+
assert.equal(find('#about-href-to-links a:contains(Three)').attr('href'), '/about?section=hello');
102+
fillIn('#section-attr-input', 'bye');
103+
});
104+
andThen(function() {
105+
assert.equal(find('#about-href-to-links a:contains(Three)').attr('href'), '/about?section=bye');
106+
});
107+
});
108+
109+
test('links without explicitly passed query params include query params of ancestor routes, but no those of child or sibling routes', function(assert) {
110+
visit('/qps');
111+
112+
andThen(function() {
113+
assert.equal(currentURL(), '/qps');
114+
assert.equal(find('#qps-href-to').attr('href'), '/qps', 'The link to the current route route has no query params');
115+
assert.equal(find('.href-tos a:eq(0)').attr('href'), '/qps/1', 'The link to a child route has no query params');
116+
fillIn('#qps-input-text', 'foo');
117+
});
118+
119+
andThen(function() {
120+
assert.equal(find('#qps-href-to').attr('href'), '/qps?string=foo', 'The link to a parent route has the query params defined on that route');
121+
assert.equal(find('.href-tos a:eq(0)').attr('href'), '/qps/1?string=foo', 'The url the current route has the query params');
122+
leftClick('.href-tos a:eq(0)');
123+
});
124+
125+
andThen(function() {
126+
assert.equal(currentURL(), '/qps/1?string=foo');
127+
fillIn('#qps-input-nested-text', 'bar');
128+
});
129+
130+
andThen(function() {
131+
assert.equal(find('#qps-href-to').attr('href'), '/qps?string=foo', 'The link to a parent route has the query param defined on thar route but not in child routes');
132+
assert.equal(find('.href-tos a:eq(0)').attr('href'), '/qps/1?nestedString=bar&string=foo', 'The url to the current route has the both the query params of parent routes and those in the current one');
133+
assert.equal(find('.href-tos a:eq(1)').attr('href'), '/qps/2?string=foo', 'The url to the current route with a different model has only the query params in the parent');
134+
assert.equal(find('#qps-details-more-href-to').attr('href'), '/qps/1/more?nestedString=bar&string=foo', 'The url to the current route has the both the query params of parent routes and those in the current one');
135+
leftClick('#qps-details-more-href-to');
136+
});
137+
138+
andThen(function() {
139+
assert.equal(currentURL(), '/qps/1?string=foo');
140+
fillIn('#qps-input-double-nested-text', 'qux');
141+
});
142+
143+
andThen(function() {
144+
assert.equal(find('#qps-href-to').attr('href'), '/qps?string=foo', 'The link to a parent route has the query param defined on thar route but not in child routes');
145+
assert.equal(find('.href-tos a:eq(0)').attr('href'), '/qps/1?nestedString=bar&string=foo', 'The url to the parent route has his query params and those of the grandparent route, but not those in the current route');
146+
assert.equal(find('.href-tos a:eq(1)').attr('href'), '/qps/2?string=foo', 'The url to the parent route with a different model has only the query params in the grand parent route');
147+
assert.equal(find('#qps-details-more-href-to').attr('href'), '/qps/1/more?doubleNestedString=qux&nestedString=bar&string=foo', 'The url to the current route has the both the query params of parent routes and those in the current one');
148+
});
149+
});

0 commit comments

Comments
 (0)