1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//! # Testing
//!
//! `freya-testing` is a special renderer that let's you run your components in a headless environment.
//! This will let you easily write unit tests for your components.
//!
//! ## Getting started
//!
//! Add `freya-testing`:
//!
//! ```toml
//! [dev-dependencies]
//! freya-testing = "0.1"
//! ```
//!
//! You can use the `launch_test` function to run the tests of your component, it will return you a set of utilities for you to interact with the component.
//!
//! For example, this will launch a state-less component and assert that it renders a label with the text `"Hello World!"`.
//!
//! ```rust, no_run
//! #[tokio::test]
//! async fn test() {
//!     fn our_component(cx: Scope) -> Element {
//!         render!(
//!             label {
//!                 "Hello World!"
//!             }
//!         )
//!     }
//!
//!     let mut utils = launch_test(our_component);
//!
//!     let root = utils.root();
//!     let label = root.get(0);
//!     let label_text = label.get(0);
//!
//!     assert_eq!(label_text.text(), Some("Hello World!"));
//! }
//! ```
//!
//! The `root()` function will give you the Root node of your app, then, with the `get` function you can retrieve a Node from it's parent given it's index position.
//!
//! ## Dynamic components
//!
//! If the component has logic that might execute asynchronously, you will need to wait for the component to update using the `wait_for_update` function before asserting the result.
//!
//! Here, the component has a state that is `false` by default, but, once mounted it will update the state to `true`.
//!
//! ```rust, no_run
//! #[tokio::test]
//! async fn dynamic_test() {
//!     fn dynamic_component(cx: Scope) -> Element {
//!         let state = use_state(cx, || false);
//!
//!         use_effect(cx, (), |_| {
//!             state.set(true);
//!             async move { }
//!         });
//!
//!         render!(
//!             label {
//!                 "Is enabled? {state}"
//!             }
//!         )
//!     }
//!
//!     let mut utils = launch_test(dynamic_component);
//!
//!     let root = utils.root();
//!     let label = root.get(0);
//!
//!     assert_eq!(label.get(0).text(), Some("Is enabled? false"));
//!
//!     // This will run the `use_effect` and update the state.
//!     utils.wait_for_update().await;
//!
//!     assert_eq!(label.get(0).text(), Some("Is enabled? true"));
//! }
//! ```
//!
//! ## Events
//!
//! We can also simulate events on the component, for example, we can simulate a click event on a `rect` and assert that the state has been updated.
//!
//! ```rust, no_run
//! #[tokio::test]
//! async fn event_test() {
//!     fn event_component(cx: Scope) -> Element {
//!         let enabled = use_state(cx, || false);
//!         render!(
//!             rect {
//!                 width: "100%",
//!                 height: "100%",
//!                 background: "red",
//!                 onclick: |_| {
//!                     enabled.set(true);
//!                 },
//!                 label {
//!                     "Is enabled? {enabled}"
//!                 }
//!             }
//!         )
//!     }
//!
//!     let mut utils = launch_test(event_component);
//!
//!     let rect = utils.root().get(0);
//!     let label = rect.get(0);
//!
//!     utils.wait_for_update().await;
//!
//!     let text = label.get(0);
//!     assert_eq!(text.text(), Some("Is enabled? false"));
//!
//!     // Push a click event to the events queue
//!     utils.push_event(FreyaEvent::Mouse {
//!         name: "click",
//!         cursor: (5.0, 5.0).into(),
//!         button: Some(MouseButton::Left),
//!     });
//!
//!     // Run the queued events and update the state
//!     utils.wait_for_update().await;
//!
//!     // Because the click event was executed and the state updated, now the text has changed too!
//!     let text = label.get(0);
//!     assert_eq!(text.text(), Some("Is enabled? true"));
//! }
//! ```
//!
//! ## Testing configuration
//!
//! The `launch_test` comes with a default configuration, but you can also pass your own config with the `launch_test_with_config` function.
//!
//! Here is an example of how to can set our custom window size:
//!
//! ```rust, no_run
//! #[tokio::test]
//! async fn test() {
//!     fn our_component(cx: Scope) -> Element {
//!         render!(
//!             label {
//!                 "Hello World!"
//!             }
//!         )
//!     }
//!
//!     let mut utils = launch_test_with_config(
//!         our_component,
//!         TestingConfig::default().with_size((500.0, 800.0).into()),
//!     );
//!
//!     let root = utils.root();
//!     let label = root.get(0);
//!     let label_text = label.get(0);
//!
//!     assert_eq!(label_text.text(), Some("Hello World!"));
//! }
//! ````