File tree Expand file tree Collapse file tree 2 files changed +22
-4
lines changed Expand file tree Collapse file tree 2 files changed +22
-4
lines changed Original file line number Diff line number Diff line change 1
1
import debounce from 'utils/debounce'
2
2
import { computeTooltipPosition } from 'utils/compute-positions'
3
+ import { cssTimeToMs } from 'utils/css-time-to-ms'
3
4
4
5
// Tell Jest to mock all timeout functions
5
6
jest . useRealTimers ( )
@@ -106,4 +107,24 @@ describe('debounce', () => {
106
107
expect ( func ) . not . toHaveBeenCalled ( )
107
108
} )
108
109
} )
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
+ } )
109
130
} )
Original file line number Diff line number Diff line change 1
1
export const cssTimeToMs = ( time : string ) : number => {
2
- const match = time . match ( / ^ ( [ \d . ] + ) ( m ? s ? ) $ / )
2
+ const match = time . match ( / ^ ( [ \d . ] + ) ( m s | s ) $ / )
3
3
if ( ! match ) {
4
4
return 0
5
5
}
6
6
const [ , amount , unit ] = match
7
- if ( unit !== 's' && unit !== 'ms' ) {
8
- return 0
9
- }
10
7
return Number ( amount ) * ( unit === 'ms' ? 1 : 1000 )
11
8
}
You can’t perform that action at this time.
0 commit comments