Skip to content

Commit

Permalink
[test] error redirections
Browse files Browse the repository at this point in the history
  • Loading branch information
bleucitron committed Aug 21, 2021
1 parent ad62940 commit 4ac042a
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/shaggy-radios-rush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

error pages can now redirect
7 changes: 7 additions & 0 deletions packages/kit/test/apps/basics/src/routes/__error.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
<script context="module">
/** @type {import('@sveltejs/kit').ErrorLoad} */
export function load({ status, error }) {
if (error && error.message.includes('/redirect/nowhere')) {
return {
status: 307,
redirect: '/redirect/c'
};
}
return {
props: { status, error }
};
Expand Down
28 changes: 28 additions & 0 deletions packages/kit/test/apps/basics/src/routes/redirect/_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,32 @@ export default function (test, is_dev) {
'This is your custom error page saying: ""redirect" property returned from load() must be accompanied by a 3xx status code"'
);
});

test('serves redirection on error', '/redirect/crashing', async ({ base, page }) => {
assert.equal(await page.url(), `${base}/redirect/c`);
assert.equal(await page.textContent('h1'), 'c');
});

test('navigates to redirection on error', '/redirect', async ({ base, page, clicknav }) => {
await clicknav('[href="/redirect/crashing"]');

assert.equal(await page.url(), `${base}/redirect/c`);
assert.equal(await page.textContent('h1'), 'c');
});

test('serves redirection on missing page', '/redirect/nowhere', async ({ base, page }) => {
assert.equal(await page.url(), `${base}/redirect/c`);
assert.equal(await page.textContent('h1'), 'c');
});

test(
'navigates to redirection on missing page',
'/redirect',
async ({ base, page, clicknav }) => {
await clicknav('[href="/redirect/nowhere"]');

assert.equal(await page.url(), `${base}/redirect/c`);
assert.equal(await page.textContent('h1'), 'c');
}
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<script context="module">
/** @type {import('@sveltejs/kit').ErrorLoad} */
export async function load() {
return {
status: 307,
redirect: './c'
};
}
</script>

<h1>Redirecting error page</h1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script context="module">
export async function load() {
throw new Error('oopsie');
}
</script>

<h1>Crashing page</h1>
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@
<a href="/redirect/loopy/b">b (loopy)</a>

<a href="/redirect/missing-status/a">a (missing-status)</a>
<a href="/redirect/missing-status/b">b (missing-status)</a>
<a href="/redirect/missing-status/b">b (missing-status)</a>

<a href="/redirect/crashing">crashing</a>
<a href="/redirect/nowhere">nowhere</a>

0 comments on commit 4ac042a

Please sign in to comment.