| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
use quasi_declare::declare; |
| 35 |
use quasi_webview::Webview; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
#[must_use] |
| 45 |
pub fn region(target_type: &str, target_id: &str) -> String { |
| 46 |
format!("follow-{target_type}-{target_id}") |
| 47 |
} |
| 48 |
|
| 49 |
declare! { |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
#[must_use] |
| 55 |
pub shape control( |
| 56 |
target_type: &str, |
| 57 |
target_id: &str, |
| 58 |
is_following: bool, |
| 59 |
follower_count: i64 |
| 60 |
) -> Node; |
| 61 |
|
| 62 |
let id = region(target_type, target_id); |
| 63 |
let route = "/api/follow/{target_type}/{target_id}"; |
| 64 |
|
| 65 |
region id as Group { |
| 66 |
given is_following { |
| 67 |
true -> act "Following ({follower_count})" to delete route replacing &id; |
| 68 |
false -> act "Follow ({follower_count})" to post route replacing &id; |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
#[must_use] |
| 75 |
pub fn answered( |
| 76 |
target_type: &str, |
| 77 |
target_id: &str, |
| 78 |
is_following: bool, |
| 79 |
follower_count: i64, |
| 80 |
) -> String { |
| 81 |
use quasi_axum::Serves as _; |
| 82 |
|
| 83 |
Webview::new().fragment(&control( |
| 84 |
target_type, |
| 85 |
target_id, |
| 86 |
is_following, |
| 87 |
follower_count, |
| 88 |
)) |
| 89 |
} |
| 90 |
|
| 91 |
declare! { |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
#[must_use] |
| 99 |
pub shape count_only(follower_count: i64) -> Option<Node>; |
| 100 |
|
| 101 |
let counted = given follower_count { |
| 102 |
1 -> "1 follower", |
| 103 |
otherwise -> "{follower_count} followers", |
| 104 |
}; |
| 105 |
|
| 106 |
text counted when follower_count over 0; |
| 107 |
} |
| 108 |
|
| 109 |
#[cfg(test)] |
| 110 |
mod tests { |
| 111 |
use super::*; |
| 112 |
use quasi_router::Node; |
| 113 |
|
| 114 |
fn html(node: &Node) -> String { |
| 115 |
use quasi_axum::Serves as _; |
| 116 |
Webview::new().fragment(node) |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
#[test] |
| 123 |
fn the_page_and_the_answer_are_the_same_markup() { |
| 124 |
for following in [true, false] { |
| 125 |
assert_eq!( |
| 126 |
html(&control("user", "abc", following, 3)), |
| 127 |
answered("user", "abc", following, 3), |
| 128 |
); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
#[test] |
| 134 |
fn each_state_offers_the_opposite_one() { |
| 135 |
let not_yet = html(&control("project", "p1", false, 0)); |
| 136 |
assert!( |
| 137 |
not_yet.contains("hx-post=\"/api/follow/project/p1\""), |
| 138 |
"{not_yet}" |
| 139 |
); |
| 140 |
assert!(not_yet.contains(">Follow (0)<"), "{not_yet}"); |
| 141 |
|
| 142 |
let already = html(&control("project", "p1", true, 1)); |
| 143 |
assert!( |
| 144 |
already.contains("hx-delete=\"/api/follow/project/p1\""), |
| 145 |
"{already}" |
| 146 |
); |
| 147 |
assert!(already.contains(">Following (1)<"), "{already}"); |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
#[test] |
| 153 |
fn the_answer_carries_the_region_it_replaces() { |
| 154 |
let id = region("user", "abc"); |
| 155 |
let rendered = answered("user", "abc", false, 0); |
| 156 |
assert!(rendered.contains(&format!("id=\"{id}\"")), "{rendered}"); |
| 157 |
assert!( |
| 158 |
rendered.contains(&format!("hx-target=\"#{id}\"")), |
| 159 |
"{rendered}" |
| 160 |
); |
| 161 |
assert!(rendered.contains("outerMorph"), "{rendered}"); |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
#[test] |
| 167 |
fn every_target_owns_its_own_region() { |
| 168 |
assert_ne!(region("user", "a"), region("project", "a")); |
| 169 |
assert_ne!(region("user", "a"), region("user", "b")); |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
#[test] |
| 175 |
fn a_count_of_none_says_nothing() { |
| 176 |
assert!(count_only(0).is_none()); |
| 177 |
assert!(html(&count_only(1).expect("drawn")).contains("1 follower")); |
| 178 |
assert!(html(&count_only(4).expect("drawn")).contains("4 followers")); |
| 179 |
} |
| 180 |
} |
| 181 |
|