From f43497d69c4998b0062f3d4b53fc64887e3dfb9c Mon Sep 17 00:00:00 2001 From: Chi Cong Tran Date: Fri, 26 Sep 2025 11:59:21 +0200 Subject: [PATCH] refactoring magic strings in layout.server file (guard), including new routes and tests --- src/routes/(angemeldet)/+layout.server.ts | 8 ++++--- src/routes/index.ts | 1 + tests/Layout.test.ts | 29 +++++++++++++++++++++++ tests/fixtures.ts | 7 ++++++ 4 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 tests/Layout.test.ts diff --git a/src/routes/(angemeldet)/+layout.server.ts b/src/routes/(angemeldet)/+layout.server.ts index 94cce7a..cdde090 100644 --- a/src/routes/(angemeldet)/+layout.server.ts +++ b/src/routes/(angemeldet)/+layout.server.ts @@ -1,10 +1,12 @@ import { redirect, type ServerLoadEvent } from '@sveltejs/kit'; import type { PageServerLoad } from '../anmeldung/$types'; +import { ROUTE_NAMES } from '..'; + export const load: PageServerLoad = (event: ServerLoadEvent) => { - if (!event.locals.user && event.url.pathname !== '/anmeldung') throw redirect(303, '/anmeldung'); + if (!event.locals.user && event.url.pathname !== ROUTE_NAMES.ANMELDUNG) + throw redirect(303, ROUTE_NAMES.ANMELDUNG); return { user: event.locals.user - }; -} +}; diff --git a/src/routes/index.ts b/src/routes/index.ts index ff968dd..16daf7a 100644 --- a/src/routes/index.ts +++ b/src/routes/index.ts @@ -17,6 +17,7 @@ export const ROUTE_NAMES = { : `/view/${vorgangToken}/${tatort}`, // Anmeldung: actions + ANMELDUNG: '/anmeldung', ANMELDUNG_LOGIN: '/anmeldung?/login', ANMELDUNG_LOGOUT: '/anmeldung?/logout' }; diff --git a/tests/Layout.test.ts b/tests/Layout.test.ts new file mode 100644 index 0000000..cce99e5 --- /dev/null +++ b/tests/Layout.test.ts @@ -0,0 +1,29 @@ +import { describe, test, expect } from 'vitest'; +import { load } from '../src/routes/(angemeldet)/+layout.server'; +import { ROUTE_NAMES } from '../src/routes'; +import { baseData, mockEvent } from './fixtures'; + +describe('+layout.server load(): Teste korrekte URL', () => { + test('Werfe redirect zu /anmeldung wenn User nicht eingeloggt', async () => { + const mockEvent = { + locals: { + user: null + }, + url: new URL(`https://example.com/not-anmeldung`) + }; + try { + load(mockEvent); + throw new Error('Expected load() to throw'); + } catch (err) { + expect(err.status).toBe(303); + expect(err.location).toBe(ROUTE_NAMES.ANMELDUNG); + } + }); +}); + +describe('+layout.server load(): Teste erfolgreichen Pfad', () => { + test('Werfe kein Fehler', async () => { + const result = load(mockEvent); + expect(result).toEqual({ user: baseData.user }); + }); +}); \ No newline at end of file diff --git a/tests/fixtures.ts b/tests/fixtures.ts index 270a845..fc56cc0 100644 --- a/tests/fixtures.ts +++ b/tests/fixtures.ts @@ -44,3 +44,10 @@ export const baseData = { url: `https://example.com/${testVorgangsList[0].vorgangToken}`, crimeNames: ['modell-A', 'Fall-A'] }; + +export const mockEvent = { + locals: { + user: baseData.user + }, + url: new URL(`https://example.com/anmeldung`) +};