/test/unit/restql/hooks/core_test.clj

https://github.com/B2W-BIT/restQL-core · Clojure · 53 lines · 46 code · 7 blank · 0 comment · 9 complexity · 3361417cfda834c7eff45d9d3c540653 MD5 · raw file

  1. (ns restql.hooks.core-test
  2. (:require [clojure.test :refer :all]
  3. [restql.hooks.core :as hooks]))
  4. (deftest testing-register-hooks
  5. (testing "Register :testing hook"
  6. (hooks/register-hook :testing [1])
  7. (is (= [1] (:testing @hooks/hook-store)))
  8. (reset! hooks/hook-store {})))
  9. (deftest testing-execute-hook
  10. (testing "Execute simple hook fn"
  11. (let [cnt (atom 0)
  12. _ (hooks/register-hook :testing [(fn [ctx] (swap! cnt inc))])
  13. ctx (hooks/execute-hook :testing)]
  14. (is (= 1 @cnt))
  15. (is (= {} ctx))
  16. (reset! hooks/hook-store {})))
  17. (testing "Execute hook with context"
  18. (let [_ (hooks/register-hook :testing [(fn [ctx] (->> ctx :a inc (assoc {} :b)))])
  19. ctx (hooks/execute-hook :testing {:a 1})]
  20. (is (= {:b 2} ctx))
  21. (reset! hooks/hook-store {})))
  22. (testing "Execute 2 hooks with context"
  23. (let [_ (hooks/register-hook :testing [(fn [ctx] (->> ctx :a inc (assoc {} :b)))
  24. (fn [ctx] (->> ctx :a inc inc (assoc {} :c)))])
  25. ctx (hooks/execute-hook :testing {:a 1})]
  26. (is (= {:b 2 :c 3} ctx))
  27. (reset! hooks/hook-store {}))))
  28. (deftest testing-execute-hook-pipeline
  29. (testing "Execute simple hook fn"
  30. (let [cnt (atom 0)
  31. _ (hooks/register-hook :testing [(fn [ctx] (swap! cnt inc))])
  32. ctx (hooks/execute-hook-pipeline :testing)]
  33. (is (= 1 @cnt))
  34. (is (= 1 ctx))
  35. (reset! hooks/hook-store {})))
  36. (testing "Execute simple hook with initial value"
  37. (let [_ (hooks/register-hook :testing [(fn [v] (+ v 1))])
  38. ctx (hooks/execute-hook-pipeline :testing 1)]
  39. (is (= 2 ctx))
  40. (reset! hooks/hook-store {})))
  41. (testing "Execute multiple hooks with initial value"
  42. (let [_ (hooks/register-hook :testing [(fn [v] (+ v 1))
  43. (fn [v] (+ v 3))])
  44. ctx (hooks/execute-hook-pipeline :testing 1)]
  45. (is (= 5 ctx))
  46. (reset! hooks/hook-store {}))))