Skip to content

Commit 85ffd0a

Browse files
committed
examples: Tests
1 parent 09ed178 commit 85ffd0a

File tree

7 files changed

+149
-0
lines changed

7 files changed

+149
-0
lines changed

Diff for: Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ members = [
136136
# "examples/server_integration", # has own workspace
137137
"examples/service_worker",
138138
"examples/subscribe",
139+
"examples/tests",
139140
"examples/tea_component",
140141
"examples/todomvc",
141142
"examples/unsaved_changes",

Diff for: examples/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ How to create and use subscriptions, streams, notifications and commands.
7373
How to write a component in The Elm architecture.
7474
You'll also learn how to pass messages to the parent component.
7575

76+
### [Tests](tests)
77+
How to test your app.
78+
7679
### [Fetch](fetch)
7780
How to make HTTP request using Fetch API.
7881

Diff for: examples/tests/Cargo.toml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[package]
2+
name = "tests"
3+
version = "0.1.0"
4+
authors = ["Martin Kavík <[email protected]>"]
5+
edition = "2018"
6+
7+
[lib]
8+
crate-type = ["cdylib"]
9+
10+
[dev-dependencies]
11+
wasm-bindgen-test = "0.3.18"
12+
13+
[dependencies]
14+
seed = {path = "../../"}

Diff for: examples/tests/Makefile.toml

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
extend = "../../Makefile.toml"
2+
3+
# ---- BUILD ----
4+
5+
[tasks.build]
6+
alias = "default_build"
7+
8+
[tasks.build_release]
9+
alias = "default_build_release"
10+
11+
# ---- START ----
12+
13+
[tasks.start]
14+
alias = "default_start"
15+
16+
[tasks.start_release]
17+
alias = "default_start_release"
18+
19+
# ---- LINT ----
20+
21+
[tasks.clippy]
22+
alias = "default_clippy"

Diff for: examples/tests/README.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
## Tests example
2+
3+
How to test your app.
4+
5+
---
6+
7+
```bash
8+
cargo make start
9+
```
10+
11+
Open [127.0.0.1:8000](http://127.0.0.1:8000) in your browser.

Diff for: examples/tests/index.html

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="utf-8" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
7+
<title>Counter example</title>
8+
</head>
9+
10+
<body>
11+
<section id="app"></section>
12+
<script type="module">
13+
import init from '/pkg/package.js';
14+
init('/pkg/package_bg.wasm');
15+
</script>
16+
</body>
17+
18+
</html>

Diff for: examples/tests/src/lib.rs

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#![allow(clippy::needless_pass_by_value, clippy::trivially_copy_pass_by_ref)]
2+
3+
use seed::{prelude::*, *};
4+
5+
// ------ ------
6+
// Init
7+
// ------ ------
8+
9+
fn init(_: Url, _: &mut impl Orders<Msg>) -> Model {
10+
Model::default()
11+
}
12+
13+
// ------ ------
14+
// Model
15+
// ------ ------
16+
17+
type Model = i32;
18+
19+
// ------ ------
20+
// Update
21+
// ------ ------
22+
23+
enum Msg {
24+
Increment,
25+
Decrement,
26+
}
27+
28+
fn update(msg: Msg, model: &mut Model, _: &mut impl Orders<Msg>) {
29+
match msg {
30+
Msg::Increment => *model += 1,
31+
Msg::Decrement => *model -= 1,
32+
}
33+
}
34+
35+
// ------ ------
36+
// View
37+
// ------ ------
38+
39+
fn view(model: &Model) -> Node<Msg> {
40+
div![
41+
button![ev(Ev::Click, |_| Msg::Decrement), "-"],
42+
div![model],
43+
button![ev(Ev::Click, |_| Msg::Increment), "+"],
44+
]
45+
}
46+
47+
// ------ ------
48+
// Start
49+
// ------ ------
50+
51+
#[wasm_bindgen(start)]
52+
pub fn start() {
53+
App::start("app", init, update, view);
54+
}
55+
56+
// ------ ------
57+
// Tests
58+
// ------ ------
59+
60+
#[cfg(test)]
61+
mod tests {
62+
use seed::log;
63+
use wasm_bindgen_test::*;
64+
65+
use super::{Model, view};
66+
67+
wasm_bindgen_test_configure!(run_in_browser);
68+
69+
#[wasm_bindgen_test]
70+
pub fn view_test() {
71+
let model: Model = 123;
72+
let node = view(&model);
73+
let x = node.to_string();
74+
75+
log!("sfesf");
76+
77+
assert_eq!(2 + 2, 4)
78+
}
79+
}
80+

0 commit comments

Comments
 (0)