Simplify multiple addXXX methods into a single add method for Footer

This commit is contained in:
Dolan
2019-06-25 01:58:09 +01:00
parent 384d144a85
commit b566b0f765
5 changed files with 15 additions and 29 deletions

View File

@ -7,21 +7,19 @@ import { Paragraph } from "./paragraph";
import { Table } from "./table"; import { Table } from "./table";
describe("FooterWrapper", () => { describe("FooterWrapper", () => {
describe("#addParagraph", () => { describe("#add", () => {
it("should call the underlying footer's addParagraph", () => { it("should call the underlying footer's addParagraph", () => {
const file = new FooterWrapper(new Media(), 1); const file = new FooterWrapper(new Media(), 1);
const spy = sinon.spy(file.Footer, "addParagraph"); const spy = sinon.spy(file.Footer, "add");
file.addParagraph(new Paragraph({})); file.add(new Paragraph({}));
expect(spy.called).to.equal(true); expect(spy.called).to.equal(true);
}); });
});
describe("#addTable", () => {
it("should call the underlying footer's addParagraph", () => { it("should call the underlying footer's addParagraph", () => {
const file = new FooterWrapper(new Media(), 1); const file = new FooterWrapper(new Media(), 1);
const spy = sinon.spy(file.Footer, "addTable"); const spy = sinon.spy(file.Footer, "add");
file.addTable( file.add(
new Table({ new Table({
rows: 1, rows: 1,
columns: 1, columns: 1,
@ -30,12 +28,10 @@ describe("FooterWrapper", () => {
expect(spy.called).to.equal(true); expect(spy.called).to.equal(true);
}); });
});
describe("#addImage", () => {
it("should call the underlying footer's addImage", () => { it("should call the underlying footer's addImage", () => {
const file = new FooterWrapper(new Media(), 1); const file = new FooterWrapper(new Media(), 1);
const spy = sinon.spy(file.Footer, "addParagraph"); const spy = sinon.spy(file.Footer, "add");
// tslint:disable-next-line:no-any // tslint:disable-next-line:no-any
file.addImage({} as any); file.addImage({} as any);

View File

@ -22,16 +22,12 @@ export class FooterWrapper {
this.relationships = new Relationships(); this.relationships = new Relationships();
} }
public addParagraph(paragraph: Paragraph): void { public add(item: Paragraph | Table): void {
this.footer.addParagraph(paragraph); this.footer.add(item);
}
public addTable(table: Table): void {
this.footer.addTable(table);
} }
public addImage(image: Image): FooterWrapper { public addImage(image: Image): FooterWrapper {
this.footer.addParagraph(image.Paragraph); this.footer.add(image.Paragraph);
return this; return this;
} }
@ -47,7 +43,7 @@ export class FooterWrapper {
): Paragraph { ): Paragraph {
const image = Media.addImage(this, buffer, width, height, drawingOptions); const image = Media.addImage(this, buffer, width, height, drawingOptions);
const paragraph = new Paragraph(image); const paragraph = new Paragraph(image);
this.addParagraph(paragraph); this.add(paragraph);
return paragraph; return paragraph;
} }

View File

@ -36,13 +36,7 @@ export class Footer extends InitializableXmlComponent {
return this.refId; return this.refId;
} }
public addParagraph(paragraph: Paragraph): Footer { public add(item: Paragraph | Table): void {
this.root.push(paragraph); this.root.push(item);
return this;
}
public addTable(table: Table): void {
this.root.push(table);
} }
} }

View File

@ -10,7 +10,7 @@ describe("HeaderWrapper", () => {
describe("#add", () => { describe("#add", () => {
it("should call the underlying header's addChildElement for Paragraph", () => { it("should call the underlying header's addChildElement for Paragraph", () => {
const wrapper = new HeaderWrapper(new Media(), 1); const wrapper = new HeaderWrapper(new Media(), 1);
const spy = sinon.spy(wrapper.Header, "addChildElement"); const spy = sinon.spy(wrapper.Header, "add");
wrapper.add(new Paragraph({})); wrapper.add(new Paragraph({}));
expect(spy.called).to.equal(true); expect(spy.called).to.equal(true);
@ -18,7 +18,7 @@ describe("HeaderWrapper", () => {
it("should call the underlying header's addChildElement for Table", () => { it("should call the underlying header's addChildElement for Table", () => {
const wrapper = new HeaderWrapper(new Media(), 1); const wrapper = new HeaderWrapper(new Media(), 1);
const spy = sinon.spy(wrapper.Header, "addChildElement"); const spy = sinon.spy(wrapper.Header, "add");
wrapper.add( wrapper.add(
new Table({ new Table({
rows: 1, rows: 1,

View File

@ -23,7 +23,7 @@ export class HeaderWrapper {
} }
public add(item: Paragraph | Table): HeaderWrapper { public add(item: Paragraph | Table): HeaderWrapper {
this.header.addChildElement(item); this.header.add(item);
return this; return this;
} }