0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-03-11 07:11:32 -05:00

Add generative tests for flows and saved-grids

This commit is contained in:
Andrey Antukh 2024-09-10 14:05:43 +02:00 committed by Alonso Torres
parent 61446592b3
commit 41ebba6ce0
2 changed files with 172 additions and 46 deletions

View file

@ -64,43 +64,44 @@
[:type [:= :set-remote-synced]]
[:remote-synced {:optional true} [:maybe :boolean]]]]])
(defn- set-default-grid-change-generator
[]
(->> (sg/elements #{:square :column :row})
(sg/mcat (fn [type]
(sg/fmap (fn [params]
{:page-id (uuid/next)
:type :mod-grid
:grid-type type
:params params})
(case type
:square (sg/generator ctg/schema:square-params)
:column (sg/generator ctg/schema:column-params)
:row (sg/generator ctg/schema:column-params)))))))
(def schema:set-default-grid-change
[:multi {:decode/json #(update % :grid-type keyword)
:gen/gen (set-default-grid-change-generator)
:dispatch :grid-type
::smd/simplified true}
[:square
[:map
[:page-id ::sm/uuid]
[:grid-type [:and :keyword [:= :square]]]
[:params [:maybe ctg/schema:square-params]]]]
(let [gen (->> (sg/elements #{:square :column :row})
(sg/mcat (fn [grid-type]
(sg/fmap (fn [params]
{:page-id (uuid/next)
:type :set-default-grid
:grid-type grid-type
:params params})
[:column
[:map
[:page-id ::sm/uuid]
[:grid-type [:and :keyword [:= :column]]]
[:params [:maybe ctg/schema:column-params]]]]
(case grid-type
:square (sg/generator ctg/schema:square-params)
:column (sg/generator ctg/schema:column-params)
:row (sg/generator ctg/schema:column-params))))))]
[:row
[:map
[:page-id ::sm/uuid]
[:grid-type [:and :keyword [:= :row]]]
[:params [:maybe ctg/schema:column-params]]]]])
[:multi {:decode/json #(update % :grid-type keyword)
:gen/gen gen
:dispatch :grid-type
::smd/simplified true}
[:square
[:map
[:type [:= :set-default-grid]]
[:page-id ::sm/uuid]
[:grid-type [:= :square]]
[:params [:maybe ctg/schema:square-params]]]]
[:column
[:map
[:type [:= :set-default-grid]]
[:page-id ::sm/uuid]
[:grid-type [:= :column]]
[:params [:maybe ctg/schema:column-params]]]]
[:row
[:map
[:type [:= :set-default-grid]]
[:page-id ::sm/uuid]
[:grid-type [:= :row]]
[:params [:maybe ctg/schema:column-params]]]]]))
(def schema:set-guide-change
(let [schema [:map {:title "SetGuideChange"}
@ -115,6 +116,20 @@
change))))]
[:schema {:gen/gen gen} schema]))
(def schema:set-flow-change
(let [schema [:map {:title "SetFlowChange"}
[:type [:= :set-flow]]
[:page-id ::sm/uuid]
[:id ::sm/uuid]
[:params [:maybe ::ctp/flow]]]
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:set-plugin-data-change
(let [types #{:file :page :shape :color :typography :component}
@ -203,15 +218,7 @@
[:ignore-touched {:optional true} :boolean]]]
[:set-guide schema:set-guide-change]
[:set-flow
[:map {:title "SetFlowChange"}
[:page-id ::sm/uuid]
[:id ::sm/uuid]
[:params [:maybe ::ctp/flow]]]]
;; Change used for all crud operation on persisted grid options on
;; the page.
[:set-flow schema:set-flow-change]
[:set-default-grid schema:set-default-grid-change]
[:fix-obj
@ -497,15 +504,29 @@
(defmethod process-change :set-flow
[data {:keys [page-id id params]}]
(if (nil? params)
(d/update-in-when data [:pages-index page-id] update :flows dissoc id)
(d/update-in-when data [:pages-index page-id] update :flows assoc id params)))
(d/update-in-when data [:pages-index page-id]
(fn [page]
(let [flows (get page :flows)
flows (dissoc flows id)]
(if (empty? flows)
(dissoc page :flows)
(assoc page :flows flows)))))
(let [params (assoc params :id id)]
(d/update-in-when data [:pages-index page-id] update :flows assoc id params))))
;; --- Grids
(defmethod process-change :set-default-grid
[data {:keys [page-id grid-type params]}]
(if (nil? params)
(d/update-in-when data [:pages-index page-id] update :default-grids dissoc grid-type)
(d/update-in-when data [:pages-index page-id]
(fn [page]
(let [default-grids (get page :default-grids)
default-grids (dissoc default-grids grid-type)]
(if (empty? default-grids)
(dissoc page :default-grids)
(assoc page :default-grids default-grids)))))
(d/update-in-when data [:pages-index page-id] update :default-grids assoc grid-type params)))
;; --- Shape / Obj

View file

@ -757,3 +757,108 @@
(smt/for [change (sg/generator ch/schema:set-plugin-data-change)]
(sm/validate ch/schema:set-plugin-data-change change))
{:num 1000})))
(t/deftest set-flow-json-encode-decode
(let [schema ch/schema:set-flow-change
encode (sm/encoder schema (sm/json-transformer))
decode (sm/decoder schema (sm/json-transformer))]
(smt/check!
(smt/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)
(= data data-3)))
{:num 1000})))
(t/deftest set-flow-1
(let [file-id (uuid/custom 2 2)
page-id (uuid/custom 1 1)
data (make-file-data file-id page-id)]
(smt/check!
(smt/for [change (sg/generator ch/schema:set-flow-change)]
(let [change (assoc change :page-id page-id)
result (ch/process-changes data [change])]
(= (:params change)
(get-in result [:pages-index page-id :flows (:id change)]))))
{:num 1000})))
(t/deftest set-flow-2
(let [file-id (uuid/custom 2 2)
page-id (uuid/custom 1 1)
data (make-file-data file-id page-id)]
(smt/check!
(smt/for [change (->> (sg/generator ch/schema:set-flow-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])]
(and (some? (:params change1))
(= (:params change1)
(get-in result1 [:pages-index page-id :flows (:id change1)]))
(nil? (:params change2))
(nil? (get-in result2 [:pages-index page-id :flows])))))
{:num 1000})))
(t/deftest set-default-grid-json-encode-decode
(let [schema ch/schema:set-default-grid-change
encode (sm/encoder schema (sm/json-transformer))
decode (sm/decoder schema (sm/json-transformer))]
(smt/check!
(smt/for [data (sg/generator schema)]
(let [data-1 (encode data)
data-2 (json-roundtrip data-1)
data-3 (decode data-2)]
;; (println "==========")
;; (app.common.pprint/pprint data-2)
;; (app.common.pprint/pprint data-3)
;; (println "==========")
(= data data-3)))
{:num 1000})))
(t/deftest set-default-grid-1
(let [file-id (uuid/custom 2 2)
page-id (uuid/custom 1 1)
data (make-file-data file-id page-id)]
(smt/check!
(smt/for [change (sg/generator ch/schema:set-default-grid-change)]
(let [change (assoc change :page-id page-id)
result (ch/process-changes data [change])]
;; (app.common.pprint/pprint change)
(= (:params change)
(get-in result [:pages-index page-id :default-grids (:grid-type change)]))))
{:num 1000})))
(t/deftest set-default-grid-2
(let [file-id (uuid/custom 2 2)
page-id (uuid/custom 1 1)
data (make-file-data file-id page-id)]
(smt/check!
(smt/for [change (->> (sg/generator ch/schema:set-default-grid-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])]
;; (app.common.pprint/pprint change1)
(and (some? (:params change1))
(= (:params change1)
(get-in result1 [:pages-index page-id :default-grids (:grid-type change1)]))
(nil? (:params change2))
(nil? (get-in result2 [:pages-index page-id :default-grids])))))
{:num 1000})))