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

Added tests #1

Merged
merged 3 commits into from
Dec 28, 2021
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
2 changes: 1 addition & 1 deletion packages/kit/src/runtime/client/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ export class Router {

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

Expand Down
107 changes: 107 additions & 0 deletions packages/kit/test/apps/basics/src/routes/routing/_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,113 @@ export default function (test, is_dev) {
assert.equal(page.url(), 'https://www.google.com/');
});


test('history index gets set on first render', '/routing/history/a', async ({ js, page }) => {
if (js) {
const state = await page.evaluate('history.state');
assert.equal(state?.['sveltekit:index'], 0);
}
});

test('history index increases after navigating by clicking a link', '/routing/history/a', async ({ js, page, clicknav }) => {
if (js) {
await clicknav('[href="/routing/history/b"]');
const state = await page.evaluate('history.state');
assert.equal(state?.['sveltekit:index'], 1);
}
});

test('history index increases after navigating by using goto', '/routing/history/a', async ({ js, app, base, page }) => {
if (js) {
await app.goto(base + '/routing/history/b');
const state = await page.evaluate('history.state');
assert.equal(state?.['sveltekit:index'], 1);
}
});

test('history index stays after navigating by using goto with replaceState', '/routing/history/a', async ({ js, app, base, page }) => {
if (js) {
await app.goto(base + '/routing/history/b', { replaceState: true });
const state = await page.evaluate('history.state');
assert.equal(state?.['sveltekit:index'], 0);
}
});

test('history index stays after fixing tralingSlash', '/routing/history/a', async ({ js, app, base, page }) => {
if (js) {
await app.goto(base + '/routing/history/b/');
const state = await page.evaluate('history.state');
assert.equal(state?.['sveltekit:index'], 1);
}
})

test('history index decreases after navigating back', '/routing/history/a', async ({ js, clicknav, app, base, page }) => {
if (js) {
await clicknav('[href="/routing/history/b"]');
await app.goto(base + '/routing/history/c');
await page.goBack();
const state1 = await page.evaluate('history.state');
assert.equal(state1?.['sveltekit:index'], 1);
await clicknav('button');
const state2 = await page.evaluate('history.state');
assert.equal(state2?.['sveltekit:index'], 0);
await clicknav('[href="/routing/history/b"]');
const state3 = await page.evaluate('history.state');
assert.equal(state3?.['sveltekit:index'], 1);
}
});

test('history index survives a reload', '/routing/history/a', async ({ js, clicknav, app, base, page }) => {
if (js) {
await clicknav('[href="/routing/history/b"]');
await page.reload({ waitUntil: 'networkidle' });
await app.goto(base + '/routing/history/c');
const state = await page.evaluate('history.state');
assert.equal(state?.['sveltekit:index'], 2);
}
});

test('onBeforeNavigate can prevent navigation by clicking a link', '/routing/history/a', async ({ js, clicknav, page, app, base }) => {
if (js) {
await app.goto(base + '/routing/history/prevent-navigation');

try {
await clicknav('[href="/routing/history/b"]');
assert.unreachable('should have thrown');
} catch (err) {
assert.instance(err, Error);
assert.match(err.message, 'Timed out');
}

const state = await page.evaluate('history.state');
assert.equal(state?.['sveltekit:index'], 1);
assert.equal(page.url(), base + '/routing/history/prevent-navigation');
assert.equal(await page.innerHTML('pre'), 'true', 'onBeforeNavigate not triggered');
}
});

test('onBeforeNavigate can prevent navigation by using goto', '/routing/history/a', async ({ js, page, app, base }) => {
if (js) {
await app.goto(base + '/routing/history/prevent-navigation-promise');
await app.goto(base + '/routing/history/b');
const state = await page.evaluate('history.state');
assert.equal(state?.['sveltekit:index'], 1);
assert.equal(page.url(), base + '/routing/history/prevent-navigation-promise');
assert.equal(await page.innerHTML('pre'), 'true', 'onBeforeNavigate not triggered');
}
});

test('onBeforeNavigate can prevent navigation using the browser controls', '/routing/history/a', async ({ js, page, app, base }) => {
if (js) {
await app.goto(base + '/routing/history/prevent-navigation');
await page.goBack();
const state = await page.evaluate('history.state');
assert.equal(state?.['sveltekit:index'], 1);
assert.equal(page.url(), base + '/routing/history/prevent-navigation');
assert.equal(await page.innerHTML('pre'), 'true', 'onBeforeNavigate not triggered');
}
});

// skipping this test because it causes a bunch of failures locally
test.skip('watch new route in dev', '/routing', async ({ page, base, js, watcher }) => {
if (!is_dev || js) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>a</h1>
<a href="/routing/history/b">b</a>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>b</h1>
<button on:click={() => history.back()}>go back</button>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>c</h1>
<button on:click={() => history.back()}>go back</button>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<script>
import { onBeforeNavigate } from '$app/navigation';

let triggered = false;
onBeforeNavigate(async () => {
triggered = true;
return false;
});
</script>

<h1>prevent navigation promise</h1>
<a href="/routing/history/b">b</a>
<pre>{triggered}</pre>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<script>
import { onBeforeNavigate } from '$app/navigation';

let triggered = false;
onBeforeNavigate(() => {
triggered = true;
return false;
});
</script>

<h1>prevent navigation</h1>
<a href="/routing/history/b">b</a>
<pre>{triggered}</pre>
3 changes: 2 additions & 1 deletion packages/kit/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,10 @@ async function setup({ port }) {
app: {
/**
* @param {string} url
* @param {{ replaceState?: boolean; noScroll?: boolean }} opts
* @returns {Promise<void>}
*/
goto: (url) => pages.js.evaluate((url) => goto(url), url),
goto: (url, opts = {}) => pages.js.evaluate(({ url, opts }) => goto(url, opts), { url, opts }),

/**
* @param {string} url
Expand Down