mirror of
https://github.com/penpot/penpot.git
synced 2025-07-20 16:57:17 +02: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
|
valid? (or (and components-v2
|
||||||
(nil? (:component-id change))
|
(nil? (:component-id change))
|
||||||
(nil? (:page-id change)))
|
(nil? (:page-id change)))
|
||||||
(ch/check-change! change))]
|
(ch/valid-change? change))]
|
||||||
|
|
||||||
(when-not valid?
|
(when-not valid?
|
||||||
(let [explain (sm/explain ::ch/change change)]
|
(let [explain (sm/explain ::ch/change change)]
|
||||||
|
|
|
@ -102,6 +102,19 @@
|
||||||
[:grid-type [:and :keyword [:= :row]]]
|
[:grid-type [:and :keyword [:= :row]]]
|
||||||
[:params [:maybe ctg/schema:column-params]]]]])
|
[: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
|
(def schema:change
|
||||||
[:schema
|
[:schema
|
||||||
[:multi {:dispatch :type
|
[:multi {:dispatch :type
|
||||||
|
@ -150,11 +163,7 @@
|
||||||
[:component-id {:optional true} ::sm/uuid]
|
[:component-id {:optional true} ::sm/uuid]
|
||||||
[:ignore-touched {:optional true} :boolean]]]
|
[:ignore-touched {:optional true} :boolean]]]
|
||||||
|
|
||||||
[:set-guide
|
[:set-guide schema:set-guide-change]
|
||||||
[:map {:title "SetGuideChange"}
|
|
||||||
[:page-id ::sm/uuid]
|
|
||||||
[:id ::sm/uuid]
|
|
||||||
[:params [:maybe ::ctp/guide]]]]
|
|
||||||
|
|
||||||
[:set-flow
|
[:set-flow
|
||||||
[:map {:title "SetFlowChange"}
|
[:map {:title "SetFlowChange"}
|
||||||
|
@ -329,11 +338,11 @@
|
||||||
(sm/register! ::change schema:change)
|
(sm/register! ::change schema:change)
|
||||||
(sm/register! ::changes schema:changes)
|
(sm/register! ::changes schema:changes)
|
||||||
|
|
||||||
(def check-change!
|
(def valid-change?
|
||||||
(sm/check-fn ::change))
|
(sm/lazy-validator schema:change))
|
||||||
|
|
||||||
(def check-changes!
|
(def valid-changes?
|
||||||
(sm/check-fn ::changes))
|
(sm/lazy-validator schema:changes))
|
||||||
|
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
;; Specific helpers
|
;; Specific helpers
|
||||||
|
@ -405,12 +414,12 @@
|
||||||
(process-changes data items true))
|
(process-changes data items true))
|
||||||
|
|
||||||
([data items verify?]
|
([data items verify?]
|
||||||
;; When verify? false we spec the schema validation. Currently used to make just
|
;; When verify? false we spec the schema validation. Currently used
|
||||||
;; 1 validation even if the changes are applied twice
|
;; to make just 1 validation even if the changes are applied twice
|
||||||
(when verify?
|
(when verify?
|
||||||
(dm/verify!
|
(dm/verify!
|
||||||
"expected valid changes"
|
"expected valid changes"
|
||||||
(check-changes! items)))
|
(valid-changes? items)))
|
||||||
|
|
||||||
(binding [*touched-changes* (volatile! #{})]
|
(binding [*touched-changes* (volatile! #{})]
|
||||||
(let [result (reduce #(or (process-change %1 %2) %1) data items)
|
(let [result (reduce #(or (process-change %1 %2) %1) data items)
|
||||||
|
@ -445,8 +454,16 @@
|
||||||
(defmethod process-change :set-guide
|
(defmethod process-change :set-guide
|
||||||
[data {:keys [page-id id params]}]
|
[data {:keys [page-id id params]}]
|
||||||
(if (nil? 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]
|
||||||
(d/update-in-when data [:pages-index page-id] update :guides assoc id params)))
|
(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
|
;; --- Flows
|
||||||
|
|
||||||
|
|
|
@ -8,11 +8,14 @@
|
||||||
(:require
|
(:require
|
||||||
[app.common.features :as ffeat]
|
[app.common.features :as ffeat]
|
||||||
[app.common.files.changes :as ch]
|
[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.file :as ctf]
|
||||||
[app.common.types.shape :as cts]
|
[app.common.types.shape :as cts]
|
||||||
[app.common.uuid :as uuid]
|
[app.common.uuid :as uuid]
|
||||||
[clojure.pprint :refer [pprint]]
|
[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
|
(defn- make-file-data
|
||||||
[file-id page-id]
|
[file-id page-id]
|
||||||
|
@ -682,3 +685,53 @@
|
||||||
|
|
||||||
(t/is (not= nil
|
(t/is (not= nil
|
||||||
(get-in res [:pages-index page-id :objects group-1-id])))))))
|
(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!
|
(dm/assert!
|
||||||
"expect valid vector of changes"
|
"expect valid vector of changes"
|
||||||
(and (cpc/check-changes! redo-changes)
|
(and (cpc/valid-changes? redo-changes)
|
||||||
(cpc/check-changes! undo-changes)))
|
(cpc/valid-changes? undo-changes)))
|
||||||
|
|
||||||
(let [commit-id (or commit-id (uuid/next))
|
(let [commit-id (or commit-id (uuid/next))
|
||||||
source (d/nilv source :local)
|
source (d/nilv source :local)
|
||||||
|
|
|
@ -676,7 +676,7 @@
|
||||||
(defn ext-library-changed
|
(defn ext-library-changed
|
||||||
[library-id modified-at revn changes]
|
[library-id modified-at revn changes]
|
||||||
(dm/assert! (uuid? library-id))
|
(dm/assert! (uuid? library-id))
|
||||||
(dm/assert! (ch/check-changes! changes))
|
(dm/assert! (ch/valid-changes? changes))
|
||||||
(ptk/reify ::ext-library-changed
|
(ptk/reify ::ext-library-changed
|
||||||
ptk/UpdateEvent
|
ptk/UpdateEvent
|
||||||
(update [_ state]
|
(update [_ state]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue