Skip to content

Commit

Permalink
Fix & test viewer width assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
mk committed Feb 9, 2022
1 parent 08584f2 commit cd600ea
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
10 changes: 5 additions & 5 deletions src/nextjournal/clerk/viewer.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -280,12 +280,12 @@
(declare wrapped-with-viewer)

(defn apply-viewer [viewers {:as viewer :keys [render-fn transform-fn]} v]
(let [v (if transform-fn
(-> v value transform-fn)
v)]
(let [v' (if transform-fn
(-> v value transform-fn)
v)]
(if (and transform-fn (not render-fn))
(wrapped-with-viewer (value v) viewers)
(wrap-value v viewer))))
(wrapped-with-viewer (value v') viewers)
(merge (wrap-value v' viewer) (when (map? v) (select-keys v [:nextjournal/width]))))))

(defn wrapped-with-viewer
([x] (wrapped-with-viewer x default-viewers))
Expand Down
21 changes: 21 additions & 0 deletions test/nextjournal/clerk/viewer_test.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(ns nextjournal.clerk.viewer-test
(:require [clojure.string :as str]
[clojure.test :refer :all]
[matcher-combinators.test :refer [match?]]
[nextjournal.clerk.viewer :as v]))

(defn find-elision [desc]
Expand Down Expand Up @@ -43,6 +44,26 @@
(let [value (reduce (fn [acc i] (vector i acc (inc i))) :fin (range 10 0 -1))]
(is (= value (describe+fetch value))))))

(deftest wrapped-with-viewer
(testing "selects number viewer"
(is (match? {:nextjournal/value 42
:nextjournal/viewer {:pred fn?}}
(v/wrapped-with-viewer 42))))

(testing "html viewer has no default width"
(is (nil? (:nextjournal/width (v/wrapped-with-viewer (v/html [:h1 "hi"]))))))

(testing "hiccup viewer width can be overriden"
(is (= :wide
(:nextjournal/width (v/wrapped-with-viewer (v/html {:nextjournal.clerk/width :wide} [:h1 "hi"]))))))

(testing "table viewer defaults to wide width"
(is (= :wide
(:nextjournal/width (v/wrapped-with-viewer (v/table {:a [1] :b [2] :c [3]}))))))
(testing "table viewer (with :transform-fn) width can be overriden"
(is (= :full
(:nextjournal/width (v/wrapped-with-viewer (v/table {:nextjournal.clerk/width :full} {:a [1] :b [2] :c [3]})))))))

(deftest assign-closing-parens
(testing "closing parenthesis are moved to right-most children in the tree"
(let [before (v/describe {:a [1 '(2 3 #{4})]
Expand Down
7 changes: 5 additions & 2 deletions test/nextjournal/clerk_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,14 @@

(deftest eval-string+doc->viewer
(testing "assigns correct width from viewer function opts"
(is (match? [{:nextjournal/width :full}]
(is (match? [{:nextjournal/width :wide}
{:nextjournal/width :full}]
(-> "^{:nextjournal.clerk/visibility :hide} (ns clerk-test-width
(:require [nextjournal.clerk :as clerk]))
(clerk/html {::clerk/width :full} [:div.bg-red-200 [:h1 \"Wide Hiccup\"]])"
(clerk/html {::clerk/width :wide} [:div.bg-red-200 [:h1 \"Wide Hiccup\"]])
(clerk/table {::clerk/width :full} {:a [1] :b [2] :c [3]})"
clerk/eval-string
view/doc->viewer
:nextjournal/value
Expand Down

0 comments on commit cd600ea

Please sign in to comment.