Skip to content

Commit

Permalink
Added optimization test
Browse files Browse the repository at this point in the history
  • Loading branch information
mweststrate committed Dec 16, 2018
1 parent 186912c commit f3eec99
Showing 1 changed file with 53 additions and 1 deletion.
54 changes: 53 additions & 1 deletion tests/react.spec.tsx
Original file line number Diff line number Diff line change
@@ -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 = () => {
Expand All @@ -19,3 +25,49 @@ test('useVal - 1 ', async () => {
counter(counter() + 1)
await waitForElement(() => re.container.innerHTML === '<h1>2</h1>')
})

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 <h1>{c}</h1>
}

const re = render(<Comp />)
expect(re.container.innerHTML).toEqual('<h1>0</h1>')
expect(called).toBe(1)

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

counter(counter() + 1)
await waitForElement(() => re.container.innerHTML === '<h1>4</h1>')
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 <h1>{c}</h1>
}

const re = render(<Comp />)
expect(re.container.innerHTML).toEqual('<h1>0</h1>')

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

0 comments on commit f3eec99

Please sign in to comment.