mirror of
https://github.com/koel/koel
synced 2024-11-28 06:50:27 +00:00
feat(test): add formatter tests
This commit is contained in:
parent
eff5626569
commit
f1c2febd38
4 changed files with 63 additions and 35 deletions
|
@ -1,31 +0,0 @@
|
|||
import { secondsToHis, parseValidationError, ServerValidationError } from '@/utils'
|
||||
|
||||
describe('services/utils', () => {
|
||||
describe('#secondsToHis', () => {
|
||||
it('formats a duration to H:i:s', () => expect(secondsToHis(7547)).toBe('02:05:47'))
|
||||
it('omits hours from short duration when formats to H:i:s', () => expect(secondsToHis(314)).toBe('05:14'))
|
||||
})
|
||||
|
||||
describe('#parseValidationError', () => {
|
||||
it('parses validation error', () => {
|
||||
const error: ServerValidationError = {
|
||||
message: 'The given data was invalid',
|
||||
errors: {
|
||||
email: [
|
||||
'The email has already been taken',
|
||||
'The domain is blacklisted'
|
||||
],
|
||||
name: [
|
||||
'The name is required'
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
expect(parseValidationError(error)).toEqual([
|
||||
'The email has already been taken',
|
||||
'The domain is blacklisted',
|
||||
'The name is required'
|
||||
])
|
||||
})
|
||||
})
|
||||
})
|
|
@ -4,7 +4,7 @@ import { eventBus } from './eventBus'
|
|||
|
||||
new class extends UnitTestCase {
|
||||
protected beforeEach () {
|
||||
super.beforeEach(() => eventBus.all = new Map());
|
||||
super.beforeEach(() => (eventBus.all = new Map()))
|
||||
}
|
||||
|
||||
protected test () {
|
||||
|
|
57
resources/assets/js/utils/formatters.spec.ts
Normal file
57
resources/assets/js/utils/formatters.spec.ts
Normal file
|
@ -0,0 +1,57 @@
|
|||
import UnitTestCase from '@/__tests__/UnitTestCase'
|
||||
import { expect, it } from 'vitest'
|
||||
import { br2nl, parseValidationError, pluralize, secondsToHis, ServerValidationError, slugToTitle } from './formatters'
|
||||
|
||||
new class extends UnitTestCase {
|
||||
protected test () {
|
||||
it.each([
|
||||
[0, '00:00'],
|
||||
[59, '00:59'],
|
||||
[60, '01:00'],
|
||||
[125, '02:05'],
|
||||
[7547, '02:05:47'],
|
||||
[137241, '38:07:21']
|
||||
])('formats %d seconds to H:i:s', (seconds, formatted) => expect(secondsToHis(seconds)).toBe(formatted))
|
||||
|
||||
it('parses validation error', () => {
|
||||
const error: ServerValidationError = {
|
||||
message: 'The given data was invalid',
|
||||
errors: {
|
||||
email: [
|
||||
'The email has already been taken',
|
||||
'The domain is blacklisted'
|
||||
],
|
||||
name: [
|
||||
'The name is required'
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
expect(parseValidationError(error)).toEqual([
|
||||
'The email has already been taken',
|
||||
'The domain is blacklisted',
|
||||
'The name is required'
|
||||
])
|
||||
})
|
||||
|
||||
it.each([
|
||||
['foo<br>bar', "foo\nbar"],
|
||||
['foo<br/>bar', "foo\nbar"],
|
||||
['foo<br />bar', "foo\nbar"],
|
||||
['foo<br>bar<br/>baz', "foo\nbar\nbaz"]
|
||||
])('converts <br> tags in %s to line breaks', (input, output) => expect(br2nl(input)).toEqual(output))
|
||||
|
||||
it.each([
|
||||
['foo', 'Foo'],
|
||||
['foo-bar', 'Foo Bar'],
|
||||
['foo-bar--baz', 'Foo Bar Baz'],
|
||||
['foo-bar--baz---', 'Foo Bar Baz']
|
||||
])('converts %s to the title counterpart', (slug, title) => expect(slugToTitle(slug)).toEqual(title))
|
||||
|
||||
it.each([
|
||||
[1, 'cat', 'cat'],
|
||||
[2, 'cat', 'cats'],
|
||||
[0, 'cat', 'cats']
|
||||
])('pluralizes %d %s', (count, noun, plural) => expect(pluralize(count, noun)).toEqual(`${count} ${plural}`))
|
||||
}
|
||||
}
|
|
@ -37,10 +37,12 @@ export const parseValidationError = (error: ServerValidationError) => {
|
|||
/**
|
||||
* Turn <br> into new line characters.
|
||||
*/
|
||||
export const br2nl = (str: string) => str ? str.replace(/<br\s*[/]?>/gi, '\n') : ''
|
||||
export const br2nl = (str: string) => str ? str.replace(/<br\s*\/?>/gi, '\n') : ''
|
||||
|
||||
export const slugToTitle = (slug: string, separator = '-') =>
|
||||
slug.split(separator).map(w => w.charAt(0).toUpperCase() + w.substring(1).toLowerCase()).join(' ')
|
||||
export const slugToTitle = (slug: string, separator = '-') => {
|
||||
let title = slug.split(separator).map(w => w.charAt(0).toUpperCase() + w.substring(1).toLowerCase()).join(' ')
|
||||
return title.replace(/\s+/g, ' ').trim()
|
||||
}
|
||||
|
||||
export const pluralize = (count: number, singular: string) =>
|
||||
count === 1 ? `${count} ${singular}` : `${count.toLocaleString()} ${singular}s`
|
||||
|
|
Loading…
Reference in a new issue