-
Notifications
You must be signed in to change notification settings - Fork 0
/
observe.test.ts
48 lines (33 loc) · 1.13 KB
/
observe.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
import {observe, unobserve} from './observe.js'
// TODO move type def to @lume/cli, map @types/jest's `expect` type into the
// global env.
declare global {
function expect(...args: any[]): any
}
describe('observe and unobserve', () => {
it('observes and unobserves objects synchronously', async () => {
const person = {
name: 'Roy',
age: '20',
}
const changes: PropertyKey[] = []
const onPropChange = (prop: PropertyKey, value: string) => {
changes.push(prop, value)
}
observe(person, ['name', 'age'], onPropChange)
person.name = 'Erika'
person.age = '30'
expect(changes).toEqual(['name', 'Erika', 'age', '30'])
person.name = 'Lars'
person.age = '40'
expect(changes).toEqual(['name', 'Erika', 'age', '30', 'name', 'Lars', 'age', '40'])
unobserve(person, ['name'], onPropChange)
person.name = 'Hansen'
person.age = '33'
expect(changes).toEqual(['name', 'Erika', 'age', '30', 'name', 'Lars', 'age', '40', 'age', '33'])
unobserve(person, onPropChange)
person.name = 'Amadar'
person.age = '28'
expect(changes).toEqual(['name', 'Erika', 'age', '30', 'name', 'Lars', 'age', '40', 'age', '33'])
})
})