Skip to main content

euca/
test.rs

1//! Test utilties.
2
3use crate::app::Application;
4use crate::app::ScheduledRender;
5use crate::app::Dispatcher;
6use crate::app::Commands;
7use crate::app::Update;
8
9use wasm_bindgen::prelude::*;
10use std::rc::Rc;
11use std::cell::RefCell;
12
13/// Test message.
14pub type Msg = ();
15/// Test command.
16pub type Cmd = ();
17/// Test key.
18pub type Key = ();
19
20/// Test app.
21pub struct App {
22    messages: Rc<RefCell<Vec<Msg>>>,
23    render: Option<ScheduledRender<Cmd>>,
24}
25
26impl App {
27    /// Get a dispatcher for this test application.
28    pub fn dispatcher() -> Dispatcher<Msg, Cmd> {
29        Dispatcher::from(Rc::new(RefCell::new(Box::new(
30            App {
31                messages: Rc::new(RefCell::new(vec![])),
32                render: None,
33            }
34        ) as Box<dyn Application<Msg, Cmd>>)))
35    }
36
37    /// Get a dispatcher that tracks messages dispatched to and pushes them to the given vec.
38    pub fn dispatcher_with_vec(messages: Rc<RefCell<Vec<Msg>>>) -> Dispatcher<Msg, Cmd> {
39        Dispatcher::from(Rc::new(RefCell::new(Box::new(
40            App {
41                messages: messages,
42                render: None,
43            }
44        ) as Box<dyn Application<Msg, Cmd>>)))
45    }
46}
47
48impl Application<Msg, Cmd> for App {
49    fn update(&mut self, msg: Msg) -> Commands<Cmd> {
50        self.messages.borrow_mut().push(msg);
51        Commands::default()
52    }
53    fn render(&mut self, _app: &Dispatcher<Msg, Cmd>) -> Vec<Cmd> { vec![] }
54    fn process(&self, _cmd: Cmd, _app: &Dispatcher<Msg, Cmd>) { }
55    fn get_scheduled_render(&mut self) -> &mut Option<ScheduledRender<Cmd>> {
56        &mut self.render
57    }
58    fn set_scheduled_render(&mut self, handle: ScheduledRender<Cmd>) {
59        self.render = Some(handle);
60    }
61    fn push_listener(&mut self, _listener: (String, Closure<dyn FnMut(web_sys::Event)>)) { }
62    fn node(&self) -> Option<web_sys::Node> { None }
63    fn nodes(&self) -> Vec<web_sys::Node> { vec![] }
64    fn create(&mut self, _app: &Dispatcher<Msg, Cmd>) -> Vec<web_sys::Node> { vec![] }
65    fn detach(&mut self, _app: &Dispatcher<Msg, Cmd>) { }
66}
67
68/// Some helpers to make testing a model easier.
69pub trait Model<Message, Command> {
70    /// Update a model with the given message.
71    ///
72    /// This function is a helper function designed to make testing models simpler. Normally during
73    /// an update to a model, the `Commands` structure must be passed in as an argument. This
74    /// function automatically does that and returns the resulting `Commands` structure. It's only
75    /// useful for unit testing.
76    fn test_update(&mut self, msg: Message) -> Commands<Command>;
77}
78
79impl<Message, Command, M: Update<Message, Command>> Model<Message, Command> for M {
80    fn test_update(&mut self, msg: Message) -> Commands<Command> {
81        let mut cmds = Commands::default();
82        Update::update(self, msg, &mut cmds);
83        cmds
84    }
85}