-
Notifications
You must be signed in to change notification settings - Fork 0
/
butterfly.test.tsx
50 lines (45 loc) · 1.35 KB
/
butterfly.test.tsx
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
import { ok } from 'node:assert/strict'
import { describe, it } from 'node:test'
import { Observable, map, scan } from 'rxjs'
import { StateSetter, butterfly } from './butterfly.js'
import { Component } from './component.js'
import { jsx } from './jsx.js'
describe('butterfly', () => {
it('is useful in functional components', () => {
const example: Component = (_props, { bindEffect, events }) => {
const click = events.click
const [toggle, setToggle] = butterfly(false)
bindEffect(click.pipe(scan((acc) => !acc, false)), (toggled: boolean) =>
setToggle(toggled),
)
const highlightClass = toggle.pipe(
map((toggled) => (toggled ? 'highlight' : '')),
)
return (
<button events={{ click }} bind={{ className: highlightClass }}>
Toggle Me
</button>
)
}
ok(example)
})
it('is useful in vm classes', () => {
class Example {
readonly #progress: Observable<number>
get progress() {
return this.#progress
}
readonly #setProgress: (progress: StateSetter<number>) => void
constructor() {
;[this.#progress, this.#setProgress] = butterfly(0)
}
shake() {
this.#setProgress(Math.random())
}
increment() {
this.#setProgress((current) => current + 0.01)
}
}
ok(Example)
})
})