Files
ccn/Services/Website/Tests/Library/Cases/Internal/Pages/IndexPageTests.swift
T

114 lines
3.7 KiB
Swift

import Elementary
import Foundation
import Testing
@testable import WebsiteLibrary
@Suite(
"IndexPage page",
.tags(.page)
)
struct IndexPageTests {
// MARK: Functional tests
@Test
func `renders its markup`() {
let html = IndexPage(
locale: .init(identifier: "en")
).render()
#expect(html.contains("<!DOCTYPE html>"))
#expect(html.contains(#"lang="en""#))
#expect(html.contains("/css/shared.css"))
#expect(html.contains("/css/index.css"))
#expect(html.contains("/favicon.ico"))
#expect(html.contains("/icon.svg"))
#expect(html.contains("/apple-touch-icon.png"))
#expect(html.contains("/site.webmanifest"))
#expect(html.contains(#"media="(prefers-color-scheme: dark)""#))
#expect(html.contains("Back issues, from shops you can vet first."))
#expect(html.contains("/js/shared.js"))
#expect(html.contains("/js/index.js"))
}
@Test
func `renders the site frame around its content`() {
let html = IndexPage(
locale: .init(identifier: "en")
).render()
#expect(html.contains(#"class="skip-link""#))
#expect(html.contains(#"class="site-header""#))
#expect(html.contains(#"<main id="main">"#))
#expect(html.contains(#"class="site-footer""#))
}
@Test
func `renders no inline styles`() {
// The Content-Security-Policy is `default-src 'self'`, which blocks `style` attributes and elements alike.
let html = IndexPage(
locale: .init(identifier: "en")
).render()
#expect(!html.contains("style="))
#expect(!html.contains("<style"))
}
@Test
func `preloads every font face unversioned`() {
// A preload URL must match the stylesheet's `@font-face` source exactly, or the browser fetches the font twice.
let html = IndexPage(
locale: .init(identifier: "en"),
assetVersion: "0123456789abcdef"
).render()
#expect(IndexPage.preloadedFonts.count == 3)
for font in IndexPage.preloadedFonts {
#expect(html.contains(#"<link rel="preload" href="\#(font.urlPath(for: .woff2))" as="font""#))
}
}
@Test
func `renders no canonical URL until an origin is given`() {
#expect(!IndexPage(locale: .init(identifier: "en")).render().contains(#"rel="canonical""#))
}
@Test
func `renders the canonical URL as the bare origin for the default language`() {
// The root is the one path with no trailing slash for `TrailingSlashRedirectMiddleware` to strip.
let html = IndexPage(
locale: .init(identifier: "en"),
siteOrigin: "https://example.com"
).render()
#expect(html.contains(#"<link rel="canonical" href="https://example.com">"#))
}
@Test
func `renders no language alternates for a single-language catalog`() {
// The template serves one language, so a set naming one edition would tell a crawler nothing.
#expect(Language.all.count == 1)
#expect(!IndexPage(
locale: .init(identifier: "en"),
siteOrigin: "https://example.com"
).render().contains("hreflang"))
}
@Test
func `renders versioned asset URLs when given a version`() {
let html = IndexPage(
locale: .init(identifier: "en"),
assetVersion: "0123456789abcdef"
).render()
#expect(html.contains("/css/shared.css?v=0123456789abcdef"))
#expect(html.contains("/css/index.css?v=0123456789abcdef"))
#expect(html.contains("/js/shared.js?v=0123456789abcdef"))
#expect(html.contains("/js/index.js?v=0123456789abcdef"))
#expect(html.contains("/favicon.ico?v=0123456789abcdef"))
}
}