finishes datasources stores

This commit is contained in:
Keviin Åberg Kultalahti 2021-03-29 12:54:41 +02:00
parent a1f242a628
commit f8475bfdc2
2 changed files with 55 additions and 21 deletions

View File

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

View File

@ -3,7 +3,7 @@ import api from 'builderStore/api'
jest.mock('builderStore/api');
const FETCH_RESPONSE = [
const SOME_DATASOURCE = [
{
"type": "datasource",
"name": "erterter",
@ -17,14 +17,27 @@ const FETCH_RESPONSE = [
}
]
const SAVE_DATASOURCE = {
"type": "datasource",
"name": "CoolDB",
"source": "REST",
"config": {
"url": "localhost",
"defaultHeaders": {}
},
"_id": "datasource_04b003a7b4a8428eadd3bb2f7eae0255",
"_rev": "1-4e72002f1011e9392e655948469b7908"
}
import { createDatasourcesStore } from "../datasources"
import { queries } from '../queries'
describe("Automation Data Object", () => {
let store = createDatasourcesStore()
beforeEach(() => {
api.get.mockReturnValueOnce({ json: () => FETCH_RESPONSE})
api.delete.mockReturnValueOnce({ json: () => ({status: 200, message: 'Datasource deleted.'})})
api.get.mockReturnValueOnce({ json: () => SOME_DATASOURCE})
api.delete.mockReturnValueOnce({status: 200, message: 'Datasource deleted.'})
store.init()
})
@ -33,22 +46,44 @@ describe("Automation Data Object", () => {
expect(value).toEqual({ list: [], selected: null})
})
it("Fetch - returns and updates store", async () => {
let value = get(store)
expect(value).toEqual({ list: [], selected: null})
it("fetches all the datasources and updates the store", async () => {
await store.fetch()
value = get(store)
expect(api.get).toBeCalledWith(`/api/datasources`)
expect(value).toEqual({ list: FETCH_RESPONSE, selected: null})
const value = get(store)
expect(value).toEqual({ list: SOME_DATASOURCE, selected: null})
})
it("Delete - calls delete endpoint, updates store and returns status", async () => {
let value = get(store)
const { _id, _rev } = FETCH_RESPONSE[0]
it("selects a datasource", async () => {
store.select(SOME_DATASOURCE._id)
const value = get(store)
expect(value.select).toEqual(SOME_DATASOURCE._id)
})
it("resets the queries store when it a new datasource is selected", async () => {
store.select(SOME_DATASOURCE._id)
const queriesValue = get(queries)
expect(queriesValue.selected).toEqual(null)
})
it("saves the datasource, updates the store and returns status message", async () => {
api.post.mockReturnValueOnce({ json: () => SAVE_DATASOURCE})
await store.save({
name: 'CoolDB',
source: 'REST',
config: SOME_DATASOURCE[0].config
})
const value = await get(store)
expect(value.list).toEqual(expect.arrayContaining([SAVE_DATASOURCE]))
})
it("deletes a datasource, updates the store and returns status message", async () => {
await store.fetch()
await store.delete(FETCH_RESPONSE[0])
expect(api.delete).toBeCalledWith(`/api/datasources/${_id}/${_rev}`)
value = await get(store)
await store.delete(SOME_DATASOURCE[0])
const value = await get(store)
expect(value).toEqual({ list: [], selected: null})
})