-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathexamples.test.ts
64 lines (51 loc) · 1.49 KB
/
examples.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
import { ref } from 'vue-demi'
import { describe, it } from 'vitest'
import { dec, is, multiply, pow, rs, set, sqrt, stringify, sum, ternary, toUpperCase } from '../src'
import { $expect } from './utils'
describe('examples', () => {
it('math', () => {
const a = ref(3)
const b = ref(4)
const c = sqrt(sum(pow(a, 2), pow(b, 2)))
$expect(is(c, 5))
set(a, 5) // shorthand for a.value = 5
set(b, 12)
$expect(is(c, 13))
})
it('json', () => {
const obj = ref<any>({ foo: 'bar' })
const str = stringify(obj)
// const clone = parse<any>(str)
$expect(is(str, '{"foo":"bar"}'))
obj.value.no = 42
$expect(is(str, '{"foo":"bar","no":42}'))
})
it('string 1', () => {
const name = ref('foo')
const message = rs`Hello ${toUpperCase(name)}!`
$expect(is(message, 'Hello FOO!'))
set(name, 'Anthony')
$expect(is(message, 'Hello ANTHONY!'))
})
it('string 2', () => {
const x = ref(9)
const y = ref(9)
const z = ref(7)
const equation = rs`${x} x ${y} + ${z} = ${sum(multiply(x, y), z)}`
$expect(is(equation, '9 x 9 + 7 = 88'))
set(x, 98)
dec(z)
$expect(is(equation, '98 x 9 + 6 = 888'))
set(x, 987)
dec(z)
$expect(is(equation, '987 x 9 + 5 = 8888'))
})
it('string 3', () => {
const mode = ref('light')
const isDark = is(mode, 'dark')
const icon = rs`mdi-${ternary(isDark, 'moon', 'sun')}`
$expect(is(icon, 'mdi-sun'))
set(mode, 'dark')
$expect(is(icon, 'mdi-moon'))
})
})