Skip to content

Commit cde3eff

Browse files
authored
Merge pull request #33 from jbpratt/root-navigation
fix: resolve navigation bug and set title
2 parents 8d3081f + 63a09e7 commit cde3eff

File tree

5 files changed

+74
-15
lines changed

5 files changed

+74
-15
lines changed

web/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
background-color: #ededed;
1818
}
1919
</style>
20-
<title>List Reader</title>
20+
<title>TestGrid</title>
2121
</head>
2222

2323
<body>

web/src/testgrid-data-content.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,6 @@ export class TestgridDataContent extends LitElement {
8282
this.highlightIndex(this.tabName);
8383
navigateTab(this.dashboardName, this.tabName!);
8484
});
85-
window.addEventListener('popstate', () => {
86-
console.log(window.location.pathname);
87-
console.log(window.location.pathname.split('/'));
88-
if (location.pathname.split('/').length === 2) {
89-
this.showTab = false;
90-
this.tabName = undefined;
91-
this.highlightIndex(this.tabName);
92-
navigateTab(this.dashboardName, this.tabName!);
93-
}
94-
});
9585
}
9686

9787
/**

web/src/testgrid-router.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,28 @@ export class TestgridRouter extends LitElement {
4646
},
4747
]);
4848

49+
private goToCurrentPath = () => this.router.goto(window.location.pathname);
50+
4951
/**
5052
* Lit-element lifecycle method.
5153
* Invoked when a component is added to the document's DOM.
5254
*/
5355
connectedCallback() {
5456
super.connectedCallback();
55-
window.addEventListener('location-changed', () => {
56-
this.router.goto(window.location.pathname);
57-
});
57+
window.addEventListener('location-changed', this.goToCurrentPath);
58+
window.addEventListener('popstate', this.goToCurrentPath);
59+
// Ensure initial render reflects current URL (including direct loads on nested paths)
60+
this.goToCurrentPath();
61+
}
62+
63+
/**
64+
* Lit-element lifecycle method.
65+
* Invoked when a component is removed from the document's DOM.
66+
*/
67+
disconnectedCallback() {
68+
super.disconnectedCallback();
69+
window.removeEventListener('location-changed', this.goToCurrentPath);
70+
window.removeEventListener('popstate', this.goToCurrentPath);
5871
}
5972

6073
/**

web/src/utils/navigation.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@ export function navigate(name: string) {
1515
* @param {string} dashboard
1616
* @param {string} tab
1717
*/
18-
export function navigateTab(dashboard: string, tab: string) {
18+
export function navigateTab(dashboard: string, tab?: string) {
1919
const url = new URL(window.location.href);
2020
if (tab === 'Summary' || tab === undefined) {
2121
url.pathname = `${dashboard}`;
2222
} else {
2323
url.pathname = `${dashboard}/${tab}`;
2424
}
2525
window.history.pushState(null, '', url);
26+
window.dispatchEvent(new CustomEvent('location-changed'));
2627
}

web/test/testgrid-router.test.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import {
2+
html,
3+
fixture,
4+
defineCE,
5+
unsafeStatic,
6+
expect,
7+
waitUntil,
8+
aTimeout,
9+
} from '@open-wc/testing';
10+
11+
import { TestgridRouter } from '../src/testgrid-router.js';
12+
import { navigate } from '../src/utils/navigation.js';
13+
14+
describe('Testgrid Router navigation', () => {
15+
let element: TestgridRouter;
16+
17+
beforeEach(async () => {
18+
// Ensure we start from root
19+
window.history.replaceState(null, '', '/');
20+
21+
const tagName = defineCE(class extends TestgridRouter {});
22+
const tag = unsafeStatic(tagName);
23+
element = await fixture(html`<${tag}></${tag}>`);
24+
});
25+
26+
it('renders the index at root and returns to it with history.back()', async () => {
27+
await waitUntil(
28+
() => element.shadowRoot!.querySelector('testgrid-index'),
29+
'Router did not render the index at root',
30+
{ timeout: 4000 }
31+
);
32+
expect(window.location.pathname).to.equal('/');
33+
34+
navigate('fake-dashboard-1');
35+
36+
await waitUntil(
37+
() => element.shadowRoot!.querySelector('testgrid-data-content'),
38+
'Router did not navigate to dashboard route',
39+
{ timeout: 4000 }
40+
);
41+
expect(window.location.pathname).to.equal('/fake-dashboard-1');
42+
43+
window.history.back();
44+
await aTimeout(0);
45+
46+
await waitUntil(
47+
() =>
48+
window.location.pathname === '/' &&
49+
!!element.shadowRoot!.querySelector('testgrid-index'),
50+
'Router did not navigate back to root and render index',
51+
{ timeout: 4000 }
52+
);
53+
});
54+
});
55+

0 commit comments

Comments
 (0)