Page MenuHomeSealhub

No OneTemporary

diff --git a/src/component-arguments/component-arguments.test.ts b/src/component-arguments/component-arguments.test.ts
index e3e9a69..ad7c167 100644
--- a/src/component-arguments/component-arguments.test.ts
+++ b/src/component-arguments/component-arguments.test.ts
@@ -1,115 +1,152 @@
import assert from "assert";
import { List } from "./list";
import { Image } from "./image";
import { ShortText } from "./short-text";
import { Structured } from "./structured";
import { Enum } from "./enum";
import { MarkdownArgument } from "./markdown";
import { Component } from "../component";
import { ComponentArgument } from "./component-argument";
import { simpleJDDContext } from "../jdd-context";
+import { URL } from "./url";
describe("component arguments", () => {
it("handles nesting objects and arrays", () => {
new List(new ShortText());
new List(
new Structured({ name: new ShortText(), surname: new ShortText() })
);
});
it("handles enums", () => {
new Enum(<const>["hamster", "dentist"]);
});
it("Gives a nice example", async () => {
await new MarkdownArgument().getExampleValue(simpleJDDContext);
const args = <const>{
text: new ShortText(),
markdown: new MarkdownArgument(),
};
const component = new (class testComponent extends Component<
typeof args
> {
getArguments() {
return args;
}
toHTML() {
return "";
}
})();
const example = await component.getExampleValues(simpleJDDContext);
assert(example.text.length > 0);
assert(example.markdown.length > 0);
});
it("allows for chaining methods when setting example values", () => {
const the_argument = new MarkdownArgument().setExampleValues([
"abc1",
"abc2",
]);
assert((the_argument as any) instanceof ComponentArgument);
});
describe("Word counting", () => {
it("Image argument", () => {
assert.strictEqual(new Image().countWords(), 0);
});
it("Enum argument", () => {
assert.strictEqual(
new Enum(<const>["hamster", "dentist"]).countWords(),
0
);
});
it("ShortText argument", () => {
assert.strictEqual(new ShortText().countWords("Test ShortText"), 2);
});
it("ShortText argument", () => {
assert.strictEqual(new ShortText().countWords("Test ShortText"), 2);
});
it("MarkdownArgument argument", () => {
assert.strictEqual(
new MarkdownArgument().countWords(
"*Lorem ipsum* dolor sit amet, **consectetur adipiscing elit**."
),
8
);
});
it("List argument", () => {
assert.strictEqual(
new List(new ShortText()).countWords([
"Test ShortText 1",
"Test ShortText 2",
]),
6
);
});
it("Structured argument", () => {
assert.strictEqual(
new Structured({
fullname: new ShortText(),
age: new ShortText(),
}).countWords({ fullname: "Joe Done", age: "34" }),
3
);
});
});
describe("List argument", () => {
it("Generates example values from provided examples, if they are provided", async () => {
const list = new List(new ShortText()).setExampleValues([
["a", "b", "c"],
]);
assert.deepStrictEqual(
await list.getExampleValue(simpleJDDContext),
["a", "b", "c"]
);
});
});
+
+ describe("test URL component argument class", () => {
+ it("allows for chaining methods when setting example values", () => {
+ const the_argument = new URL("relative").setExampleValues([
+ "/example",
+ "https://example.com",
+ ]);
+ assert((the_argument as any) instanceof ComponentArgument);
+ });
+
+ it("returns example url of type based on specyfic urlType argument", () => {
+ const relativeUrl = new URL("relative").getExampleValue();
+ assert(
+ relativeUrl &&
+ typeof relativeUrl === "string" &&
+ !relativeUrl.includes("http")
+ );
+
+ const absoluteUrl = new URL("absolute").getExampleValue();
+ assert(
+ absoluteUrl &&
+ typeof absoluteUrl === "string" &&
+ absoluteUrl.includes("http")
+ );
+ });
+
+ it("returns correct component argument type", () => {
+ const argumentType = new URL("relative");
+ assert(argumentType instanceof URL);
+ assert(argumentType.getTypeName() === "url");
+
+ const argumentType2 = new URL("absolute");
+ assert(argumentType2 instanceof URL);
+ assert(argumentType2.getTypeName() === "url");
+ });
+ });
});
diff --git a/src/component-arguments/component-arguments.ts b/src/component-arguments/component-arguments.ts
index 2de00ea..601348d 100644
--- a/src/component-arguments/component-arguments.ts
+++ b/src/component-arguments/component-arguments.ts
@@ -1,8 +1,9 @@
export { Enum } from "./enum";
export { Image } from "./image";
export { File } from "./file";
export { List } from "./list";
export { MarkdownArgument as Markdown } from "./markdown";
export { ShortText } from "./short-text";
export { Structured } from "./structured";
export * from "./table";
+export { URL } from "./url";
diff --git a/src/component-arguments/url.ts b/src/component-arguments/url.ts
new file mode 100644
index 0000000..959a976
--- /dev/null
+++ b/src/component-arguments/url.ts
@@ -0,0 +1,43 @@
+import { JDDContext } from "../jdd-context";
+import { ComponentArgument } from "./component-argument";
+import { is, predicates } from "@sealcode/ts-predicates";
+import { MaybePromise } from "../utils/util-types";
+
+export class URL extends ComponentArgument<string> {
+ public readonly urlType: Readonly<"relative" | "absolute">;
+ constructor(urlType: Readonly<"relative" | "absolute">) {
+ super();
+ this.urlType = urlType;
+ }
+
+ getTypeName() {
+ return `url`;
+ }
+
+ getEmptyValue() {
+ return "";
+ }
+
+ getExampleValue() {
+ if (this.urlType === "relative") {
+ return "/example";
+ }
+
+ return "https://example.com";
+ }
+
+ countWords(): number {
+ return 0;
+ }
+
+ parseFormInput(
+ _: JDDContext,
+ input: unknown
+ ): MaybePromise<string | string[] | null> {
+ if (is(input, predicates.string)) {
+ return input;
+ } else {
+ return null;
+ }
+ }
+}

File Metadata

Mime Type
text/x-diff
Expires
Tue, Dec 24, 08:03 (23 h, 46 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
557053
Default Alt Text
(5 KB)

Event Timeline