Skip to content

Commit

Permalink
Add more detailed tests to the starter app
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobobryant committed Apr 16, 2024
1 parent 1d92648 commit 146f2b1
Showing 1 changed file with 41 additions and 1 deletion.
42 changes: 41 additions & 1 deletion starter/test/com/example_test.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,46 @@
(ns com.example-test
;; If you add more test files, require them here so that they'll get loaded by com.example/on-save
(:require [clojure.test :refer [deftest is]]))
(:require [cheshire.core :as cheshire]
[clojure.string :as str]
[clojure.test :refer [deftest is]]
[com.biffweb :as biff :refer [test-xtdb-node]]
[com.example :as main]
[com.example.app :as app]
[malli.generator :as mg]
[rum.core :as rum]
[xtdb.api :as xt]))

(deftest example-test
(is (= 4 (+ 2 2))))

(defn get-context [node]
{:biff.xtdb/node node
:biff/db (xt/db node)
:biff/malli-opts #'main/malli-opts})

(deftest send-message-test
(with-open [node (test-xtdb-node [])]
(let [message (mg/generate :string)
user (mg/generate :user main/malli-opts)
ctx (assoc (get-context node) :session {:uid (:xt/id user)})
_ (app/send-message ctx {:text (cheshire/generate-string {:text message})})
db (xt/db node) ; get a fresh db value so it contains any transactions
; that send-message submitted.
doc (biff/lookup db :msg/text message)]
(is (some? doc))
(is (= (:msg/user doc) (:xt/id user))))))

(deftest chat-test
(let [n-messages (+ 3 (rand-int 10))
now (java.util.Date.)
messages (for [doc (mg/sample :msg (assoc main/malli-opts :size n-messages))]
(assoc doc :msg/sent-at now))]
(with-open [node (test-xtdb-node messages)]
(let [response (app/chat {:biff/db (xt/db node)})
html (rum/render-html response)]
(is (str/includes? html "Messages sent in the past 10 minutes:"))
(is (not (str/includes? html "No messages yet.")))
;; If you add Jsoup to your dependencies, you can use DOM selectors instead of just regexes:
;(is (= n-messages (count (.select (Jsoup/parse html) "#messages > *"))))
(is (= n-messages (count (re-seq #"init send newMessage to #message-header" html))))
(is (every? #(str/includes? html (:msg/text %)) messages))))))

0 comments on commit 146f2b1

Please sign in to comment.