Skip to content

Commit

Permalink
[query-params-new] Issue - emberjs#4570 don't serialize null or undef…
Browse files Browse the repository at this point in the history
…ined into url

# Conflicts:
#	packages/ember/tests/routing/query_params_test.js
#	packages_es6/ember-routing/lib/system/route.js
  • Loading branch information
raytiley authored and CvX committed Aug 30, 2017
1 parent 9057741 commit c157b99
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/ember-routing/lib/system/router.js
Expand Up @@ -701,7 +701,9 @@ const EmberRouter = EmberObject.extend(Evented, {
@param {String} type
*/
_serializeQueryParam(value, type) {
if (type === 'array') {
if (value === null || value === undefined) {
return null;
} else if (type === 'array') {
return JSON.stringify(value);
}

Expand Down
57 changes: 57 additions & 0 deletions packages/ember/tests/routing/query_params_test.js
Expand Up @@ -1153,6 +1153,63 @@ moduleFor('Query Params - main', class extends QueryParamTestCase {
});
}

test("Setting bound query pram property to null or undefined do not seralize to url", function() {
Router.map(function() {
this.route("home", { path: '/home' });
});

App.HomeController = Ember.Controller.extend({
queryParams: ['foo'],
foo: [1, 2]
});

startingURL = '/home';
bootApplication();

var controller = container.lookup('controller:home');

deepEqual(controller.get('foo'), [1,2]);
equal(router.get('location.path'), "/home");

Ember.run(controller, 'set', 'foo', [1,3]);
equal(router.get('location.path'), "/home?foo=%5B1%2C3%5D");

Ember.run(router, 'transitionTo', '/home');
deepEqual(controller.get('foo'), [1,2]);

Ember.run(controller, 'set', 'foo', null);
equal(router.get('location.path'), "/home", "Setting property to null");

Ember.run(controller, 'set', 'foo', [1,3]);
equal(router.get('location.path'), "/home?foo=%5B1%2C3%5D");

Ember.run(controller, 'set', 'foo', undefined);
equal(router.get('location.path'), "/home", "Setting property to undefined");
});

test("{{link-to}} with null or undefined qps do not get serialized into url", function() {
Ember.TEMPLATES.home = Ember.Handlebars.compile(
"{{link-to 'Home' 'home' (query-params foo=nullValue) id='null-link'}}" +
"{{link-to 'Home' 'home' (query-params foo=undefinedValue) id='undefined-link'}}");
Router.map(function() {
this.route("home", { path: '/home' });
});

App.HomeController = Ember.Controller.extend({
queryParams: ['foo'],
foo: [],
nullValue: null,
undefinedValue: undefined
});

startingURL = '/home';
bootApplication();

var controller = container.lookup('controller:home');
equal(Ember.$('#null-link').attr('href'), "/home");
equal(Ember.$('#undefined-link').attr('href'), "/home");
});

['@test A child of a resource route still defaults to parent route\'s model even if the child route has a query param'](assert) {
assert.expect(2);

Expand Down

0 comments on commit c157b99

Please sign in to comment.