minor test refactoring

This commit is contained in:
Hamcha 2025-01-26 15:32:27 +01:00
parent d63b566873
commit 0bc40b492f
Signed by: hamcha
GPG key ID: 1669C533B8CF6D89
2 changed files with 50 additions and 60 deletions
src/inbound/renderer

View file

@ -74,14 +74,15 @@ pub fn BlockElement(block: Block) -> Element {
#[cfg(test)]
mod tests {
use meta::{PageContext, SiteContext};
use meta::PageContext;
use crate::{
domain::entities::{
self,
cursor::Paginated,
site::{CollectionKind, Image, SiteInfo},
site::{CollectionKind, Image},
},
inbound::renderer::testing,
outbound::services::site::{MockSiteService, SiteServiceProvider},
};
@ -90,11 +91,12 @@ mod tests {
#[test]
fn block_renders_text() {
let text = "Hello, world!";
let block = Block::Text {
text: text.to_string(),
};
let element = rsx! {
BlockElement { block }
BlockElement {
block: Block::Text {
text: text.to_string(),
},
}
};
let elem_str = dioxus::ssr::render_element(element);
assert!(elem_str.contains(text));
@ -102,19 +104,21 @@ mod tests {
#[test]
fn block_gallery_renders_images() {
let images = vec![
Image {
src: "https://example.com/image1.jpg".to_string(),
caption: "Image 1".to_string(),
},
Image {
src: "https://example.com/image2.jpg".to_string(),
caption: "Image 2".to_string(),
},
];
let block = Block::Gallery { images };
let element = rsx! {
BlockElement { block }
BlockElement {
block: Block::Gallery {
images: vec![
Image {
src: "https://example.com/image1.jpg".to_string(),
caption: "Image 1".to_string(),
},
Image {
src: "https://example.com/image2.jpg".to_string(),
caption: "Image 2".to_string(),
},
],
},
}
};
let elem_str = dioxus::ssr::render_element(element);
assert!(elem_str.contains("<img"));
@ -124,17 +128,19 @@ mod tests {
#[test]
fn post_renders_blocks() {
let blocks = vec![
Block::Text {
text: "Hello, world!".to_string(),
},
Block::Text {
text: "Something else!".to_string(),
},
];
let post = Post { blocks };
let element = rsx! {
PostElement { post }
PostElement {
post: Post {
blocks: vec![
Block::Text {
text: "Hello, world!".to_string(),
},
Block::Text {
text: "Something else!".to_string(),
},
],
},
}
};
let elem_str = dioxus::ssr::render_element(element);
assert!(elem_str.contains("Hello, world!"));
@ -149,13 +155,7 @@ mod tests {
}
});
app.provide_root_context(SiteContext {
info: SiteInfo {
title: "test".to_string(),
domain: "test".to_string(),
pages: vec![],
},
});
testing::add_test_site_context(&mut app);
let mut mock_service = MockSiteService::new();
mock_service
@ -196,13 +196,7 @@ mod tests {
}
});
app.provide_root_context(SiteContext {
info: SiteInfo {
title: "test".to_string(),
domain: "test".to_string(),
pages: vec![],
},
});
testing::add_test_site_context(&mut app);
app.provide_root_context(PageContext {
data: entities::site::Page {
@ -250,13 +244,7 @@ mod tests {
}
});
app.provide_root_context(SiteContext {
info: SiteInfo {
title: "test".to_string(),
domain: "test".to_string(),
pages: vec![],
},
});
testing::add_test_site_context(&mut app);
app.provide_root_context(PageContext {
data: entities::site::Page {
@ -288,13 +276,7 @@ mod tests {
}
});
app.provide_root_context(SiteContext {
info: SiteInfo {
title: "test".to_string(),
domain: "test".to_string(),
pages: vec![],
},
});
testing::add_test_site_context(&mut app);
app.provide_root_context(PageContext {
data: entities::site::Page {

View file

@ -1,7 +1,15 @@
use dioxus::prelude::*;
#[component]
pub fn UseSiteContext() -> Element {
use_context_provider(|| Signal::new(0));
rsx! {}
use crate::domain::entities::site::SiteInfo;
use super::meta::SiteContext;
pub fn add_test_site_context(app: &mut VirtualDom) {
app.provide_root_context(SiteContext {
info: SiteInfo {
title: "test".to_string(),
domain: "test".to_string(),
pages: vec![],
},
});
}