budibase/packages/core/test/collectionApi.delete.spec.js

82 lines
2.6 KiB
JavaScript
Raw Normal View History

import {
setupApphierarchy,
basicAppHierarchyCreator_WithFields,
} from "./specHelpers"
import { $, splitKey } from "../src/common"
import { keys, filter } from "lodash/fp"
import { permission } from "../src/authApi/permissions"
2019-07-15 08:12:52 +02:00
describe("collectionApi > delete", () => {
it("should remove every key in collection's path", async () => {
const { recordApi, collectionApi, appHierarchy } = await setupApphierarchy(
basicAppHierarchyCreator_WithFields
)
const record1 = recordApi.getNew("/customers", "customer")
record1.surname = "Ledog"
await recordApi.save(record1)
const record2 = recordApi.getNew("/customers", "customer")
record2.surname = "Zeecat"
await recordApi.save(record2)
const childRecord = recordApi.getNew(`${record1.key}/invoices`, "invoice")
await recordApi.save(childRecord)
await collectionApi.delete("/customers")
const remainingKeys = $(recordApi._storeHandle.data, [
keys,
filter(k => splitKey(k)[0] === "customers"),
])
expect(remainingKeys).toEqual([
"/customers",
`/customers/${appHierarchy.customerRecord.nodeId}`,
])
})
it("should not delete anything that is not in its path", async () => {
const { recordApi, collectionApi } = await setupApphierarchy(
basicAppHierarchyCreator_WithFields
)
const customer = recordApi.getNew("/customers", "customer")
customer.surname = "Ledog"
await recordApi.save(customer)
const partner = recordApi.getNew("/partners", "partner")
await recordApi.save(partner)
const allKeys = keys(recordApi._storeHandle.data)
const customerKeys = $(allKeys, [
filter(k => splitKey(k)[0] === "customers"),
])
const expectedRemainingKeys = allKeys.length - customerKeys.length + 2
// +2 because is should keep the collection folders: /customers & /customers/1
await collectionApi.delete("/customers")
const remainingKeys = keys(recordApi._storeHandle.data)
expect(remainingKeys.length).toBe(expectedRemainingKeys)
})
it("should throw error when user user does not have permission", async () => {
const { collectionApi, app } = await setupApphierarchy(
basicAppHierarchyCreator_WithFields
)
app.removePermission(permission.manageCollection.get("/customers"))
expect(collectionApi.delete("/customers")).rejects.toThrow(/Unauthorized/)
})
it("should not depend on having any other permissions", async () => {
const { collectionApi, app } = await setupApphierarchy(
basicAppHierarchyCreator_WithFields
)
app.withOnlyThisPermission(permission.manageCollection.get("/customers"))
await collectionApi.delete("/customers")
})
})