From 6c011eb9b53d1c0702a6ec2f56c2f5b16d34281f Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Sat, 22 May 2021 21:12:47 -0700 Subject: [PATCH] fix: Guard empty documentStructure --- server/models/Collection.js | 5 ++++- server/models/Collection.test.js | 9 +++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/server/models/Collection.js b/server/models/Collection.js index 61f5d0c7..62ab9f88 100644 --- a/server/models/Collection.js +++ b/server/models/Collection.js @@ -442,7 +442,10 @@ Collection.prototype.getDocumentParents = function ( }); }; - loopChildren(this.documentStructure); + if (this.documentStructure) { + loopChildren(this.documentStructure); + } + return result; }; diff --git a/server/models/Collection.test.js b/server/models/Collection.test.js index 8d65f247..3858d799 100644 --- a/server/models/Collection.test.js +++ b/server/models/Collection.test.js @@ -54,6 +54,15 @@ describe("getDocumentParents", () => { const result = collection.getDocumentParents(parent.id); expect(result.length).toBe(0); }); + + test("should not error if documentStructure is empty", async () => { + const parent = await buildDocument(); + await buildDocument(); + const collection = await buildCollection(); + + const result = collection.getDocumentParents(parent.id); + expect(result).toBe(undefined); + }); }); describe("getDocumentTree", () => {