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

119 lines
5.2 KiB
Swift

import Elementary
import NIOCore
import Persistence
import Testing
@testable import WebsiteLibrary
@Suite(
"Selling pages",
.tags(.page)
)
struct SellingPagesTests {
// MARK: GradingGuidePage tests
@Test
func `grading guide prints the whole scale and defines every defect`() {
let html = GradingGuidePage().render()
for step in Grading.scale {
#expect(html.contains("<strong>\(step.grade.oneDecimal)</strong><span>\(step.code)</span>"))
}
for defect in Grading.Defect.allCases {
#expect(html.contains(defect.label))
#expect(html.contains(defect.definition))
}
#expect(html.contains("Not at this grade"))
#expect(html.contains("/css/guide.css"))
#expect(!html.contains("noindex"))
#expect(!html.contains("style="))
}
// MARK: NewListingPage tests
@Test
func `new-listing form posts as multipart and offers the guide's vocabulary`() {
let html = NewListingPage(shop: .stub()).render()
#expect(html.contains(#"action="/sell/new" method="post" enctype="multipart/form-data""#))
#expect(html.contains(#"type="file" name="photos" accept="image/jpeg,image/png,image/webp" multiple"#))
#expect(html.contains(#"name="format" value="single-issues" checked"#))
#expect(html.contains(#"name="grading" value="slabbed" checked"#))
#expect(html.contains(#"<option value="9.4">9.4 · NM</option>"#))
#expect(html.contains(#"name="defects" value="crease""#))
#expect(html.contains(#"data-scope="collected""#))
#expect(html.contains(#"href="/grading-guide""#))
#expect(html.contains("Ships from Amsterdam, NL"))
#expect(!html.contains(#"role="alert""#))
#expect(!html.contains("style="))
}
@Test
func `new-listing form gives back what was typed, with each problem tied to its field`() {
let html = NewListingPage(
shop: .stub(),
form: ListingFormTests.form(["grading": ["raw"], "grade": ["9.4"], "defects": ["crease"], "notes": ["Light <wear>"]]),
problems: [.defects: "A colour-breaking crease caps a raw book at 7.0.", .price: "Enter the asking price in whole euros."]
).render()
#expect(html.contains("2 things to fix before publishing."))
#expect(html.contains("Photos have to be picked again"))
#expect(html.contains(#"name="series" value="Nightward""#))
#expect(html.contains(#"<option value="9.4" selected>"#))
#expect(html.contains(#"name="grading" value="raw" checked"#))
#expect(html.contains(#"name="defects" value="crease" checked"#))
#expect(html.contains(#"id="price-error">Enter the asking price in whole euros."#))
#expect(html.contains(#"aria-invalid="true" aria-describedby="price-error""#))
#expect(html.contains("Light &lt;wear&gt;"))
}
// MARK: Photo tests
@Test
func `listing shows its first photo in place of the placeholder cover`() {
let bare = ListingCard(.stub()).render()
let shot = ListingCard(.stub(photos: ["/uploads/a.jpg", "/uploads/b.jpg"])).render()
let page = ListingPage(listing: .stub(defects: ["Spine stress / ticks"], photos: ["/uploads/a.jpg", "/uploads/b.jpg"]), related: []).render()
#expect(bare.contains("cover--navy"))
#expect(!bare.contains("<img"))
#expect(shot.contains(#"<img class="photo" src="/uploads/a.jpg" alt="Photo of Nightward #1, front""#))
#expect(!shot.contains("cover--navy"))
#expect(page.contains(#"src="/uploads/b.jpg""#))
#expect(page.contains("<dt>Declared defects</dt><dd>Spine stress / ticks</dd>"))
}
// MARK: MultipartForm tests
@Test
func `multipart form tells text from files, and drops what was left blank`() throws {
let body = [
"--x", #"Content-Disposition: form-data; name="series""#, "", " Nightward ",
"--x", #"Content-Disposition: form-data; name="defects""#, "", "crease",
"--x", #"Content-Disposition: form-data; name="defects""#, "", "foxing",
"--x", #"Content-Disposition: form-data; name="issue""#, "", "",
"--x", #"Content-Disposition: form-data; name="photos"; filename="front.jpg""#, "Content-Type: image/jpeg", "", "JPEGDATA",
"--x", #"Content-Disposition: form-data; name="photos"; filename="""#, "Content-Type: application/octet-stream", "", "",
"--x--", "",
].joined(separator: "\r\n")
let form = try #require(MultipartForm(body: .init(string: body), contentType: #"multipart/form-data; boundary="x""#))
#expect(form.value("series") == "Nightward")
#expect(form.values("defects") == ["crease", "foxing"])
#expect(form.value("issue") == nil)
#expect(form.values("photos").isEmpty)
#expect(form.files("photos").map { String(buffer: $0) } == ["JPEGDATA"])
#expect(ListingForm(form).has(.defects, value: "foxing"))
}
@Test
func `multipart form is nothing without a multipart content type`() {
#expect(MultipartForm(body: .init(string: "a=b"), contentType: "application/x-www-form-urlencoded") == nil)
#expect(MultipartForm(body: .init(), contentType: nil) == nil)
}
}