Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Drawing } from './Drawing.js';
import { uniqueId } from '../../utilities/uniqueId.js';
import { Util } from '../Util.js';
import type { MediaMeta } from '../Workbook.js';
import type { XMLDOM } from '../XMLDOM.js';
import { Drawing } from './Drawing.js';

export class Picture extends Drawing {
id = uniqueId('Picture');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class TwoCellAnchor {

constructor(config: DualAnchorOption) {
if (config) {
this.setFrom(config.from.x, config.from.y, config.to.xOff, config.to.yOff);
this.setFrom(config.from.x, config.from.y, config.from.xOff, config.from.yOff);
this.setTo(config.to.x, config.to.y, config.to.xOff, config.to.yOff);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { describe, expect, it } from 'vitest';

import { Util } from '../../Util';
import { OneCellAnchor } from '../OneCellAnchor';

describe('OneCellAnchor', () => {
it('should set xOff and yOff when provided', () => {
const anchor = new OneCellAnchor({ x: 1, y: 2, xOff: true, yOff: false, width: 10, height: 20 });
expect(anchor.xOff).toBe(true);
expect(anchor.yOff).toBe(false);
});

it('should not set xOff and yOff when not provided', () => {
const anchor = new OneCellAnchor({ x: 1, y: 2, width: 10, height: 20 });
expect(anchor.xOff).toBeNull();
expect(anchor.yOff).toBeNull();
});

it('should set xOff and yOff via setPos', () => {
const anchor = new OneCellAnchor({ x: 1, y: 2, width: 10, height: 20 });
anchor.setPos(3, 4, false, true);
expect(anchor.xOff).toBe(false);
expect(anchor.yOff).toBe(true);
});

it('should set and get position and dimensions correctly', () => {
const anchor = new OneCellAnchor({ x: 5, y: 6, width: 100, height: 200 });
expect(anchor.x).toBe(5);
expect(anchor.y).toBe(6);
expect(anchor.width).toBe(100);
expect(anchor.height).toBe(200);
anchor.setPos(7, 8, true, false);
expect(anchor.x).toBe(7);
expect(anchor.y).toBe(8);
expect(anchor.xOff).toBe(true);
expect(anchor.yOff).toBe(false);
anchor.setDimensions(300, 400);
expect(anchor.width).toBe(300);
expect(anchor.height).toBe(400);
});

it('should create correct XML structure in toXML', () => {
// Minimal mock for XMLDOM and Util
const xmlDoc = {
createElement: (nodeName: string) => ({
nodeName,
children: [] as any[],
appendChild(child: any) {
(this.children as any[]).push(child);
},
setAttribute() {},
toString() {
return `<${nodeName}/>`;
},
}),
createTextNode: (text: string) => ({ text }),
};
// Patch Util.createElement to use our mock
const origCreateElement = Util.createElement;
Util.createElement = (doc: any, name: string) => doc.createElement(name);
const anchor = new OneCellAnchor({ x: 2, y: 3, xOff: true, yOff: false, width: 50, height: 60 });
const xml = anchor.toXML(xmlDoc as any, {});
// Check structure
expect(xml.nodeName).toBe('xdr:oneCellAnchor');
expect(xml.children.length).toBeGreaterThan(0);
// Restore Util.createElement
Util.createElement = origCreateElement;
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { describe, expect, it } from 'vitest';

import { Util } from '../../Util';
import { Picture } from '../Picture';

describe('Picture', () => {
it('should initialize with unique ids and default values', () => {
const pic = new Picture();
expect(typeof pic.id).toBe('string');
expect(typeof pic.pictureId).toBe('number');
expect(pic.fill).toEqual({});
expect(pic.mediaData).toBeNull();
expect(pic.description).toBe('');
});

it('should set media, description, fill type, and fill config', () => {
const pic = new Picture();
const media = { fileName: 'img.png', rId: 'rId1', id: '1', data: '', contentType: 'image/png', extension: 'png' };
pic.setMedia(media);
expect(pic.mediaData).toBe(media);
pic.setDescription('desc');
expect(pic.description).toBe('desc');
pic.setFillType('solid');
expect(pic.fill.type).toBe('solid');
pic.setFillConfig({ color: 'red', opacity: 0.5 });
expect(pic.fill.color).toBe('red');
expect(pic.fill.opacity).toBe(0.5);
});

it('should get media type and data', () => {
const pic = new Picture();
const media = { fileName: 'img.png', rId: 'rId1', id: '2', data: '', contentType: 'image/png', extension: 'png' };
pic.setMedia(media);
expect(pic.getMediaType()).toBe('image');
expect(pic.getMediaData()).toBe(media);
});

it('should set relationship id on mediaData', () => {
const pic = new Picture();
const media = { fileName: 'img.png', rId: '', id: '3', data: '', contentType: 'image/png', extension: 'png' };
pic.setMedia(media);
pic.setRelationshipId('rId2');
expect(pic.mediaData!.rId).toBe('rId2');
});

it('should create correct XML structure in toXML', () => {
// Minimal mock for XMLDOM, Util, and anchor
const xmlDoc = {
createElement: (nodeName: string) => ({
nodeName,
children: [] as any[],
appendChild(child: any) {
(this.children as any[]).push(child);
},
setAttribute() {},
toString() {
return `<${nodeName}/>`;
},
}),
createTextNode: (text: string) => ({ text }),
};
const origCreateElement = Util.createElement;
Util.createElement = (doc: any, name: string, attrs?: any) => doc.createElement(name);
const pic = new Picture();
pic.anchor = { toXML: (doc: any, node: any) => ({ nodeName: 'anchored', children: [node] }) } as any;
pic.setMedia({ fileName: 'img.png', rId: 'rId1', id: '4', data: '', contentType: 'image/png', extension: 'png' });
pic.setDescription('desc');
const xml = pic.toXML(xmlDoc as any);
expect(xml.nodeName).toBe('anchored');
expect(xml.children[0].nodeName).toBe('xdr:pic');
Util.createElement = origCreateElement;
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { describe, expect, it } from 'vitest';

import { Util } from '../../Util';
import { TwoCellAnchor } from '../TwoCellAnchor';

describe('TwoCellAnchor', () => {
it('should set from and to positions and offsets via constructor', () => {
const anchor = new TwoCellAnchor({
from: { x: 1, y: 2, xOff: true, yOff: false, width: 10, height: 20 },
to: { x: 3, y: 4, xOff: false, yOff: true, width: 30, height: 40 },
});
expect(anchor.from.x).toBe(1);
expect(anchor.from.y).toBe(2);
expect(anchor.from.xOff).toBe(true);
expect(anchor.from.yOff).toBe(false);
expect(anchor.to.x).toBe(3);
expect(anchor.to.y).toBe(4);
expect(anchor.to.xOff).toBe(false);
expect(anchor.to.yOff).toBe(true);
});

it('should set from and to via setFrom and setTo', () => {
const anchor = new TwoCellAnchor({
from: { x: 0, y: 0, width: 1, height: 1 },
to: { x: 0, y: 0, width: 1, height: 1 },
});
anchor.setFrom(5, 6, true, false);
anchor.setTo(7, 8, false, true);
expect(anchor.from.x).toBe(5);
expect(anchor.from.y).toBe(6);
expect(anchor.from.xOff).toBe(true);
expect(anchor.from.yOff).toBe(false);
expect(anchor.to.x).toBe(7);
expect(anchor.to.y).toBe(8);
expect(anchor.to.xOff).toBe(false);
expect(anchor.to.yOff).toBe(true);
});

it('should create correct XML structure in toXML', () => {
// Minimal mock for XMLDOM and Util
const xmlDoc = {
createElement: (nodeName: string) => ({
nodeName,
children: [] as any[],
appendChild(child: any) {
(this.children as any[]).push(child);
},
setAttribute() {},
toString() {
return `<${nodeName}/>`;
},
}),
createTextNode: (text: string) => ({ text }),
};
// Patch Util.createElement to use our mock
const origCreateElement = Util.createElement;
Util.createElement = (doc: any, name: string) => doc.createElement(name);
const anchor = new TwoCellAnchor({
from: { x: 1, y: 2, xOff: true, yOff: false, width: 10, height: 20 },
to: { x: 3, y: 4, xOff: false, yOff: true, width: 30, height: 40 },
});
const xml = anchor.toXML(xmlDoc as any, { nodeName: 'content' });
expect(xml.nodeName).toBe('xdr:twoCellAnchor');
expect(xml.children.length).toBeGreaterThan(0);
// Restore Util.createElement
Util.createElement = origCreateElement;
});
});
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { describe, expect, test } from 'vitest';

import { Picture } from '../Excel/Drawing/Picture.js';
import { Drawings } from '../Excel/Drawings.js';
import { Positioning } from '../Excel/Positioning.js';
import { createWorkbook } from '../factory.js';
import { createWorkbook } from '../../factory.js';
import { Picture } from '../Drawing/Picture.js';
import { Drawings } from '../Drawings.js';
import { Positioning } from '../Positioning.js';

describe('Drawings', () => {
test('Drawings', async () => {
Expand Down Expand Up @@ -76,6 +76,7 @@ describe('Drawings', () => {

const wsXML = fruitWorkbook.toXML();
expect(wsXML.documentElement.children.length).toBe(2);
expect(drawings.getCount()).toBe(3);
});

test('toXML with missing relationship', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, test } from 'vitest';

import { Pane } from '../Excel/Pane.js';
import { Pane } from '../Pane.js';

describe('Pane', () => {
test('Pane with invalid state', () => {
Expand All @@ -17,4 +17,10 @@ describe('Pane', () => {
const doc = { createElement: () => ({ setAttribute: () => {} }) };
expect(() => pane.exportXML(doc as any)).not.toThrow();
});

test('freezePane sets _freezePane correctly', () => {
const pane = new Pane();
pane.freezePane(2, 3, 'B2');
expect(pane._freezePane).toEqual({ xSplit: 2, ySplit: 3, cell: 'B2' });
});
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, test } from 'vitest';

import { RelationshipManager } from '../Excel/RelationshipManager.js';
import { RelationshipManager } from '../RelationshipManager.js';

describe('RelationshipManager', () => {
test('toXML with targetMode', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { describe, expect, test } from 'vitest';

import { SharedStrings } from '../Excel/SharedStrings.js';
import { SharedStrings } from '../SharedStrings.js';

describe('SharedStrings', () => {
test('toXML with whitespace string', () => {
const ss = new SharedStrings();
ss.stringArray = ['with space'];
expect(() => ss.toXML()).not.toThrow();
});

test('exportData returns strings object', () => {
const ss = new SharedStrings();
ss.addString('foo');
expect(ss.exportData()).toEqual({ foo: 0 });
});
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, test } from 'vitest';

import { SheetView } from '../Excel/SheetView.js';
import { SheetView } from '../SheetView.js';

describe('SheetView', () => {
test('exportXML with all options', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, expect, test } from 'vitest';

import { StyleSheet } from '../Excel/StyleSheet.js';
import { XMLNode } from '../Excel/XMLDOM.js';
import { StyleSheet } from '../StyleSheet.js';
import { XMLNode } from '../XMLDOM.js';

describe('StyleSheet', () => {
test('createFormat with empty object', () => {
Expand Down
Loading