forked from vueuse/vueuse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.ts
99 lines (71 loc) · 1.89 KB
/
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { ref } from 'vue-demi'
import { syncRef } from '.'
describe('syncRef', () => {
it('should work', () => {
const a = ref('foo')
const b = ref('bar')
const stop = syncRef(a, b)
expect(b.value).toBe('foo')
a.value = 'bar'
expect(a.value).toBe('bar')
expect(b.value).toBe('bar')
b.value = 'foo'
expect(a.value).toBe('foo')
expect(b.value).toBe('foo')
stop()
a.value = 'bar2'
expect(a.value).toBe('bar2')
expect(b.value).toBe('foo')
})
it('trl', () => {
const left = ref('left')
const right = ref('right')
syncRef(left, right, { direction: 'rtl' })
expect(left.value).toBe('right')
expect(right.value).toBe('right')
left.value = 'bar'
expect(left.value).toBe('bar')
expect(right.value).toBe('right')
right.value = 'foobar'
expect(left.value).toBe('foobar')
expect(right.value).toBe('foobar')
})
it('works with mutual convertors', () => {
const left = ref(10)
const right = ref(1)
syncRef(left, right, {
transform: {
ltr: left => left * 2,
rtl: right => Math.round(right / 2),
},
})
// check immediately sync
expect(right.value).toBe(20)
expect(left.value).toBe(10)
left.value = 30
expect(right.value).toBe(60)
expect(left.value).toBe(30)
right.value = 10
expect(right.value).toBe(10)
expect(left.value).toBe(5)
})
it('works with only rtl convertor', () => {
const left = ref(10)
const right = ref(2)
syncRef(left, right, {
direction: 'rtl',
transform: {
rtl: right => Math.round(right / 2),
},
})
// check immediately sync
expect(right.value).toBe(2)
expect(left.value).toBe(1)
left.value = 10
expect(right.value).toBe(2)
expect(left.value).toBe(10)
right.value = 10
expect(right.value).toBe(10)
expect(left.value).toBe(5)
})
})