|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 2 | +import { computed, ref } from 'vue' |
| 3 | +import { useBytesFormatter } from '../../../../app/composables/useNumberFormatter' |
| 4 | + |
| 5 | +describe('useBytesFormatter', () => { |
| 6 | + beforeEach(() => { |
| 7 | + vi.stubGlobal('computed', computed) |
| 8 | + vi.stubGlobal('useI18n', () => ({ |
| 9 | + locale: ref('en-US'), |
| 10 | + t: (key: string, params?: { size?: string }) => { |
| 11 | + const size = params?.size ?? '' |
| 12 | + |
| 13 | + if (key === 'package.size.b') return `${size} B` |
| 14 | + if (key === 'package.size.kb') return `${size} kB` |
| 15 | + if (key === 'package.size.mb') return `${size} MB` |
| 16 | + |
| 17 | + return key |
| 18 | + }, |
| 19 | + })) |
| 20 | + }) |
| 21 | + |
| 22 | + afterEach(() => { |
| 23 | + vi.unstubAllGlobals() |
| 24 | + }) |
| 25 | + |
| 26 | + it('formats values below 1 kB in bytes', () => { |
| 27 | + const { format } = useBytesFormatter() |
| 28 | + |
| 29 | + expect(format(0)).toBe('0 B') |
| 30 | + expect(format(999)).toBe('999 B') |
| 31 | + }) |
| 32 | + |
| 33 | + it('formats kB values using decimal base (1000)', () => { |
| 34 | + const { format } = useBytesFormatter() |
| 35 | + |
| 36 | + expect(format(1000)).toBe('1 kB') |
| 37 | + expect(format(8414)).toBe('8.4 kB') |
| 38 | + }) |
| 39 | + |
| 40 | + it('formats MB values using decimal base (1000 * 1000)', () => { |
| 41 | + const { format } = useBytesFormatter() |
| 42 | + |
| 43 | + expect(format(1_000_000)).toBe('1 MB') |
| 44 | + expect(format(1_500_000)).toBe('1.5 MB') |
| 45 | + }) |
| 46 | +}) |
0 commit comments