finishes datasources stores

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

View File

@ -46,7 +46,6 @@ export function createDatasourcesStore(_api = api) {
}, },
delete: async datasource => { delete: async datasource => {
const response = 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
@ -54,7 +53,7 @@ export function createDatasourcesStore(_api = api) {
return { list: sources, selected: null } return { list: sources, selected: null }
}) })
return json return response
}, },
} }
} }

View File

@ -3,7 +3,7 @@ import api from 'builderStore/api'
jest.mock('builderStore/api'); jest.mock('builderStore/api');
const FETCH_RESPONSE = [ const SOME_DATASOURCE = [
{ {
"type": "datasource", "type": "datasource",
"name": "erterter", "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 { createDatasourcesStore } from "../datasources"
import { queries } from '../queries'
describe("Automation Data Object", () => { describe("Automation Data Object", () => {
let store = createDatasourcesStore() let store = createDatasourcesStore()
beforeEach(() => { beforeEach(() => {
api.get.mockReturnValueOnce({ json: () => FETCH_RESPONSE}) api.get.mockReturnValueOnce({ json: () => SOME_DATASOURCE})
api.delete.mockReturnValueOnce({ json: () => ({status: 200, message: 'Datasource deleted.'})}) api.delete.mockReturnValueOnce({status: 200, message: 'Datasource deleted.'})
store.init() store.init()
}) })
@ -33,22 +46,44 @@ describe("Automation Data Object", () => {
expect(value).toEqual({ list: [], selected: null}) expect(value).toEqual({ list: [], selected: null})
}) })
it("Fetch - returns and updates store", async () => { it("fetches all the datasources and updates the store", async () => {
let value = get(store)
expect(value).toEqual({ list: [], selected: null})
await store.fetch() await store.fetch()
value = get(store) const value = get(store)
expect(api.get).toBeCalledWith(`/api/datasources`) expect(value).toEqual({ list: SOME_DATASOURCE, selected: null})
expect(value).toEqual({ list: FETCH_RESPONSE, selected: null})
}) })
it("Delete - calls delete endpoint, updates store and returns status", async () => {
let value = get(store) it("selects a datasource", async () => {
const { _id, _rev } = FETCH_RESPONSE[0] 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.fetch()
await store.delete(FETCH_RESPONSE[0]) await store.delete(SOME_DATASOURCE[0])
expect(api.delete).toBeCalledWith(`/api/datasources/${_id}/${_rev}`) const value = await get(store)
value = await get(store)
expect(value).toEqual({ list: [], selected: null}) expect(value).toEqual({ list: [], selected: null})
}) })