Skip to content

Commit 37cf894

Browse files
authored
Merge pull request #1 from PatrickG/master
Added tests
2 parents 460bb3a + e5c2056 commit 37cf894

File tree

8 files changed

+142
-2
lines changed

8 files changed

+142
-2
lines changed

packages/kit/src/runtime/client/router.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ export class Router {
371371

372372
if (incorrect) {
373373
info.path = has_trailing_slash ? info.path.slice(0, -1) : info.path + '/';
374-
history.replaceState({}, '', `${this.base}${info.path}${location.search}`);
374+
history.replaceState(history.state || {}, '', `${this.base}${info.path}${location.search}`);
375375
}
376376
}
377377

packages/kit/test/apps/basics/src/routes/routing/_tests.js

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,113 @@ export default function (test, is_dev) {
230230
assert.equal(page.url(), 'https://www.google.com/');
231231
});
232232

233+
234+
test('history index gets set on first render', '/routing/history/a', async ({ js, page }) => {
235+
if (js) {
236+
const state = await page.evaluate('history.state');
237+
assert.equal(state?.['sveltekit:index'], 0);
238+
}
239+
});
240+
241+
test('history index increases after navigating by clicking a link', '/routing/history/a', async ({ js, page, clicknav }) => {
242+
if (js) {
243+
await clicknav('[href="/routing/history/b"]');
244+
const state = await page.evaluate('history.state');
245+
assert.equal(state?.['sveltekit:index'], 1);
246+
}
247+
});
248+
249+
test('history index increases after navigating by using goto', '/routing/history/a', async ({ js, app, base, page }) => {
250+
if (js) {
251+
await app.goto(base + '/routing/history/b');
252+
const state = await page.evaluate('history.state');
253+
assert.equal(state?.['sveltekit:index'], 1);
254+
}
255+
});
256+
257+
test('history index stays after navigating by using goto with replaceState', '/routing/history/a', async ({ js, app, base, page }) => {
258+
if (js) {
259+
await app.goto(base + '/routing/history/b', { replaceState: true });
260+
const state = await page.evaluate('history.state');
261+
assert.equal(state?.['sveltekit:index'], 0);
262+
}
263+
});
264+
265+
test('history index stays after fixing tralingSlash', '/routing/history/a', async ({ js, app, base, page }) => {
266+
if (js) {
267+
await app.goto(base + '/routing/history/b/');
268+
const state = await page.evaluate('history.state');
269+
assert.equal(state?.['sveltekit:index'], 1);
270+
}
271+
})
272+
273+
test('history index decreases after navigating back', '/routing/history/a', async ({ js, clicknav, app, base, page }) => {
274+
if (js) {
275+
await clicknav('[href="/routing/history/b"]');
276+
await app.goto(base + '/routing/history/c');
277+
await page.goBack();
278+
const state1 = await page.evaluate('history.state');
279+
assert.equal(state1?.['sveltekit:index'], 1);
280+
await clicknav('button');
281+
const state2 = await page.evaluate('history.state');
282+
assert.equal(state2?.['sveltekit:index'], 0);
283+
await clicknav('[href="/routing/history/b"]');
284+
const state3 = await page.evaluate('history.state');
285+
assert.equal(state3?.['sveltekit:index'], 1);
286+
}
287+
});
288+
289+
test('history index survives a reload', '/routing/history/a', async ({ js, clicknav, app, base, page }) => {
290+
if (js) {
291+
await clicknav('[href="/routing/history/b"]');
292+
await page.reload({ waitUntil: 'networkidle' });
293+
await app.goto(base + '/routing/history/c');
294+
const state = await page.evaluate('history.state');
295+
assert.equal(state?.['sveltekit:index'], 2);
296+
}
297+
});
298+
299+
test('onBeforeNavigate can prevent navigation by clicking a link', '/routing/history/a', async ({ js, clicknav, page, app, base }) => {
300+
if (js) {
301+
await app.goto(base + '/routing/history/prevent-navigation');
302+
303+
try {
304+
await clicknav('[href="/routing/history/b"]');
305+
assert.unreachable('should have thrown');
306+
} catch (err) {
307+
assert.instance(err, Error);
308+
assert.match(err.message, 'Timed out');
309+
}
310+
311+
const state = await page.evaluate('history.state');
312+
assert.equal(state?.['sveltekit:index'], 1);
313+
assert.equal(page.url(), base + '/routing/history/prevent-navigation');
314+
assert.equal(await page.innerHTML('pre'), 'true', 'onBeforeNavigate not triggered');
315+
}
316+
});
317+
318+
test('onBeforeNavigate can prevent navigation by using goto', '/routing/history/a', async ({ js, page, app, base }) => {
319+
if (js) {
320+
await app.goto(base + '/routing/history/prevent-navigation-promise');
321+
await app.goto(base + '/routing/history/b');
322+
const state = await page.evaluate('history.state');
323+
assert.equal(state?.['sveltekit:index'], 1);
324+
assert.equal(page.url(), base + '/routing/history/prevent-navigation-promise');
325+
assert.equal(await page.innerHTML('pre'), 'true', 'onBeforeNavigate not triggered');
326+
}
327+
});
328+
329+
test('onBeforeNavigate can prevent navigation using the browser controls', '/routing/history/a', async ({ js, page, app, base }) => {
330+
if (js) {
331+
await app.goto(base + '/routing/history/prevent-navigation');
332+
await page.goBack();
333+
const state = await page.evaluate('history.state');
334+
assert.equal(state?.['sveltekit:index'], 1);
335+
assert.equal(page.url(), base + '/routing/history/prevent-navigation');
336+
assert.equal(await page.innerHTML('pre'), 'true', 'onBeforeNavigate not triggered');
337+
}
338+
});
339+
233340
// skipping this test because it causes a bunch of failures locally
234341
test.skip('watch new route in dev', '/routing', async ({ page, base, js, watcher }) => {
235342
if (!is_dev || js) {
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<h1>a</h1>
2+
<a href="/routing/history/b">b</a>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<h1>b</h1>
2+
<button on:click={() => history.back()}>go back</button>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<h1>c</h1>
2+
<button on:click={() => history.back()}>go back</button>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<script>
2+
import { onBeforeNavigate } from '$app/navigation';
3+
4+
let triggered = false;
5+
onBeforeNavigate(async () => {
6+
triggered = true;
7+
return false;
8+
});
9+
</script>
10+
11+
<h1>prevent navigation promise</h1>
12+
<a href="/routing/history/b">b</a>
13+
<pre>{triggered}</pre>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<script>
2+
import { onBeforeNavigate } from '$app/navigation';
3+
4+
let triggered = false;
5+
onBeforeNavigate(() => {
6+
triggered = true;
7+
return false;
8+
});
9+
</script>
10+
11+
<h1>prevent navigation</h1>
12+
<a href="/routing/history/b">b</a>
13+
<pre>{triggered}</pre>

packages/kit/test/test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,10 @@ async function setup({ port }) {
102102
app: {
103103
/**
104104
* @param {string} url
105+
* @param {{ replaceState?: boolean; noScroll?: boolean }} opts
105106
* @returns {Promise<void>}
106107
*/
107-
goto: (url) => pages.js.evaluate((url) => goto(url), url),
108+
goto: (url, opts = {}) => pages.js.evaluate(({ url, opts }) => goto(url, opts), { url, opts }),
108109

109110
/**
110111
* @param {string} url

0 commit comments

Comments
 (0)