fixing tests

This commit is contained in:
Dolan Miu
2016-05-01 22:24:20 +01:00
parent 3f8d23662e
commit 879ac13863
9 changed files with 35 additions and 30 deletions

View File

@ -5,11 +5,6 @@ import {Paragraph} from "../paragraph";
export class Document extends XmlComponent { export class Document extends XmlComponent {
private body: Body; private body: Body;
xmlKeys = {
document: "w:document",
body: "w:body"
};
constructor() { constructor() {
super("w:document"); super("w:document");
@ -36,7 +31,13 @@ export class Document extends XmlComponent {
this.root.push(this.body); this.root.push(this.body);
} }
addParagraph(paragraph: Paragraph) { addParagraph(paragraph: Paragraph): void {
this.body.push(paragraph); this.body.push(paragraph);
} }
clearVariables(): void {
console.log("clearing");
this.body.clearVariables();
delete this.body;
}
} }

View File

@ -8,6 +8,8 @@ export abstract class BaseXmlComponent {
} }
abstract replaceKey(): void; abstract replaceKey(): void;
clearVariables(): void {
};
} }
export abstract class XmlComponent extends BaseXmlComponent { export abstract class XmlComponent extends BaseXmlComponent {
@ -23,7 +25,9 @@ export abstract class XmlComponent extends BaseXmlComponent {
//console.log(this.root); //console.log(this.root);
if (this.root !== undefined) { if (this.root !== undefined) {
this.root.forEach(root => { this.root.forEach(root => {
root.replaceKey(); if (root && root instanceof BaseXmlComponent) {
root.replaceKey();
}
}); });
this[this.rootKey] = this.root; this[this.rootKey] = this.root;
delete this.root; delete this.root;

View File

@ -5,8 +5,9 @@ export class Formatter {
format(input: any): Object { format(input: any): Object {
this.replaceKeys(input); this.replaceKeys(input);
input.clearVariables();
var newJson = this.clense(input); var newJson = this.clense(input);
console.log(JSON.stringify(newJson, null, " "));
return newJson; return newJson;
} }
@ -26,7 +27,7 @@ export class Formatter {
//console.log(value); //console.log(value);
//parent.replaceKey(); //parent.replaceKey();
}); });
return input; return input;
} }

View File

@ -46,6 +46,7 @@ export abstract class Packer {
prefix: "root" prefix: "root"
});*/ });*/
var xmlDocument = xml(this.formatter.format(this.document)); var xmlDocument = xml(this.formatter.format(this.document));
console.log(xmlDocument);
var xmlStyle = xml(this.style); var xmlStyle = xml(this.style);
var xmlProperties = xml(this.formatter.format(this.properties), { declaration: { standalone: 'yes', encoding: 'UTF-8' } }); var xmlProperties = xml(this.formatter.format(this.properties), { declaration: { standalone: 'yes', encoding: 'UTF-8' } });

View File

@ -2,13 +2,13 @@ import {XmlComponent} from "../docx/xml-components";
import {DocumentAttributes} from "../docx/xml-components/document-attributes"; import {DocumentAttributes} from "../docx/xml-components/document-attributes";
abstract class Component extends XmlComponent { abstract class Component extends XmlComponent {
protected createNullBlockOrValue(value: string): XmlComponent { protected createNullBlockOrValue(value: string): any {
/*if (value === undefined) { if (value === undefined) {
return [{}]; return [{}];
} else { } else {
return value; return value;
}*/ }
return null; //return null;
} }
} }
export class Title extends Component { export class Title extends Component {

View File

@ -36,12 +36,11 @@ describe("Formatter", () => {
assert(stringifiedJson.indexOf("xmlKeys") < 0); assert(stringifiedJson.indexOf("xmlKeys") < 0);
}); });
it.only("should format simple paragraph with bold text", () => { it("should format simple paragraph with bold text", () => {
var paragraph = new docx.Paragraph(); var paragraph = new docx.Paragraph();
paragraph.addText(new docx.TextRun("test").bold()); paragraph.addText(new docx.TextRun("test").bold());
var newJson = formatter.format(paragraph); var newJson = formatter.format(paragraph);
newJson = jsonify(newJson); newJson = jsonify(newJson);
console.log(JSON.stringify(newJson, null, " "));
assert.isDefined(newJson["w:p"][1]["w:r"][0]["w:rPr"][0]["w:b"][0]["_attr"]["w:val"]); assert.isDefined(newJson["w:p"][1]["w:r"][0]["w:rPr"][0]["w:b"][0]["_attr"]["w:val"]);
}); });

View File

@ -25,7 +25,7 @@ describe("Packer", () => {
packer = new LocalPacker(document, DefaultStyle(), properties, "build/tests/test.docx"); packer = new LocalPacker(document, DefaultStyle(), properties, "build/tests/test.docx");
}); });
describe('#pack()', () => { describe.only('#pack()', () => {
it("should create a standard docx file", (done) => { it("should create a standard docx file", (done) => {
packer.pack(); packer.pack();

View File

@ -15,13 +15,14 @@ describe("ParagraphStyle", () => {
it("should create a style with given value", () => { it("should create a style with given value", () => {
style = new Style("test"); style = new Style("test");
var newJson = jsonify(style); var newJson = jsonify(style);
assert(newJson.pStyle[0]._attr.val === "test"); console.log(newJson.root[0].root.val);
assert.equal(newJson.root[0].root.val, "test");
}); });
it("should create a style with blank val", () => { it("should create a style with blank val", () => {
style = new Style(""); style = new Style("");
var newJson = jsonify(style); var newJson = jsonify(style);
assert(newJson.pStyle[0]._attr.val === ""); assert.equal(newJson.root[0].root.val, "");
}); });
}); });

View File

@ -34,8 +34,7 @@ describe("Paragraph", () => {
it("should add heading style to JSON", () => { it("should add heading style to JSON", () => {
paragraph.heading1(); paragraph.heading1();
var newJson = jsonify(paragraph); var newJson = jsonify(paragraph);
assert.equal(newJson.root[0].root[1].root[0].root.val, "Heading1");
assert(newJson.root[1].root[1].root[0]._attr.val === "Heading1");
}); });
}); });
@ -44,7 +43,7 @@ describe("Paragraph", () => {
paragraph.heading2(); paragraph.heading2();
var newJson = jsonify(paragraph); var newJson = jsonify(paragraph);
assert(newJson.p[1].pPr[1].pStyle[0]._attr.val === "Heading2"); assert.equal(newJson.root[0].root[1].root[0].root.val, "Heading2");
}); });
}); });
@ -53,7 +52,7 @@ describe("Paragraph", () => {
paragraph.heading3(); paragraph.heading3();
var newJson = jsonify(paragraph); var newJson = jsonify(paragraph);
assert(newJson.p[1].pPr[1].pStyle[0]._attr.val === "Heading3"); assert.equal(newJson.root[0].root[1].root[0].root.val, "Heading3");
}); });
}); });
@ -62,7 +61,7 @@ describe("Paragraph", () => {
paragraph.title(); paragraph.title();
var newJson = jsonify(paragraph); var newJson = jsonify(paragraph);
assert(newJson.p[1].pPr[1].pStyle[0]._attr.val === "Title"); assert.equal(newJson.root[0].root[1].root[0].root.val, "Title");
}); });
}); });
@ -71,7 +70,7 @@ describe("Paragraph", () => {
paragraph.center(); paragraph.center();
var newJson = jsonify(paragraph); var newJson = jsonify(paragraph);
assert(newJson.p[1].pPr[1].jc[0]._attr.val === "center"); assert.equal(newJson.root[0].root[1].root[0].root.val, "center");
}); });
}); });
@ -79,8 +78,7 @@ describe("Paragraph", () => {
it("should add thematic break to JSON", () => { it("should add thematic break to JSON", () => {
paragraph.thematicBreak(); paragraph.thematicBreak();
var newJson = jsonify(paragraph); var newJson = jsonify(paragraph);
assert.equal(newJson.root[0].root[1].rootKey, "w:pBdr");
assert.isDefined(newJson.p[1].pPr[1].pBdr);
}); });
}); });
@ -88,13 +86,13 @@ describe("Paragraph", () => {
it("should add page break to JSON", () => { it("should add page break to JSON", () => {
paragraph.pageBreak(); paragraph.pageBreak();
var newJson = jsonify(paragraph); var newJson = jsonify(paragraph);
assert.isDefined(newJson.p[1].pPr[1].r[1].br); assert.equal(newJson.root[0].root[1].root[1].rootKey, "w:br");
}); });
it("should add page break with 'page' type", () => { it("should add page break with 'page' type", () => {
paragraph.pageBreak(); paragraph.pageBreak();
var newJson = jsonify(paragraph); var newJson = jsonify(paragraph);
assert(newJson.p[1].pPr[1].r[1].br[0]._attr.type === "page"); assert.equal(newJson.root[0].root[1].root[1].root[0].root.type, "page");
}); });
}); });
@ -102,13 +100,13 @@ describe("Paragraph", () => {
it("should add list paragraph style to JSON", () => { it("should add list paragraph style to JSON", () => {
paragraph.bullet(); paragraph.bullet();
var newJson = jsonify(paragraph); var newJson = jsonify(paragraph);
assert(newJson.p[1].pPr[1].pStyle[0]._attr.val === "ListParagraph"); assert.equal(newJson.root[0].root[1].root[0].root.val, "ListParagraph");
}); });
it("it should add numbered properties", () => { it("it should add numbered properties", () => {
paragraph.bullet(); paragraph.bullet();
var newJson = jsonify(paragraph); var newJson = jsonify(paragraph);
assert.isDefined(newJson.p[1].pPr[2].numPr); assert.isDefined(newJson.root[0].root[2]);
}); });
}); });
}); });