Lines
97.22 %
Functions
85.71 %
Branches
100 %
use anyhow::{Result, anyhow};
use axum::http::{HeaderMap, HeaderValue, StatusCode};
use axum::response::Response;
use forms::REPORT_BALANCE;
use super::{SHELL_ID, report_invalid};
use crate::response::WebError;
fn shown() -> Vec<(String, String)> {
vec![("sort_order".into(), "name_asc".into())]
}
fn refused(trigger: Option<&str>) -> Result<Response> {
let mut headers = HeaderMap::new();
if let Some(trigger) = trigger {
headers.insert("hx-trigger", HeaderValue::from_str(trigger)?);
let editor = |oob: bool| Ok(format!("<editor oob=\"{oob}\"/>"));
report_invalid(
&headers,
&REPORT_BALANCE,
"<form id=\"nf-report-balance\"></form>".into(),
&shown(),
Some(&editor),
)
.map_err(|e| anyhow!("{e:?}"))
async fn body(response: Response) -> Result<String> {
let bytes = axum::body::to_bytes(response.into_body(), usize::MAX).await?;
Ok(String::from_utf8(bytes.to_vec())?)
#[tokio::test]
async fn the_shell_gets_the_form_editor_and_controls_inside_the_filters_wrapper() -> Result<()> {
let response = refused(Some(SHELL_ID))?;
assert_eq!(response.status(), StatusCode::UNPROCESSABLE_ENTITY);
assert!(response.headers().get("hx-retarget").is_none());
let html = body(response).await?;
assert!(html.contains("class=\"report-filters\""), "{html}");
assert!(html.contains("hx-boost=\"true\""), "{html}");
assert!(html.contains("<form id=\"nf-report-balance\">"), "{html}");
assert!(html.contains("<editor oob=\"false\"/>"), "{html}");
assert!(
html.contains("form=\"nf-report-balance\" name=\"sort_order\" value=\"name_asc\""),
"{html}"
);
Ok(())
async fn a_loaded_table_swaps_the_form_in_place() -> Result<()> {
for trigger in [None, Some("nf-report-balance")] {
let response = refused(trigger)?;
assert_eq!(
response.headers().get("hx-retarget"),
Some(&HeaderValue::from_static("#nf-report-balance"))
assert!(!html.contains("report-filters"), "{html}");
assert!(html.contains("<editor oob=\"true\"/>"), "{html}");
#[test]
fn no_editor_renders_nothing_beside_the_form() -> Result<()> {
let response = report_invalid(
&HeaderMap::new(),
"<form></form>".into(),
&[],
None,
.map_err(|e| anyhow!("{e:?}"))?;
fn an_editor_failure_is_the_error() {
let broken = |_: bool| Err(WebError::internal("editor", "broken"));
let failed = report_invalid(
String::new(),
Some(&broken),
assert!(failed.is_err());