Skip to content

Commit

Permalink
test: css time to ms
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrieljablonski committed Feb 18, 2024
1 parent 76dcf30 commit 008090f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
21 changes: 21 additions & 0 deletions src/test/utils.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import debounce from 'utils/debounce'
import { computeTooltipPosition } from 'utils/compute-positions'
import { cssTimeToMs } from 'utils/css-time-to-ms'

// Tell Jest to mock all timeout functions
jest.useRealTimers()
Expand Down Expand Up @@ -106,4 +107,24 @@ describe('debounce', () => {
expect(func).not.toHaveBeenCalled()
})
})

describe('css time to ms', () => {
test('converts time correctly', () => {
expect(cssTimeToMs('1s')).toBe(1000)
expect(cssTimeToMs('1ms')).toBe(1)
expect(cssTimeToMs('1.5s')).toBe(1500)
expect(cssTimeToMs('1.5ms')).toBe(1.5)
})

test('returns 0 if no time is provided', () => {
expect(cssTimeToMs('')).toBe(0)
})

test('returns 0 if unsupported unit', () => {
expect(cssTimeToMs('1h')).toBe(0)
})

test('returns 0 if no unit', () => {
expect(cssTimeToMs('1000')).toBe(0)
})
})
5 changes: 1 addition & 4 deletions src/utils/css-time-to-ms.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
export const cssTimeToMs = (time: string): number => {
const match = time.match(/^([\d.]+)(m?s?)$/)
const match = time.match(/^([\d.]+)(ms|s)$/)
if (!match) {
return 0
}
const [, amount, unit] = match
if (unit !== 's' && unit !== 'ms') {
return 0
}
return Number(amount) * (unit === 'ms' ? 1 : 1000)
}

0 comments on commit 008090f

Please sign in to comment.