mirror of
https://github.com/penpot/penpot.git
synced 2025-03-28 15:41:25 -05:00
✨ Add generative tests for set-guide change
This commit is contained in:
parent
fa75a3539f
commit
4f84e77b10
5 changed files with 89 additions and 19 deletions
|
@ -53,7 +53,7 @@
|
|||
valid? (or (and components-v2
|
||||
(nil? (:component-id change))
|
||||
(nil? (:page-id change)))
|
||||
(ch/check-change! change))]
|
||||
(ch/valid-change? change))]
|
||||
|
||||
(when-not valid?
|
||||
(let [explain (sm/explain ::ch/change change)]
|
||||
|
|
|
@ -102,6 +102,19 @@
|
|||
[:grid-type [:and :keyword [:= :row]]]
|
||||
[:params [:maybe ctg/schema:column-params]]]]])
|
||||
|
||||
(def schema:set-guide-change
|
||||
(let [schema [:map {:title "SetGuideChange"}
|
||||
[:type [:= :set-guide]]
|
||||
[:page-id ::sm/uuid]
|
||||
[:id ::sm/uuid]
|
||||
[:params [:maybe ::ctp/guide]]]
|
||||
gen (->> (sg/generator schema)
|
||||
(sg/fmap (fn [change]
|
||||
(if (some? (:params change))
|
||||
(update change :params assoc :id (:id change))
|
||||
change))))]
|
||||
[:schema {:gen/gen gen} schema]))
|
||||
|
||||
(def schema:change
|
||||
[:schema
|
||||
[:multi {:dispatch :type
|
||||
|
@ -150,11 +163,7 @@
|
|||
[:component-id {:optional true} ::sm/uuid]
|
||||
[:ignore-touched {:optional true} :boolean]]]
|
||||
|
||||
[:set-guide
|
||||
[:map {:title "SetGuideChange"}
|
||||
[:page-id ::sm/uuid]
|
||||
[:id ::sm/uuid]
|
||||
[:params [:maybe ::ctp/guide]]]]
|
||||
[:set-guide schema:set-guide-change]
|
||||
|
||||
[:set-flow
|
||||
[:map {:title "SetFlowChange"}
|
||||
|
@ -329,11 +338,11 @@
|
|||
(sm/register! ::change schema:change)
|
||||
(sm/register! ::changes schema:changes)
|
||||
|
||||
(def check-change!
|
||||
(sm/check-fn ::change))
|
||||
(def valid-change?
|
||||
(sm/lazy-validator schema:change))
|
||||
|
||||
(def check-changes!
|
||||
(sm/check-fn ::changes))
|
||||
(def valid-changes?
|
||||
(sm/lazy-validator schema:changes))
|
||||
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
;; Specific helpers
|
||||
|
@ -405,12 +414,12 @@
|
|||
(process-changes data items true))
|
||||
|
||||
([data items verify?]
|
||||
;; When verify? false we spec the schema validation. Currently used to make just
|
||||
;; 1 validation even if the changes are applied twice
|
||||
;; When verify? false we spec the schema validation. Currently used
|
||||
;; to make just 1 validation even if the changes are applied twice
|
||||
(when verify?
|
||||
(dm/verify!
|
||||
"expected valid changes"
|
||||
(check-changes! items)))
|
||||
(valid-changes? items)))
|
||||
|
||||
(binding [*touched-changes* (volatile! #{})]
|
||||
(let [result (reduce #(or (process-change %1 %2) %1) data items)
|
||||
|
@ -445,8 +454,16 @@
|
|||
(defmethod process-change :set-guide
|
||||
[data {:keys [page-id id params]}]
|
||||
(if (nil? params)
|
||||
(d/update-in-when data [:pages-index page-id] update :guides dissoc id)
|
||||
(d/update-in-when data [:pages-index page-id] update :guides assoc id params)))
|
||||
(d/update-in-when data [:pages-index page-id]
|
||||
(fn [page]
|
||||
(let [guides (get page :guides)
|
||||
guides (dissoc guides id)]
|
||||
(if (empty? guides)
|
||||
(dissoc page :guides)
|
||||
(assoc page :guides guides)))))
|
||||
|
||||
(let [params (assoc params :id id)]
|
||||
(d/update-in-when data [:pages-index page-id] update :guides assoc id params))))
|
||||
|
||||
;; --- Flows
|
||||
|
||||
|
|
|
@ -8,11 +8,14 @@
|
|||
(:require
|
||||
[app.common.features :as ffeat]
|
||||
[app.common.files.changes :as ch]
|
||||
[app.common.schema :as sm]
|
||||
[app.common.schema.generators :as sg]
|
||||
[app.common.types.file :as ctf]
|
||||
[app.common.types.shape :as cts]
|
||||
[app.common.uuid :as uuid]
|
||||
[clojure.pprint :refer [pprint]]
|
||||
[clojure.test :as t]))
|
||||
[clojure.test :as t]
|
||||
[common-tests.types.shape-decode-encode-test :refer [json-roundtrip]]))
|
||||
|
||||
(defn- make-file-data
|
||||
[file-id page-id]
|
||||
|
@ -682,3 +685,53 @@
|
|||
|
||||
(t/is (not= nil
|
||||
(get-in res [:pages-index page-id :objects group-1-id])))))))
|
||||
|
||||
(t/deftest set-guide-json-encode-decode
|
||||
(let [schema ch/schema:set-guide-change
|
||||
encode (sm/encoder schema (sm/json-transformer))
|
||||
decode (sm/decoder schema (sm/json-transformer))]
|
||||
(sg/check!
|
||||
(sg/for [data (sg/generator schema)]
|
||||
(let [data-1 (encode data)
|
||||
data-2 (json-roundtrip data-1)
|
||||
data-3 (decode data-2)]
|
||||
;; (app.common.pprint/pprint data-2)
|
||||
;; (app.common.pprint/pprint data-3)
|
||||
(t/is (= data data-3))))
|
||||
{:num 1000})))
|
||||
|
||||
(t/deftest set-guide-1
|
||||
(let [file-id (uuid/custom 2 2)
|
||||
page-id (uuid/custom 1 1)
|
||||
data (make-file-data file-id page-id)]
|
||||
|
||||
(sg/check!
|
||||
(sg/for [change (sg/generator ch/schema:set-guide-change)]
|
||||
(let [change (assoc change :page-id page-id)
|
||||
result (ch/process-changes data [change])]
|
||||
(t/is (= (:params change)
|
||||
(get-in result [:pages-index page-id :guides (:id change)])))))
|
||||
{:num 1000})))
|
||||
|
||||
(t/deftest set-guide-2
|
||||
(let [file-id (uuid/custom 2 2)
|
||||
page-id (uuid/custom 1 1)
|
||||
data (make-file-data file-id page-id)]
|
||||
|
||||
(sg/check!
|
||||
(sg/for [change (->> (sg/generator ch/schema:set-guide-change)
|
||||
(sg/filter :params))]
|
||||
(let [change1 (assoc change :page-id page-id)
|
||||
result1 (ch/process-changes data [change1])
|
||||
|
||||
change2 (assoc change1 :params nil)
|
||||
result2 (ch/process-changes result1 [change2])]
|
||||
|
||||
(t/is (some? (:params change1)))
|
||||
(t/is (= (:params change1)
|
||||
(get-in result1 [:pages-index page-id :guides (:id change1)])))
|
||||
|
||||
(t/is (nil? (:params change2)))
|
||||
(t/is (nil? (get-in result2 [:pages-index page-id :guides])))))
|
||||
|
||||
{:num 1000})))
|
||||
|
|
|
@ -110,8 +110,8 @@
|
|||
|
||||
(dm/assert!
|
||||
"expect valid vector of changes"
|
||||
(and (cpc/check-changes! redo-changes)
|
||||
(cpc/check-changes! undo-changes)))
|
||||
(and (cpc/valid-changes? redo-changes)
|
||||
(cpc/valid-changes? undo-changes)))
|
||||
|
||||
(let [commit-id (or commit-id (uuid/next))
|
||||
source (d/nilv source :local)
|
||||
|
|
|
@ -676,7 +676,7 @@
|
|||
(defn ext-library-changed
|
||||
[library-id modified-at revn changes]
|
||||
(dm/assert! (uuid? library-id))
|
||||
(dm/assert! (ch/check-changes! changes))
|
||||
(dm/assert! (ch/valid-changes? changes))
|
||||
(ptk/reify ::ext-library-changed
|
||||
ptk/UpdateEvent
|
||||
(update [_ state]
|
||||
|
|
Loading…
Add table
Reference in a new issue