Skip to content

Commit 2aaecc0

Browse files
committed
refactor(KitFallback): Simplify and add testing
1 parent a31319a commit 2aaecc0

5 files changed

Lines changed: 99 additions & 7 deletions

File tree

src/lib/KitFallback.server.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { createRawSnippet } from "svelte";
2+
import { afterAll, beforeAll, describe, expect, test, vi } from "vitest";
3+
import { render } from "svelte/server";
4+
import { init } from "./init.js";
5+
6+
// @ts-expect-error Not all properties are mocked.
7+
vi.mock(import("$app/state"), () => {
8+
return {
9+
page: {
10+
url: new URL("http://localhost/"),
11+
state: { path: "/", hash: {} },
12+
},
13+
};
14+
});
15+
16+
describe("KitFallback", () => {
17+
let cleanup: () => void;
18+
beforeAll(() => {
19+
cleanup = init();
20+
});
21+
afterAll(() => {
22+
cleanup();
23+
});
24+
25+
test("Should not render when not in browser environment.", async () => {
26+
const KitFallbackTester = (await import("./KitFallbackTester.test.svelte")).default;
27+
const testId = "evidence-01";
28+
const children = createRawSnippet(() => ({
29+
render() {
30+
return `<span data-testid="${testId}">Here</span>`;
31+
},
32+
}));
33+
const c = render(KitFallbackTester, { props: { children } });
34+
expect(c.body).not.toContain(testId);
35+
});
36+
});

src/lib/KitFallback.svelte

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import { Fallback } from '@svelte-router/core';
44
import type { ComponentProps } from 'svelte';
55
6-
let { when, ...restProps }: ComponentProps<typeof Fallback> = $props();
6+
let { ...restProps }: ComponentProps<typeof Fallback> = $props();
77
</script>
88

9-
<Fallback when={(rs, noMatches) => browser && (when?.(rs, noMatches) || (!when && noMatches))} {...restProps} />
9+
{#if browser}
10+
<Fallback {...restProps} />
11+
{/if}

src/lib/KitFallback.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { createRawSnippet } from "svelte";
2+
import { afterAll, afterEach, beforeAll, describe, expect, test, vi } from "vitest";
3+
import { render } from "vitest-browser-svelte";
4+
import { init } from "./init.js";
5+
6+
// @ts-expect-error Not all properties are mocked.
7+
vi.mock(import("$app/state"), () => {
8+
return {
9+
page: {
10+
url: new URL("http://localhost/"),
11+
state: { path: "/", hash: {} },
12+
},
13+
};
14+
});
15+
16+
describe("KitFallback", () => {
17+
let cleanup: () => void;
18+
beforeAll(() => {
19+
cleanup = init();
20+
});
21+
afterAll(() => {
22+
cleanup();
23+
});
24+
25+
test("Should render when in browser environment.", async () => {
26+
const KitFallbackTester = (await import("./KitFallbackTester.test.svelte")).default;
27+
const testId = "evidence-01";
28+
const children = createRawSnippet(() => ({
29+
render() {
30+
return `<span data-testid="${testId}">Here</span>`;
31+
},
32+
}));
33+
const { getByTestId } = render(KitFallbackTester, { children });
34+
expect(getByTestId(testId).element()).toBeDefined();
35+
});
36+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<script lang="ts">
2+
import { Router } from "@svelte-router/core";
3+
import KitFallback from "./KitFallback.svelte";
4+
import type { Snippet } from "svelte";
5+
6+
type Props = {
7+
children?: Snippet;
8+
};
9+
10+
let { children }: Props = $props();
11+
</script>
12+
13+
<Router>
14+
<KitFallback>
15+
{@render children?.()}
16+
</KitFallback>
17+
</Router>

vite.config.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { defineConfig } from 'vitest/config';
22
import { sveltekit } from '@sveltejs/kit/vite';
3-
import { playwright, PlaywrightBrowserProvider } from '@vitest/browser-playwright';
3+
import { playwright } from '@vitest/browser-playwright';
44

55
export default defineConfig({
66
plugins: [sveltekit()],
@@ -9,6 +9,7 @@ export default defineConfig({
99
},
1010
test: {
1111
expect: { requireAssertions: true },
12+
includeTaskLocation: true,
1213
projects: [
1314
{
1415
extends: './vite.config.ts',
@@ -17,10 +18,10 @@ export default defineConfig({
1718
browser: {
1819
enabled: true,
1920
provider: playwright(),
20-
instances: [{ browser: 'firefox' }]
21+
instances: [{ browser: 'firefox' }, { browser: 'chromium' }]
2122
},
22-
include: ['src/**/*.svelte.{test,spec}.{js,ts}'],
23-
exclude: ['src/lib/server/**'],
23+
include: ['src/**/*.svelte.{test,spec}.{js,ts}', 'src/**/*.{test,spec}.{js,ts}'],
24+
exclude: ['src/lib/server/**', 'src/**/*.server.{test,spec}.{js,ts}'],
2425
setupFiles: ['./vitest-setup-client.ts']
2526
}
2627
},
@@ -29,7 +30,7 @@ export default defineConfig({
2930
test: {
3031
name: 'server',
3132
environment: 'node',
32-
include: ['src/**/*.{test,spec}.{js,ts}'],
33+
include: ['src/**/*.server.{test,spec}.{js,ts}'],
3334
exclude: ['src/**/*.svelte.{test,spec}.{js,ts}']
3435
}
3536
}

0 commit comments

Comments
 (0)