|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | + |
| 3 | +import { cn, enumToReverseObject, formatInterval, formatNanosecondsInterval, truncateText } from './utils' |
| 4 | + |
| 5 | +describe('utils', () => { |
| 6 | + describe('cn', () => { |
| 7 | + it('merges tailwind conflicting classes', () => { |
| 8 | + expect(cn('p-2', 'p-4')).toBe('p-4') |
| 9 | + expect(cn('text-red-500', 'text-blue-500')).toBe('text-blue-500') |
| 10 | + }) |
| 11 | + |
| 12 | + it('supports conditional values', () => { |
| 13 | + const shouldInclude = false |
| 14 | + expect(cn('a', shouldInclude && 'b', undefined, null, 'c')).toBe('a c') |
| 15 | + }) |
| 16 | + }) |
| 17 | + |
| 18 | + describe('truncateText', () => { |
| 19 | + it('returns the original string when under the limit', () => { |
| 20 | + expect(truncateText('hello', 10)).toBe('hello') |
| 21 | + }) |
| 22 | + |
| 23 | + it('truncates and appends ellipsis by default', () => { |
| 24 | + expect(truncateText('hello world', 8)).toBe('hello...') |
| 25 | + }) |
| 26 | + |
| 27 | + it('uses a custom ellipsis', () => { |
| 28 | + expect(truncateText('hello world', 8, '…')).toBe('hello w…') |
| 29 | + }) |
| 30 | + |
| 31 | + it('truncates ellipsis itself when limit is shorter than ellipsis', () => { |
| 32 | + expect(truncateText('hello world', 2, '...')).toBe('..') |
| 33 | + }) |
| 34 | + }) |
| 35 | + |
| 36 | + describe('formatInterval', () => { |
| 37 | + it('formats milliseconds into a human readable string', () => { |
| 38 | + expect(formatInterval(0)).toBe('') |
| 39 | + expect(formatInterval(1000)).toBe('1 second') |
| 40 | + expect(formatInterval(61_000)).toBe('1 minute 1 second') |
| 41 | + }) |
| 42 | + }) |
| 43 | + |
| 44 | + describe('formatNanosecondsInterval', () => { |
| 45 | + it('converts nanoseconds to milliseconds before formatting', () => { |
| 46 | + expect(formatNanosecondsInterval(1_000_000_000)).toBe('1 second') |
| 47 | + }) |
| 48 | + }) |
| 49 | + |
| 50 | + describe('enumToReverseObject', () => { |
| 51 | + it('builds a numeric reverse map from a TS numeric enum-like object', () => { |
| 52 | + const enumLike = { 0: 'A', 1: 'B', A: 0, B: 1 } as const |
| 53 | + expect(enumToReverseObject(enumLike)).toEqual({ 0: 'A', 1: 'B' }) |
| 54 | + }) |
| 55 | + }) |
| 56 | +}) |
0 commit comments