From bde62473a4fc26dda622cc5a5a1a9dba359be796 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Moya?= Date: Fri, 15 Jan 2021 10:52:17 +0100 Subject: [PATCH] :heavy_check_mark: Allow for tests of data module at frontend --- common/app/common/uuid_impl.js | 3 +- frontend/src/app/config.cljs | 3 +- frontend/src/app/main/streams.cljs | 5 +- frontend/src/app/util/globals.js | 42 ++++++++++++++++ frontend/tests/app/test_library_sync.cljs | 61 +++++++++++++++++++++++ 5 files changed, 110 insertions(+), 4 deletions(-) create mode 100644 frontend/src/app/util/globals.js create mode 100644 frontend/tests/app/test_library_sync.cljs diff --git a/common/app/common/uuid_impl.js b/common/app/common/uuid_impl.js index 951557932..40c944f10 100644 --- a/common/app/common/uuid_impl.js +++ b/common/app/common/uuid_impl.js @@ -31,7 +31,8 @@ goog.scope(function() { } else if (typeof require === "function") { const crypto = require("crypto"); return (buf) => { - crypto.getRandomValues(buf); + const bytes = crypto.randomBytes(buf.length); + buf.set(bytes) return buf; }; } else { diff --git a/frontend/src/app/config.cljs b/frontend/src/app/config.cljs index 3bd23d011..6f0eb2591 100644 --- a/frontend/src/app/config.cljs +++ b/frontend/src/app/config.cljs @@ -13,6 +13,7 @@ [app.common.data :as d] [app.common.spec :as us] [app.common.version :as v] + [app.util.globals :as globals] [app.util.object :as obj] [app.util.dom :as dom] [app.util.avatars :as avatars] @@ -73,7 +74,7 @@ (def login-with-ldap (obj/get global "appLoginWithLDAP" false)) (def worker-uri (obj/get global "appWorkerURI" "/js/worker.js")) (def public-uri (or (obj/get global "appPublicURI") - (.-origin ^js js/location))) + (.-origin ^js globals/location))) (def media-uri (str public-uri "/assets")) (def version (delay (parse-version global))) (def target (delay (parse-target global))) diff --git a/frontend/src/app/main/streams.cljs b/frontend/src/app/main/streams.cljs index 54b633d0e..b8a8e952a 100644 --- a/frontend/src/app/main/streams.cljs +++ b/frontend/src/app/main/streams.cljs @@ -10,7 +10,8 @@ [beicon.core :as rx] [app.main.store :as st] [app.main.refs :as refs] - [app.common.geom.point :as gpt])) + [app.common.geom.point :as gpt] + [app.util.globals :as globals])) ;; --- User Events @@ -103,7 +104,7 @@ (defonce window-blur - (->> (rx/from-event js/window "blur") + (->> (rx/from-event globals/window "blur") (rx/share))) (defonce keyboard-alt diff --git a/frontend/src/app/util/globals.js b/frontend/src/app/util/globals.js new file mode 100644 index 000000000..a4f5577f3 --- /dev/null +++ b/frontend/src/app/util/globals.js @@ -0,0 +1,42 @@ +/** + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This Source Code Form is "Incompatible With Secondary Licenses", as + * defined by the Mozilla Public License, v. 2.0. + * + * Copyright (c) 2020 UXBOX Labs SL + */ + +/* + * Provide instances of some javascript global objects, by looking for + * them in the browser, the current worker or creating a dummy for test + * environment. + */ + +"use strict"; + +goog.provide("app.util.globals"); + +goog.scope(function() { + var win; + if (typeof goog.global.window !== "undefined") { + win = goog.global.window; + } else { + win = {}; + } + + app.util.globals.window = win; + + var loc; + if (typeof goog.global.location !== "undefined") { + loc = goog.global.location; + } else { + loc = {}; + } + + app.util.globals.location = loc; +}); + diff --git a/frontend/tests/app/test_library_sync.cljs b/frontend/tests/app/test_library_sync.cljs new file mode 100644 index 000000000..ec9ba9aeb --- /dev/null +++ b/frontend/tests/app/test_library_sync.cljs @@ -0,0 +1,61 @@ +(ns app.test-library-sync + (:require [cljs.test :as t :include-macros true] + [cljs.pprint :refer [pprint]] + [beicon.core :as rx] + [potok.core :as ptk] + [app.main.data.workspace.libraries :as dwl])) + +;; ---- Helpers + +(defn do-update + [state event cb] + (let [new-state (ptk/update event state)] + (cb new-state))) + +(defn do-watch + [state event cb] + (->> (ptk/watch event state nil) + (rx/reduce conj []) + (rx/subs cb))) + +(defn do-watch-update + [state event & cbs] + (do-watch state event + (fn [events] + (t/is (= (count events) (count cbs))) + (reduce + (fn [new-state [event cb]] + (do-update new-state event cb)) + state + (map list events cbs))))) + +;; ---- Tests + +(t/deftest synctest + (t/testing "synctest" + (let [state {:workspace-local {:color-for-rename "something"}}] + (do-update + state + dwl/clear-color-for-rename + (fn [new-state] + (t/is (= (get-in new-state [:workspace-local :color-for-rename]) + nil))))))) + +(t/deftest asynctest + (t/testing "asynctest" + (t/async done + (let [state {} + color {:color "#ffffff"}] + (do-watch-update + state + (dwl/add-recent-color color) + (fn [new-state] + (t/is (= (get-in new-state [:workspace-file + :data + :recent-colors]) + [color])) + (t/is (= (get-in new-state [:workspace-data + :recent-colors]) + [color])) + (done))))))) +