UNPKG

@rr0/cms

Version:

RR0 Content Management System (CMS)

96 lines (95 loc) 3.96 kB
import { beforeEach, describe, expect, test } from "@javarome/testscript"; import { cmsTestUtil } from "../../../test/index.js"; import { rr0TestCases } from "./RR0TestCases.js"; import { DatasourceTestCase } from "../DatasourceTestCase.js"; import { Level2Date as EdtfDate } from "@rr0/time"; import { HtmlTag } from "../../../util/html/HtmlTag.js"; import { RR0Datasource } from "./RR0Datasource.js"; import { TimeTextBuilder } from "../../text/TimeTextBuilder.js"; import { RR0CaseSummaryMapper } from "./RR0CaseSummaryMapper.js"; import { RR0FileDatasource } from "./RR0FileDatasource.js"; import { NamedPlace } from "@rr0/place"; export class RR0TestDatasource extends RR0Datasource { constructor() { super(); this.timeTextBuilder = new TimeTextBuilder(cmsTestUtil.intlOptions); } async readCases(_context) { return rr0TestCases; } } export class RR0TestMapping { constructor(actions) { this.actions = actions; this.datasource = new RR0TestDatasource(); } init(build) { this.mapper = new RR0CaseSummaryMapper(new URL("https://rr0.org"), "time", ["Beau, Jérôme"]); this.backupDatasource = new class extends RR0FileDatasource { }(this.mapper); return this; } } describe("RR0CaseSource", () => { const rr0TestMapping = new RR0TestMapping({ read: ["fetch"], write: [] }); const testCase = new class extends DatasourceTestCase { constructor(mapping, sourceCases) { super(mapping, sourceCases); } getTime(c) { return c.time; } sortComparator(c1, c2) { return !c1.time || c2.time && c1.time.isBefore(c2.time) ? -1 : !c2.time || c1.time.isAfter(c2.time) ? 1 : 0; } /** * Specialization of sources for RR0 cases */ expectedSourceStr(context, expectedSources, _nativeCase) { return expectedSources.map(source => { const sourceItems = []; let authorStr = source.authors.map(author => `<span class="people">${author}</span>`).join(" &amp; "); if (authorStr) { authorStr += "&nbsp;: "; } if (source.title) { sourceItems.push(source.title); } const publication = source.publication; if (publication) { if (publication.publisher) { sourceItems.push(`<i>${publication.publisher}</i>`); } if (publication.time) { const sourceContext = context.clone(); sourceContext.time.date = source.publication.time; const timeStr = this.timeTextBuilder.build(sourceContext); sourceItems.push(timeStr); } } const index = source.index; if (index) { sourceItems.push(index); } return " " + HtmlTag.toString("span", authorStr + sourceItems.join(", "), { class: "source" }); }).join(""); } }(rr0TestMapping.init(cmsTestUtil), rr0TestCases); let context; beforeEach(() => { context = cmsTestUtil.time.newHtmlContext("1/9/7/0/03/index.html"); context.time.setYear(1970); context.time.setMonth(3); }); test("fetch", async () => { await testCase.testFetch(context); }); test("render", { skip: true }, async () => { await testCase.testRender(context); }); test("id", async () => { expect(RR0Datasource.id(EdtfDate.fromString("1972-08-12"), new NamedPlace("Chatillon"))).toBe("1972-08-12$Chatillon"); expect(RR0Datasource.id(EdtfDate.fromString("1972-08-12"), undefined)).toBe("1972-08-12$"); expect(RR0Datasource.id(undefined, undefined)).toBe("rr0-1$"); }); });