Files
dify/web/utils/format.spec.ts
longbingljw 36e6205c6c feat:update latest commit (#51)
* test: adding some web tests (#27792)

* feat: add validation to prevent saving empty opening statement in conversation opener modal (#27843)

* fix(web): improve the consistency of the inputs-form UI (#27837)

* fix(web): increase z-index of PortalToFollowElemContent (#27823)

* fix: installation_id is missing when in tools page (#27849)

* fix: avoid passing empty uniqueIdentifier to InstallFromMarketplace (#27802)

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>

* test: create new test scripts and update some existing test scripts o… (#27850)

* feat: change feedback to forum (#27862)

* chore: translate i18n files and update type definitions (#27868)

Co-authored-by: crazywoola <100913391+crazywoola@users.noreply.github.com>

* Fix/template transformer line number (#27867)

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: crazywoola <100913391+crazywoola@users.noreply.github.com>

* bump vite to 6.4.1 (#27877)

* Add WEAVIATE_GRPC_ENDPOINT as designed in weaviate migration guide (#27861)

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>

* Fix: correct DraftWorkflowApi.post response model (#27289)

Signed-off-by: Yongtao Huang <yongtaoh2022@gmail.com>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>

* fix Version 2.0.0-beta.2: Chat annotations Api Error #25506  (#27206)

Co-authored-by: crazywoola <100913391+crazywoola@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: Asuka Minato <i@asukaminato.eu.org>

* fix jina reader creadential migration command (#27883)

* fix agent putout the output of workflow-tool twice (#26835) (#27087)

* fix jina reader transform (#27922)

* fix: prevent fetch version info in enterprise edition (#27923)

* fix(api): fix `VariablePool.get` adding unexpected keys to variable_dictionary (#26767)

Co-authored-by: -LAN- <laipz8200@outlook.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* refactor: implement tenant self queue for rag tasks (#27559)

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: -LAN- <laipz8200@outlook.com>

* fix: bump brotli to 1.2.0 resloved CVE-2025-6176 (#27950)

Signed-off-by: kenwoodjw <blackxin55+@gmail.com>

---------

Signed-off-by: Yongtao Huang <yongtaoh2022@gmail.com>
Signed-off-by: kenwoodjw <blackxin55+@gmail.com>
Co-authored-by: aka James4u <smart.jamesjin@gmail.com>
Co-authored-by: Novice <novice12185727@gmail.com>
Co-authored-by: yangzheli <43645580+yangzheli@users.noreply.github.com>
Co-authored-by: Elliott <105957288+Elliott-byte@users.noreply.github.com>
Co-authored-by: crazywoola <100913391+crazywoola@users.noreply.github.com>
Co-authored-by: johnny0120 <johnny0120@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: Gritty_dev <101377478+codomposer@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: wangjifeng <163279492+kk-wangjifeng@users.noreply.github.com>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: Boris Polonsky <BorisPolonsky@users.noreply.github.com>
Co-authored-by: Yongtao Huang <yongtaoh2022@gmail.com>
Co-authored-by: Cursx <33718736+Cursx@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Asuka Minato <i@asukaminato.eu.org>
Co-authored-by: Jyong <76649700+JohnJyong@users.noreply.github.com>
Co-authored-by: red_sun <56100962+redSun64@users.noreply.github.com>
Co-authored-by: NFish <douxc512@gmail.com>
Co-authored-by: QuantumGhost <obelisk.reg+git@gmail.com>
Co-authored-by: -LAN- <laipz8200@outlook.com>
Co-authored-by: hj24 <huangjian@dify.ai>
Co-authored-by: kenwoodjw <blackxin55+@gmail.com>
2025-11-07 17:25:58 +08:00

197 lines
6.5 KiB
TypeScript

import { downloadFile, formatFileSize, formatNumber, formatNumberAbbreviated, formatTime } from './format'
describe('formatNumber', () => {
test('should correctly format integers', () => {
expect(formatNumber(1234567)).toBe('1,234,567')
})
test('should correctly format decimals', () => {
expect(formatNumber(1234567.89)).toBe('1,234,567.89')
})
test('should correctly handle string input', () => {
expect(formatNumber('1234567')).toBe('1,234,567')
})
test('should correctly handle zero', () => {
expect(formatNumber(0)).toBe(0)
})
test('should correctly handle negative numbers', () => {
expect(formatNumber(-1234567)).toBe('-1,234,567')
})
test('should correctly handle empty input', () => {
expect(formatNumber('')).toBe('')
})
})
describe('formatFileSize', () => {
test('should return the input if it is falsy', () => {
expect(formatFileSize(0)).toBe(0)
})
test('should format bytes correctly', () => {
expect(formatFileSize(500)).toBe('500.00 bytes')
})
test('should format kilobytes correctly', () => {
expect(formatFileSize(1500)).toBe('1.46 KB')
})
test('should format megabytes correctly', () => {
expect(formatFileSize(1500000)).toBe('1.43 MB')
})
test('should format gigabytes correctly', () => {
expect(formatFileSize(1500000000)).toBe('1.40 GB')
})
test('should format terabytes correctly', () => {
expect(formatFileSize(1500000000000)).toBe('1.36 TB')
})
test('should format petabytes correctly', () => {
expect(formatFileSize(1500000000000000)).toBe('1.33 PB')
})
})
describe('formatTime', () => {
test('should return the input if it is falsy', () => {
expect(formatTime(0)).toBe(0)
})
test('should format seconds correctly', () => {
expect(formatTime(30)).toBe('30.00 sec')
})
test('should format minutes correctly', () => {
expect(formatTime(90)).toBe('1.50 min')
})
test('should format hours correctly', () => {
expect(formatTime(3600)).toBe('1.00 h')
})
test('should handle large numbers', () => {
expect(formatTime(7200)).toBe('2.00 h')
})
})
describe('downloadFile', () => {
test('should create a link and trigger a download correctly', () => {
// Mock data
const blob = new Blob(['test content'], { type: 'text/plain' })
const fileName = 'test-file.txt'
const mockUrl = 'blob:mockUrl'
// Mock URL.createObjectURL
const createObjectURLMock = jest.fn().mockReturnValue(mockUrl)
const revokeObjectURLMock = jest.fn()
Object.defineProperty(window.URL, 'createObjectURL', { value: createObjectURLMock })
Object.defineProperty(window.URL, 'revokeObjectURL', { value: revokeObjectURLMock })
// Mock createElement and appendChild
const mockLink = {
href: '',
download: '',
click: jest.fn(),
remove: jest.fn(),
}
const createElementMock = jest.spyOn(document, 'createElement').mockReturnValue(mockLink as any)
const appendChildMock = jest.spyOn(document.body, 'appendChild').mockImplementation((node: Node) => {
return node
})
// Call the function
downloadFile({ data: blob, fileName })
// Assertions
expect(createObjectURLMock).toHaveBeenCalledWith(blob)
expect(createElementMock).toHaveBeenCalledWith('a')
expect(mockLink.href).toBe(mockUrl)
expect(mockLink.download).toBe(fileName)
expect(appendChildMock).toHaveBeenCalledWith(mockLink)
expect(mockLink.click).toHaveBeenCalled()
expect(mockLink.remove).toHaveBeenCalled()
expect(revokeObjectURLMock).toHaveBeenCalledWith(mockUrl)
// Clean up mocks
jest.restoreAllMocks()
})
})
describe('formatNumberAbbreviated', () => {
it('should return number as string when less than 1000', () => {
expect(formatNumberAbbreviated(0)).toBe('0')
expect(formatNumberAbbreviated(1)).toBe('1')
expect(formatNumberAbbreviated(999)).toBe('999')
})
it('should format thousands with k suffix', () => {
expect(formatNumberAbbreviated(1000)).toBe('1k')
expect(formatNumberAbbreviated(1200)).toBe('1.2k')
expect(formatNumberAbbreviated(1500)).toBe('1.5k')
expect(formatNumberAbbreviated(9999)).toBe('10k')
})
it('should format millions with M suffix', () => {
expect(formatNumberAbbreviated(1000000)).toBe('1M')
expect(formatNumberAbbreviated(1500000)).toBe('1.5M')
expect(formatNumberAbbreviated(2300000)).toBe('2.3M')
expect(formatNumberAbbreviated(999999999)).toBe('1000M')
})
it('should format billions with B suffix', () => {
expect(formatNumberAbbreviated(1000000000)).toBe('1B')
expect(formatNumberAbbreviated(1500000000)).toBe('1.5B')
expect(formatNumberAbbreviated(2300000000)).toBe('2.3B')
})
it('should remove .0 from whole numbers', () => {
expect(formatNumberAbbreviated(1000)).toBe('1k')
expect(formatNumberAbbreviated(2000000)).toBe('2M')
expect(formatNumberAbbreviated(3000000000)).toBe('3B')
})
it('should keep decimal for non-whole numbers', () => {
expect(formatNumberAbbreviated(1100)).toBe('1.1k')
expect(formatNumberAbbreviated(1500000)).toBe('1.5M')
expect(formatNumberAbbreviated(2700000000)).toBe('2.7B')
})
it('should handle edge cases', () => {
expect(formatNumberAbbreviated(950)).toBe('950')
expect(formatNumberAbbreviated(1001)).toBe('1k')
expect(formatNumberAbbreviated(999999)).toBe('1000k')
})
})
describe('formatNumber edge cases', () => {
it('should handle very large numbers', () => {
expect(formatNumber(1234567890123)).toBe('1,234,567,890,123')
})
it('should handle numbers with many decimal places', () => {
expect(formatNumber(1234.56789)).toBe('1,234.56789')
})
it('should handle negative decimals', () => {
expect(formatNumber(-1234.56)).toBe('-1,234.56')
})
it('should handle string with decimals', () => {
expect(formatNumber('9876543.21')).toBe('9,876,543.21')
})
})
describe('formatFileSize edge cases', () => {
it('should handle exactly 1024 bytes', () => {
expect(formatFileSize(1024)).toBe('1.00 KB')
})
it('should handle fractional bytes', () => {
expect(formatFileSize(512.5)).toBe('512.50 bytes')
})
})
describe('formatTime edge cases', () => {
it('should handle exactly 60 seconds', () => {
expect(formatTime(60)).toBe('1.00 min')
})
it('should handle exactly 3600 seconds', () => {
expect(formatTime(3600)).toBe('1.00 h')
})
it('should handle fractional seconds', () => {
expect(formatTime(45.5)).toBe('45.50 sec')
})
it('should handle very large durations', () => {
expect(formatTime(86400)).toBe('24.00 h') // 24 hours
})
})