Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/main/frontend/handler/route.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,22 @@
"Import data into Logseq"
"Logseq"))

(defn- get-current-graph-name
[]
(when-let [db (state/get-current-repo)]
(when-let [repo-name (db/get-repo-name db)]
(let [short-repo-name (db/get-short-repo-name repo-name)]
(if (= config/local-repo short-repo-name) "Demo" short-repo-name)))))

(defn update-page-title!
[route]
(let [{:keys [data path-params]} route
title (get-title (:name data) path-params)
hls? (pdf-utils/hls-file? title)]
(util/set-title! (if hls? (pdf-utils/fix-local-asset-pagename title) title))))
title (get-title (:name data) path-params)
hls? (pdf-utils/hls-file? title)
title (if hls? (pdf-utils/fix-local-asset-pagename title) title)
graph-name (get-current-graph-name)
full-title (if graph-name (str title " — " graph-name) title)]
(util/set-title! full-title)))

(defn update-page-label!
[route]
Expand Down
68 changes: 68 additions & 0 deletions src/test/frontend/handler/route_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
(:require [frontend.handler.route :as route-handler]
[frontend.test.helper :as test-helper :refer [load-test-files]]
[frontend.db.utils :as db-utils]
[frontend.state :as state]
[frontend.db :as db]
[frontend.config :as config]
[frontend.util :as util]
[clojure.test :refer [deftest is use-fixtures testing]]))

(use-fixtures :each {:before test-helper/start-test-db!
Expand Down Expand Up @@ -58,3 +62,67 @@ foo:: bar
(is (= {:to :page :path-params {:name "page name"}}
(#'route-handler/default-page-route "Page name"))
"Generates a case insensitive page link")))

(deftest get-current-graph-name-test
(testing "returns nil when there is no current repo"
(with-redefs [state/get-current-repo (constantly nil)]
(is (nil? (#'route-handler/get-current-graph-name))
"Returns nil when no current repo is set")))

(testing "returns 'Demo' when the short repo name is the local-repo value"
(with-redefs [state/get-current-repo (constantly "local")
db/get-repo-name (constantly config/local-repo)
db/get-short-repo-name (constantly config/local-repo)]
(is (= "Demo" (#'route-handler/get-current-graph-name))
"Returns 'Demo' for the local demo repo")))

(testing "returns the short repo name for a regular repo"
(with-redefs [state/get-current-repo (constantly "https://github.com/user/my-notes")
db/get-repo-name (constantly "user/my-notes")
db/get-short-repo-name (constantly "my-notes")]
(is (= "my-notes" (#'route-handler/get-current-graph-name))
"Returns the short repo name for a regular repo")))

(testing "returns nil when get-repo-name returns nil"
(with-redefs [state/get-current-repo (constantly "some-repo")
db/get-repo-name (constantly nil)]
(is (nil? (#'route-handler/get-current-graph-name))
"Returns nil when repo name cannot be determined"))))

(deftest update-page-title-test
(testing "sets title without graph name when no current repo"
(let [captured (atom nil)]
(with-redefs [state/get-current-repo (constantly nil)
util/set-title! (fn [t] (reset! captured t))]
(route-handler/update-page-title! {:data {:name :home} :path-params {}})
(is (= "Logseq" @captured)
"Title is unchanged when there is no current graph"))))

(testing "appends graph name to title when current repo exists"
(let [captured (atom nil)]
(with-redefs [state/get-current-repo (constantly "https://github.com/user/my-notes")
db/get-repo-name (constantly "user/my-notes")
db/get-short-repo-name (constantly "my-notes")
util/set-title! (fn [t] (reset! captured t))]
(route-handler/update-page-title! {:data {:name :home} :path-params {}})
(is (= "Logseq — my-notes" @captured)
"Title includes graph name separated by em dash"))))

(testing "uses 'Demo' label for the local demo repo"
(let [captured (atom nil)]
(with-redefs [state/get-current-repo (constantly config/local-repo)
db/get-repo-name (constantly config/local-repo)
db/get-short-repo-name (constantly config/local-repo)
util/set-title! (fn [t] (reset! captured t))]
(route-handler/update-page-title! {:data {:name :home} :path-params {}})
(is (= "Logseq — Demo" @captured)
"Title uses 'Demo' for the local demo repo"))))

(testing "title has no graph suffix when graph name is nil"
(let [captured (atom nil)]
(with-redefs [state/get-current-repo (constantly "some-repo")
db/get-repo-name (constantly nil)
util/set-title! (fn [t] (reset! captured t))]
(route-handler/update-page-title! {:data {:name :settings} :path-params {}})
(is (= "Settings" @captured)
"Title is unchanged when graph name cannot be determined")))))
Loading