Skip to content

Commit

Permalink
ensure deserializeQueryParam is called for lazy routes
Browse files Browse the repository at this point in the history
(cherry picked from commit b3559e4)
  • Loading branch information
brendenpalmer authored and kategengler committed Aug 9, 2021
1 parent a4bfee9 commit 8cf4f4a
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 4 deletions.
Expand Up @@ -974,5 +974,64 @@ moduleFor(
assert.equal(error.message, 'Whoops! Something went wrong...');
});
}

['@test visit() with `shouldRender: true` queryParams are properly deserialized for lazy routes'](
assert
) {
assert.expect(2);

let hooks = [];

this.setupAppAndRoutableEngine(hooks);

this.add(
'engine:blog',
Engine.extend({
Resolver: ModuleBasedTestResolver,

init() {
this._super(...arguments);
this.register(
'controller:application',
Controller.extend({
queryParams: ['lazyQueryParam'],
})
);

this.register(
'template:application',
compile('Engine<div class="lazy-query-param">{{this.lazyQueryParam}}</div>{{outlet}}')
);

this.register(
'route:application',
Route.extend({
queryParams: {
lazyQueryParam: {
defaultValue: null,
},
},
deserializeQueryParam() {
hooks.push('engine - deserialize query param');
return 'foo';
},
model() {
hooks.push('engine - application');
},
})
);
},
})
);

return this.visit('/blog?lazyQueryParam=bar', { shouldRender: true }).then(() => {
assert.deepEqual(
hooks,
['application - application', 'engine - deserialize query param', 'engine - application'],
'the expected hooks were fired'
);
assert.strictEqual(this.element.querySelector('.lazy-query-param').innerHTML, 'foo');
});
}
}
);
18 changes: 14 additions & 4 deletions packages/@ember/-internals/routing/lib/system/route.ts
Expand Up @@ -2026,11 +2026,21 @@ export function getFullQueryParams(router: EmberRouter, state: TransitionState<R
return state['fullQueryParams'];
}

state['fullQueryParams'] = {};
assign(state['fullQueryParams'], state.queryParams);
let fullQueryParamsState = {};
let haveAllRouteInfosResolved = state.routeInfos.every((routeInfo) => routeInfo.route);

router._deserializeQueryParams(state.routeInfos, state['fullQueryParams'] as QueryParam);
return state['fullQueryParams'];
assign(fullQueryParamsState, state.queryParams);

router._deserializeQueryParams(state.routeInfos, fullQueryParamsState as QueryParam);

// only cache query params state if all routeinfos have resolved; it's possible
// for lazy routes to not have resolved when `getFullQueryParams` is called, so
// we wait until all routes have resolved prior to caching query params state
if (haveAllRouteInfosResolved) {
state['fullQueryParams'] = fullQueryParamsState;
}

return fullQueryParamsState;
}

function getQueryParamsFor(route: Route, state: TransitionState<Route>) {
Expand Down

0 comments on commit 8cf4f4a

Please sign in to comment.