Ver código fonte

enhance(rtc): update testcase

rcmerci 2 anos atrás
pai
commit
af416ec352

+ 5 - 2
src/main/frontend/db/rtc/ws.cljs

@@ -8,6 +8,9 @@
             [cljs.core.async :as async :refer [<! chan go offer!]]
             [frontend.state :as state]))
 
+(def WebSocketOPEN (if (= *target* "nodejs")
+                     1
+                     js/WebSocket.OPEN))
 
 (def ws-addr config/RTC-WS-URL)
 
@@ -24,7 +27,7 @@
 
 (defn send!
   [ws message]
-  (assert (= js/WebSocket.OPEN (.-readyState ws)))
+  (assert (= WebSocketOPEN (.-readyState ws)))
   (let [decoded-message (rtc-const/data-to-ws-decoder message)]
     (assert (rtc-const/data-to-ws-validator decoded-message) message)
     (.send ws (js/JSON.stringify (clj->js (rtc-const/data-to-ws-encoder decoded-message))))))
@@ -37,7 +40,7 @@
   (go
     (let [ws @(:*ws state)]
       (when (or (nil? ws)
-                (> (.-readyState ws) js/WebSocket.OPEN))
+                (> (.-readyState ws) WebSocketOPEN))
         (let [ws-opened-ch (chan)
               token (state/get-auth-id-token)
               ws* (ws-listen token (:data-from-ws-chan state) ws-opened-ch)]

+ 6 - 5
src/test/frontend/db/rtc/mock.cljs

@@ -1,6 +1,7 @@
 (ns frontend.db.rtc.mock
   (:require [clojure.core.async :as async]
-            [frontend.db.rtc.const :as rtc-const]))
+            [frontend.db.rtc.const :as rtc-const]
+            [spy.core :as spy]))
 
 ;;; websocket
 (defrecord Mock-WebSocket [onopen onmessage onclose onerror readyState push-data-chan ^:mutable push-data-fn]
@@ -13,7 +14,6 @@
                   js/JSON.parse
                   (js->clj :keywordize-keys true)
                   rtc-const/data-to-ws-decoder)]
-      (prn :got-ws-msg msg)
       (async/put! onmessage msg)))
 
   (set-push-data-fn [_ f]
@@ -32,7 +32,8 @@
 (defn mock-websocket
   [data-from-ws-chan]
   (let [stop-push-data-loop-ch (async/chan)
-        ws (->Mock-WebSocket nil (async/chan 10) nil nil js/WebSocket.OPEN data-from-ws-chan default-push-data-fn)]
+        ws (->Mock-WebSocket nil (async/chan 10) nil nil 1
+                             data-from-ws-chan (spy/spy default-push-data-fn))]
     (async/go-loop []
       (let [{:keys [stop msg]}
             (async/alt!
@@ -43,8 +44,8 @@
           (do (prn :mock-ws-loop-stop) nil)
 
           msg
-          (do (when-let [push-data-fn (.-push-data-fn ws)]
-                (push-data-fn msg (.-push-data-chan ws)))
+          (do (when-let [push-data-fn (:push-data-fn ws)]
+                (push-data-fn msg (:push-data-chan ws)))
               (recur)))))
     ws))
 

+ 7 - 7
src/test/frontend/db/rtc_test.cljs

@@ -1,15 +1,15 @@
 (ns frontend.db.rtc-test
-  #_ :clj-kondo/ignore
   (:require [clojure.test :as t :refer [deftest is use-fixtures]]
             [frontend.db.rtc.fixture :as rtc-fixture]
-            [frontend.test.helper :as test-helper]))
+            [frontend.test.helper :as test-helper]
+            [spy.core :as spy]))
 
 (use-fixtures :each
   test-helper/start-and-destroy-db-map-fixture
   rtc-fixture/start-and-stop-rtc-loop-fixture)
 
-
-;; FIXME: Re-enable when test doesn't fail test suite hard
-#_(deftest rtc-loop-test
-    (prn :*test-rtc-state @rtc-fixture/*test-rtc-state)
-    (is true))
+(deftest rtc-loop-init-test
+  (let [ws @(:*ws @rtc-fixture/*test-rtc-state)
+        push-data-fn (:push-data-fn ws)
+        last-ws-msg (first (spy/last-call push-data-fn))]
+    (is (= "register-graph-updates" (:action last-ws-msg)))))