I am trying to write a test like this:
(deftest login-form-rendering
(async done (with-redefs [http/post fake-login-success]
(with-mounted-component (c/login-form login!)
(fn [c div]
(.click (.getElementById js/document "Login"))
(is (= (:page @app-state) :location)))))
(done)))
I have this mock:
(defn fake-login-success [& _]
(let [ch (chan)
response {:status 200}]
(go (>! ch response)
ch)))
The login function does this:
(defn login! [username password]
(go (let [result (->> {:form-params {:username @username :password @password}}
(http/post "/api/login")
(<!))
{status :status body :body} result]
(case status
401 (swap! app-state assoc :page :bad-login)
200 (swap! app-state assoc :page :location)))))
The login form is a reagent component that takes a callback for onClick. app-state is a globally accessible atom.
The problem I'm facing is that the go block inside login! is never executed. Is there something I need to do to flush the channel or something?
I see there is also this unanswered question that's similar: Testing core.async code in ClojureScript. One difference seems to be that I don't have an explicit channel in my code under test. That's being generated by the cljs-http post.