Skip to content

Commit

Permalink
✅ Printout page tests
Browse files Browse the repository at this point in the history
Why:
- Continuing the migration from SPA to SSR.
  • Loading branch information
luontola committed Jul 10, 2024
1 parent e3ad0fc commit 7ba368a
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 6 deletions.
6 changes: 4 additions & 2 deletions src/territory_bro/ui/printouts_page.clj
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
[territory-bro.ui.map-interaction-help :as map-interaction-help]
[territory-bro.ui.printout-templates :as printout-templates])
(:import (io.nayuki.qrcodegen QrCode QrCode$Ecc)
(java.time Duration LocalDate ZoneId)
(java.time Clock Duration LocalDate)
(net.greypanther.natsort CaseInsensitiveSimpleNaturalComparator)
(territory_bro QrCodeGenerator)))

(def ^:dynamic ^Clock *clock* (Clock/systemUTC))

(def templates
[{:id "TerritoryCard"
:fn printout-templates/territory-card
Expand Down Expand Up @@ -88,7 +90,7 @@
template (->> templates
(filter #(= (:template form) (:id %)))
(first))
print-date (LocalDate/now ^ZoneId (:timezone congregation))]
print-date (LocalDate/now (.withZone *clock* (:timezone congregation)))]
(h/html
[:div.no-print
[:h1 (i18n/t "PrintoutPage.title")]
Expand Down
3 changes: 2 additions & 1 deletion test/territory_bro/domain/testdata.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
;; The license text is at http://www.apache.org/licenses/LICENSE-2.0

(ns territory-bro.domain.testdata
(:require [clojure.test :refer :all]))
(:import (java.time ZoneId)))

(def ^ZoneId timezone-helsinki (ZoneId/of "Europe/Helsinki"))
(def ^String wkt-helsinki "MULTIPOLYGON(((25.16935159999999883 59.94343860000000035, 24.9423060999999997 59.92248599999999925, 24.83473199999999892 60.25849960000000038, 25.25289319999999904 60.29745340000000198, 25.16935159999999883 59.94343860000000035)))")
(def ^String wkt-south-helsinki "MULTIPOLYGON(((24.78280260000000013 60.0999604000000005, 24.93151419999999874 60.19257259999999832, 25.02800410000000042 60.13760800000000017, 24.85113349999999954 60.02417539999999718, 24.78280260000000013 60.0999604000000005)))")
(def ^String wkt-helsinki-rautatientori "MULTIPOLYGON(((24.94342665377167 60.1718191832177,24.943515229701223 60.17066264744194,24.94459290351074 60.17068467707519,24.94448956492627 60.171837540600144,24.94342665377167 60.1718191832177)))")
Expand Down
62 changes: 62 additions & 0 deletions test/territory_bro/ui/printout_templates_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
;; Copyright © 2015-2024 Esko Luontola
;; This software is released under the Apache License 2.0.
;; The license text is at http://www.apache.org/licenses/LICENSE-2.0

(ns territory-bro.ui.printout-templates-test
(:require [clojure.string :as str]
[clojure.test :refer :all]
[territory-bro.ui.html :as html]
[territory-bro.ui.printout-templates :as printout-templates])
(:import (java.time LocalDate)
(java.util UUID)))

(use-fixtures :once (fn [f]
(binding [html/*page-path* "page-url"]
(f))))

(deftest print-date-notice-test
(is (= "Printed 2000-12-31 with TerritoryBro.com [content]"
(-> (printout-templates/print-date-notice (LocalDate/of 2000 12 31) "[content]")
html/visible-text))))

(def territory-printout-model
{:territory {:id (UUID. 0 1)
:number "123"
:addresses "The Addresses"
:region "The Region"
:location "MULTIPOLYGON(territory)"}
:congregation-boundary "MULTIPOLYGON(congregation boundary)"
:enclosing-region "MULTIPOLYGON(region)"
:enclosing-minimap-viewport "POLYGON(minimap viewport)"
:map-raster "osmhd"
:print-date (LocalDate/of 2024 7 10)})

(def region-printout-model) ; TODO

(deftest territory-card-test
(testing "no data"
(is (= (html/normalize-whitespace
"Territory Map Card
Printed with TerritoryBro.com
Please keep this card in the envelope. Do not soil, mark or bend it.
Each time the territory is covered, please inform the brother who cares for the territory files.")
(-> (printout-templates/territory-card nil)
html/visible-text))))

(testing "full data"
(let [html (printout-templates/territory-card territory-printout-model)]
(is (= (html/normalize-whitespace
"Territory Map Card
The Region
123
Printed 2024-07-10 with TerritoryBro.com
The Addresses
Please keep this card in the envelope. Do not soil, mark or bend it.
Each time the territory is covered, please inform the brother who cares for the territory files.")
(html/visible-text html)))
(is (str/includes? html "territory=\"MULTIPOLYGON(territory)\""))
(is (str/includes? html "congregation-boundary=\"MULTIPOLYGON(congregation boundary)\""))
(is (str/includes? html "enclosing-region=\"MULTIPOLYGON(region)\""))
(is (str/includes? html "enclosing-minimap-viewport=\"POLYGON(minimap viewport)\""))
(is (str/includes? html "map-raster=\"osmhd\""))
(is (str/includes? html "hx-get=\"page-url/qr-code/00000000-0000-0000-0000-000000000001\"")))))
47 changes: 44 additions & 3 deletions test/territory_bro/ui/printouts_page_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,25 @@
;; The license text is at http://www.apache.org/licenses/LICENSE-2.0

(ns territory-bro.ui.printouts-page-test
(:require [clojure.test :refer :all]
(:require [clojure.string :as str]
[clojure.test :refer :all]
[clojure.walk :as walk]
[territory-bro.api-test :as at]
[territory-bro.domain.testdata :as testdata]
[territory-bro.gis.geometry :as geometry]
[territory-bro.infra.authentication :as auth]
[territory-bro.test.fixtures :refer :all]
[territory-bro.ui.html :as html]
[territory-bro.ui.map-interaction-help-test :as map-interaction-help-test]
[territory-bro.ui.printouts-page :as printouts-page])
(:import (java.time ZoneId)
(:import (java.time Clock Duration ZoneOffset ZonedDateTime)
(java.util UUID)))

(def default-model
{:congregation {:id (UUID. 0 1)
:name "Example Congregation"
:location (str (geometry/parse-wkt testdata/wkt-helsinki))
:timezone (ZoneId/of "Europe/Helsinki")}
:timezone testdata/timezone-helsinki}
:regions [{:id (UUID. 0 2)
:name "the region"
:location testdata/wkt-south-helsinki}]
Expand Down Expand Up @@ -88,3 +91,41 @@
(UUID. 0 2)}
(printouts-page/parse-uuid-multiselect ["00000000-0000-0000-0000-000000000001"
"00000000-0000-0000-0000-000000000002"]))))

(deftest render-qr-code-svg-test
(let [svg (printouts-page/render-qr-code-svg "foo")]
(is (str/includes? svg "viewBox=\"0 0 21 21\""))
(is (str/includes? svg "M0,0h1v1h-1z M1,0h1v1h-1z"))))

(deftest view-test
(binding [printouts-page/*clock* (-> (.toInstant (ZonedDateTime/of 2000 12 31 23 59 0 0 testdata/timezone-helsinki))
(Clock/fixed ZoneOffset/UTC))]
(testing "territory printout"
(is (= (html/normalize-whitespace
"Printouts
Print options
Template [Territory card]
Language [English]
Background map [World - OpenStreetMap]
Regions [Example Congregation]
Territories [123 - the region]
Territory Map Card
the region
123
Printed 2000-12-31 with TerritoryBro.com
the addresses
Please keep this card in the envelope. Do not soil, mark or bend it.
Each time the territory is covered, please inform the brother who cares for the territory files."
map-interaction-help-test/default-visible-text)
(-> (printouts-page/view default-model)
html/visible-text))))

(testing "region printout") ; TODO

(binding [printouts-page/*clock* (Clock/offset printouts-page/*clock* (Duration/ofMinutes 1))]
(testing "print date uses the congregation timezone"
(is (str/includes? (-> (printouts-page/view default-model)
html/visible-text)
"Printed 2001-01-01"))))))

0 comments on commit 7ba368a

Please sign in to comment.