Files
docx-js/src/file/paragraph/run/run.spec.ts

432 lines
14 KiB
TypeScript
Raw Normal View History

2019-06-26 22:12:18 +01:00
import { expect } from "chai";
2018-10-26 01:04:07 +01:00
import { Formatter } from "export/formatter";
2019-11-21 01:02:46 +00:00
// import { FootnoteReferenceRun } from "file/footnotes/footnote/run/reference-run";
import { ShadingType } from "file/shading";
2018-10-26 01:04:07 +01:00
import { Run } from "./";
2020-05-22 12:22:45 +08:00
import { EmphasisMarkType } from "./emphasis-mark";
2019-11-21 01:02:46 +00:00
import { PageNumber } from "./run";
2019-06-17 01:51:57 +01:00
import { UnderlineType } from "./underline";
2016-03-30 03:58:53 +01:00
2016-07-13 00:59:53 +01:00
describe("Run", () => {
2016-05-26 15:08:34 +01:00
describe("#bold()", () => {
2016-03-30 04:43:56 +01:00
it("it should add bold to the properties", () => {
2019-06-17 01:51:57 +01:00
const run = new Run({
bold: true,
});
2019-06-26 22:12:18 +01:00
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [
{
"w:rPr": [
{ "w:b": {} },
2019-06-26 22:12:18 +01:00
{
"w:bCs": {},
2019-06-26 22:12:18 +01:00
},
],
},
],
});
2016-03-30 04:43:56 +01:00
});
});
2016-03-30 03:58:53 +01:00
describe("#italics()", () => {
2016-03-30 04:43:56 +01:00
it("it should add italics to the properties", () => {
2019-06-17 01:51:57 +01:00
const run = new Run({
italics: true,
});
2019-06-26 22:12:18 +01:00
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [
{
"w:rPr": [
{ "w:i": {} },
2019-06-26 22:12:18 +01:00
{
"w:iCs": {},
2019-06-26 22:12:18 +01:00
},
],
},
],
});
2016-03-30 04:43:56 +01:00
});
});
2016-05-26 15:08:34 +01:00
describe("#underline()", () => {
it("should default to 'single' and no color", () => {
2019-06-17 01:51:57 +01:00
const run = new Run({
underline: {},
});
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [{ "w:rPr": [{ "w:u": { _attr: { "w:val": "single" } } }] }],
});
});
it("should set the style type and color if given", () => {
2019-06-17 01:51:57 +01:00
const run = new Run({
underline: {
type: UnderlineType.DOUBLE,
color: "990011",
},
});
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [{ "w:rPr": [{ "w:u": { _attr: { "w:val": "double", "w:color": "990011" } } }] }],
});
});
2016-03-30 03:58:53 +01:00
});
2020-05-22 12:22:45 +08:00
describe("#emphasisMark()", () => {
it("should default to 'dot'", () => {
const run = new Run({
emphasisMark: {},
});
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [{ "w:rPr": [{ "w:em": { _attr: { "w:val": "dot" } } }] }],
});
});
it("should set the style type if given", () => {
const run = new Run({
emphasisMark: {
type: EmphasisMarkType.DOT,
},
});
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [{ "w:rPr": [{ "w:em": { _attr: { "w:val": "dot" } } }] }],
});
});
});
2016-07-12 08:46:26 +01:00
describe("#smallCaps()", () => {
it("it should add smallCaps to the properties", () => {
2019-06-17 01:51:57 +01:00
const run = new Run({
smallCaps: true,
});
2019-06-26 22:12:18 +01:00
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [{ "w:rPr": [{ "w:smallCaps": {} }] }],
2019-06-26 22:12:18 +01:00
});
2016-07-12 08:46:26 +01:00
});
});
describe("#caps()", () => {
it("it should add caps to the properties", () => {
2019-06-17 01:51:57 +01:00
const run = new Run({
allCaps: true,
});
2019-06-26 22:12:18 +01:00
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [{ "w:rPr": [{ "w:caps": {} }] }],
2019-06-26 22:12:18 +01:00
});
2016-07-12 08:46:26 +01:00
});
});
describe("#strike()", () => {
it("it should add strike to the properties", () => {
2019-06-17 01:51:57 +01:00
const run = new Run({
strike: true,
});
2019-06-26 22:12:18 +01:00
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [{ "w:rPr": [{ "w:strike": {} }] }],
2019-06-26 22:12:18 +01:00
});
2016-07-12 08:46:26 +01:00
});
});
2016-03-30 03:58:53 +01:00
2016-07-12 08:46:26 +01:00
describe("#doubleStrike()", () => {
2021-03-13 20:23:15 +00:00
it("it should add double strike to the properties", () => {
2019-06-17 01:51:57 +01:00
const run = new Run({
doubleStrike: true,
});
2019-06-26 22:12:18 +01:00
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [{ "w:rPr": [{ "w:dstrike": {} }] }],
2019-06-26 22:12:18 +01:00
});
2021-03-13 20:23:15 +00:00
});
});
describe("#emboss()", () => {
it("it should add emboss to the properties", () => {
const run = new Run({
emboss: true,
});
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [{ "w:rPr": [{ "w:emboss": {} }] }],
2021-03-13 20:23:15 +00:00
});
});
});
describe("#imprint()", () => {
it("it should add imprint to the properties", () => {
const run = new Run({
imprint: true,
});
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [{ "w:rPr": [{ "w:imprint": {} }] }],
2021-03-13 20:23:15 +00:00
});
2016-07-12 08:46:26 +01:00
});
});
describe("#subScript()", () => {
it("it should add subScript to the properties", () => {
const run = new Run({
subScript: true,
});
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [{ "w:rPr": [{ "w:vertAlign": { _attr: { "w:val": "subscript" } } }] }],
});
});
});
describe("#superScript()", () => {
it("it should add superScript to the properties", () => {
const run = new Run({
superScript: true,
});
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [{ "w:rPr": [{ "w:vertAlign": { _attr: { "w:val": "superscript" } } }] }],
});
});
});
2019-08-05 13:42:45 +03:00
describe("#highlight()", () => {
it("it should add highlight to the properties", () => {
2019-08-06 21:37:33 +01:00
const run = new Run({
highlight: "005599",
});
2019-08-05 13:42:45 +03:00
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [
{
"w:rPr": [
2019-08-06 13:39:05 +03:00
{ "w:highlight": { _attr: { "w:val": "005599" } } },
2019-08-05 13:42:45 +03:00
{
"w:highlightCs": {
_attr: {
"w:val": "005599",
},
},
},
],
},
],
});
});
});
describe("#shadow()", () => {
it("it should add shadow to the properties", () => {
2019-08-06 21:37:33 +01:00
const run = new Run({
2019-08-06 23:08:21 +01:00
shading: {
2021-05-29 05:51:06 +03:00
type: ShadingType.PERCENT_10,
2019-08-06 21:37:33 +01:00
fill: "00FFFF",
color: "FF0000",
},
});
2019-08-05 13:42:45 +03:00
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [
{
2021-05-20 04:08:00 +03:00
"w:rPr": [{ "w:shd": { _attr: { "w:val": "pct10", "w:fill": "00FFFF", "w:color": "FF0000" } } }],
2019-08-05 13:42:45 +03:00
},
],
});
});
});
2016-07-12 08:46:26 +01:00
describe("#break()", () => {
it("it should add break to the run", () => {
2020-12-23 23:31:28 +00:00
const run = new Run({
break: 1,
});
2019-06-26 22:12:18 +01:00
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [{ "w:br": {} }],
});
2016-07-12 08:46:26 +01:00
});
2020-12-23 23:31:28 +00:00
it("it should add two breaks to the run", () => {
const run = new Run({
break: 2,
});
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [
{ "w:br": {} },
{
"w:br": {},
},
],
});
});
2016-07-12 08:46:26 +01:00
});
2016-03-30 03:58:53 +01:00
2017-03-08 20:29:12 +01:00
describe("#font()", () => {
it("should set the font as named", () => {
2019-06-17 01:51:57 +01:00
const run = new Run({
font: {
name: "Times",
},
});
2017-03-08 20:29:12 +01:00
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
2018-07-24 12:37:15 +08:00
"w:r": [
{
"w:rPr": [
2020-05-22 12:22:45 +08:00
{
"w:rFonts": {
_attr: {
"w:ascii": "Times",
"w:cs": "Times",
"w:eastAsia": "Times",
"w:hAnsi": "Times",
},
},
},
2018-07-24 12:37:15 +08:00
],
},
],
2017-03-08 20:29:12 +01:00
});
});
2020-06-07 12:38:31 +08:00
it("should set the font for ascii and eastAsia", () => {
const run = new Run({
font: {
ascii: "Times",
eastAsia: "KaiTi",
},
});
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [
{
"w:rPr": [
{
"w:rFonts": {
_attr: {
"w:ascii": "Times",
"w:eastAsia": "KaiTi",
},
},
},
],
},
],
});
});
2017-03-08 20:29:12 +01:00
});
2017-03-12 17:31:36 +01:00
describe("#color", () => {
it("should set the run to the color given", () => {
2019-06-17 01:51:57 +01:00
const run = new Run({
color: "001122",
});
2017-03-12 17:31:36 +01:00
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [{ "w:rPr": [{ "w:color": { _attr: { "w:val": "001122" } } }] }],
2017-03-12 17:31:36 +01:00
});
});
});
describe("#size", () => {
it("should set the run to the given size", () => {
2019-06-17 01:51:57 +01:00
const run = new Run({
size: 24,
});
2017-03-12 17:31:36 +01:00
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
2018-07-25 15:02:58 +03:00
"w:r": [
{
"w:rPr": [{ "w:sz": { _attr: { "w:val": 24 } } }, { "w:szCs": { _attr: { "w:val": 24 } } }],
2018-07-25 15:02:58 +03:00
},
],
2017-03-12 17:31:36 +01:00
});
});
});
2017-03-12 17:35:15 +01:00
2018-07-24 18:52:45 +03:00
describe("#rtl", () => {
it("should set the run to the RTL mode", () => {
2019-06-17 01:51:57 +01:00
const run = new Run({
rightToLeft: true,
});
2018-07-24 18:52:45 +03:00
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [{ "w:rPr": [{ "w:rtl": {} }] }],
2018-07-24 18:52:45 +03:00
});
});
});
2019-01-15 21:40:19 +00:00
describe("#numberOfTotalPages", () => {
it("should set the run to the RTL mode", () => {
2019-11-21 01:02:46 +00:00
const run = new Run({
children: [PageNumber.TOTAL_PAGES],
});
2019-01-15 21:40:19 +00:00
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [
{ "w:fldChar": { _attr: { "w:fldCharType": "begin" } } },
2019-01-15 21:40:19 +00:00
{ "w:instrText": [{ _attr: { "xml:space": "preserve" } }, "NUMPAGES"] },
{ "w:fldChar": { _attr: { "w:fldCharType": "separate" } } },
{ "w:fldChar": { _attr: { "w:fldCharType": "end" } } },
2019-01-15 21:40:19 +00:00
],
2019-08-09 11:56:22 +03:00
});
});
});
describe("#numberOfTotalPagesSection", () => {
it("should set the run to the RTL mode", () => {
2019-11-21 01:02:46 +00:00
const run = new Run({
children: [PageNumber.TOTAL_PAGES_IN_SECTION],
});
2019-08-09 11:56:22 +03:00
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [
{ "w:fldChar": { _attr: { "w:fldCharType": "begin" } } },
{ "w:instrText": [{ _attr: { "xml:space": "preserve" } }, "SECTIONPAGES"] },
{ "w:fldChar": { _attr: { "w:fldCharType": "separate" } } },
{ "w:fldChar": { _attr: { "w:fldCharType": "end" } } },
],
2019-01-15 21:40:19 +00:00
});
});
});
describe("#pageNumber", () => {
it("should set the run to the RTL mode", () => {
2019-11-21 01:02:46 +00:00
const run = new Run({
children: [PageNumber.CURRENT],
});
2019-01-15 21:40:19 +00:00
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [
{ "w:fldChar": { _attr: { "w:fldCharType": "begin" } } },
2019-01-15 21:40:19 +00:00
{ "w:instrText": [{ _attr: { "xml:space": "preserve" } }, "PAGE"] },
{ "w:fldChar": { _attr: { "w:fldCharType": "separate" } } },
{ "w:fldChar": { _attr: { "w:fldCharType": "end" } } },
2019-01-15 21:40:19 +00:00
],
});
});
});
2017-03-12 17:35:15 +01:00
describe("#style", () => {
it("should set the style to the given styleId", () => {
2019-06-17 01:51:57 +01:00
const run = new Run({
style: "myRunStyle",
});
2017-03-12 17:35:15 +01:00
const tree = new Formatter().format(run);
expect(tree).to.deep.equal({
"w:r": [{ "w:rPr": [{ "w:rStyle": { _attr: { "w:val": "myRunStyle" } } }] }],
2017-03-12 17:35:15 +01:00
});
});
});
2017-03-08 20:28:59 +01:00
});