test: enable Node-test on namespaces with dependency on hickory

This commit is contained in:
Junyi Du
2022-10-18 13:09:26 +08:00
committed by Tienson Qin
parent 94e099ad5d
commit 5937082b49
6 changed files with 96 additions and 44 deletions

View File

@@ -0,0 +1,12 @@
(ns frontend.extensions.hickory
"A shim for conditional reading of the hickory lib in Node,
which requires DOM"
(:require #?(:node-test [lambdaisland.glogi :as log]
:default [hickory.core :as hickory])))
#?(:node-test (defn html->hiccup
[html]
(log/error :exception "Calling hickory from Node test environment is not expected!"))
:default (defn html->hiccup
[html]
(hickory/as-hiccup (hickory/parse html))))

View File

@@ -4,7 +4,7 @@
[clojure.walk :as walk]
[frontend.config :as config]
[frontend.util :as util]
[hickory.core :as hickory]))
[frontend.extensions.hickory :as hickory]))
(defonce *inside-pre? (atom false))
(defn- hiccup-without-style
@@ -273,7 +273,7 @@
(defn convert
[format html]
(when-not (string/blank? html)
(let [hiccup (hickory/as-hiccup (hickory/parse html))
(let [hiccup (hickory/html->hiccup html)
decoded-hiccup (html-decode-hiccup hiccup)]
(hiccup->doc format decoded-hiccup))))

View File

@@ -60,6 +60,8 @@
nil)))
(defn- try-parse-as-json
"Result is not only to be an Object.
Maybe JSON types like string, number, boolean, null, array"
[text]
(try (js/JSON.parse text)
(catch :default _ #js{})))

View File

@@ -1,39 +1,41 @@
;; FIXME
;; https://github.com/davidsantiago/hickory/issues/17
;; hictory doesnt work in Node
;; (ns frontend.extensions.zotero.extractor-test
;; (:require [clojure.edn :as edn]
;; [clojure.test :as test :refer [deftest is testing]]
;; [shadow.resource :as rc]
;; [clojure.string :as str]
;; [frontend.extensions.zotero.extractor :as extractor]))
;; 2022.10.17 Fixed via frontend.extensions.hickory
;; TODO fix the remaining cases
(ns frontend.extensions.zotero.extractor-test
(:require [clojure.edn :as edn]
[clojure.test :as test :refer [deftest is testing]]
[shadow.resource :as rc]
;; [clojure.string :as str]
[frontend.extensions.zotero.extractor :as extractor]))
;; (def data
;; (-> (rc/inline "fixtures/zotero.edn")
;; (edn/read-string)))
(def data
(-> (rc/inline "fixtures/zotero.edn")
(edn/read-string)))
;; (deftest extract-test
;; (testing "journal article"
;; (let [{:keys [page-name properties]}
;; (extractor/extract (:journal-article-sample-1 data))]
(deftest extract-test
(testing "journal article"
(let [{:keys [page-name properties]}
(extractor/extract (:journal-article-sample-1 data))]
;; (testing "page name prefer citation key"
;; (is (= "@efroniHowCombineTreeSearch2019" page-name)))
(testing "page name prefer citation key"
(is (= "@efroniHowCombineTreeSearch2019" page-name)))
;; (testing "convert date"
;; (is (= "[[Feb 17th, 2019]]" (-> properties :date))))
(testing "convert date"
(is (= "[[Feb 17th, 2019]]" (-> properties :date))))
;; (testing "convert date"
;; (is (= "[[Feb 17th, 2019]]" (-> properties :date))))
(testing "convert date"
(is (= "[[Feb 17th, 2019]]" (-> properties :date))))
;; (testing "original title"
;; (is (= "How to Combine Tree-Search Methods in Reinforcement Learning" (-> properties :original-title))))
(testing "original title"
(is (= "How to Combine Tree-Search Methods in Reinforcement Learning" (-> properties :original-title))))
;; (testing "double quote when containing comma"
;; (is (= "\"arXiv:1809.01843 [cs, stat]\"" (-> properties :publication-title))))
(testing "double quote when containing comma"
(is (= "\"arXiv:1809.01843 [cs, stat]\"" (-> properties :publication-title))))
;; (testing "skip when containing newline"
;; (is (nil? (-> properties :extra))))))
(testing "skip when containing newline"
(is (nil? (-> properties :extra))))))
;; (testing "another journal article"
;; (let [{:keys [page-name properties]}
@@ -47,27 +49,29 @@
;; (testing "tags"
;; (is (= 17 tags)))))
;; (testing "book"
;; (let [{:keys [page-name properties]}
;; (extractor/extract (:book-sample-1 data))]
(testing "book"
(let [{:keys [page-name properties]}
(extractor/extract (:book-sample-1 data))]
;; (testing "page name"
;; (is (= "@1984" page-name)))
(testing "page name"
(is (= "@1984" page-name)))
;; (testing "author"
;; (is (= "[[George Orwell]]" (-> properties :authors))))
(testing "author"
(is (= '("George Orwell") (-> properties :authors))))
;; (testing "preserve unparsable date"
;; (is (= "1984" (-> properties :date))))))
(testing "preserve unparsable date"
(is (= "1984" (-> properties :date))))))
;; (testing "newpaper article"
;; (let [{:keys [page-name properties]}
;; (extractor/extract (:newspaper-article-sample-1 data))]
;; (is (= "A Letter to Our Readers About Digital Subscriptions" (-> properties :original-title)))
(testing "newpaper article"
(let [{:keys [_page-name properties]}
(extractor/extract (:newspaper-article-sample-1 data))]
(is (= "A Letter to Our Readers About Digital Subscriptions" (-> properties :original-title)))
;; (testing "use parsed date when possible"
;; (is (= "[[Mar 28th, 2011]]" (-> properties :date))))))
(testing "use parsed date when possible"
(is (= "[[Mar 28th, 2011]]" (-> properties :date))))))
;; 2022.10.18. Should be deprecated since Hickory is invalid in Node test
;; (testing "note"
;; (let [result (extractor/extract (:note-sample-1 data))]
;; (is (str/starts-with? result "This study shows")))))
;; (is (str/starts-with? result "This study shows"))))
)

View File

@@ -0,0 +1,32 @@
(ns frontend.handler.paste-test
(:require [cljs.test :refer [deftest are]]
[goog.object :as gobj]
[frontend.handler.paste :as paste-handler]))
(deftest try-parse-as-json-result-parse-test
(are [x y] (let [result (#'paste-handler/try-parse-as-json x)
obj-result (if (object? result) result #js{})]
(gobj/get obj-result "foo") ;; This op shouldn't throw
(gobj/getValueByKeys obj-result "foo" "bar") ;; This op shouldn't throw
(gobj/equals result y))
"{\"number\": 1234}" #js{:number 1234}
"1234" 1234
"null" nil
"true" true
"[1234, 5678]" #js[1234 5678]
;; invalid JSON
"{number: 1234}" #js{}))
(deftest try-parse-as-json-result-get-test
(are [x y z] (let [result (#'paste-handler/try-parse-as-json x)
obj-result (if (object? result) result #js{})]
(and (gobj/equals (gobj/get obj-result "foo") y)
(gobj/equals (gobj/getValueByKeys obj-result "foo" "bar") z)))
"{\"foo\": {\"bar\": 1234}}" #js{:bar 1234} 1234
"{\"number\": 1234}" nil nil
"1234" nil nil
"null" nil nil
"true" nil nil
"[{\"number\": 1234}]" nil nil
;; invalid JSON
"{number: 1234}" nil nil))