-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Some proper ClojureScript unit tests
- Loading branch information
Showing
4 changed files
with
109 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))))))))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}]} |