Skip to content

Commit

Permalink
Fix and test for #39
Browse files Browse the repository at this point in the history
  • Loading branch information
oliyh committed Mar 1, 2019
1 parent 1d6a407 commit 2d3a0e8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/re_graph/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
(re-frame/reg-event-fx
::query
interceptors
(fn [{:keys [db]} [query variables callback-event :as event]]
(fn [{:keys [db dispatchable-event]} [query variables callback-event :as event]]
(let [query (str "query " (string/replace query #"^query\s?" ""))]
(cond
(get-in db [:websocket :ready?])
Expand All @@ -51,7 +51,7 @@
:variables variables}}]})

(get-in db [:websocket])
{:db (update-in db [:websocket :queue] conj event)}
{:db (update-in db [:websocket :queue] conj dispatchable-event)}

:else
{::internals/send-http [(:http-url db)
Expand Down
17 changes: 13 additions & 4 deletions test/re_graph/core_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,9 @@
(testing "messages are queued when websocket isn't ready"

(dispatch [::re-graph/subscribe :my-sub "{ things { id } }" {:some "variable"} [::on-thing]])
(dispatch [::re-graph/query "{ more_things { id } }" {:some "other-variable"} [::on-thing]])

(is (= 1 (count (get-in (db-instance) [:websocket :queue]))))
(is (= 2 (count (get-in (db-instance) [:websocket :queue]))))

(testing "and sent when websocket opens"

Expand All @@ -141,7 +142,13 @@
(first @ws-messages))))

(is (= [::websocket-connection expected-subscription-payload]
(second @ws-messages))))
(second @ws-messages)))

(is (= [::websocket-connection {:type "start",
:payload
{:query "query { more_things { id } }",
:variables {:some "other-variable"}}}]
((juxt first (comp #(dissoc % :id) second)) (last @ws-messages)))))

(is (empty? (get-in (db-instance) [:websocket :queue]))))))

Expand All @@ -151,8 +158,10 @@
::internals/send-ws
(fn [[ws payload]]
(is (= ::websocket-connection ws))
(is (= {:id "my-sub" :type "stop"}
payload)))))
(is (or (= {:id "my-sub" :type "stop"}
payload)
(= {:type "stop"}
(dissoc payload :id)))))))

(testing "the websocket is closed"
(re-frame/reg-fx
Expand Down

0 comments on commit 2d3a0e8

Please sign in to comment.