Skip to content

Commit 008090f

Browse files
test: css time to ms
1 parent 76dcf30 commit 008090f

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

src/test/utils.spec.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import debounce from 'utils/debounce'
22
import { computeTooltipPosition } from 'utils/compute-positions'
3+
import { cssTimeToMs } from 'utils/css-time-to-ms'
34

45
// Tell Jest to mock all timeout functions
56
jest.useRealTimers()
@@ -106,4 +107,24 @@ describe('debounce', () => {
106107
expect(func).not.toHaveBeenCalled()
107108
})
108109
})
110+
111+
describe('css time to ms', () => {
112+
test('converts time correctly', () => {
113+
expect(cssTimeToMs('1s')).toBe(1000)
114+
expect(cssTimeToMs('1ms')).toBe(1)
115+
expect(cssTimeToMs('1.5s')).toBe(1500)
116+
expect(cssTimeToMs('1.5ms')).toBe(1.5)
117+
})
118+
119+
test('returns 0 if no time is provided', () => {
120+
expect(cssTimeToMs('')).toBe(0)
121+
})
122+
123+
test('returns 0 if unsupported unit', () => {
124+
expect(cssTimeToMs('1h')).toBe(0)
125+
})
126+
127+
test('returns 0 if no unit', () => {
128+
expect(cssTimeToMs('1000')).toBe(0)
129+
})
109130
})

src/utils/css-time-to-ms.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
export const cssTimeToMs = (time: string): number => {
2-
const match = time.match(/^([\d.]+)(m?s?)$/)
2+
const match = time.match(/^([\d.]+)(ms|s)$/)
33
if (!match) {
44
return 0
55
}
66
const [, amount, unit] = match
7-
if (unit !== 's' && unit !== 'ms') {
8-
return 0
9-
}
107
return Number(amount) * (unit === 'ms' ? 1 : 1000)
118
}

0 commit comments

Comments
 (0)