2022-02-23 22:53:39 +01:00
|
|
|
/*
|
2021-06-18 13:05:06 +02:00
|
|
|
* @jest-environment jsdom
|
|
|
|
*/
|
|
|
|
import Atom, {val} from './Atom'
|
2022-12-01 14:47:20 +01:00
|
|
|
import prism from './prisms/prism/prism'
|
2021-06-18 13:05:06 +02:00
|
|
|
import Ticker from './Ticker'
|
|
|
|
|
|
|
|
describe(`v2 atom`, () => {
|
|
|
|
describe(`identity pointers`, () => {
|
|
|
|
it(`should work`, () => {
|
|
|
|
const data = {foo: 'hi', bar: 0}
|
|
|
|
const a = new Atom(data)
|
|
|
|
const dataP = a.pointer
|
|
|
|
const bar = dataP.bar
|
|
|
|
expect(val(bar)).toEqual(0)
|
|
|
|
|
|
|
|
const d = prism(() => {
|
|
|
|
return val(bar)
|
|
|
|
})
|
|
|
|
expect(d.getValue()).toEqual(0)
|
|
|
|
const ticker = new Ticker()
|
|
|
|
const changes: number[] = []
|
2022-12-01 13:40:02 +01:00
|
|
|
d.onChange(ticker, (c) => {
|
2021-06-18 13:05:06 +02:00
|
|
|
changes.push(c)
|
|
|
|
})
|
|
|
|
a.setState({...data, bar: 1})
|
|
|
|
ticker.tick()
|
|
|
|
expect(changes).toHaveLength(1)
|
|
|
|
expect(changes[0]).toEqual(1)
|
|
|
|
a.setState({...data, bar: 1})
|
|
|
|
ticker.tick()
|
|
|
|
expect(changes).toHaveLength(1)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|