Full document, spreadsheet, slideshow, and diagram tooling
1import { describe, it, expect } from 'vitest';
2import { normalizeRange, isInRange } from '../src/sheets/selection-utils.js';
3
4describe('normalizeRange', () => {
5 it('returns same values when already normalized', () => {
6 expect(normalizeRange({ startCol: 1, startRow: 1, endCol: 3, endRow: 5 }))
7 .toEqual({ startCol: 1, startRow: 1, endCol: 3, endRow: 5 });
8 });
9
10 it('swaps when start > end', () => {
11 expect(normalizeRange({ startCol: 5, startRow: 10, endCol: 2, endRow: 3 }))
12 .toEqual({ startCol: 2, startRow: 3, endCol: 5, endRow: 10 });
13 });
14
15 it('handles mixed: col reversed, row normal', () => {
16 expect(normalizeRange({ startCol: 4, startRow: 1, endCol: 1, endRow: 3 }))
17 .toEqual({ startCol: 1, startRow: 1, endCol: 4, endRow: 3 });
18 });
19
20 it('handles single cell (start === end)', () => {
21 expect(normalizeRange({ startCol: 3, startRow: 7, endCol: 3, endRow: 7 }))
22 .toEqual({ startCol: 3, startRow: 7, endCol: 3, endRow: 7 });
23 });
24});
25
26describe('isInRange', () => {
27 it('returns true for cell inside range', () => {
28 const range = { startCol: 2, startRow: 2, endCol: 5, endRow: 5 };
29 expect(isInRange(3, 3, range)).toBe(true);
30 });
31
32 it('returns true for cell on range boundary', () => {
33 const range = { startCol: 2, startRow: 2, endCol: 5, endRow: 5 };
34 expect(isInRange(2, 2, range)).toBe(true);
35 expect(isInRange(5, 5, range)).toBe(true);
36 expect(isInRange(2, 5, range)).toBe(true);
37 expect(isInRange(5, 2, range)).toBe(true);
38 });
39
40 it('returns false for cell outside range', () => {
41 const range = { startCol: 2, startRow: 2, endCol: 5, endRow: 5 };
42 expect(isInRange(1, 3, range)).toBe(false);
43 expect(isInRange(6, 3, range)).toBe(false);
44 expect(isInRange(3, 1, range)).toBe(false);
45 expect(isInRange(3, 6, range)).toBe(false);
46 });
47
48 it('returns false for null range', () => {
49 expect(isInRange(1, 1, null)).toBe(false);
50 });
51
52 it('works with reversed range (auto-normalizes)', () => {
53 const range = { startCol: 5, startRow: 5, endCol: 2, endRow: 2 };
54 expect(isInRange(3, 3, range)).toBe(true);
55 expect(isInRange(1, 1, range)).toBe(false);
56 });
57});