react_test.cljs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. (ns frontend.react-test
  2. (:require [frontend.react :as r]
  3. [cljs.test :refer [deftest is are testing use-fixtures run-tests]]
  4. [frontend.fixtures :as fixtures]))
  5. (use-fixtures :each
  6. fixtures/react-components
  7. fixtures/react-impl)
  8. (deftest simple-react-test
  9. (let [react-ref (atom 1)]
  10. (r/defc simple-component
  11. []
  12. (+ 2 (r/react react-ref)))
  13. (let [result (r/with-key 1 (simple-component))]
  14. (is (= 3 @result))
  15. (reset! react-ref 2)
  16. (is (= 4 @result)))))
  17. (deftest nest-component-test
  18. (let [a (atom 1)
  19. b (atom 2)]
  20. (r/defc inner
  21. []
  22. (let [r (r/react b)]
  23. r))
  24. (r/defc out
  25. []
  26. (let [out (r/react a)
  27. inner-result (r/with-key "1" (inner))]
  28. (+ out @inner-result)))
  29. (let [out-result (r/with-key "2" (out))]
  30. (is (= 3 @out-result))
  31. (reset! b 4)
  32. (is (= 5 @out-result)))))
  33. #_(deftest defc-params-test
  34. (let [a (atom 1)
  35. b (atom 2)]
  36. (r/defc inner-1
  37. [c]
  38. (+ c (r/react b)))
  39. (r/defc out-1
  40. []
  41. (let [out (r/react a)
  42. inner-result (r/with-key 1 (inner-1 5))]
  43. (+ out @inner-result)))
  44. (let [out-result (r/with-key 2 (out-1))]
  45. (is (= 8 @out-result))
  46. (reset! b 4)
  47. (is (= 10 @out-result)))))