mirror of
https://github.com/navidrome/navidrome.git
synced 2025-04-15 19:50:37 +03:00
Add tests for formatters
This commit is contained in:
parent
9d2426a601
commit
11012302fd
5
.github/workflows/pipeline.yml
vendored
5
.github/workflows/pipeline.yml
vendored
@ -82,6 +82,11 @@ jobs:
|
||||
cd ui
|
||||
npm run check-formatting
|
||||
|
||||
- name: npm test
|
||||
run: |
|
||||
cd ui
|
||||
npm test
|
||||
|
||||
- name: npm build
|
||||
run: |
|
||||
cd ui
|
||||
|
@ -1,6 +1,19 @@
|
||||
import React from 'react'
|
||||
import PropTypes from 'prop-types'
|
||||
import { formatRange } from '../utils'
|
||||
|
||||
export const formatRange = (record, source) => {
|
||||
const nameCapitalized = source.charAt(0).toUpperCase() + source.slice(1)
|
||||
const min = record[`min${nameCapitalized}`]
|
||||
const max = record[`max${nameCapitalized}`]
|
||||
let range = []
|
||||
if (min) {
|
||||
range.push(min)
|
||||
}
|
||||
if (max && max !== min) {
|
||||
range.push(max)
|
||||
}
|
||||
return range.join('-')
|
||||
}
|
||||
|
||||
export const RangeField = ({ className, record = {}, source }) => {
|
||||
return <span className={className}>{formatRange(record, source)}</span>
|
||||
|
@ -10,20 +10,6 @@ export const formatBytes = (bytes, decimals = 2) => {
|
||||
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i]
|
||||
}
|
||||
|
||||
export const formatRange = (record, source) => {
|
||||
const nameCapitalized = source.charAt(0).toUpperCase() + source.slice(1)
|
||||
const min = record[`min${nameCapitalized}`]
|
||||
const max = record[`max${nameCapitalized}`]
|
||||
let range = []
|
||||
if (min) {
|
||||
range.push(min)
|
||||
}
|
||||
if (max && max !== min) {
|
||||
range.push(max)
|
||||
}
|
||||
return range.join('-')
|
||||
}
|
||||
|
||||
export const formatDuration = (d) => {
|
||||
const hours = Math.floor(d / 3600)
|
||||
const minutes = Math.floor(d / 60) % 60
|
||||
|
27
ui/src/utils/formatters.test.js
Normal file
27
ui/src/utils/formatters.test.js
Normal file
@ -0,0 +1,27 @@
|
||||
import React from 'react'
|
||||
import ReactDOM from 'react-dom'
|
||||
import { formatBytes, formatDuration } from './formatters'
|
||||
|
||||
describe('formatBytes', () => {
|
||||
it('format bytes', () => {
|
||||
expect(formatBytes(0)).toEqual('0 Bytes')
|
||||
expect(formatBytes(1000)).toEqual('1000 Bytes')
|
||||
expect(formatBytes(1024)).toEqual('1 KB')
|
||||
expect(formatBytes(1024 * 1024)).toEqual('1 MB')
|
||||
expect(formatBytes(1024 * 1024 * 1024)).toEqual('1 GB')
|
||||
expect(formatBytes(1024 * 1024 * 1024 * 1024)).toEqual('1 TB')
|
||||
})
|
||||
})
|
||||
|
||||
const day = 86400
|
||||
const hour = 3600
|
||||
const minute = 60
|
||||
|
||||
describe('formatDuration', () => {
|
||||
it('format seconds', () => {
|
||||
expect(formatDuration(0)).toEqual('00:00')
|
||||
expect(formatDuration(59)).toEqual('00:59')
|
||||
expect(formatDuration(hour + minute + 1)).toEqual('01:01:01')
|
||||
expect(formatDuration(2 * day + 3 * hour + 7 * minute)).toEqual('51:07:00')
|
||||
})
|
||||
})
|
Loading…
x
Reference in New Issue
Block a user