inertia_rust/
test.rs

1use serde::Deserialize;
2use serde_json::{Map, Value};
3use std::collections::HashMap;
4
5pub type DeserializedDeferredProps = Option<HashMap<String, Vec<String>>>;
6
7#[derive(Deserialize, PartialEq, Clone, Debug)]
8#[allow(unused)]
9pub struct AssertableInertia {
10    pub component: String,
11    pub props: Map<String, Value>,
12    pub url: String,
13    pub version: Option<String>,
14
15    #[serde(rename = "clearHistory")]
16    pub clear_history: bool,
17
18    #[serde(rename = "encryptHistory")]
19    pub encrypt_history: bool,
20
21    #[serde(rename = "deferredProps")]
22    pub deferred_props: DeserializedDeferredProps,
23
24    #[serde(rename = "mergeProps")]
25    pub merge_props: Option<Vec<String>>,
26}
27
28pub trait InertiaTestRequest {
29    /// Injects the Inertia header to the request, making the incoming response
30    /// being a JSON instead of an HTML body.
31    ///
32    /// This allows the response to be serialized to an `AssertableInertia` instance.
33    fn inertia(self) -> Self;
34}
35
36pub trait IntoAssertableInertia {
37    /// Extract an `AssertableInertia` instance from `self`. This is only intended for
38    /// testing and may interrupt your application at any moment, since it may panic on
39    /// any error.
40    ///
41    /// # Panic
42    /// If any error comes from an `Result` it will panic, since it will always dangerously
43    /// unwrap the result.
44    fn into_assertable_inertia(self) -> AssertableInertia;
45}
46
47impl AssertableInertia {
48    pub fn get_component(&self) -> &str {
49        &self.component
50    }
51
52    pub fn get_props(&self) -> &Map<String, Value> {
53        &self.props
54    }
55
56    pub fn get_url(&self) -> &str {
57        &self.url
58    }
59
60    pub fn get_version(&self) -> Option<&String> {
61        self.version.as_ref()
62    }
63
64    pub fn get_clear_history(&self) -> bool {
65        self.clear_history
66    }
67
68    pub fn get_encrypt_history(&self) -> bool {
69        self.encrypt_history
70    }
71
72    pub fn get_deferred_props(&self) -> Option<&HashMap<String, Vec<String>>> {
73        self.deferred_props.as_ref()
74    }
75
76    pub fn get_merge_props(&self) -> Option<&Vec<String>> {
77        self.merge_props.as_ref()
78    }
79}