feat(test): add formatter tests

This commit is contained in:
Phan An 2022-05-14 17:37:25 +02:00
parent eff5626569
commit f1c2febd38
No known key found for this signature in database
GPG key ID: A81E4477F0BB6FDC
4 changed files with 63 additions and 35 deletions

View file

@ -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'
])
})
})
})

View file

@ -4,7 +4,7 @@ import { eventBus } from './eventBus'
new class extends UnitTestCase { new class extends UnitTestCase {
protected beforeEach () { protected beforeEach () {
super.beforeEach(() => eventBus.all = new Map()); super.beforeEach(() => (eventBus.all = new Map()))
} }
protected test () { protected test () {

View 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}`))
}
}

View file

@ -37,10 +37,12 @@ export const parseValidationError = (error: ServerValidationError) => {
/** /**
* Turn <br> into new line characters. * 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 = '-') => export const slugToTitle = (slug: string, separator = '-') => {
slug.split(separator).map(w => w.charAt(0).toUpperCase() + w.substring(1).toLowerCase()).join(' ') 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) => export const pluralize = (count: number, singular: string) =>
count === 1 ? `${count} ${singular}` : `${count.toLocaleString()} ${singular}s` count === 1 ? `${count} ${singular}` : `${count.toLocaleString()} ${singular}s`