|
| 1 | +import * as chai from "chai"; |
| 2 | +import { describe, it } from "mocha"; |
| 3 | + |
| 4 | +import { getDefaultViewFromQuery, resolveViewToPlugin } from "../../packages/yasr/src/defaultView.js"; |
| 5 | + |
| 6 | +const expect = chai.expect; |
| 7 | + |
| 8 | +const PLUGINS = { |
| 9 | + Table: { label: "Table" }, |
| 10 | + Geo: { label: "Geo" }, |
| 11 | + Graph: { label: "Graph" }, |
| 12 | + Chart: { label: "Chart" }, |
| 13 | + response: { label: "Response" }, |
| 14 | +}; |
| 15 | + |
| 16 | +describe("Yasr default view from a query comment", () => { |
| 17 | + describe("getDefaultViewFromQuery", () => { |
| 18 | + it("reads the view from a leading comment", () => { |
| 19 | + expect(getDefaultViewFromQuery("#defaultView:Map\nSELECT * WHERE { ?s ?p ?o }")).to.equal("Map"); |
| 20 | + }); |
| 21 | + |
| 22 | + it("allows whitespace and any keyword casing", () => { |
| 23 | + expect(getDefaultViewFromQuery(" # defaultview : Graph\nCONSTRUCT WHERE { ?s ?p ?o }")).to.equal("Graph"); |
| 24 | + }); |
| 25 | + |
| 26 | + it("finds the comment on a later line", () => { |
| 27 | + const query = "PREFIX ex: <http://example.org/>\n# a title\n#defaultView:Chart\nSELECT * WHERE { ?s ?p ?o }"; |
| 28 | + expect(getDefaultViewFromQuery(query)).to.equal("Chart"); |
| 29 | + }); |
| 30 | + |
| 31 | + it("ignores the keyword outside a comment line", () => { |
| 32 | + const query = 'SELECT * WHERE { ?s ?p "#defaultView:Map" }'; |
| 33 | + expect(getDefaultViewFromQuery(query)).to.equal(undefined); |
| 34 | + }); |
| 35 | + |
| 36 | + it("returns undefined without a comment or query", () => { |
| 37 | + expect(getDefaultViewFromQuery("SELECT * WHERE { ?s ?p ?o }")).to.equal(undefined); |
| 38 | + expect(getDefaultViewFromQuery(undefined)).to.equal(undefined); |
| 39 | + }); |
| 40 | + }); |
| 41 | + |
| 42 | + describe("resolveViewToPlugin", () => { |
| 43 | + it("matches a plugin name case-insensitively", () => { |
| 44 | + expect(resolveViewToPlugin("geo", PLUGINS)).to.equal("Geo"); |
| 45 | + expect(resolveViewToPlugin("RESPONSE", PLUGINS)).to.equal("response"); |
| 46 | + }); |
| 47 | + |
| 48 | + it("matches a plugin label", () => { |
| 49 | + expect(resolveViewToPlugin("Kaart", { Geo: { label: "Kaart" } })).to.equal("Geo"); |
| 50 | + }); |
| 51 | + |
| 52 | + it("maps Wikidata Query Service view names", () => { |
| 53 | + expect(resolveViewToPlugin("Map", PLUGINS)).to.equal("Geo"); |
| 54 | + expect(resolveViewToPlugin("BarChart", PLUGINS)).to.equal("Chart"); |
| 55 | + expect(resolveViewToPlugin("LineChart", PLUGINS)).to.equal("Chart"); |
| 56 | + }); |
| 57 | + |
| 58 | + it("returns undefined for an unknown view or a plugin that isn't available", () => { |
| 59 | + expect(resolveViewToPlugin("Timeline", PLUGINS)).to.equal(undefined); |
| 60 | + expect(resolveViewToPlugin("Map", { Table: { label: "Table" } })).to.equal(undefined); |
| 61 | + }); |
| 62 | + }); |
| 63 | +}); |
0 commit comments