|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { escHtml, formatDatetime, formatDate, sortRows, uniqueValues, filterRows, PAGE_SIZE, SELECT_THRESHOLD } from '../lib/utils.js'; |
| 3 | + |
| 4 | +describe('escHtml', () => { |
| 5 | + it('escapes ampersands', () => expect(escHtml('a&b')).toBe('a&b')); |
| 6 | + it('escapes less-than', () => expect(escHtml('a<b')).toBe('a<b')); |
| 7 | + it('escapes greater-than', () => expect(escHtml('a>b')).toBe('a>b')); |
| 8 | + it('escapes double quotes', () => expect(escHtml('a"b')).toBe('a"b')); |
| 9 | + it('handles null', () => expect(escHtml(null)).toBe('')); |
| 10 | + it('handles undefined', () => expect(escHtml(undefined)).toBe('')); |
| 11 | + it('coerces numbers to string', () => expect(escHtml(42)).toBe('42')); |
| 12 | +}); |
| 13 | + |
| 14 | +describe('formatDatetime', () => { |
| 15 | + it('formats ISO UTC to YYYY-MM-DD HH:MM', () => expect(formatDatetime('2024-03-15T09:05:00Z')).toBe('2024-03-15 09:05')); |
| 16 | + it('returns empty for null', () => expect(formatDatetime(null)).toBe('')); |
| 17 | + it('returns raw string for invalid date', () => expect(formatDatetime('not-a-date')).toBe('not-a-date')); |
| 18 | + it('handles empty string', () => expect(formatDatetime('')).toBe('')); |
| 19 | +}); |
| 20 | + |
| 21 | +describe('formatDate', () => { |
| 22 | + it('formats ISO UTC to YYYY-MM-DD', () => expect(formatDate('2024-03-15T09:05:00Z')).toBe('2024-03-15')); |
| 23 | + it('returns empty for null', () => expect(formatDate(null)).toBe('')); |
| 24 | + it('returns raw string for invalid date', () => expect(formatDate('not-a-date')).toBe('not-a-date')); |
| 25 | + it('handles empty string', () => expect(formatDate('')).toBe('')); |
| 26 | +}); |
| 27 | + |
| 28 | +describe('sortRows', () => { |
| 29 | + it('sorts rows ascending by column', () => { |
| 30 | + const rows = [{ name: 'Bob', age: 30 }, { name: 'Alice', age: 25 }]; |
| 31 | + sortRows(rows, 'name', 'asc'); |
| 32 | + expect(rows[0].name).toBe('Alice'); |
| 33 | + expect(rows[1].name).toBe('Bob'); |
| 34 | + }); |
| 35 | + |
| 36 | + it('sorts rows descending by column', () => { |
| 37 | + const rows = [{ name: 'Bob', age: 30 }, { name: 'Alice', age: 25 }]; |
| 38 | + sortRows(rows, 'name', 'desc'); |
| 39 | + expect(rows[0].name).toBe('Bob'); |
| 40 | + expect(rows[1].name).toBe('Alice'); |
| 41 | + }); |
| 42 | + |
| 43 | + it('handles missing values as empty strings (sort to start in ascending)', () => { |
| 44 | + const rows = [{ name: 'Bob' }, { name: 'Alice' }, { age: 25 }]; |
| 45 | + sortRows(rows, 'name', 'asc'); |
| 46 | + // Missing 'name' property coerces to '', which sorts first |
| 47 | + expect(rows[0].age).toBe(25); |
| 48 | + expect(rows[0].name).toBeUndefined(); |
| 49 | + expect(rows[1].name).toBe('Alice'); |
| 50 | + expect(rows[2].name).toBe('Bob'); |
| 51 | + }); |
| 52 | +}); |
| 53 | + |
| 54 | +describe('uniqueValues', () => { |
| 55 | + it('returns unique values from a column', () => { |
| 56 | + const rows = [{ type: 'A' }, { type: 'B' }, { type: 'A' }, { type: 'C' }]; |
| 57 | + const values = uniqueValues(rows, 'type'); |
| 58 | + expect(values).toEqual(['A', 'B', 'C']); |
| 59 | + }); |
| 60 | + |
| 61 | + it('filters out null and empty values', () => { |
| 62 | + const rows = [{ type: 'A' }, { type: null }, { type: '' }, { type: 'B' }]; |
| 63 | + const values = uniqueValues(rows, 'type'); |
| 64 | + expect(values).toEqual(['A', 'B']); |
| 65 | + }); |
| 66 | + |
| 67 | + it('returns sorted values', () => { |
| 68 | + const rows = [{ type: 'Z' }, { type: 'A' }, { type: 'M' }]; |
| 69 | + const values = uniqueValues(rows, 'type'); |
| 70 | + expect(values).toEqual(['A', 'M', 'Z']); |
| 71 | + }); |
| 72 | +}); |
| 73 | + |
| 74 | +describe('filterRows', () => { |
| 75 | + it('returns all rows when no filters are applied', () => { |
| 76 | + const rows = [{ name: 'Alice', age: 25 }, { name: 'Bob', age: 30 }]; |
| 77 | + const result = filterRows(rows, {}); |
| 78 | + expect(result).toEqual(rows); |
| 79 | + }); |
| 80 | + |
| 81 | + it('filters rows by a single column', () => { |
| 82 | + const rows = [{ name: 'Alice', age: 25 }, { name: 'Bob', age: 30 }]; |
| 83 | + const result = filterRows(rows, { name: 'Alice' }); |
| 84 | + expect(result).toHaveLength(1); |
| 85 | + expect(result[0].name).toBe('Alice'); |
| 86 | + }); |
| 87 | + |
| 88 | + it('uses case-insensitive substring matching', () => { |
| 89 | + const rows = [{ name: 'Alice' }, { name: 'Alison' }, { name: 'Bob' }]; |
| 90 | + const result = filterRows(rows, { name: 'ALI' }); |
| 91 | + expect(result).toHaveLength(2); |
| 92 | + }); |
| 93 | + |
| 94 | + it('filters by multiple columns (AND condition)', () => { |
| 95 | + const rows = [ |
| 96 | + { name: 'Alice', age: 25 }, |
| 97 | + { name: 'Alice', age: 30 }, |
| 98 | + { name: 'Bob', age: 25 }, |
| 99 | + ]; |
| 100 | + const result = filterRows(rows, { name: 'Alice', age: '25' }); |
| 101 | + expect(result).toHaveLength(1); |
| 102 | + expect(result[0].name).toBe('Alice'); |
| 103 | + }); |
| 104 | +}); |
| 105 | + |
| 106 | +describe('Constants', () => { |
| 107 | + it('PAGE_SIZE is 100', () => expect(PAGE_SIZE).toBe(100)); |
| 108 | + it('SELECT_THRESHOLD is 40', () => expect(SELECT_THRESHOLD).toBe(40)); |
| 109 | +}); |
0 commit comments