Files
todo-baggins/dioxus-i18n/tests/common/test_hook.rs
Matouš Volf 2c2ad7ad21
Some checks failed
hadolint check / hadolint check (pull_request) Successful in 13s
actionlint check / actionlint check (pull_request) Successful in 7s
conventional pull request title check / conventional pull request title check (pull_request) Successful in 3s
conventional commit messages check / conventional commit messages check (pull_request) Successful in 6s
dotenv-linter check / dotenv-linter check (pull_request) Successful in 7s
GitLeaks check / GitLeaks check (pull_request) Successful in 13s
markdownlint check / markdownlint check (pull_request) Failing after 54s
Prettier check / Prettier check (pull_request) Failing after 51s
htmlhint check / htmlhint check (pull_request) Successful in 1m3s
checkov check / checkov check (pull_request) Failing after 2m26s
ShellCheck check / ShellCheck check (pull_request) Successful in 1m14s
Stylelint check / Stylelint check (pull_request) Successful in 1m27s
Rust check / Rust check (pull_request) Failing after 11m40s
yamllint check / yamllint check (pull_request) Successful in 13m36s
fix: automatically reconnect after losing a WebSocket connection
2026-01-23 11:04:10 +01:00

86 lines
2.0 KiB
Rust

// Lifted from: https://dioxuslabs.com/learn/0.6/cookbook/testing
//
// Much curtialed functionality and massaged to use in the local testing
// here. This hook isn't intended for reuse.
//
use dioxus::{dioxus_core::NoOpMutations, prelude::*};
use futures::FutureExt;
use std::{cell::RefCell, fmt::Debug, rc::Rc};
pub(crate) fn test_hook<V: 'static>(
initialize: impl FnMut() -> V + 'static,
check: impl FnMut(V, &mut Assertions) + 'static,
) {
#[derive(Props)]
struct MockAppComponent<I: 'static, C: 'static> {
hook: Rc<RefCell<I>>,
check: Rc<RefCell<C>>,
}
impl<I, C> PartialEq for MockAppComponent<I, C> {
fn eq(&self, _: &Self) -> bool {
true
}
}
impl<I, C> Clone for MockAppComponent<I, C> {
fn clone(&self) -> Self {
Self {
hook: self.hook.clone(),
check: self.check.clone(),
}
}
}
fn mock_app<I: FnMut() -> V, C: FnMut(V, &mut Assertions), V>(
props: MockAppComponent<I, C>,
) -> Element {
let value = props.hook.borrow_mut()();
let mut assertions = Assertions::new();
props.check.borrow_mut()(value, &mut assertions);
rsx! { div {} }
}
let mut vdom = VirtualDom::new_with_props(
mock_app,
MockAppComponent {
hook: Rc::new(RefCell::new(initialize)),
check: Rc::new(RefCell::new(check)),
},
);
vdom.rebuild_in_place();
while vdom.wait_for_work().now_or_never().is_some() {
vdom.render_immediate(&mut NoOpMutations);
}
}
#[derive(Debug)]
pub(crate) struct Assertions {}
impl Assertions {
pub fn new() -> Self {
Self {}
}
pub fn assert<T>(&mut self, actual: T, expected: T, id: &str)
where
T: PartialEq + Debug,
{
if actual != expected {
eprintln!(
"***** ERROR in {}: actual: '{:?}' != expected: '{:?}' *****\n",
id, actual, expected
);
std::process::exit(-1);
};
}
}