diff --git a/common/src/app/common/logic/variant_properties.cljc b/common/src/app/common/logic/variant_properties.cljc index ad937ac5a0..f38b357167 100644 --- a/common/src/app/common/logic/variant_properties.cljc +++ b/common/src/app/common/logic/variant_properties.cljc @@ -135,22 +135,35 @@ changes (let [data (pcb/get-library-data changes) objects (pcb/get-objects changes) - related-components (cfv/find-variant-components data objects variant-id)] - (reduce (fn [changes component] - (let [props (:variant-properties component) - props' (d/reorder props from-pos to-space-between-pos) - main-id (:main-instance-id component) - name (ctv/properties-to-name props')] - (if (not= props props') - (-> changes - (pcb/update-component (:id component) - #(assoc % :variant-properties props') - {:apply-changes-local-library? true}) - (pcb/update-shapes [main-id] - #(assoc % :variant-name name))) - changes))) - changes - related-components)))) + related-components (cfv/find-variant-components data objects variant-id) + + changes' (reduce (fn [changes component] + (let [props (:variant-properties component) + props' (d/reorder props from-pos to-space-between-pos) + main-id (:main-instance-id component) + name (ctv/properties-to-name props')] + (if (not= props props') + (-> changes + (pcb/update-component (:id component) + #(assoc % :variant-properties props') + {:apply-changes-local-library? true}) + (pcb/update-shapes [main-id] + #(assoc % :variant-name name))) + changes))) + changes + related-components)] + + ;; Nothing to validate if the order did not change + (if (identical? changes changes') + changes + ;; All the components of the variant change, so validate the + ;; container, which checks every main instance and that all of them + ;; keep the same order of properties + (pcb/validate-shapes changes' + (pcb/get-page-id changes') + [variant-id] + (str "generate-reorder-variant-poperties: " variant-id + " from: " from-pos " to: " to-space-between-pos)))))) (defn generate-add-new-property "Add a new variant property to all components in the variant and their respective main instances. diff --git a/common/test/common_tests/logic/variant_properties_test.cljc b/common/test/common_tests/logic/variant_properties_test.cljc index c9de512936..2880ad1a93 100644 --- a/common/test/common_tests/logic/variant_properties_test.cljc +++ b/common/test/common_tests/logic/variant_properties_test.cljc @@ -432,7 +432,8 @@ comp01' (thc/get-component file' :c01)] (t/is (= "Property 2" (-> comp01' :variant-properties first :name))) - (t/is (= "Property 1" (-> comp01' :variant-properties last :name))))) + (t/is (= "Property 1" (-> comp01' :variant-properties last :name))) + (t/is (thf/validates-shapes-last? changes (:id page) [v-id])))) (t/deftest test-reorder-variant-properties-clamped-pos (let [file (-> (thf/sample-file :file1) @@ -449,13 +450,32 @@ file' (thf/apply-changes file changes) comp01' (thc/get-component file' :c01)] (t/is (= "Property 2" (-> comp01' :variant-properties first :name))) - (t/is (= "Property 1" (-> comp01' :variant-properties last :name)))) + (t/is (= "Property 1" (-> comp01' :variant-properties last :name))) + (t/is (thf/validates-shapes-last? changes (:id page) [v-id]))) (let [changes (clvp/generate-reorder-variant-poperties base-changes v-id 1 -1) file' (thf/apply-changes file changes) comp01' (thc/get-component file' :c01)] (t/is (= "Property 2" (-> comp01' :variant-properties first :name))) - (t/is (= "Property 1" (-> comp01' :variant-properties last :name)))))) + (t/is (= "Property 1" (-> comp01' :variant-properties last :name))) + (t/is (thf/validates-shapes-last? changes (:id page) [v-id]))))) + +(t/deftest test-reorder-variant-properties-validates-container + (let [file (-> (thf/sample-file :file1) + (thv/add-variant-two-properties :v01 :c01 :m01 :c02 :m02)) + v-id (-> (ths/get-shape file :v01) :id) + page (thf/current-page file) + + changes (-> (pcb/empty-changes nil) + (pcb/with-page-id (:id page)) + (pcb/with-library-data (:data file)) + (pcb/with-objects (:objects page)) + (clvp/generate-reorder-variant-poperties v-id 0 2))] + + ;; All the components of the variant change, so validate the container, + ;; which checks every main instance and that all of them keep the same + ;; order of properties + (t/is (thf/validates-shapes-last? changes (:id page) [v-id])))) ;; ============================================================================= ;; generate-add-new-property tests