From c5ec3cfb132f019e33218de7140e89bcef821a14 Mon Sep 17 00:00:00 2001 From: Michael Shanks Date: Wed, 25 Mar 2020 11:11:28 +0000 Subject: [PATCH] canDelete methods for index and record --- .../core/src/templateApi/canDeleteRecord.js | 7 +++-- .../core/test/templateApi.canDelete.spec.js | 29 +++++++++++++++++-- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/packages/core/src/templateApi/canDeleteRecord.js b/packages/core/src/templateApi/canDeleteRecord.js index c438bfb58d..1601afa0cf 100644 --- a/packages/core/src/templateApi/canDeleteRecord.js +++ b/packages/core/src/templateApi/canDeleteRecord.js @@ -20,8 +20,7 @@ export const canDeleteRecord = recordNode => { ]) const belongsToAncestor = i => - ancestors.includes(i.parent()) - + ancestors.includes(i.parent()) const errorsForNode = node => { const errorsThisNode = $(flatHierarchy, [ @@ -40,5 +39,7 @@ export const canDeleteRecord = recordNode => { return errorsThisNode } - return errorsForNode(recordNode) + const errors = errorsForNode(recordNode) + + return { errors, canDelete: errors.length === 0 } } \ No newline at end of file diff --git a/packages/core/test/templateApi.canDelete.spec.js b/packages/core/test/templateApi.canDelete.spec.js index 69fff08f7a..8751068ba3 100644 --- a/packages/core/test/templateApi.canDelete.spec.js +++ b/packages/core/test/templateApi.canDelete.spec.js @@ -2,6 +2,7 @@ import { setupApphierarchy, basicAppHierarchyCreator_WithFields, stubEventHandler, + basicAppHierarchyCreator_WithFields_AndIndexes, } from "./specHelpers" import { canDeleteIndex } from "../src/templateApi/canDeleteIndex" import { canDeleteRecord } from "../src/templateApi/canDeleteRecord" @@ -49,15 +50,37 @@ describe("canDeleteIndex", () => { describe("canDeleteRecord", () => { - it("should return no errors when deletion is valid", () => { + it("should return no errors when deletion is valid", async () => { const { appHierarchy } = await setupApphierarchy( basicAppHierarchyCreator_WithFields ) - appHierarchy.root. - const result = canDeleteIndex(appHierarchy.customerRecord) + appHierarchy.root.indexes = appHierarchy.root.indexes.filter(i => !i.allowedRecordNodeIds.includes(appHierarchy.customerRecord.nodeId)) + const result = canDeleteRecord(appHierarchy.customerRecord) expect(result.canDelete).toBe(true) expect(result.errors).toEqual([]) }) + + it("should return errors when record is referenced by hierarchal index", async () => { + const { appHierarchy } = await setupApphierarchy( + basicAppHierarchyCreator_WithFields + ) + + const result = canDeleteRecord(appHierarchy.customerRecord) + + expect(result.canDelete).toBe(false) + expect(result.errors.some(e => e.includes("customer_index"))).toBe(true) + }) + + it("should return errors when record has a child which cannot be deleted", async () => { + const { appHierarchy } = await setupApphierarchy( + basicAppHierarchyCreator_WithFields_AndIndexes + ) + + const result = canDeleteRecord(appHierarchy.customerRecord) + + expect(result.canDelete).toBe(false) + expect(result.errors.some(e => e.includes("Outstanding Invoices"))).toBe(true) + }) }) \ No newline at end of file