diff --git a/tests/react.spec.tsx b/tests/react.spec.tsx index e9d3ed6..1caa76d 100644 --- a/tests/react.spec.tsx +++ b/tests/react.spec.tsx @@ -1,8 +1,14 @@ -import { val } from 'rval' +import { val, drv } from 'rval' import { useVal } from 'rval/react' import * as React from 'react' import { render, waitForElement } from 'react-testing-library' +function delay(time) { + return new Promise(resolve => { + setTimeout(resolve, time) + }) +} + test('useVal - 1 ', async () => { const counter = val(0) const Comp = () => { @@ -19,3 +25,49 @@ test('useVal - 1 ', async () => { counter(counter() + 1) await waitForElement(() => re.container.innerHTML === '

2

') }) + +test('useVal - mimimum computations - 1', async () => { + const counter = val(0) + let called = 0 + const doubler = drv(() => { + called++ + return counter() * 2 + }) + + const Comp = () => { + const c = useVal(doubler) + return

{c}

+ } + + const re = render() + expect(re.container.innerHTML).toEqual('

0

') + expect(called).toBe(1) + + counter(counter() + 1) + await waitForElement(() => re.container.innerHTML === '

2

') + expect(called).toBe(2) + + counter(counter() + 1) + await waitForElement(() => re.container.innerHTML === '

4

') + expect(called).toBe(3) +}) + +test('useVal - mimimum computations - 1', async () => { + const counter = val(0) + let called = 0 + const doubler = drv(() => { + called++ + return counter() * 2 + }) + + const Comp = () => { + const c = useVal(doubler) + return

{c}

+ } + + const re = render() + expect(re.container.innerHTML).toEqual('

0

') + + await delay(100) + expect(called).toBe(1) // and not 2! +})