tests
This commit is contained in:
@ -160,6 +160,8 @@ export class File {
|
|||||||
public createInternalHyperLink(anchor: string, text?: string): Hyperlink {
|
public createInternalHyperLink(anchor: string, text?: string): Hyperlink {
|
||||||
text = text === undefined ? anchor : text;
|
text = text === undefined ? anchor : text;
|
||||||
const hyperlink = new Hyperlink(text, this.docRelationships.RelationshipCount, anchor);
|
const hyperlink = new Hyperlink(text, this.docRelationships.RelationshipCount, anchor);
|
||||||
|
// NOTE: unlike File#createHyperlink(), since the link is to an internal bookmark
|
||||||
|
// we don't need to create a new relationship.
|
||||||
return hyperlink;
|
return hyperlink;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
42
src/file/paragraph/links/bookmark.spec.ts
Normal file
42
src/file/paragraph/links/bookmark.spec.ts
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
import { assert } from "chai";
|
||||||
|
|
||||||
|
import { Utility } from "../../../tests/utility";
|
||||||
|
import { Bookmark } from "./";
|
||||||
|
|
||||||
|
describe("Bookmark", () => {
|
||||||
|
let bookmark: Bookmark;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
bookmark = new Bookmark("anchor", "Internal Link", 0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should create a bookmark with three root elements", () => {
|
||||||
|
const newJson = Utility.jsonify(bookmark);
|
||||||
|
assert.equal(newJson.rootKey, undefined);
|
||||||
|
assert.equal(newJson.start.rootKey, "w:bookmarkStart");
|
||||||
|
assert.equal(newJson.text.rootKey, "w:r");
|
||||||
|
assert.equal(newJson.end.rootKey, "w:bookmarkEnd");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should create a bookmark with the correct attributes on the bookmark start element", () => {
|
||||||
|
const newJson = Utility.jsonify(bookmark);
|
||||||
|
const attributes = {
|
||||||
|
name: "anchor",
|
||||||
|
id: "1",
|
||||||
|
};
|
||||||
|
assert.equal(JSON.stringify(newJson.start.root[0].root), JSON.stringify(attributes));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should create a bookmark with the correct attributes on the text element", () => {
|
||||||
|
const newJson = Utility.jsonify(bookmark);
|
||||||
|
assert.equal(JSON.stringify(newJson.text.root[1].root[1]), JSON.stringify("Internal Link"));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should create a bookmark with the correct attributes on the bookmark end element", () => {
|
||||||
|
const newJson = Utility.jsonify(bookmark);
|
||||||
|
const attributes = {
|
||||||
|
id: "1",
|
||||||
|
};
|
||||||
|
assert.equal(JSON.stringify(newJson.end.root[0].root), JSON.stringify(attributes));
|
||||||
|
});
|
||||||
|
});
|
@ -16,7 +16,7 @@ export class Bookmark {
|
|||||||
constructor(name: string, text: string, relationshipsCount: number) {
|
constructor(name: string, text: string, relationshipsCount: number) {
|
||||||
this.linkId = relationshipsCount + 1;
|
this.linkId = relationshipsCount + 1;
|
||||||
|
|
||||||
this.start = new BookmarkStart(name, text, this.linkId);
|
this.start = new BookmarkStart(name, this.linkId);
|
||||||
this.text = new TextRun(text);
|
this.text = new TextRun(text);
|
||||||
this.end = new BookmarkEnd(this.linkId);
|
this.end = new BookmarkEnd(this.linkId);
|
||||||
}
|
}
|
||||||
@ -25,7 +25,7 @@ export class Bookmark {
|
|||||||
export class BookmarkStart extends XmlComponent {
|
export class BookmarkStart extends XmlComponent {
|
||||||
public linkId: number;
|
public linkId: number;
|
||||||
|
|
||||||
constructor(name: string, text: string, relationshipsCount: number) {
|
constructor(name: string, relationshipsCount: number) {
|
||||||
super("w:bookmarkStart");
|
super("w:bookmarkStart");
|
||||||
|
|
||||||
this.linkId = relationshipsCount;
|
this.linkId = relationshipsCount;
|
||||||
|
@ -20,8 +20,8 @@ describe("Hyperlink", () => {
|
|||||||
it("should create a hyperlink with right attributes", () => {
|
it("should create a hyperlink with right attributes", () => {
|
||||||
const newJson = Utility.jsonify(hyperlink);
|
const newJson = Utility.jsonify(hyperlink);
|
||||||
const attributes = {
|
const attributes = {
|
||||||
id: "rId1",
|
|
||||||
history: 1,
|
history: 1,
|
||||||
|
id: "rId1",
|
||||||
};
|
};
|
||||||
assert.equal(JSON.stringify(newJson.root[0].root), JSON.stringify(attributes));
|
assert.equal(JSON.stringify(newJson.root[0].root), JSON.stringify(attributes));
|
||||||
});
|
});
|
||||||
@ -36,5 +36,20 @@ describe("Hyperlink", () => {
|
|||||||
};
|
};
|
||||||
expect(tree["w:hyperlink"][1]).to.deep.equal(runJson);
|
expect(tree["w:hyperlink"][1]).to.deep.equal(runJson);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("with optional anchor parameter", () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
hyperlink = new Hyperlink("Anchor Text", 0, "anchor");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should create an internal link with anchor tag", () => {
|
||||||
|
const newJson = Utility.jsonify(hyperlink);
|
||||||
|
const attributes = {
|
||||||
|
history: 1,
|
||||||
|
anchor: "anchor",
|
||||||
|
};
|
||||||
|
assert.equal(JSON.stringify(newJson.root[0].root), JSON.stringify(attributes));
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Reference in New Issue
Block a user