wip: roles store test

This commit is contained in:
Keviin Åberg Kultalahti 2021-03-29 18:23:53 +02:00
parent 292a891883
commit dd21ee2386
3 changed files with 71 additions and 2 deletions

View File

@ -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 => {

View File

@ -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"
}
]

View File

@ -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)
})
})