Unit tests for endpoints and renderer
2 minutes read
Tests for server-side logic: sitemap generation helpers, RSS feed sanitizer, and the page renderer. Most of these functions are currently not exported and need to be made testable first.
Part of Add tests and CI integration (sub-task 3 of 4). Requires testing environment setup first. Can be done in parallel with utility tests.
Prerequisite: export helper functions
Before writing tests, these currently-private functions need to be exported (or extracted into separate testable modules):
sitemap-appendix.xml/+server.js:getRouteFromPath,calculatePriority,extractSectionsfeed.xml/+server.js:sanitizeContentForRSSpage-renderer.svelte.js:getCoverUrl,createSvelteKitStateMocks,createRenderContext
Sitemap helpers
From src/routes/sitemap-appendix.xml/+server.js:
getRouteFromPath(filePath)
- Strips
+page.svelteand..prefix - Prepends base path
calculatePriority(path)
/(root) returns"0.9"- Each nesting level reduces by 0.1
- Minimum priority is
"0.1"(deeply nested paths don't go below) - Returns string with one decimal place
extractSections(pagePaths)
- Extracts parent sections from page paths
- Returns sorted, deduplicated array
- Each section has trailing slash
- Handles deeply nested paths (creates all intermediate sections)
Feed sanitizer
From src/routes/feed.xml/+server.js:
sanitizeContentForRSS(html)
- Removes
onload,onerrorand other event handler attributes - Removes single-quoted event handlers
- Removes
styleattributes (both quote types) - Removes
javascript:URLs - Removes
data-*attributes - Removes
classattributes - Removes
idattributes - Preserves actual content text and tag structure
- Test with HTML containing all pattern types combined
Page renderer
From src/lib/page-renderer.svelte.js:
getCoverUrl(cover)
- Returns empty string for
null/undefined/falsy - Returns the string directly if
coveris a string - Extracts
img.srcfrom object form - Returns empty string if object has no
img.src
createSvelteKitStateMocks(filePath, pageData)
- Returns object with
url,params,route,data,form,error,status urlis a valid URL object based on filePathdata.infomerges defaults with providedpageData- Default date is
'01-01-2024'
createRenderContext
- Returns a Map
- Has
'meta'key with a Store instance - Has
'__request__'key with page mock
Date formatting in renderBlogPage
DD-MM-YYYYconverts toYYYY-MM-DDT00:00:00-00:00- Fallback to
01-01-2024when no date provided
EXCLUDE_PATHS filtering
- Pages matching excluded paths return
null - Pages not in the set are rendered normally
Read next