Skip to content

Commit e9ee5d8

Browse files
authored
Merge pull request ghiscoding#141 from ghiscoding/chore/coverage2
chore: add full test coverage
2 parents 39524d9 + 84c26ae commit e9ee5d8

18 files changed

+894
-90
lines changed

packages/excel-builder-vanilla/src/Excel/Drawing/Picture.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { Drawing } from './Drawing.js';
21
import { uniqueId } from '../../utilities/uniqueId.js';
32
import { Util } from '../Util.js';
43
import type { MediaMeta } from '../Workbook.js';
54
import type { XMLDOM } from '../XMLDOM.js';
5+
import { Drawing } from './Drawing.js';
66

77
export class Picture extends Drawing {
88
id = uniqueId('Picture');

packages/excel-builder-vanilla/src/Excel/Drawing/TwoCellAnchor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export class TwoCellAnchor {
88

99
constructor(config: DualAnchorOption) {
1010
if (config) {
11-
this.setFrom(config.from.x, config.from.y, config.to.xOff, config.to.yOff);
11+
this.setFrom(config.from.x, config.from.y, config.from.xOff, config.from.yOff);
1212
this.setTo(config.to.x, config.to.y, config.to.xOff, config.to.yOff);
1313
}
1414
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { describe, expect, it } from 'vitest';
2+
3+
import { Util } from '../../Util';
4+
import { OneCellAnchor } from '../OneCellAnchor';
5+
6+
describe('OneCellAnchor', () => {
7+
it('should set xOff and yOff when provided', () => {
8+
const anchor = new OneCellAnchor({ x: 1, y: 2, xOff: true, yOff: false, width: 10, height: 20 });
9+
expect(anchor.xOff).toBe(true);
10+
expect(anchor.yOff).toBe(false);
11+
});
12+
13+
it('should not set xOff and yOff when not provided', () => {
14+
const anchor = new OneCellAnchor({ x: 1, y: 2, width: 10, height: 20 });
15+
expect(anchor.xOff).toBeNull();
16+
expect(anchor.yOff).toBeNull();
17+
});
18+
19+
it('should set xOff and yOff via setPos', () => {
20+
const anchor = new OneCellAnchor({ x: 1, y: 2, width: 10, height: 20 });
21+
anchor.setPos(3, 4, false, true);
22+
expect(anchor.xOff).toBe(false);
23+
expect(anchor.yOff).toBe(true);
24+
});
25+
26+
it('should set and get position and dimensions correctly', () => {
27+
const anchor = new OneCellAnchor({ x: 5, y: 6, width: 100, height: 200 });
28+
expect(anchor.x).toBe(5);
29+
expect(anchor.y).toBe(6);
30+
expect(anchor.width).toBe(100);
31+
expect(anchor.height).toBe(200);
32+
anchor.setPos(7, 8, true, false);
33+
expect(anchor.x).toBe(7);
34+
expect(anchor.y).toBe(8);
35+
expect(anchor.xOff).toBe(true);
36+
expect(anchor.yOff).toBe(false);
37+
anchor.setDimensions(300, 400);
38+
expect(anchor.width).toBe(300);
39+
expect(anchor.height).toBe(400);
40+
});
41+
42+
it('should create correct XML structure in toXML', () => {
43+
// Minimal mock for XMLDOM and Util
44+
const xmlDoc = {
45+
createElement: (nodeName: string) => ({
46+
nodeName,
47+
children: [] as any[],
48+
appendChild(child: any) {
49+
(this.children as any[]).push(child);
50+
},
51+
setAttribute() {},
52+
toString() {
53+
return `<${nodeName}/>`;
54+
},
55+
}),
56+
createTextNode: (text: string) => ({ text }),
57+
};
58+
// Patch Util.createElement to use our mock
59+
const origCreateElement = Util.createElement;
60+
Util.createElement = (doc: any, name: string) => doc.createElement(name);
61+
const anchor = new OneCellAnchor({ x: 2, y: 3, xOff: true, yOff: false, width: 50, height: 60 });
62+
const xml = anchor.toXML(xmlDoc as any, {});
63+
// Check structure
64+
expect(xml.nodeName).toBe('xdr:oneCellAnchor');
65+
expect(xml.children.length).toBeGreaterThan(0);
66+
// Restore Util.createElement
67+
Util.createElement = origCreateElement;
68+
});
69+
});
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import { describe, expect, it } from 'vitest';
2+
3+
import { Util } from '../../Util';
4+
import { Picture } from '../Picture';
5+
6+
describe('Picture', () => {
7+
it('should initialize with unique ids and default values', () => {
8+
const pic = new Picture();
9+
expect(typeof pic.id).toBe('string');
10+
expect(typeof pic.pictureId).toBe('number');
11+
expect(pic.fill).toEqual({});
12+
expect(pic.mediaData).toBeNull();
13+
expect(pic.description).toBe('');
14+
});
15+
16+
it('should set media, description, fill type, and fill config', () => {
17+
const pic = new Picture();
18+
const media = { fileName: 'img.png', rId: 'rId1', id: '1', data: '', contentType: 'image/png', extension: 'png' };
19+
pic.setMedia(media);
20+
expect(pic.mediaData).toBe(media);
21+
pic.setDescription('desc');
22+
expect(pic.description).toBe('desc');
23+
pic.setFillType('solid');
24+
expect(pic.fill.type).toBe('solid');
25+
pic.setFillConfig({ color: 'red', opacity: 0.5 });
26+
expect(pic.fill.color).toBe('red');
27+
expect(pic.fill.opacity).toBe(0.5);
28+
});
29+
30+
it('should get media type and data', () => {
31+
const pic = new Picture();
32+
const media = { fileName: 'img.png', rId: 'rId1', id: '2', data: '', contentType: 'image/png', extension: 'png' };
33+
pic.setMedia(media);
34+
expect(pic.getMediaType()).toBe('image');
35+
expect(pic.getMediaData()).toBe(media);
36+
});
37+
38+
it('should set relationship id on mediaData', () => {
39+
const pic = new Picture();
40+
const media = { fileName: 'img.png', rId: '', id: '3', data: '', contentType: 'image/png', extension: 'png' };
41+
pic.setMedia(media);
42+
pic.setRelationshipId('rId2');
43+
expect(pic.mediaData!.rId).toBe('rId2');
44+
});
45+
46+
it('should create correct XML structure in toXML', () => {
47+
// Minimal mock for XMLDOM, Util, and anchor
48+
const xmlDoc = {
49+
createElement: (nodeName: string) => ({
50+
nodeName,
51+
children: [] as any[],
52+
appendChild(child: any) {
53+
(this.children as any[]).push(child);
54+
},
55+
setAttribute() {},
56+
toString() {
57+
return `<${nodeName}/>`;
58+
},
59+
}),
60+
createTextNode: (text: string) => ({ text }),
61+
};
62+
const origCreateElement = Util.createElement;
63+
Util.createElement = (doc: any, name: string, attrs?: any) => doc.createElement(name);
64+
const pic = new Picture();
65+
pic.anchor = { toXML: (doc: any, node: any) => ({ nodeName: 'anchored', children: [node] }) } as any;
66+
pic.setMedia({ fileName: 'img.png', rId: 'rId1', id: '4', data: '', contentType: 'image/png', extension: 'png' });
67+
pic.setDescription('desc');
68+
const xml = pic.toXML(xmlDoc as any);
69+
expect(xml.nodeName).toBe('anchored');
70+
expect(xml.children[0].nodeName).toBe('xdr:pic');
71+
Util.createElement = origCreateElement;
72+
});
73+
});
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { describe, expect, it } from 'vitest';
2+
3+
import { Util } from '../../Util';
4+
import { TwoCellAnchor } from '../TwoCellAnchor';
5+
6+
describe('TwoCellAnchor', () => {
7+
it('should set from and to positions and offsets via constructor', () => {
8+
const anchor = new TwoCellAnchor({
9+
from: { x: 1, y: 2, xOff: true, yOff: false, width: 10, height: 20 },
10+
to: { x: 3, y: 4, xOff: false, yOff: true, width: 30, height: 40 },
11+
});
12+
expect(anchor.from.x).toBe(1);
13+
expect(anchor.from.y).toBe(2);
14+
expect(anchor.from.xOff).toBe(true);
15+
expect(anchor.from.yOff).toBe(false);
16+
expect(anchor.to.x).toBe(3);
17+
expect(anchor.to.y).toBe(4);
18+
expect(anchor.to.xOff).toBe(false);
19+
expect(anchor.to.yOff).toBe(true);
20+
});
21+
22+
it('should set from and to via setFrom and setTo', () => {
23+
const anchor = new TwoCellAnchor({
24+
from: { x: 0, y: 0, width: 1, height: 1 },
25+
to: { x: 0, y: 0, width: 1, height: 1 },
26+
});
27+
anchor.setFrom(5, 6, true, false);
28+
anchor.setTo(7, 8, false, true);
29+
expect(anchor.from.x).toBe(5);
30+
expect(anchor.from.y).toBe(6);
31+
expect(anchor.from.xOff).toBe(true);
32+
expect(anchor.from.yOff).toBe(false);
33+
expect(anchor.to.x).toBe(7);
34+
expect(anchor.to.y).toBe(8);
35+
expect(anchor.to.xOff).toBe(false);
36+
expect(anchor.to.yOff).toBe(true);
37+
});
38+
39+
it('should create correct XML structure in toXML', () => {
40+
// Minimal mock for XMLDOM and Util
41+
const xmlDoc = {
42+
createElement: (nodeName: string) => ({
43+
nodeName,
44+
children: [] as any[],
45+
appendChild(child: any) {
46+
(this.children as any[]).push(child);
47+
},
48+
setAttribute() {},
49+
toString() {
50+
return `<${nodeName}/>`;
51+
},
52+
}),
53+
createTextNode: (text: string) => ({ text }),
54+
};
55+
// Patch Util.createElement to use our mock
56+
const origCreateElement = Util.createElement;
57+
Util.createElement = (doc: any, name: string) => doc.createElement(name);
58+
const anchor = new TwoCellAnchor({
59+
from: { x: 1, y: 2, xOff: true, yOff: false, width: 10, height: 20 },
60+
to: { x: 3, y: 4, xOff: false, yOff: true, width: 30, height: 40 },
61+
});
62+
const xml = anchor.toXML(xmlDoc as any, { nodeName: 'content' });
63+
expect(xml.nodeName).toBe('xdr:twoCellAnchor');
64+
expect(xml.children.length).toBeGreaterThan(0);
65+
// Restore Util.createElement
66+
Util.createElement = origCreateElement;
67+
});
68+
});

packages/excel-builder-vanilla/src/__tests__/Drawings.spec.ts renamed to packages/excel-builder-vanilla/src/Excel/__tests__/Drawings.spec.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { describe, expect, test } from 'vitest';
22

3-
import { Picture } from '../Excel/Drawing/Picture.js';
4-
import { Drawings } from '../Excel/Drawings.js';
5-
import { Positioning } from '../Excel/Positioning.js';
6-
import { createWorkbook } from '../factory.js';
3+
import { createWorkbook } from '../../factory.js';
4+
import { Picture } from '../Drawing/Picture.js';
5+
import { Drawings } from '../Drawings.js';
6+
import { Positioning } from '../Positioning.js';
77

88
describe('Drawings', () => {
99
test('Drawings', async () => {
@@ -76,6 +76,7 @@ describe('Drawings', () => {
7676

7777
const wsXML = fruitWorkbook.toXML();
7878
expect(wsXML.documentElement.children.length).toBe(2);
79+
expect(drawings.getCount()).toBe(3);
7980
});
8081

8182
test('toXML with missing relationship', () => {

packages/excel-builder-vanilla/src/__tests__/Pane.spec.ts renamed to packages/excel-builder-vanilla/src/Excel/__tests__/Pane.spec.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, expect, test } from 'vitest';
22

3-
import { Pane } from '../Excel/Pane.js';
3+
import { Pane } from '../Pane.js';
44

55
describe('Pane', () => {
66
test('Pane with invalid state', () => {
@@ -17,4 +17,10 @@ describe('Pane', () => {
1717
const doc = { createElement: () => ({ setAttribute: () => {} }) };
1818
expect(() => pane.exportXML(doc as any)).not.toThrow();
1919
});
20+
21+
test('freezePane sets _freezePane correctly', () => {
22+
const pane = new Pane();
23+
pane.freezePane(2, 3, 'B2');
24+
expect(pane._freezePane).toEqual({ xSplit: 2, ySplit: 3, cell: 'B2' });
25+
});
2026
});

packages/excel-builder-vanilla/src/__tests__/RelationshipManager.spec.ts renamed to packages/excel-builder-vanilla/src/Excel/__tests__/RelationshipManager.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, expect, test } from 'vitest';
22

3-
import { RelationshipManager } from '../Excel/RelationshipManager.js';
3+
import { RelationshipManager } from '../RelationshipManager.js';
44

55
describe('RelationshipManager', () => {
66
test('toXML with targetMode', () => {

packages/excel-builder-vanilla/src/__tests__/SharedStrings.spec.ts renamed to packages/excel-builder-vanilla/src/Excel/__tests__/SharedStrings.spec.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
import { describe, expect, test } from 'vitest';
22

3-
import { SharedStrings } from '../Excel/SharedStrings.js';
3+
import { SharedStrings } from '../SharedStrings.js';
44

55
describe('SharedStrings', () => {
66
test('toXML with whitespace string', () => {
77
const ss = new SharedStrings();
88
ss.stringArray = ['with space'];
99
expect(() => ss.toXML()).not.toThrow();
1010
});
11+
12+
test('exportData returns strings object', () => {
13+
const ss = new SharedStrings();
14+
ss.addString('foo');
15+
expect(ss.exportData()).toEqual({ foo: 0 });
16+
});
1117
});

packages/excel-builder-vanilla/src/__tests__/SheetView.spec.ts renamed to packages/excel-builder-vanilla/src/Excel/__tests__/SheetView.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, expect, test } from 'vitest';
22

3-
import { SheetView } from '../Excel/SheetView.js';
3+
import { SheetView } from '../SheetView.js';
44

55
describe('SheetView', () => {
66
test('exportXML with all options', () => {

0 commit comments

Comments
 (0)