chore: fix ssr tests

This commit is contained in:
Greg Johnston 2024-08-02 09:40:18 -04:00
parent 34382c0c23
commit 82ea4eb7ce
1 changed files with 115 additions and 108 deletions

View File

@ -1,27 +1,31 @@
use leptos::{ #[cfg(not(feature = "ssr"))]
pub mod tests {
use leptos::{
server, server,
server_fn::{codec, ServerFn, ServerFnError}, server_fn::{codec, ServerFn, ServerFnError},
}; };
use std::any::TypeId; use std::any::TypeId;
#[test] #[test]
fn server_default() { fn server_default() {
#[server] #[server]
pub async fn my_server_action() -> Result<(), ServerFnError> { pub async fn my_server_action() -> Result<(), ServerFnError> {
Ok(()) Ok(())
} }
assert_eq!( assert_eq!(
<MyServerAction as ServerFn>::PATH.trim_end_matches(char::is_numeric), <MyServerAction as ServerFn>::PATH
.trim_end_matches(char::is_numeric),
"/api/my_server_action" "/api/my_server_action"
); );
assert_eq!( assert_eq!(
TypeId::of::<<MyServerAction as ServerFn>::InputEncoding>(), TypeId::of::<<MyServerAction as ServerFn>::InputEncoding>(),
TypeId::of::<codec::PostUrl>() TypeId::of::<codec::PostUrl>()
); );
} }
#[test] #[test]
fn server_full_legacy() { fn server_full_legacy() {
#[server(FooBar, "/foo/bar", "Cbor", "my_path")] #[server(FooBar, "/foo/bar", "Cbor", "my_path")]
pub async fn my_server_action() -> Result<(), ServerFnError> { pub async fn my_server_action() -> Result<(), ServerFnError> {
Ok(()) Ok(())
@ -31,10 +35,10 @@ fn server_full_legacy() {
TypeId::of::<<FooBar as ServerFn>::InputEncoding>(), TypeId::of::<<FooBar as ServerFn>::InputEncoding>(),
TypeId::of::<codec::Cbor>() TypeId::of::<codec::Cbor>()
); );
} }
#[test] #[test]
fn server_all_keywords() { fn server_all_keywords() {
#[server(endpoint = "my_path", encoding = "Cbor", prefix = "/foo/bar", name = FooBar)] #[server(endpoint = "my_path", encoding = "Cbor", prefix = "/foo/bar", name = FooBar)]
pub async fn my_server_action() -> Result<(), ServerFnError> { pub async fn my_server_action() -> Result<(), ServerFnError> {
Ok(()) Ok(())
@ -44,10 +48,10 @@ fn server_all_keywords() {
TypeId::of::<<FooBar as ServerFn>::InputEncoding>(), TypeId::of::<<FooBar as ServerFn>::InputEncoding>(),
TypeId::of::<codec::Cbor>() TypeId::of::<codec::Cbor>()
); );
} }
#[test] #[test]
fn server_mix() { fn server_mix() {
#[server(FooBar, endpoint = "my_path")] #[server(FooBar, endpoint = "my_path")]
pub async fn my_server_action() -> Result<(), ServerFnError> { pub async fn my_server_action() -> Result<(), ServerFnError> {
Ok(()) Ok(())
@ -57,10 +61,10 @@ fn server_mix() {
TypeId::of::<<FooBar as ServerFn>::InputEncoding>(), TypeId::of::<<FooBar as ServerFn>::InputEncoding>(),
TypeId::of::<codec::PostUrl>() TypeId::of::<codec::PostUrl>()
); );
} }
#[test] #[test]
fn server_name() { fn server_name() {
#[server(name = FooBar)] #[server(name = FooBar)]
pub async fn my_server_action() -> Result<(), ServerFnError> { pub async fn my_server_action() -> Result<(), ServerFnError> {
Ok(()) Ok(())
@ -73,42 +77,44 @@ fn server_name() {
TypeId::of::<<FooBar as ServerFn>::InputEncoding>(), TypeId::of::<<FooBar as ServerFn>::InputEncoding>(),
TypeId::of::<codec::PostUrl>() TypeId::of::<codec::PostUrl>()
); );
} }
#[test] #[test]
fn server_prefix() { fn server_prefix() {
#[server(prefix = "/foo/bar")] #[server(prefix = "/foo/bar")]
pub async fn my_server_action() -> Result<(), ServerFnError> { pub async fn my_server_action() -> Result<(), ServerFnError> {
Ok(()) Ok(())
} }
assert_eq!( assert_eq!(
<MyServerAction as ServerFn>::PATH.trim_end_matches(char::is_numeric), <MyServerAction as ServerFn>::PATH
.trim_end_matches(char::is_numeric),
"/foo/bar/my_server_action" "/foo/bar/my_server_action"
); );
assert_eq!( assert_eq!(
TypeId::of::<<MyServerAction as ServerFn>::InputEncoding>(), TypeId::of::<<MyServerAction as ServerFn>::InputEncoding>(),
TypeId::of::<codec::PostUrl>() TypeId::of::<codec::PostUrl>()
); );
} }
#[test] #[test]
fn server_encoding() { fn server_encoding() {
#[server(encoding = "GetJson")] #[server(encoding = "GetJson")]
pub async fn my_server_action() -> Result<(), ServerFnError> { pub async fn my_server_action() -> Result<(), ServerFnError> {
Ok(()) Ok(())
} }
assert_eq!( assert_eq!(
<MyServerAction as ServerFn>::PATH.trim_end_matches(char::is_numeric), <MyServerAction as ServerFn>::PATH
.trim_end_matches(char::is_numeric),
"/api/my_server_action" "/api/my_server_action"
); );
assert_eq!( assert_eq!(
TypeId::of::<<MyServerAction as ServerFn>::InputEncoding>(), TypeId::of::<<MyServerAction as ServerFn>::InputEncoding>(),
TypeId::of::<codec::GetUrl>() TypeId::of::<codec::GetUrl>()
); );
} }
#[test] #[test]
fn server_endpoint() { fn server_endpoint() {
#[server(endpoint = "/path/to/my/endpoint")] #[server(endpoint = "/path/to/my/endpoint")]
pub async fn my_server_action() -> Result<(), ServerFnError> { pub async fn my_server_action() -> Result<(), ServerFnError> {
Ok(()) Ok(())
@ -121,4 +127,5 @@ fn server_endpoint() {
TypeId::of::<<MyServerAction as ServerFn>::InputEncoding>(), TypeId::of::<<MyServerAction as ServerFn>::InputEncoding>(),
TypeId::of::<codec::PostUrl>() TypeId::of::<codec::PostUrl>()
); );
}
} }