wip: queries store tests

This commit is contained in:
Keviin Åberg Kultalahti 2021-03-29 14:57:01 +02:00
parent 58546754b8
commit d0aca74e4a
5 changed files with 75 additions and 46 deletions

View File

@ -68,17 +68,15 @@ export const getFrontendStore = () => {
// Initialise backend stores // Initialise backend stores
const [ const [
_datasources,
_integrations, _integrations,
_queries, _queries,
_tables, _tables,
] = await Promise.all([ ] = await Promise.all([
api.get(`/api/datasources`).then(r => r.json()),
api.get("/api/integrations").then(r => r.json()), api.get("/api/integrations").then(r => r.json()),
api.get(`/api/queries`).then(r => r.json()), api.get(`/api/queries`).then(r => r.json()),
api.get(`/api/tables`).then(r => r.json()), api.get(`/api/tables`).then(r => r.json()),
]) ])
datasources.set({ list: _datasources, selected: null }) datasources.init()
integrations.set(_integrations) integrations.set(_integrations)
queries.set({ list: _queries, selected: null }) queries.set({ list: _queries, selected: null })
database.set(application.instance) database.set(application.instance)

View File

@ -12,9 +12,12 @@ export function createDatasourcesStore(_api = api) {
return { return {
subscribe, subscribe,
set,
update, update,
init: () => set(INITIAL_DATASOURCE_VALUES), init: async () => {
const response = await api.get(`/api/datasources`)
const json = await response.json()
set({ list: json, selected: null })
},
fetch: async () => { fetch: async () => {
const response = await api.get(`/api/datasources`) const response = await api.get(`/api/datasources`)
const json = await response.json() const json = await response.json()

View File

@ -3,31 +3,7 @@ import api from 'builderStore/api'
jest.mock('builderStore/api'); jest.mock('builderStore/api');
const SOME_DATASOURCE = [ import { SOME_DATASOURCE, SAVE_DATASOURCE} from './fixtures/datasources'
{
"type": "datasource",
"name": "erterter",
"source": "REST",
"config": {
"url": "localhost",
"defaultHeaders": {}
},
"_id": "datasource_04b003a7b4a8428eadd3bb2f7eae0255",
"_rev": "1-4e72002f1011e9392e655948469b7908"
}
]
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' import { queries } from '../queries'
@ -36,27 +12,27 @@ describe("Datasources Store", () => {
let store = createDatasourcesStore() let store = createDatasourcesStore()
beforeEach(() => { beforeEach(() => {
api.get.mockReturnValueOnce({ json: () => SOME_DATASOURCE})
store.init() store.init()
}) })
it("Inits properly", () => { it("Initialises correctly", async () => {
const value = get(store) api.get.mockReturnValue({ json: () => [SOME_DATASOURCE]})
expect(value).toEqual({ list: [], selected: null})
await store.init()
expect(get(store)).toEqual({ list: [SOME_DATASOURCE], selected: null})
}) })
it("fetches all the datasources and updates the store", async () => { it("fetches all the datasources and updates the store", async () => {
api.get.mockReturnValue({ json: () => [SOME_DATASOURCE]})
await store.fetch() await store.fetch()
const value = get(store) expect(get(store)).toEqual({ list: [SOME_DATASOURCE], selected: null})
expect(value).toEqual({ list: SOME_DATASOURCE, selected: null})
}) })
it("selects a datasource", async () => { it("selects a datasource", async () => {
store.select(SOME_DATASOURCE._id) store.select(SOME_DATASOURCE._id)
const value = get(store) expect(get(store).select).toEqual(SOME_DATASOURCE._id)
expect(value.select).toEqual(SOME_DATASOURCE._id)
}) })
it("resets the queries store when it a new datasource is selected", async () => { it("resets the queries store when it a new datasource is selected", async () => {
@ -67,7 +43,7 @@ describe("Datasources Store", () => {
}) })
it("saves the datasource, updates the store and returns status message", async () => { it("saves the datasource, updates the store and returns status message", async () => {
api.post.mockReturnValueOnce({ json: () => SAVE_DATASOURCE}) api.post.mockReturnValue({ json: () => SAVE_DATASOURCE})
await store.save({ await store.save({
name: 'CoolDB', name: 'CoolDB',
@ -75,17 +51,17 @@ describe("Datasources Store", () => {
config: SOME_DATASOURCE[0].config config: SOME_DATASOURCE[0].config
}) })
const value = await get(store)
expect(value.list).toEqual(expect.arrayContaining([SAVE_DATASOURCE])) expect(get(store).list).toEqual(expect.arrayContaining([SAVE_DATASOURCE]))
}) })
it("deletes a datasource, updates the store and returns status message", async () => { it("deletes a datasource, updates the store and returns status message", async () => {
api.delete.mockReturnValueOnce({status: 200, message: 'Datasource deleted.'}) api.get.mockReturnValue({ json: () => [SOME_DATASOURCE]})
await store.fetch() await store.fetch()
api.delete.mockReturnValue({status: 200, message: 'Datasource deleted.'})
await store.delete(SOME_DATASOURCE[0]) await store.delete(SOME_DATASOURCE[0])
const value = await get(store) expect(get(store)).toEqual({ list: [], selected: null})
expect(value).toEqual({ list: [], selected: null})
}) })
}) })

View File

@ -0,0 +1,26 @@
export const SOME_DATASOURCE = [
{
"type": "datasource",
"name": "erterter",
"source": "REST",
"config": {
"url": "localhost",
"defaultHeaders": {}
},
"_id": "datasource_04b003a7b4a8428eadd3bb2f7eae0255",
"_rev": "1-4e72002f1011e9392e655948469b7908"
}
]
export const SAVE_DATASOURCE = {
"type": "datasource",
"name": "CoolDB",
"source": "REST",
"config": {
"url": "localhost",
"defaultHeaders": {}
},
"_id": "datasource_04b003a7b4a8428eadd3bb2f7eae0255",
"_rev": "1-4e72002f1011e9392e655948469b7908"
}

View File

@ -0,0 +1,26 @@
// import api from 'builderStore/api'
// jest.mock('builderStore/api');
// const PERMISSIONS_FOR_RESOURCE = {
// "write": "BASIC",
// "read": "BASIC"
// }
// import { createQueriesStore } from "../queries"
// describe("Queries Store", () => {
// const store = createQueriesStore()
// it("fetches permissions for specific resource", async () => {
// api.get.mockReturnValueOnce({ json: () => PERMISSIONS_FOR_RESOURCE})
// const resourceId = "ta_013657543b4043b89dbb17e9d3a4723a"
// const permissions = await store.forResource(resourceId)
// expect(api.get).toBeCalledWith(`/api/permission/${resourceId}`)
// expect(permissions).toEqual(PERMISSIONS_FOR_RESOURCE)
// })
// })