Skip to content

Commit 6dea533

Browse files
committed
test(cookbook-app-react-router): stop reading window.location in tests
1 parent 9f1822c commit 6dea533

8 files changed

Lines changed: 19 additions & 27 deletions

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"@equinor/fusion-framework-cookbook-app-react-router": patch
3+
---
4+
5+
Tests no longer read or seed `window.location`/`window.history`. Now that
6+
`resolveFusion` defaults navigation to in-memory history, real browser
7+
location never reflects the app's navigation state — assertions read
8+
`app.navigation.path.pathname` instead, and link `href` assertions match on
9+
path rather than full origin.

cookbooks/app-react-router/src/__tests__/routing-with-api-mock.test.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import App from '../App';
99
testWithApiMock(
1010
'navigates to Products and renders the loaded, seeded catalogue',
1111
async ({ render }) => {
12-
window.history.pushState(null, '', '/');
1312
const { getByTitle, getByText, unmount } = await render(<App />);
1413

1514
await getByTitle('Products').click();
@@ -23,7 +22,6 @@ testWithApiMock(
2322
testWithApiMock(
2423
'navigates to a product detail route and renders the loaded product',
2524
async ({ render, app }) => {
26-
window.history.pushState(null, '', '/');
2725
const { getByText, unmount } = await render(<App />);
2826

2927
app.navigation.navigate('/products/1');
@@ -37,7 +35,6 @@ testWithApiMock(
3735
testWithApiMock(
3836
'navigates to Users and renders the loaded, seeded directory',
3937
async ({ render }) => {
40-
window.history.pushState(null, '', '/');
4138
const { getByTitle, getByText, unmount } = await render(<App />);
4239

4340
await getByTitle('Users').click();

cookbooks/app-react-router/src/__tests__/routing.test.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,16 @@ test('navigates to a route nested under prefix() and renders it', async ({ rende
3232
});
3333

3434
test('navigation module navigates the app to a new route', async ({ render, app }) => {
35-
// the browser page (and its history) is shared across tests in this file, so start from a
36-
// known location rather than assuming this is the first test to touch it
37-
window.history.pushState(null, '', '/');
38-
3935
const { getByRole, unmount } = await render(<App />);
4036

41-
expect(window.location.pathname).toBe('/');
37+
expect(app.navigation.path.pathname).toBe('/');
4238

4339
app.navigation.navigate('/pages/error-test');
4440

4541
// error-test's loader always throws, so the router's error boundary is what renders here,
4642
// not the route's default-exported component.
4743
await expect.element(getByRole('heading', { name: /error encountered/i })).toBeInTheDocument();
48-
expect(window.location.pathname).toBe('/pages/error-test');
44+
expect(app.navigation.path.pathname).toBe('/pages/error-test');
4945

5046
await unmount();
5147
});

cookbooks/app-react-router/src/components/Navigation.test.tsx

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import { testWithRouter } from '../__tests__/test-with-router';
44
import { Navigation } from './Navigation';
55

66
testWithRouter('renders a sidebar link for each top-level page', async ({ render }) => {
7-
window.history.pushState(null, '', '/');
8-
97
const { getByText, unmount } = await render(<Navigation />);
108

119
await expect.element(getByText('Home')).toBeInTheDocument();
@@ -16,30 +14,26 @@ testWithRouter('renders a sidebar link for each top-level page', async ({ render
1614
await unmount();
1715
});
1816

19-
testWithRouter('navigating to a page updates the URL', async ({ render }) => {
20-
window.history.pushState(null, '', '/');
21-
17+
testWithRouter('navigating to a page updates the URL', async ({ render, app }) => {
2218
const { getByText, unmount } = await render(<Navigation />);
2319

2420
await getByText('Products').click();
2521

26-
expect(window.location.pathname).toBe('/products');
22+
await vi.waitFor(() => expect(app.navigation.path.pathname).toBe('/products'));
2723

2824
await unmount();
2925
});
3026

31-
testWithRouter('the active link swaps when navigating to a different page', async ({ render }) => {
32-
window.history.pushState(null, '', '/');
33-
27+
testWithRouter('the active link swaps when navigating to a different page', async ({ render, app }) => {
3428
const { getByText, unmount } = await render(<Navigation />);
3529

3630
// active state is only reflected as a styled-components color, not a DOM attribute
3731
const colorOf = (label: string) => getComputedStyle(getByText(label).element()).color;
3832

3933
// navigate via the router's own click handler first, so its internal location is
40-
// guaranteed to be in sync with the URL rather than relying on the initial pushState above
34+
// guaranteed to be in sync rather than relying on the initial location above
4135
await getByText('Home').click();
42-
await vi.waitFor(() => expect(window.location.pathname).toBe('/'));
36+
await vi.waitFor(() => expect(app.navigation.path.pathname).toBe('/'));
4337

4438
// Users is never navigated to in this test, so its color is a stable "inactive" reference
4539
const inactiveColor = colorOf('Users');

cookbooks/app-react-router/src/components/product/ProductCard.test.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ testWithRouter(
1919
await expect.element(getByText(/ in stock/i)).toBeInTheDocument();
2020

2121
const link = getByRole('link', { name: /view details/i });
22-
await expect
23-
.element(link)
24-
.toHaveAttribute('href', `${window.location.origin}/products/${baseProduct.id}`);
22+
await expect.element(link).toHaveAttribute('href', expect.stringContaining(`/products/${baseProduct.id}`));
2523

2624
await unmount();
2725
},

cookbooks/app-react-router/src/routes/layout.test.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import Layout from './layout';
88
test('renders the header, sidebar navigation, and the matched child route content', async ({
99
render,
1010
}) => {
11-
window.history.pushState(null, '', '/');
1211
const routes: RouteObject[] = [
1312
{
1413
path: '/',
@@ -32,7 +31,6 @@ test('renders the loader while a navigation is pending, then the outlet once it
3231
render,
3332
app,
3433
}) => {
35-
window.history.pushState(null, '', '/');
3634
// held open until the assertion below has observed the loading state
3735
let resolveLoader = (_value?: unknown) => {};
3836
const pendingLoad = new Promise((resolve) => {

cookbooks/app-react-router/src/routes/products/[id]/index.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ testWithRouter('renders the loaded product in the details view', async ({ render
2626
await expect.element(getByText(/ in stock/i)).toBeInTheDocument();
2727

2828
const backLink = getByRole('link', { name: /back to products/i });
29-
await expect.element(backLink).toHaveAttribute('href', `${window.location.origin}/products`);
29+
await expect.element(backLink).toHaveAttribute('href', expect.stringContaining('/products'));
3030

3131
await unmount();
3232
});

cookbooks/app-react-router/src/routes/users/[id]/index.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ testWithRouter(
2525
await expect.element(getByText(user.name)).toBeInTheDocument();
2626

2727
const backLink = getByRole('link', { name: /back to users/i });
28-
await expect.element(backLink).toHaveAttribute('href', `${window.location.origin}/users`);
28+
await expect.element(backLink).toHaveAttribute('href', expect.stringContaining('/users'));
2929

3030
await unmount();
3131
},

0 commit comments

Comments
 (0)