diff --git a/common/src/app/common/logic/variants.cljc b/common/src/app/common/logic/variants.cljc index 6f85dc11e0..a302b044d1 100644 --- a/common/src/app/common/logic/variants.cljc +++ b/common/src/app/common/logic/variants.cljc @@ -68,12 +68,12 @@ (let [data (pcb/get-library-data changes) objects (pcb/get-objects changes) component-id (:component-id shape) - value (str ctv/value-prefix - (-> (cfv/extract-properties-values data objects variant-id) - last - :value - count - inc)) + values (-> (cfv/extract-properties-values data objects variant-id) last :value) + ;; the first "Value n" from the number of values up that no member has + value (->> (iterate inc (inc (count values))) + (map #(str ctv/value-prefix %)) + (remove (set values)) + first) [new-shape changes] (-> changes (cld/generate-duplicate-component diff --git a/common/test/common_tests/logic/composable_gen_test.cljc b/common/test/common_tests/logic/composable_gen_test.cljc index f90a5ef1ca..e14db90912 100644 --- a/common/test/common_tests/logic/composable_gen_test.cljc +++ b/common/test/common_tests/logic/composable_gen_test.cljc @@ -1693,6 +1693,13 @@ :ops [[:add-variant 0] [:switch 0] [:add-variant 1] [:undo] [:redo] [:sync 0]]})] (t/is (= 2 (get-in result [:executed :add-variant]))))) +(t/deftest adding-a-variant-after-deleting-a-member-takes-a-value-no-member-has + (t/is (map? (cg/run {:world [2 2 {:variants true :text-styles true :layout nil :variant-diff false + :layout-item-margins false :grid-square false :rich-variants true + :layout-children false :grid-empty false :layout-copies true :text true}] + :ops [[:add-variant 0] [:switch 0] [:add-variant 0 0 0 0] [:delete 2 0 0 0] + [:add-variant 0 0 0 0]]})))) + (t/deftest random-variant-compositions (let [coverage (atom {}) failure (atom nil)] diff --git a/common/test/common_tests/logic/variants_test.cljc b/common/test/common_tests/logic/variants_test.cljc index 399e16dbbd..bea6b11449 100644 --- a/common/test/common_tests/logic/variants_test.cljc +++ b/common/test/common_tests/logic/variants_test.cljc @@ -22,6 +22,7 @@ [app.common.test-helpers.variants :as thv] [app.common.types.component :as ctk] [app.common.types.variant :as ctv] + [app.common.uuid :as uuid] [clojure.test :as t] [cuerdas.core :as str])) @@ -490,6 +491,31 @@ (t/is (= (count objects') 7)))) +(t/deftest test-add-new-variant-takes-a-value-no-member-has + (let [;; ==== Setup + file (-> (thf/sample-file :file1) + (thv/add-variant :v01 :c01 :m01 :c02 :m02) + (thc/update-component :c02 {:variant-properties [{:name "Property 1" :value "Value 3"}]}) + (ths/update-shape :m02 :variant-name "Value 3")) + page (thf/current-page file) + new-id (uuid/next) + + ;; ==== Action + changes (-> (pcb/empty-changes nil (:id page)) + (pcb/with-library-data (:data file)) + (pcb/with-objects (:objects page)) + (clv/generate-add-new-variant (ths/get-shape file :m01) (thi/id :v01) new-id (uuid/next) 0)) + file' (thf/apply-changes file changes) + + ;; ==== Get + values (map #(-> % :variant-properties first :value) + (vals (get-in file' [:data :components])))] + + ;; ==== Check + ;; two values make the next one "Value 3", which a member already has + (t/is (= "Value 4" (get-in file' [:data :components new-id :variant-properties 0 :value]))) + (t/is (apply distinct? values)))) + (t/deftest test-delete-variant ;; When a variant container becomes empty, it id automatically deleted (let [;; ==== Setup