Commit 90e443d1 authored by Diegodlh's avatar Diegodlh
Browse files

support fixed selection

parent 066f27a3
import {
Selection,
CitoidSelection,
XPathSelection,
SelectionConfigTypeError,
UndefinedSelectionConfigError,
FixedSelection,
} from "./selection";
import { Webpage } from "../webpage/webpage";
import * as nodeFetch from "node-fetch";
......@@ -116,3 +118,39 @@ describe("Citoid selection", () => {
}).toThrow(SelectionConfigTypeError);
});
});
describe("Fixed selection", () => {
it("returns a fixed value regardless of target", () => {
const selection = Selection.create({
type: "fixed",
config: "fixed value",
});
const target = new Webpage("https://example.com/article");
return selection
.select(target)
.then((value) => expect(value).toEqual(["fixed value"]));
});
it("fails on non-string configuration value", () => {
const selection = new FixedSelection();
expect(() => {
selection.config = 0 as never;
}).toThrow();
});
it("does not use any external resources", () => {
const selection = new FixedSelection("fixed value");
const target = new Webpage("https://example.com/article");
const fetchSpy = jest.spyOn(mockNodeFetch, "default");
selection.select(target);
expect(fetchSpy).toHaveBeenCalledTimes(0);
});
it("accepts empty string as config", () => {
const selection = Selection.create({ type: "fixed", config: "" });
const target = new Webpage("https://example.com/article");
return selection.select(target).then((value) => {
expect(value).toEqual([""]);
});
});
});
......@@ -25,6 +25,9 @@ export abstract class Selection extends TranslationStep {
case "xpath":
return new XPathSelection(config);
break;
case "fixed":
return new FixedSelection(config);
break;
default:
throw new Error(`Unknown selection of type ${selection.type}`);
}
......@@ -195,6 +198,35 @@ function isAttr(node: Node): node is Attr {
return (node as Attr).value !== undefined;
}
export class FixedSelection extends Selection {
readonly type: SelectionType = "fixed";
protected _config = "";
constructor(value?: string) {
super();
if (value) this.config = value;
}
get config(): string {
return this._config;
}
set config(config: string) {
if (typeof config === "string") {
this._config = config;
} else {
throw new SelectionConfigTypeError(this.type, config);
}
}
select(target: Webpage): Promise<StepOutput> {
return Promise.resolve([this.config]);
}
suggest(target: Webpage, query: string): Promise<string> {
return Promise.resolve(query);
}
}
export class SelectionConfigTypeError extends TypeError {
constructor(selectionType: SelectionType, config: string) {
super(
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment