diff --git a/packages/builder/src/stores/backend/roles.js b/packages/builder/src/stores/backend/roles.js index 146ba9fbd8..b927f00cac 100644 --- a/packages/builder/src/stores/backend/roles.js +++ b/packages/builder/src/stores/backend/roles.js @@ -3,7 +3,7 @@ import api from "builderStore/api" export function createRolesStore() { - const { subscribe, set } = writable([]) + const { subscribe, update, set } = writable([]) return { subscribe, @@ -12,7 +12,10 @@ export function createRolesStore() { }, delete: async role => { const response = await api.delete(`/api/roles/${role._id}/${role._rev}`) - set(await getRoles()) + update(state => { + state = state.filter(existing => existing._id !== role._id) + return state + }) return response }, save: async role => { diff --git a/packages/builder/src/stores/backend/tests/fixtures/roles.js b/packages/builder/src/stores/backend/tests/fixtures/roles.js new file mode 100644 index 0000000000..1831bdbed2 --- /dev/null +++ b/packages/builder/src/stores/backend/tests/fixtures/roles.js @@ -0,0 +1,32 @@ +export const ROLES = [ + { + "name": "Test", + "permissionId": "admin", + "inherits": "ADMIN", + "_id": "role_04681b7e71914a0aa53e09a5bea3584f", + "_rev": "1-179c71ea61d7fd987306b84b6d64b00e" + }, + { + "_id": "ADMIN", + "name": "Admin", + "permissionId": "admin", + "inherits": "POWER" + }, + { + "_id": "POWER", + "name": "Power", + "permissionId": "power", + "inherits": "BASIC" + }, + { + "_id": "BASIC", + "name": "Basic", + "permissionId": "write", + "inherits": "PUBLIC" + }, + { + "_id": "PUBLIC", + "name": "Public", + "permissionId": "public" + } +] \ No newline at end of file diff --git a/packages/builder/src/stores/backend/tests/roles.spec.js b/packages/builder/src/stores/backend/tests/roles.spec.js index e69de29bb2..8ecd9c338e 100644 --- a/packages/builder/src/stores/backend/tests/roles.spec.js +++ b/packages/builder/src/stores/backend/tests/roles.spec.js @@ -0,0 +1,34 @@ +import { get } from 'svelte/store' +import api from 'builderStore/api' + +jest.mock('builderStore/api'); + +import { createRolesStore } from "../roles" +import { ROLES } from './fixtures/roles' + +describe("Roles Store", () => { + let store = createRolesStore() + + beforeEach( async() => { + store = createRolesStore() + }) + + it("fetches roles from backend", async () => { + api.get.mockReturnValue({ json: () => ROLES}) + await store.fetch() + + expect(api.get).toBeCalledWith("/api/roles") + expect(get(store)).toEqual(ROLES) + }) + + it("deletes a role", async () => { + api.get.mockReturnValue({ json: () => ROLES}) + await store.fetch() + + const {_id, _rev} = ROLES[0] + api.delete.mockReturnValue({status: 200, message: `Role deleted.`}) + await store.delete(`/api/roles/${_id}/${_rev}`) + + expect(get(store)).toEqual(ROLES) + }) +}) \ No newline at end of file