Selkie--Test--Supply
NAME
Selkie::Test::Supply - Observation and assertion helpers for widget Supply emissions
SYNOPSIS
use Test;
use Selkie::Test::Keys;
use Selkie::Test::Supply;
use Selkie::Widget::Button;
my $btn = Selkie::Widget::Button.new(label => 'OK');
$btn.set-focused(True);
# Collect everything a Supply emits during an action
my @emissions = collect-from $btn.on-press, {
press-key($btn, 'enter');
press-key($btn, 'space');
};
is @emissions.elems, 2, 'two presses';
# Or assert directly:
emitted-once-ok $btn.on-press, True, 'Enter presses the button', {
press-key($btn, 'enter');
};
done-testing;DESCRIPTION
Widgets expose user-level events as Supplys ā on-press, on-change, on-activate, etc. Testing them without these helpers is a tap-collect-assert dance in every test. This module provides the pattern as three named functions.
Supply emissions from widgets are synchronous ā the .emit call in a handler fires the tap inline ā so collect-from works without needing event-loop integration. The tap is closed automatically at the end of the block.
EXAMPLES
Count emissions
emitted-count-is $list.on-select, 3, 'three cursor moves', {
press-keys($list, 'down', 'down', 'down');
};Capture and inspect
my @emitted = collect-from $input.on-change, {
type-text($input, 'hi');
};
is @emitted[*-1], 'hi', 'last emission is the final text';Assert nothing emitted
emitted-count-is $btn.on-press, 0, 'unfocused button ignores key', {
press-key($btn, 'enter');
};SEE ALSO
Selkie::Test::Keys ā for synthesising the events that trigger the emissions
Selkie::Test::Store ā for store-dispatch-based tests
sub collect-from
sub collect-from(
Supply:D $supply,
&block
) returns ListTap a Supply, run a block, return every emission in order. The tap is closed when the block finishes. Emissions are collected in the same thread, so this works for widget Supplies that emit synchronously from event handlers.
sub emitted-once-ok
sub emitted-once-ok(
Supply:D $supply,
$expected,
Str:D $desc,
&block
) returns MuAssertion: the supply emitted exactly once, with the given value, during the block. Produces two test lines ā one for the count, one for the value (only if the count was 1).
sub emitted-count-is
sub emitted-count-is(
Supply:D $supply,
Int:D $n,
Str:D $desc,
&block
) returns MuAssertion: the supply emitted exactly $n times during the block. $n may be 0 to assert nothing was emitted.