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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//! # Testing
//!
//! `freya-testing` is a headless renderer for freya components, which means you can simulate a graphical environment
//! with no need to actually draw anything, a perfect fit for testing.
//!
//! You can use the `launch_test` function to run the tests of the component.
//! It returns a set of utilities to interact with the component.
//!
//! ## Stateless example
//!
//! Simply asserts that the component renders a label with the text `"Hello World!"`.
//!
//! ```rust, no_run
//! #[tokio::test]
//! async fn test() {
//!     fn our_component() -> Element {
//!         rsx!(
//!             label {
//!                 "Hello World!"
//!             }
//!         )
//!     }
//!
//!     let mut utils = launch_test(our_component);
//!
//!     let root = utils.root(); // Get the root element of your app
//!     let label = root.get(0); // Get the children of the root in the index 0
//!     let label_text = label.get(0);
//!
//!     assert_eq!(label_text.text(), Some("Hello World!"));
//! }
//! ```
//!
//! ## Stateful example
//!
//! If the component has logic that might execute asynchronously, you 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 updates the state to `true`.
//!
//! ```rust, no_run
//! #[tokio::test]
//! async fn dynamic_test() {
//!     fn dynamic_component() -> Element {
//!         let mut state = use_signal(|| false);
//!
//!         use_hook(move || {
//!             state.set(true);
//!         });
//!
//!         rsx!(
//!             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 poll the VirtualDOM and apply the new changes
//!     utils.wait_for_update().await;
//!
//!     assert_eq!(label.get(0).text(), Some("Is enabled? true"));
//! }
//! ```
//!
//! ## Events example
//!
//! You can simulate events on the component, for example, simulate a click event on a `rect` and assert that the state was updated.
//!
//! ```rust, no_run
//! #[tokio::test]
//! async fn event_test() {
//!     fn event_component() -> Element {
//!         let mut enabled = use_signal(|| false);
//!
//!         rsx!(
//!             rect {
//!                 width: "100%",
//!                 height: "100%",
//!                 background: "red",
//!                 onclick: move |_| {
//!                     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(PlatformEvent::Mouse {
//!         name: "click",
//!         cursor: (5.0, 5.0).into(),
//!         button: Some(MouseButton::Left),
//!     });
//!
//!     // Poll the VirtualDOM with the new events
//!     utils.wait_for_update().await;
//!
//!     // Because the click event was sent, and the state updated, the text was changed as well!
//!     let text = label.get(0);
//!     assert_eq!(text.text(), Some("Is enabled? true"));
//! }
//! ```
//!
//! ## Configuration example
//!
//! The `launch_test` comes with a default configuration, but you can 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() -> Element {
//!         rsx!(
//!             label {
//!                 "Hello World!"
//!             }
//!         )
//!     }
//!
//!     let mut utils = launch_test_with_config(
//!         our_component,
//!         TestingConfig {
//!             size: (500.0, 800.0).into(),
//!             ..TestingConfig::default()
//!         },
//!     );
//!
//!     let root = utils.root();
//!     let label = root.get(0);
//!     let label_text = label.get(0);
//!
//!     assert_eq!(label_text.text(), Some("Hello World!"));
//! }
//! ````

pub mod config;
pub mod launch;
pub mod test_handler;
pub mod test_node;
pub mod test_utils;

const SCALE_FACTOR: f64 = 1.0;

pub mod prelude {
    pub use freya_core::prelude::*;
    pub use freya_node_state::*;

    pub use crate::{
        config::*,
        launch::*,
        test_handler::*,
        test_node::*,
        test_utils::*,
    };
}