Skip to content

Commit bae081a

Browse files
committed
chore(frame-router): refactoring to pass tests, adding tests for query params
1 parent 79ec9ea commit bae081a

File tree

2 files changed

+55
-5
lines changed

2 files changed

+55
-5
lines changed

packages/iframe-coordinator/src/elements/frame-router.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,9 +269,12 @@ export default class FrameRouterElement extends HTMLElement {
269269

270270
private _processHostUrl(hostUrl: string) {
271271
const hostUrlObject = new URL(hostUrl);
272-
if (window.location.hash) {
273-
hostUrlObject.pathname = stripTrailingSlash(hostUrlObject.pathname) + "/";
274-
hostUrlObject.hash = "#";
272+
if (!hostUrlObject.hash) {
273+
hostUrlObject.pathname = stripTrailingSlash(hostUrlObject.pathname);
274+
if (window.location.hash) {
275+
hostUrlObject.pathname += "/";
276+
hostUrlObject.hash = "#";
277+
}
275278
}
276279
return hostUrlObject.href;
277280
}

packages/iframe-coordinator/src/specs/FrameRouter.spec.ts

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ const ENV_DATA_WITH_HASH = {
1010
hostRootUrl: "https://example.com/root/#/",
1111
};
1212

13+
const ENV_DATA_WITH_QUERY = {
14+
...ENV_DATA,
15+
hostRootUrl: "https://example.com/root/?foo=bar",
16+
};
17+
18+
const ENV_DATA_WITH_QUERY_AND_HASH = {
19+
...ENV_DATA,
20+
hostRootUrl: "https://example.com/root/?foo=bar#/",
21+
};
22+
1323
describe("The frame router element", () => {
1424
beforeAll(() => {
1525
window.customElements.define("frame-router", FrameRouterElement);
@@ -25,7 +35,7 @@ describe("The frame router element", () => {
2535
* behavior.
2636
*/
2737
describe("Host URL management", () => {
28-
it("Removes a trailing slash on the host URL if present", () => {
38+
it("Removes a trailing slash on the host URL if present with no hash", () => {
2939
const router = new FrameRouterElement();
3040
router.clientConfig = {
3141
clients: {},
@@ -65,6 +75,43 @@ describe("The frame router element", () => {
6575
});
6676
});
6777

68-
// TODO: Test expected query behavior here
78+
it("Removes trailing slash from pathname when the provided host URL with query params; urlObject and location have no hash", () => {
79+
const router = new FrameRouterElement();
80+
router.clientConfig = {
81+
clients: {},
82+
envData: ENV_DATA_WITH_QUERY,
83+
};
84+
//@ts-ignore
85+
expect(router._envData).toEqual({
86+
...ENV_DATA_WITH_QUERY,
87+
hostRootUrl: "https://example.com/root?foo=bar",
88+
});
89+
});
90+
91+
it("Does not add slash between hash and query params if location has hash; urlObject has no hash", () => {
92+
window.location.hash = "foo";
93+
const router = new FrameRouterElement();
94+
router.clientConfig = {
95+
clients: {},
96+
envData: ENV_DATA_WITH_QUERY,
97+
};
98+
//@ts-ignore
99+
expect(router._envData).toEqual({
100+
...ENV_DATA_WITH_QUERY,
101+
hostRootUrl: "https://example.com/root/?foo=bar#",
102+
});
103+
});
104+
105+
it("Does not modify provided host URL with query params if urlObject has hash; location hash N/A", () => {
106+
const router = new FrameRouterElement();
107+
router.clientConfig = {
108+
clients: {},
109+
envData: ENV_DATA_WITH_QUERY_AND_HASH,
110+
};
111+
//@ts-ignore
112+
expect(router._envData).toEqual({
113+
...ENV_DATA_WITH_QUERY_AND_HASH,
114+
});
115+
});
69116
});
70117
});

0 commit comments

Comments
 (0)