adds fetch and delete tests to datasources store

This commit is contained in:
Keviin Åberg Kultalahti 2021-03-29 12:03:34 +02:00
parent 6711801f97
commit a1f242a628
2 changed files with 38 additions and 13 deletions

View File

@ -45,13 +45,16 @@ export function createDatasourcesStore(_api = api) {
return json return json
}, },
delete: async datasource => { delete: async datasource => {
await api.delete(`/api/datasources/${datasource._id}/${datasource._rev}`) const response = await api.delete(`/api/datasources/${datasource._id}/${datasource._rev}`)
const json = await response.json()
update(state => { update(state => {
const sources = state.list.filter( const sources = state.list.filter(
existing => existing._id !== datasource._id existing => existing._id !== datasource._id
) )
return { sources, selected: null } return { list: sources, selected: null }
}) })
return json
}, },
} }
} }

View File

@ -3,31 +3,53 @@ import api from 'builderStore/api'
jest.mock('builderStore/api'); jest.mock('builderStore/api');
const FETCH_RESPONSE = [
{
"type": "datasource",
"name": "erterter",
"source": "REST",
"config": {
"url": "localhost",
"defaultHeaders": {}
},
"_id": "datasource_04b003a7b4a8428eadd3bb2f7eae0255",
"_rev": "1-4e72002f1011e9392e655948469b7908"
}
]
import { createDatasourcesStore } from "../datasources" import { createDatasourcesStore } from "../datasources"
describe("Automation Data Object", () => { describe("Automation Data Object", () => {
let store = createDatasourcesStore() let store = createDatasourcesStore()
beforeEach(() => { beforeEach(() => {
api.get.mockReturnValueOnce({ json: () => FETCH_RESPONSE})
api.delete.mockReturnValueOnce({ json: () => ({status: 200, message: 'Datasource deleted.'})})
store.init() store.init()
}) })
it("Inits properly", () => { it("Inits properly", () => {
const value = get(store) const value = get(store)
expect(value).toEqual({ list: [], selected: null}) expect(value).toEqual({ list: [], selected: null})
}) })
it("Fetch returns and updates store", async () => { it("Fetch - returns and updates store", async () => {
api.get.mockReturnValueOnce({ json: () => 'some-cool-value'}) let value = get(store)
expect(value).toEqual({ list: [], selected: null})
store.fetch()
await store.fetch()
value = get(store)
expect(api.get).toBeCalledWith(`/api/datasources`) expect(api.get).toBeCalledWith(`/api/datasources`)
expect(value).toEqual({ list: FETCH_RESPONSE, selected: null})
// expect(get(store)).toEqual({ list: [], selected: null})
}) })
}) it("Delete - calls delete endpoint, updates store and returns status", async () => {
let value = get(store)
const { _id, _rev } = FETCH_RESPONSE[0]
await store.fetch()
await store.delete(FETCH_RESPONSE[0])
expect(api.delete).toBeCalledWith(`/api/datasources/${_id}/${_rev}`)
value = await get(store)
expect(value).toEqual({ list: [], selected: null})
})
})