Skip to content

Commit 4631a48

Browse files
committed
test use_reducer
1 parent 8a445bf commit 4631a48

File tree

1 file changed

+50
-43
lines changed

1 file changed

+50
-43
lines changed

tests/test_core/test_hooks.py

Lines changed: 50 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -390,68 +390,75 @@ def effect():
390390
assert effect_run_count.current == 2
391391

392392

393-
def test_use_reducer():
394-
assert False
393+
async def test_use_reducer():
394+
saved_count = idom.Ref(None)
395+
saved_dispatch = idom.Ref(None)
396+
397+
def reducer(count, action):
398+
if action == "increment":
399+
return count + 1
400+
elif action == "decrement":
401+
return count - 1
402+
else:
403+
raise ValueError(f"Unknown action '{action}'")
395404

405+
@idom.element
406+
async def Counter(initial_count):
407+
saved_count.current, saved_dispatch.current = idom.hooks.use_reducer(
408+
reducer, initial_count
409+
)
410+
return idom.html.div()
396411

397-
def test_use_reducer_dispatch_callback_identity_is_preserved():
398-
assert False
412+
async with idom.Layout(Counter(0)) as layout:
413+
await layout.render()
399414

415+
assert saved_count.current == 0
400416

401-
def test_use_callback_identity():
402-
assert False
417+
saved_dispatch.current("increment")
418+
await layout.render()
403419

420+
assert saved_count.current == 1
404421

405-
def test_use_callback_memoization():
406-
assert False
422+
saved_dispatch.current("decrement")
423+
await layout.render()
407424

425+
assert saved_count.current == 0
408426

409-
def test_use_memo(display, driver, driver_wait):
410-
trigger_count = 0
411427

412-
# use constants to ensure identity comparison works as expected
413-
left_const = "left"
414-
right_const = "right"
428+
async def test_use_reducer_dispatch_callback_identity_is_preserved():
429+
saved_dispatchers = []
430+
431+
def reducer(count, action):
432+
if action == "increment":
433+
return count + 1
434+
else:
435+
raise ValueError(f"Unknown action '{action}'")
415436

416437
@idom.element
417-
async def ComponentWithMemo():
418-
location, set_location = idom.hooks.use_state(left_const)
438+
async def ElementWithUseReduce():
439+
saved_dispatchers.append(idom.hooks.use_reducer(reducer, 0)[1])
440+
return idom.html.div()
419441

420-
@idom.hooks.use_memo(args=location)
421-
def count():
422-
nonlocal trigger_count
423-
trigger_count += 1
424-
return trigger_count
442+
async with idom.Layout(ElementWithUseReduce()) as layout:
443+
for _ in range(3):
444+
await layout.render()
445+
saved_dispatchers[-1]("increment")
425446

426-
async def on_left_button_click(event):
427-
set_location(left_const)
447+
first_dispatch = saved_dispatchers[0]
448+
for d in saved_dispatchers[1:]:
449+
assert first_dispatch is d
428450

429-
async def on_right_button_click(event):
430-
set_location(right_const)
431451

432-
return idom.html.div(
433-
idom.html.button(
434-
{"onClick": on_left_button_click, "id": "left-button"}, "left button"
435-
),
436-
idom.html.button(
437-
{"onClick": on_right_button_click, "id": "right-button"}, "right button"
438-
),
439-
f"Memo trigger count: {count}",
440-
)
452+
def test_use_callback_identity():
453+
assert False
441454

442-
display(ComponentWithMemo) # initial render triggers: yes
443455

444-
left_client_button = driver.find_element_by_id("left-button")
445-
right_client_button = driver.find_element_by_id("right-button")
456+
def test_use_callback_memoization():
457+
assert False
446458

447-
right_client_button.click() # trigger: yes
448-
right_client_button.click() # trigger: no
449-
right_client_button.click() # trigger: no
450-
left_client_button.click() # trigger: yes
451-
left_client_button.click() # trigger: no
452-
right_client_button.click() # trigger: yes
453459

454-
driver_wait.until(lambda drv: trigger_count == 4)
460+
def test_use_memo():
461+
assert False
455462

456463

457464
def test_use_memo_always_runs_if_args_are_none():

0 commit comments

Comments
 (0)