Skip to content

Commit

Permalink
Some proper ClojureScript unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
heralden committed Oct 16, 2019
1 parent fe3f765 commit e4234ca
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 6 deletions.
3 changes: 2 additions & 1 deletion project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
[binaryage/oops "0.7.0"]
[inflections "0.13.2"]
[criterium "0.4.5"]
[org.intermine/imcljs "1.0.1"]]
[org.intermine/imcljs "1.0.1"]
[day8.re-frame/test "0.1.5"]]

:plugins [[lein-cljsbuild "1.1.7"]
[lein-less "1.7.5"]
Expand Down
48 changes: 44 additions & 4 deletions test/cljs/im_tables/core_test.cljs
Original file line number Diff line number Diff line change
@@ -1,6 +1,46 @@
(ns im-tables.core-test
(:require [cljs.test :refer-macros [deftest testing is]]))
(:require [cljs.test :refer-macros [deftest testing is use-fixtures]]
[im-tables.test-utils :as utils]
[re-frame.core :as rf]
[day8.re-frame.test :refer-macros [run-test-sync run-test-async wait-for]]
[im-tables.events]
[im-tables.subs]))

(deftest fake-test
(testing "fake description"
(is (= 1 1))))
(use-fixtures :each utils/fixtures)

(def im-config {:service {:root "beta.humanmine.org/beta"}
:query {:from "Gene"
:select ["symbol"
"secondaryIdentifier"
"dataSets.description"
"primaryIdentifier"
"organism.name"
"dataSets.name"]}
:settings {:pagination {:limit 10}
:links {:vocab {:mine "BananaMine"}
:url (fn [vocab] (str "#/reportpage/"
(:mine vocab) "/"
(:class vocab) "/"
(:id vocab)))}}})

(deftest load-im-tables
(run-test-async
(let [loc [:default]]
(rf/dispatch-sync [:im-tables/load loc im-config])
(wait-for [:main/initial-query-response]
(testing "im-table runs query"
(let [response @(rf/subscribe [:main/query-response loc])]
(is (some? response))))))))

(deftest column-summary
(run-test-async
(let [loc [:default]]
(rf/dispatch-sync [:im-tables/load loc im-config])
(wait-for [:main/initial-query-response]
(rf/dispatch-sync [:im-tables.main/init loc])
(wait-for [(utils/match-times {:main/save-column-summary 6
:main/save-decon-count 3})]
(testing "at least one non-empty column summary"
(let [summaries @(rf/subscribe [:summaries/column-summaries loc])]
(is (some (every-pred map? not-empty)
(map :response (vals summaries)))))))))))
62 changes: 62 additions & 0 deletions test/cljs/im_tables/test_utils.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
(ns im-tables.test-utils
(:require [cljs.core.async :refer [chan put!]]
[re-frame.core :as rf]))

;; Tips for writing tests:
;; https://github.com/intermine/bluegenes/blob/dev/docs/developing.md

;; Remember to activate the fixtures in your testing namespace if you're
;; going to use exports from this namespace:
;;
;; (use-fixtures :each utils/fixtures)

(defn stub-fetch-fn
"We often want to stub imcljs.fetch functions using with-redefs. Instead of
having to define a function to create, put and return a channel, call this
function with the value you wish returned and it will do it for you."
[v]
(fn [& _]
(let [c (chan 1)]
(put! c v)
c)))

(def stubbed-variables
"Add functions that reset any globally stubbed variables to this atom.
(swap! stubbed-variables conj #(set! fetch/session orig-fn))
A fixture will run all these functions and empty the atom before tests."
(atom '()))

;; This will be used to clear app-db between test runs.
(rf/reg-event-db
:clear-db
(fn [_db] {}))

(def fixtures
"Necessary fixtures to use the exports from this namespace.
Use by calling use-fixtures from your testing namespace:
(use-fixtures :each utils/fixtures)"
{:before (fn []
(when-let [vars (seq @stubbed-variables)]
(doseq [restore-fn vars]
(restore-fn))
(reset! stubbed-variables '()))
(rf/dispatch-sync [:clear-db]))})

(defn- events-matched?
[matches matchm]
(every? (fn [[event-id times]]
(= (get matches event-id) times))
matchm))

(defn match-times
"For use inside `(day8.re-frame.test/wait-for [(match-times matchm)])`.
Expects an event-id matching count map as argument:
{:main/save-column-summary 6
:main/save-decon-count 3}
Waits until all event-id keys in the map have occurred the exact amount of
times specified as the value. Useful when you know a chain dispatch can fire
an X amount of requests, and you just have to wait until they all finish."
[matchm]
(let [matches (atom nil)]
(fn [[event-id]]
(events-matched? (swap! matches update event-id (fnil inc 0)) matchm))))
2 changes: 1 addition & 1 deletion tests.edn
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#kaocha/v1
{:tests [{:id :unit-cljs
:type :kaocha.type/cljs
:cljs/repl-env cljs.repl.node/repl-env
:cljs/repl-env cljs.repl.browser/repl-env
:cljs/timeout 60000}]}

0 comments on commit e4234ca

Please sign in to comment.