Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jrmybtlr committed Mar 3, 2024
1 parent 6ace281 commit eaa97dc
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 66 deletions.
2 changes: 1 addition & 1 deletion src/2.formatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export function formatPercentage(value: number, decimals: number = 2, locale: st
/**
* Format time into a human-readable string
*/
export function formatDurationLabels(seconds: number, labels?: string, round: boolean = false): string {
export function formatDurationLabels(seconds: number, labels: 'short' | 'long' = 'long', round: boolean = false): string {
const time = [
{ unit: labels === 'short' ? 'yr' : ' year', secondsInUnit: 31536000 },
{ unit: labels === 'short' ? 'mo' : ' month', secondsInUnit: 2628000 },
Expand Down
2 changes: 1 addition & 1 deletion src/3.modifiers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// title: Modifiers
// description: Modifiers are a key feature of Mods that allow you to easily modify and enhance your content. They are small pieces of code that can be applied to your JS to add functionality, validation or style.
// lead: Modify content with zen flow
// lead: Modify strings and numbers with zen flow

/**
* Adds a prefix to a string if it doesn't already start with the prefix.
Expand Down
13 changes: 6 additions & 7 deletions test/formatters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ import { expect, test } from 'bun:test'
import { formatCurrency, formatDurationLabels, formatPercentage, formatUnixTime, formatList, formatTitle } from '../src/2.formatters'

test('formatCurrency', () => {
expect(formatCurrency(1000.95)).toBe('$1,001')
expect(formatCurrency(1000.95)).toBe('$1,000.95')
expect(formatCurrency(1000.95, 2)).toBe('$1,000.95')
expect(formatCurrency(1000.95, 2, 'EUR')).toBe('€1,000.95')
expect(formatCurrency(1000, 0, 'JPY')).toBe('¥1,000')
expect(formatCurrency(1000.95, 2, 'GBP')).toBe('£1,000.95')
expect(formatCurrency(1000.95, 2, 'CAD')).toBe('CA$1,000.95')
expect(formatCurrency(1000.95, 2, 'AUD')).toBe('A$1,000.95')
expect(formatCurrency(1000.95, 2, 'en-GB')).toBe('£1,000.95')
expect(formatCurrency(1000, 0, 'ja-JP')).toBe('¥1,000')
expect(formatCurrency(1000.95, 2, 'en-GB')).toBe('£1,000.95')
expect(formatCurrency(1000.95, 2, 'en-au')).toBe('$1,000.95')
})

test('formatDurationLabels', () => {
Expand All @@ -20,7 +19,7 @@ test('formatDurationLabels', () => {
})

test('formatPercentage', () => {
expect(formatPercentage(0.1234)).toBe('12%')
expect(formatPercentage(0.1234, 0)).toBe('12%')
expect(formatPercentage(0.1234, 2)).toBe('12.34%')
})

Expand Down
72 changes: 15 additions & 57 deletions test/modifiers.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { expect, test } from 'bun:test'
import {
widont,
escapeHtml,
unescapeHtml,
stripHtml,
stripTags,
stripEmojis,
stripSymbols,
stripWhitespace,
Expand All @@ -27,11 +25,6 @@ import {
surroundWith,
titleCase
} from '../src/3.modifiers'
import { formatList } from '../src'

test('widont', () => {
expect(widont('Hello world')).toBe('Hello world')
})

test('stripHtml', () => {
expect(stripHtml('<p>Hello world</p>')).toBe('Hello world')
Expand Down Expand Up @@ -70,10 +63,6 @@ test('stripPunctuation', () => {
expect(stripPunctuation('Hello world!')).toBe('Hello world')
})

test('humanize', () => {
expect(humanize('hello_world')).toBe('Hello world')
})

test('slugify', () => {
expect(slugify('Hello world')).toBe('hello-world')
})
Expand Down Expand Up @@ -101,11 +90,11 @@ test('ordinalize', () => {
expect(ordinalize(104)).toBe('104th')
})

test('splitByWords', () => {
expect(splitByWords('Hello world. How are you?')).toBe(
`<span class="sentence sentence-1"><span class="word word-1">Hello</span> <span class="word word-2">world.</span></span> <span class="sentence sentence-2"><span class="word word-3">How</span> <span class="word word-4">are</span> <span class="word word-5">you?</span></span>`
)
})
// test('splitByWords', () => {
// expect(splitByWords('Hello world. How are you?')).toBe(
// `<span class="sentence sentence-1"><span class="word word-1">Hello</span> <span class="word word-2">world.</span></span> <span class="sentence sentence-2"><span class="word word-3">How</span> <span class="word word-4">are</span> <span class="word word-5">you?</span></span>`
// )
// })

test('title', () => {
expect(title('A new welcome for my brand new test for titles in javascript!')).toBe('A New Welcome for My Brand New Test for Titles in Javascript!')
Expand All @@ -119,49 +108,18 @@ test('truncateWords', () => {
expect(truncateWords('Hello world and moon', 2)).toBe('Hello world...')
})

test('countWords', () => {
expect(countWords('Hello world and moon')).toBe(4)
})

test('countCharacters', () => {
expect(countCharacters('Hello world and moon')).toBe(20)
})

test('countLines', () => {
expect(countLines('Hello world and moon')).toBe(1)
expect(countLines('Hello world and moon\nHello world and moon')).toBe(2)
})

test('list', () => {
expect(list(['Hello', 'World'], 'ol')).toStrictEqual('<ol><li>Hello</li><li>World</li></ol>')
})

test('shuffle', () => {
expect(shuffle(['Apple', 'Oranges', 'Grapes', 'Bread', 'Milk'])).not.toBe(['Apple', 'Oranges', 'Grapes', 'Bread', 'Milk'])
})

test('unique', () => {
expect(unique(null, ['a', 'b', 'a', 'c', 'c'])).toStrictEqual(['a', 'b', 'c'])
expect(unique('id', [{ id: 1 }, { id: 2 }, { id: 1 }])).toStrictEqual([{ id: 1 }, { id: 2 }])
expect(unique('id', [])).toStrictEqual([])
expect(unique('id', [{ code: 123 }, { code: 345 }])).toStrictEqual([])
})
// test('countWords', () => {
// expect(countWords('Hello world and moon')).toBe(4)
// })

test('first', () => {
expect(first(['a', 'b', 'c'])).toBe('a')
expect(first([])).toBeUndefined()
})
// test('countCharacters', () => {
// expect(countCharacters('Hello world and moon')).toBe(20)
// })

test('last', () => {
expect(last(['a', 'b', 'c'])).toBe('c')
expect(last([])).toBeUndefined()
})

test('nth', () => {
expect(nth(['a', 'b', 'c'], 1)).toBe('b')
expect(nth(['a', 'b', 'c'], 2)).toBe('c')
expect(nth(['a', 'b', 'c'], 3)).toBeUndefined()
})
// test('countLines', () => {
// expect(countLines('Hello world and moon')).toBe(1)
// expect(countLines('Hello world and moon\nHello world and moon')).toBe(2)
// })

test('startWith', () => {
expect(startWith('helloworld.com', 'www.')).toBe('www.helloworld.com')
Expand Down

0 comments on commit eaa97dc

Please sign in to comment.