parent.rs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #![allow(unused)]
  2. use std::rc::Rc;
  3. use dioxus::prelude::*;
  4. #[derive(Routable, Clone, PartialEq, Debug)]
  5. #[rustfmt::skip]
  6. enum Route {
  7. #[route("/")]
  8. RootIndex {},
  9. #[nest("/fixed")]
  10. #[layout(Fixed)]
  11. #[route("/")]
  12. FixedIndex {},
  13. #[route("/fixed")]
  14. FixedFixed {},
  15. #[end_layout]
  16. #[end_nest]
  17. #[nest("/:id")]
  18. #[layout(Parameter)]
  19. #[route("/")]
  20. ParameterIndex { id: u8 },
  21. #[route("/fixed")]
  22. ParameterFixed { id: u8 },
  23. #[end_layout]
  24. #[end_nest]
  25. #[nest("/hash")]
  26. #[route("/")]
  27. HashIndex {},
  28. #[nest("/:id")]
  29. #[route("/?:query")]
  30. HashId { id: u8, query: String },
  31. #[layout(Parameter)]
  32. #[route("/path/?:query#:hash")]
  33. HashQuery { id: u8, query: String, hash: String },
  34. }
  35. #[test]
  36. fn get_parent() {
  37. assert_eq!(Route::RootIndex {}.parent(), None);
  38. assert_eq!(Route::FixedIndex {}.parent(), Some(Route::RootIndex {}));
  39. assert_eq!(Route::FixedFixed {}.parent(), Some(Route::FixedIndex {}));
  40. assert_eq!(
  41. Route::ParameterIndex { id: 0 }.parent(),
  42. Some(Route::RootIndex {})
  43. );
  44. assert_eq!(
  45. Route::ParameterFixed { id: 0 }.parent(),
  46. Some(Route::ParameterIndex { id: 0 })
  47. );
  48. assert_eq!(
  49. Route::HashQuery {
  50. id: 0,
  51. query: "query".into(),
  52. hash: "hash".into()
  53. }
  54. .parent(),
  55. Some(Route::HashId {
  56. id: 0,
  57. query: "".into()
  58. })
  59. );
  60. assert_eq!(
  61. Route::HashId {
  62. id: 0,
  63. query: "query".into()
  64. }
  65. .parent(),
  66. Some(Route::HashIndex {})
  67. );
  68. assert_eq!(Route::HashIndex {}.parent(), Some(Route::RootIndex {}));
  69. }
  70. #[test]
  71. fn is_child() {
  72. assert!(!Route::RootIndex {}.is_child_of(&Route::RootIndex {}));
  73. assert!(Route::FixedIndex {}.is_child_of(&Route::RootIndex {}));
  74. assert!(!Route::FixedIndex {}.is_child_of(&Route::FixedIndex {}));
  75. assert!(Route::FixedFixed {}.is_child_of(&Route::FixedIndex {}));
  76. assert!(!Route::FixedFixed {}.is_child_of(&Route::FixedFixed {}));
  77. assert!(Route::ParameterIndex { id: 0 }.is_child_of(&Route::RootIndex {}));
  78. assert!(!Route::ParameterIndex { id: 0 }.is_child_of(&Route::ParameterIndex { id: 0 }));
  79. assert!(Route::ParameterFixed { id: 0 }.is_child_of(&Route::ParameterIndex { id: 0 }));
  80. assert!(!Route::ParameterFixed { id: 0 }.is_child_of(&Route::ParameterFixed { id: 0 }));
  81. assert!(Route::HashQuery {
  82. id: 0,
  83. query: "query".into(),
  84. hash: "hash".into()
  85. }
  86. .is_child_of(&Route::HashId {
  87. id: 0,
  88. query: "query".into()
  89. }));
  90. assert!(!Route::HashQuery {
  91. id: 0,
  92. query: "query".into(),
  93. hash: "hash".into()
  94. }
  95. .is_child_of(&Route::HashQuery {
  96. id: 0,
  97. query: "query".into(),
  98. hash: "hash".into()
  99. }));
  100. assert!(Route::HashId {
  101. id: 0,
  102. query: "query".into()
  103. }
  104. .is_child_of(&Route::HashIndex {}));
  105. assert!(!Route::HashId {
  106. id: 0,
  107. query: "query".into()
  108. }
  109. .is_child_of(&Route::HashId {
  110. id: 0,
  111. query: "query".into()
  112. }));
  113. assert!(Route::HashIndex {}.is_child_of(&Route::RootIndex {}));
  114. assert!(!Route::HashIndex {}.is_child_of(&Route::HashIndex {}));
  115. }
  116. #[component]
  117. fn RootIndex() -> Element {
  118. rsx! { h2 { "Root Index" } }
  119. }
  120. #[component]
  121. fn Fixed() -> Element {
  122. rsx! {
  123. h2 { "Fixed" }
  124. Outlet::<Route> { }
  125. }
  126. }
  127. #[component]
  128. fn FixedIndex() -> Element {
  129. rsx! { h3 { "Fixed - Index" } }
  130. }
  131. #[component]
  132. fn FixedFixed() -> Element {
  133. rsx! { h3 { "Fixed - Fixed"} }
  134. }
  135. #[component]
  136. fn Parameter(id: u8) -> Element {
  137. rsx! {
  138. h2 { "Parameter {id}" }
  139. Outlet::<Route> { }
  140. }
  141. }
  142. #[component]
  143. fn ParameterIndex(id: u8) -> Element {
  144. rsx! { h3 { "Parameter - Index" } }
  145. }
  146. #[component]
  147. fn ParameterFixed(id: u8) -> Element {
  148. rsx! { h3 { "Parameter - Fixed" } }
  149. }
  150. #[component]
  151. fn HashQuery(id: u8, query: String, hash: String) -> Element {
  152. rsx! {
  153. h2 { "Hash Query" }
  154. h3 { "id: {id}" }
  155. h3 { "query: {query}" }
  156. h3 { "hash: {hash}" }
  157. }
  158. }
  159. #[component]
  160. fn HashIndex() -> Element {
  161. rsx! { h3 { "Hash Index" } }
  162. }
  163. #[component]
  164. fn HashId(id: u8, query: String) -> Element {
  165. rsx! {
  166. h3 { "Hash Id {id}" }
  167. h3 { "query: {query}" }
  168. }
  169. }