/
index.test.ts
41 lines (30 loc) · 975 Bytes
/
index.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { ref } from 'vue-demi'
import { useToNumber } from '.'
describe('useToNumber', () => {
it('default', () => {
const value = ref<string | number>('123.345')
const float = useToNumber(value)
const int = useToNumber(value, { method: 'parseInt' })
expect(float.value).toBe(123.345)
expect(int.value).toBe(123)
value.value = 'hi'
expect(float.value).toBe(NaN)
expect(int.value).toBe(NaN)
value.value = 123.4
expect(float.value).toBe(123.4)
expect(int.value).toBe(123.4)
value.value = '-43.53'
expect(float.value).toBe(-43.53)
expect(int.value).toBe(-43)
})
it('radix', () => {
const value = ref<string | number>('0xFA')
const int = useToNumber(value, { method: 'parseInt', radix: 16 })
expect(int.value).toBe(250)
})
it('nanToZero', () => {
const value = ref<string | number>('Hi')
const float = useToNumber(value, { nanToZero: true })
expect(float.value).toBe(0)
})
})