From 146f2b1c8e0563bd288795851beae8b985cacac7 Mon Sep 17 00:00:00 2001 From: Jacob O'Bryant Date: Mon, 15 Apr 2024 18:54:12 -0700 Subject: [PATCH] Add more detailed tests to the starter app --- starter/test/com/example_test.clj | 42 ++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/starter/test/com/example_test.clj b/starter/test/com/example_test.clj index d017b6b3..77a7bdea 100644 --- a/starter/test/com/example_test.clj +++ b/starter/test/com/example_test.clj @@ -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))))))