diff --git a/common/src/app/common/files/helpers.cljc b/common/src/app/common/files/helpers.cljc index f669d34aac..eb8c5bb70b 100644 --- a/common/src/app/common/files/helpers.cljc +++ b/common/src/app/common/files/helpers.cljc @@ -355,7 +355,8 @@ prt (get objects pid) shapes (:shapes prt) pos (d/index-of shapes id)] - (if (= 0 pos) nil (nth shapes (dec pos))))) + (when (and (some? pos) (pos? pos)) + (nth shapes (dec pos))))) (defn get-immediate-children "Retrieve resolved shape objects that are immediate children diff --git a/common/test/common_tests/files/helpers_test.cljc b/common/test/common_tests/files/helpers_test.cljc index 00d30dbf30..6141a40b78 100644 --- a/common/test/common_tests/files/helpers_test.cljc +++ b/common/test/common_tests/files/helpers_test.cljc @@ -7,6 +7,7 @@ (ns common-tests.files.helpers-test (:require [app.common.files.helpers :as cfh] + [app.common.uuid :as uuid] [clojure.test :as t])) (t/deftest test-generate-unique-name @@ -36,3 +37,19 @@ #{"base-name 1" "base-name 2"} :immediate-suffix? true) "base-name 3"))) + +(t/deftest test-get-prev-sibling + (let [parent-id (uuid/custom 1 1) + child-a (uuid/custom 1 2) + child-b (uuid/custom 1 3) + orphan-id (uuid/custom 1 4) + objects {parent-id {:id parent-id :shapes [child-a child-b]} + child-a {:id child-a :parent-id parent-id} + child-b {:id child-b :parent-id parent-id} + orphan-id {:id orphan-id :parent-id parent-id}}] + (t/testing "Returns previous sibling when present in parent ordering" + (t/is (= child-a + (cfh/get-prev-sibling objects child-b)))) + + (t/testing "Returns nil when the shape is missing from parent ordering" + (t/is (nil? (cfh/get-prev-sibling objects orphan-id))))))