Files
docx-js/src/file/paragraph/links/hyperlink.spec.ts

131 lines
4.1 KiB
TypeScript
Raw Normal View History

2019-06-27 01:35:58 +01:00
import { expect } from "chai";
2018-05-06 22:20:56 -05:00
2018-10-26 01:04:07 +01:00
import { Formatter } from "export/formatter";
import { TextRun } from "../run";
import { ConcreteHyperlink, ExternalHyperlink, InternalHyperlink } from "./hyperlink";
2018-05-06 22:20:56 -05:00
describe("ConcreteHyperlink", () => {
let hyperlink: ConcreteHyperlink;
2018-05-06 22:20:56 -05:00
2018-05-06 23:18:00 -05:00
beforeEach(() => {
hyperlink = new ConcreteHyperlink(
2021-08-27 16:53:11 -06:00
[
new TextRun({
text: "https://example.com",
style: "Hyperlink",
}),
],
"superid",
);
2018-05-06 23:18:00 -05:00
});
2018-05-06 22:20:56 -05:00
describe("#constructor()", () => {
it("should create a hyperlink with correct root key", () => {
const tree = new Formatter().format(hyperlink);
2019-06-27 01:35:58 +01:00
expect(tree).to.deep.equal({
"w:hyperlink": [
{
_attr: {
"w:history": 1,
2019-11-08 10:50:18 +01:00
"r:id": "rIdsuperid",
2019-06-27 01:35:58 +01:00
},
},
{
"w:r": [
{ "w:rPr": [{ "w:rStyle": { _attr: { "w:val": "Hyperlink" } } }] },
{ "w:t": [{ _attr: { "xml:space": "preserve" } }, "https://example.com"] },
],
},
2018-05-06 23:18:00 -05:00
],
2019-06-27 01:35:58 +01:00
});
2018-05-06 22:20:56 -05:00
});
2018-07-24 16:56:27 -04:00
describe("with optional anchor parameter", () => {
beforeEach(() => {
hyperlink = new ConcreteHyperlink(
2021-08-27 16:53:11 -06:00
[
new TextRun({
text: "Anchor Text",
style: "Hyperlink",
}),
],
"superid2",
"anchor",
);
2018-07-24 16:56:27 -04:00
});
it("should create an internal link with anchor tag", () => {
2019-06-27 01:35:58 +01:00
const tree = new Formatter().format(hyperlink);
expect(tree).to.deep.equal({
"w:hyperlink": [
{
_attr: {
"w:history": 1,
"w:anchor": "anchor",
},
},
{
"w:r": [
{ "w:rPr": [{ "w:rStyle": { _attr: { "w:val": "Hyperlink" } } }] },
{ "w:t": [{ _attr: { "xml:space": "preserve" } }, "Anchor Text"] },
],
},
],
});
2018-07-24 16:56:27 -04:00
});
});
2018-05-06 22:20:56 -05:00
});
});
describe("ExternalHyperlink", () => {
describe("#constructor()", () => {
it("should create", () => {
const externalHyperlink = new ExternalHyperlink({
2021-08-27 16:53:11 -06:00
children: [new TextRun("test")],
link: "http://www.google.com",
});
expect(externalHyperlink.options.link).to.equal("http://www.google.com");
});
});
});
describe("InternalHyperlink", () => {
describe("#constructor()", () => {
it("should create", () => {
const internalHyperlink = new InternalHyperlink({
2021-08-31 19:54:33 -06:00
children: [new TextRun("test")],
anchor: "test-id",
});
const tree = new Formatter().format(internalHyperlink);
expect(tree).to.deep.equal({
"w:hyperlink": [
{
_attr: {
"w:anchor": "test-id",
"w:history": 1,
},
},
{
"w:r": [
{
"w:t": [
{
_attr: {
"xml:space": "preserve",
},
},
"test",
],
},
],
},
],
});
});
});
});