Skip to content

Commit

Permalink
Merge pull request #14537 from trentmwillis/qp-undefined
Browse files Browse the repository at this point in the history
[BUGFIX beta] Improve behavior for QPs with undefined values
  • Loading branch information
rwjblue authored Oct 28, 2016
2 parents c44f0ce + 098300d commit d37d38b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
2 changes: 2 additions & 0 deletions packages/ember-routing/lib/system/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,8 @@ const EmberRouter = EmberObject.extend(Evented, {
if (qp) {
delete queryParams[key];
queryParams[qp.urlKey] = qp.route.serializeQueryParam(value, qp.urlKey, qp.type);
} else if (value === undefined) {
return; // We don't serialize undefined values
} else {
queryParams[key] = this._serializeQueryParam(value, typeOf(value));
}
Expand Down
17 changes: 8 additions & 9 deletions packages/ember/tests/routing/query_params_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1231,14 +1231,14 @@ moduleFor('Query Params - main', class extends QueryParamTestCase {
});
}

['@test Undefined isn\'t deserialized into a string'](assert) {
assert.expect(3);
['@test undefined isn\'t serialized or deserialized into a string'](assert) {
assert.expect(4);

this.router.map(function() {
this.route('example');
});

this.registerTemplate('application', '{{link-to \'Example\' \'example\' id=\'the-link\'}}');
this.registerTemplate('application', '{{link-to \'Example\' \'example\' (query-params foo=undefined) id=\'the-link\'}}');

this.setSingleQPController('example', 'foo', undefined, {
foo: undefined
Expand All @@ -1250,13 +1250,12 @@ moduleFor('Query Params - main', class extends QueryParamTestCase {
}
}));

return this.visit('/').then(() => {
let $link = jQuery('#the-link');
assert.equal($link.attr('href'), '/example');
run($link, 'click');
return this.visitAndAssert('/').then(() => {
assert.equal(this.$('#the-link').attr('href'), '/example', 'renders without undefined qp serialized');

let controller = this.getController('example');
assert.equal(get(controller, 'foo'), undefined);
return this.transitionTo('example', { queryParams: { foo: undefined } }).then(() => {
this.assertCurrentPath('/example');
});
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { RSVP } from 'ember-runtime';
import { Route } from 'ember-routing';

import { QueryParamTestCase, moduleFor } from 'internal-test-helpers';

Expand Down Expand Up @@ -176,4 +177,32 @@ moduleFor('Query Params - async get handler', class extends QueryParamTestCase {
});
});
}

['@test undefined isn\'t serialized or deserialized into a string'](assert) {
assert.expect(4);

this.router.map(function() {
this.route('example');
});

this.registerTemplate('application', '{{link-to \'Example\' \'example\' (query-params foo=undefined) id=\'the-link\'}}');

this.setSingleQPController('example', 'foo', undefined, {
foo: undefined
});

this.registerRoute('example', Route.extend({
model(params) {
assert.deepEqual(params, { foo: undefined });
}
}));

return this.visitAndAssert('/').then(() => {
assert.equal(this.$('#the-link').attr('href'), '/example', 'renders without undefined qp serialized');

return this.transitionTo('example', { queryParams: { foo: undefined } }).then(() => {
this.assertCurrentPath('/example');
});
});
}
});

0 comments on commit d37d38b

Please sign in to comment.