Fix flash message display in todo example.

This commit is contained in:
xelivous 2022-02-23 13:53:31 -08:00 committed by Sergio Benitez
parent 5593685455
commit 13d8e74655
2 changed files with 18 additions and 10 deletions

View File

@ -138,9 +138,8 @@ fn test_bad_form_submissions() {
.dispatch() .dispatch()
.await; .await;
let mut cookies = res.headers().get("Set-Cookie"); assert!(!res.cookies().iter().any(|c| c.value().contains("error")));
assert_eq!(res.status(), Status::UnprocessableEntity); assert_eq!(res.status(), Status::UnprocessableEntity);
assert!(!cookies.any(|value| value.contains("error")));
// Submit a form with an empty description. We look for 'error' in the // Submit a form with an empty description. We look for 'error' in the
// cookies which corresponds to flash message being set as an error. // cookies which corresponds to flash message being set as an error.
@ -150,8 +149,18 @@ fn test_bad_form_submissions() {
.dispatch() .dispatch()
.await; .await;
let mut cookies = res.headers().get("Set-Cookie"); // Check that the flash cookie set and that we're redirected to index.
assert!(cookies.any(|value| value.contains("error"))); assert!(res.cookies().iter().any(|c| c.value().contains("error")));
assert_eq!(res.status(), Status::SeeOther);
// The flash cookie should still be present and the error message should
// be rendered the index.
let body = client.get("/").dispatch().await.into_string().await.unwrap();
assert!(body.contains("Description cannot be empty."));
// Check that the flash is cleared upon another visit to the index.
let body = client.get("/").dispatch().await.into_string().await.unwrap();
assert!(!body.contains("Description cannot be empty."));
// Submit a form without a description. Expect a 422 but no flash error. // Submit a form without a description. Expect a 422 but no flash error.
let res = client.post("/todo") let res = client.post("/todo")
@ -160,8 +169,7 @@ fn test_bad_form_submissions() {
.dispatch() .dispatch()
.await; .await;
let mut cookies = res.headers().get("Set-Cookie"); assert!(!res.cookies().iter().any(|c| c.value().contains("error")));
assert_eq!(res.status(), Status::UnprocessableEntity); assert_eq!(res.status(), Status::UnprocessableEntity);
assert!(!cookies.any(|value| value.contains("error")));
}) })
} }

View File

@ -23,10 +23,10 @@
<div class="ten columns"> <div class="ten columns">
<input type="text" placeholder="enter a task description..." <input type="text" placeholder="enter a task description..."
name="description" id="description" value="" autofocus name="description" id="description" value="" autofocus
class="u-full-width {% if msg %}field-{{msg.0}}{% endif %}" /> class="u-full-width {% if flash %}field-{{flash.0}}{% endif %}" />
{% if msg %} {% if flash %}
<small class="field-{{msg.0}}-msg"> <small class="field-{{flash.0}}-msg">
{{ msg.1 }} {{ flash.1 }}
</small> </small>
{% endif %} {% endif %}
</div> </div>