|
| 1 | +#[cfg(all(test, feature = "variant"))] |
| 2 | +mod test { |
| 3 | + use tailwind_fuse::*; |
| 4 | + |
| 5 | + #[derive(TwVariant, Debug, PartialEq)] |
| 6 | + enum BtnColor { |
| 7 | + #[tw(default, class = "bg-blue-500 text-blue-100")] |
| 8 | + Blue, |
| 9 | + #[tw(class = "bg-red-500 text-red-100")] |
| 10 | + Red, |
| 11 | + } |
| 12 | + |
| 13 | + #[test] |
| 14 | + fn btn_color() { |
| 15 | + assert_eq!(BtnColor::Blue.as_class(), "bg-blue-500 text-blue-100"); |
| 16 | + assert_eq!(BtnColor::Red.as_class(), "bg-red-500 text-red-100"); |
| 17 | + |
| 18 | + assert_eq!(BtnColor::default(), BtnColor::Blue); |
| 19 | + } |
| 20 | + |
| 21 | + #[test] |
| 22 | + fn btn_color_with_default() { |
| 23 | + #[derive(TwVariant)] |
| 24 | + #[tw(class = "text-white")] |
| 25 | + enum BtnColor { |
| 26 | + #[tw(default, class = "bg-blue-500")] |
| 27 | + Blue, |
| 28 | + #[tw(class = "bg-red-500")] |
| 29 | + Red, |
| 30 | + } |
| 31 | + assert_eq!(BtnColor::Blue.as_class(), "text-white bg-blue-500"); |
| 32 | + assert_eq!(BtnColor::Red.as_class(), "text-white bg-red-500"); |
| 33 | + } |
| 34 | + |
| 35 | + #[derive(TwVariant)] |
| 36 | + enum BtnSize { |
| 37 | + #[tw(default, class = "h-9 px-4 py-2")] |
| 38 | + Default, |
| 39 | + #[tw(class = "h-8 rounded-md px-3 text-xs")] |
| 40 | + Sm, |
| 41 | + #[tw(class = "h-10 rounded-lg px-8")] |
| 42 | + Lg, |
| 43 | + } |
| 44 | + |
| 45 | + #[derive(TwClass)] |
| 46 | + struct Btn { |
| 47 | + size: BtnSize, |
| 48 | + color: BtnColor, |
| 49 | + } |
| 50 | + |
| 51 | + #[test] |
| 52 | + fn test_btn() { |
| 53 | + let button = Btn { |
| 54 | + size: Default::default(), |
| 55 | + color: Default::default(), |
| 56 | + }; |
| 57 | + |
| 58 | + assert_eq!(button.to_class(), "h-9 px-4 py-2 bg-blue-500 text-blue-100"); |
| 59 | + assert_eq!( |
| 60 | + button.with_class("text-lg"), |
| 61 | + "h-9 px-4 py-2 bg-blue-500 text-blue-100 text-lg", |
| 62 | + "append class" |
| 63 | + ); |
| 64 | + assert_eq!( |
| 65 | + button.with_class("text-red-500"), |
| 66 | + "h-9 px-4 py-2 bg-blue-500 text-red-500", |
| 67 | + "color conflict" |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + #[test] |
| 72 | + fn test_btn_no_merge() { |
| 73 | + #[derive(TwClass, Default)] |
| 74 | + #[tw(merger = TailwindJoin)] |
| 75 | + struct Btn { |
| 76 | + size: BtnSize, |
| 77 | + color: BtnColor, |
| 78 | + } |
| 79 | + |
| 80 | + let button = Btn::default(); |
| 81 | + |
| 82 | + assert_eq!(button.to_class(), "h-9 px-4 py-2 bg-blue-500 text-blue-100"); |
| 83 | + assert_eq!( |
| 84 | + button.with_class("h-10"), |
| 85 | + "h-9 px-4 py-2 bg-blue-500 text-blue-100 h-10" |
| 86 | + ) |
| 87 | + } |
| 88 | + |
| 89 | + #[test] |
| 90 | + fn test_class_builder() { |
| 91 | + assert_eq!( |
| 92 | + Btn::builder() |
| 93 | + .size(BtnSize::Sm) |
| 94 | + .color(BtnColor::Red) |
| 95 | + .to_class(), |
| 96 | + "h-8 rounded-md px-3 text-xs bg-red-500 text-red-100" |
| 97 | + ); |
| 98 | + |
| 99 | + assert_eq!( |
| 100 | + Btn::builder() |
| 101 | + .size(BtnSize::Sm) |
| 102 | + .color(BtnColor::Red) |
| 103 | + .with_class("flex"), |
| 104 | + "h-8 rounded-md px-3 text-xs bg-red-500 text-red-100 flex" |
| 105 | + ); |
| 106 | + |
| 107 | + assert_eq!( |
| 108 | + Btn::builder().size(BtnSize::Lg).to_class(), |
| 109 | + "h-10 rounded-lg px-8 bg-blue-500 text-blue-100" |
| 110 | + ); |
| 111 | + |
| 112 | + assert_eq!( |
| 113 | + Btn::builder().to_class(), |
| 114 | + "h-9 px-4 py-2 bg-blue-500 text-blue-100" |
| 115 | + ); |
| 116 | + |
| 117 | + assert_eq!( |
| 118 | + Btn::builder().with_class("grid"), |
| 119 | + "h-9 px-4 py-2 bg-blue-500 text-blue-100 grid" |
| 120 | + ); |
| 121 | + } |
| 122 | + |
| 123 | + #[test] |
| 124 | + fn variant_join() { |
| 125 | + assert_eq!( |
| 126 | + tw_merge!(BtnColor::Blue, "text-lg",), |
| 127 | + "bg-blue-500 text-blue-100 text-lg" |
| 128 | + ); |
| 129 | + } |
| 130 | +} |
0 commit comments