Testing
.
A Silk UI program is a model, messages, and functions between them. A test needs no window, no browser and no mocks of the screen. It sends messages and looks at the model.
Note:
Tests are where the rules pay off: one model, messages as the only input, and the world behind services. Step 5 of the tutorial tests a whole app this way.
All the tests on this page are the real tests of the examples in these docs. They run on every change to the repository.
Shell
Copy
silk test --manifest-path docs/tests/silk.toml --root src/tests.silk
Messages in, model out
tests/src/core.silk
Copy
effect
fn
counterBody
() -> () !
AssertionError
<'
static
> |
TreeError
|
OutOfMemoryError
? &
mut
Allocator
{
let
mut
page =
Html
.
make
()
let
mut
app =
run
program.make<
Counter
,
CounterMessage
,
NoCommand
>()
run
app.
start
(&
mut
page,
0.0
)
run
drain
(&
mut
app, &
mut
page)
run
Testing
.
expect
(app.model.value.count ==
0
,
"starts at zero"
) app.
send
(
CounterMessage
.
ClickedIncrement
) app.
send
(
CounterMessage
.
ClickedIncrement
) app.
send
(
CounterMessage
.
ClickedDecrement
)
run
drain
(&
mut
app, &
mut
page)
run
Testing
.
expect
(app.model.value.count ==
1
,
"two up and one down is one"
) app.
send
(
CounterMessage
.
ClickedReset
)
run
drain
(&
mut
app, &
mut
page)
run
Testing
.
expect
(app.model.value.count ==
0
,
"reset goes back to zero"
) }
tests/src/core.silk
Copy
test
effect
fn
theCounterCounts
() -> () !
AssertionError
<'
static
> |
TreeError
|
OutOfMemoryError
{
let
mut
allocator =
Allocator
.
systemAllocatorProvider
()
run
counterBody
() |>
Effect
.provideMut<
Allocator
>(&
mut
allocator) }
Step by step:
1.
program.make
makes the first model and a runtime for it.
2.
app.start
renders the first view.
Html.make()
is a page that exists only as text in memory, which is all a test needs.
3.
app.send(message)
queues a message, exactly as a click would.
4.
drain
runs the program until it has nothing left to do: every queued message, the commands they return, and the messages those end in.
5.
app.model.value
is the model.
The test function itself, the one marked
test
, only provides what the body needs. Here that is memory.
Commands
A program with commands asks for services. A test provides ones it controls, as Commands showed for the dice:
tests/src/core.silk
Copy
test
effect
fn
aRollAsksTheDie
() -> () !
AssertionError
<'
static
> |
TreeError
|
OutOfMemoryError
{
let
mut
allocator =
Allocator
.
systemAllocatorProvider
()
let
mut
die =
Loaded
{ byte:
9
, asked:
0
}
run
diceBody
() |>
Effect
.provideMut<
Random
>(&
mut
die) |>
Effect
.provideMut<
Allocator
>(&
mut
allocator)
run
Testing
.
expect
(die.asked ==
51
,
"one command ran for each roll"
) }
Because
drain
also runs the commands, the test above checks the whole round trip: the click, the command, the service, the message that came back, and the model after it.
Time
A test does not wait for timers. It moves the clock.
tests/src/core.silk
Copy
effect
fn
stopwatchBody
() -> () !
AssertionError
<'
static
> |
TreeError
|
OutOfMemoryError
? &
mut
Allocator
{
let
mut
page =
Html
.
make
()
let
mut
app =
run
program.make<
Stopwatch
,
StopwatchMessage
,
NoCommand
>()
run
app.
start
(&
mut
page,
0.0
)
run
drain
(&
mut
app, &
mut
page)
run
Testing
.
expect
(app.
nextDeadline
().
isNone
(),
"no timer while the watch is stopped"
) app.
send
(
StopwatchMessage
.
ClickedStartStop
)
run
drain
(&
mut
app, &
mut
page)
run
tick
(&
mut
app, &
mut
page,
0.15
,
false
)
run
tick
(&
mut
app, &
mut
page,
0.25
,
false
)
run
tick
(&
mut
app, &
mut
page,
0.35
,
false
)
run
Testing
.
expect
(app.model.value.tenths ==
3
,
"one tick for each tenth of a second"
) app.
send
(
StopwatchMessage
.
ClickedLap
)
run
tick
(&
mut
app, &
mut
page,
0.45
,
false
)
run
tick
(&
mut
app, &
mut
page,
0.55
,
false
) app.
send
(
StopwatchMessage
.
ClickedLap
)
run
drain
(&
mut
app, &
mut
page)
run
Testing
.
expect
(app.model.value.laps.
length
() ==
2
,
"two laps"
)
run
Testing
.
expect
(app.model.value.laps.
asSlice
()[
1
].took ==
2
,
"a lap is the time since the lap before"
) app.
send
(
StopwatchMessage
.
ClickedStartStop
)
run
drain
(&
mut
app, &
mut
page)
run
tick
(&
mut
app, &
mut
page,
5.0
,
false
)
run
Testing
.
expect
(app.model.value.tenths ==
5
,
"a stopped watch does not tick"
) }
tick(&mut app, &mut page, 0.15, false)
says "it is now 0.15 seconds". Timers that are due fire.
app.nextDeadline()
is when the next timer is due, or nothing when no timer is running.
Clicking and typing for real
The tests above send messages directly. That is fast and covers
update
, but it skips the view: nothing checks that the button is there, or that clicking it sends the right message.
The harness closes that gap. It runs the program on the real UI tree, with real layout, and feeds it pointer and keyboard input the way a window does.
tests/src/core.silk
Copy
effect
fn
todoBody
() -> () !
AssertionError
<'
static
> |
TreeError
|
OutOfMemoryError
? &
mut
Allocator
{
let
viewport =
FrameInput
{ width:
480.0
, height:
360.0
, scale:
1.0
, time:
0.0
}
let
fonts =
run
fixtures.
fonts
()
let
mut
tree =
run
UiTree
.
make
(&fonts)
let
mut
app =
run
program.make<
Todo
,
TodoMessage
,
NoCommand
>()
run
app.
start
(&
mut
tree,
0.0
)
run
drain
(&
mut
app, &
mut
tree)
run
Testing
.
expect
(
remaining
(&app.model.value) ==
2
,
"starts with two things to do"
)
// Click the field, type, press Enter.
run
Testing
.
expect
(
run
click
(&
mut
app, &
mut
tree, viewport,
"draft"
),
"the field is there"
)
run
typeText
(&
mut
app, &
mut
tree, viewport,
"Ship it"
)
run
press
(&
mut
app, &
mut
tree, viewport,
"Enter"
,
0
)
run
Testing
.
expect
(app.model.value.items.
length
() ==
3
,
"Enter adds the item"
)
run
Testing
.
expect
(
String
.
byteLength
(
String
.
view
(&app.model.value.draft)) ==
0
,
"and empties the field"
)
// A checkbox is found by its label, as a person finds it.
run
Testing
.
expect
(
run
click
(&
mut
app, &
mut
tree, viewport,
"Ship it"
),
"the new item shows"
)
run
Testing
.
expect
(
remaining
(&app.model.value) ==
2
,
"checking it leaves two"
) }
•
click(..., "draft")
finds a node by its test id, its label or its text, and clicks its middle. It returns false when there is no such node.
•
typeText
types into whatever has focus.
•
press
presses a key.
If the field were covered by another node, or the button were off screen, the click would miss and the test would fail. That is the kind of bug a message-only test cannot find.
From
loom_tree.harness
Does
click
,
clickRole
,
clickAt
Clicks a node by name, by role and name, or at a point
typeText
,
press
Types text, presses a key
hover
,
drag
,
wheel
Moves, drags and scrolls
snapshot
The whole tree as text, to compare with a saved one
Which kind to write
Test
Good for
Messages in, model out
The logic of
update
. Write many of these
With fake services
Commands and what the program does with their answers
With the harness
The few paths where the view matters: forms, keyboard, focus
Native apps add one more tool, a shell that writes down what was asked of it: see Testing without a window.
Recap
•
Make the program, send messages, read the model.
•
Provide a fake service to control what commands see.
•
Move time with
tick
and the address with
navigate
.
•
Use the harness for the few paths where the view itself matters.