Skip to content

Commit ae79a9e

Browse files
committed
Add tests
1 parent 0bafacd commit ae79a9e

File tree

1 file changed

+93
-1
lines changed

1 file changed

+93
-1
lines changed

src/vscode-table/calculations.test.ts

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
/* eslint-disable @typescript-eslint/no-unused-expressions */
22
import {expect} from '@open-wc/testing';
3-
import {parseSizeAttributeToPercent} from './calculations.js';
3+
import {
4+
calculateColumnWidths,
5+
parseSizeAttributeToPercent,
6+
Percent,
7+
percent,
8+
} from './calculations.js';
49

510
describe('parseSizeAttributeToPercent', () => {
611
const base = 200;
@@ -85,3 +90,90 @@ describe('parseSizeAttributeToPercent', () => {
8590
expect(parseSizeAttributeToPercent(50, 0)).to.be.null;
8691
});
8792
});
93+
94+
describe('calculateColumnWidths', () => {
95+
it('returns unchanged widths when delta is 0', () => {
96+
const widths = [percent(25), percent(25), percent(50)];
97+
98+
const result = calculateColumnWidths(widths, 1, percent(0), percent(10));
99+
100+
expect(result).to.deep.equal(widths);
101+
});
102+
103+
it('returns unchanged widths for invalid splitter index', () => {
104+
const widths = [percent(30), percent(30), percent(40)];
105+
106+
expect(
107+
calculateColumnWidths(widths, -1, percent(10), percent(10))
108+
).to.deep.equal(widths);
109+
expect(
110+
calculateColumnWidths(widths, 2, percent(10), percent(10))
111+
).to.deep.equal(widths);
112+
});
113+
114+
it('shrinks right column and grows left column when dragging right (delta > 0)', () => {
115+
const widths = [percent(30), percent(30), percent(40)];
116+
117+
const result = calculateColumnWidths(widths, 1, percent(10), percent(10));
118+
119+
expect(result).to.deep.equal([percent(30), percent(40), percent(30)]);
120+
});
121+
122+
it('shrinks left column and grows right column when dragging left (delta < 0)', () => {
123+
const widths = [percent(30), percent(30), percent(40)];
124+
125+
const result = calculateColumnWidths(widths, 1, percent(-10), percent(10));
126+
127+
expect(result).to.deep.equal([percent(30), percent(20), percent(50)]);
128+
});
129+
130+
it('respects minWidth when shrinking', () => {
131+
const widths = [percent(30), percent(20), percent(50)];
132+
133+
const result = calculateColumnWidths(widths, 0, percent(15), percent(20));
134+
135+
// right side shrinks, left side grows
136+
expect(result).to.deep.equal([percent(45), percent(20), percent(35)]);
137+
});
138+
139+
it('shrinks multiple columns sequentially when needed', () => {
140+
const widths = [percent(40), percent(30), percent(30)];
141+
142+
const result = calculateColumnWidths(widths, 0, percent(25), percent(10));
143+
144+
expect(result).to.deep.equal([percent(65), percent(10), percent(25)]);
145+
});
146+
147+
it('aborts if total available shrink space is insufficient', () => {
148+
const widths = [percent(40), percent(15), percent(45)];
149+
150+
const result = calculateColumnWidths(widths, 0, percent(20), percent(10));
151+
expect(result).to.not.deep.equal(widths);
152+
153+
const impossible = calculateColumnWidths(
154+
widths,
155+
0,
156+
percent(50),
157+
percent(10)
158+
);
159+
expect(impossible).to.deep.equal(widths);
160+
});
161+
162+
it('only grows the nearest column on the growing side', () => {
163+
const widths = [percent(20), percent(40), percent(40)];
164+
165+
const result = calculateColumnWidths(widths, 1, percent(10), percent(10));
166+
167+
expect(result).to.deep.equal([percent(20), percent(50), percent(30)]);
168+
});
169+
170+
it('preserves total width sum', () => {
171+
const widths = [percent(25), percent(25), percent(50)];
172+
173+
const result = calculateColumnWidths(widths, 0, percent(15), percent(10));
174+
175+
const sum = (arr: Percent[]) => arr.reduce((a, b) => a + b, 0);
176+
177+
expect(sum(result)).to.equal(sum(widths));
178+
});
179+
});

0 commit comments

Comments
 (0)