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

[BUGFIX lts] ensure deserializeQueryParam is called for lazy routes #19697

Merged
Merged
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -2082,11 +2082,21 @@ export function getFullQueryParams(router: EmberRouter, state: TransitionState<R
return state['fullQueryParams'];
}

state['fullQueryParams'] = {};
Object.assign(state['fullQueryParams'], state.queryParams);
let fullQueryParamsState = {};
let haveAllRouteInfosResolved = state.routeInfos.every((routeInfo) => routeInfo.route);
rwjblue marked this conversation as resolved.
Show resolved Hide resolved

router._deserializeQueryParams(state.routeInfos, state['fullQueryParams'] as QueryParam);
return state['fullQueryParams'];
Object.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