diff --git a/packages/builder/src/builderStore/store/frontend.js b/packages/builder/src/builderStore/store/frontend.js index ac78f1a829..88a280a006 100644 --- a/packages/builder/src/builderStore/store/frontend.js +++ b/packages/builder/src/builderStore/store/frontend.js @@ -68,17 +68,15 @@ export const getFrontendStore = () => { // Initialise backend stores const [ - _datasources, _integrations, _queries, _tables, ] = await Promise.all([ - api.get(`/api/datasources`).then(r => r.json()), api.get("/api/integrations").then(r => r.json()), api.get(`/api/queries`).then(r => r.json()), api.get(`/api/tables`).then(r => r.json()), ]) - datasources.set({ list: _datasources, selected: null }) + datasources.init() integrations.set(_integrations) queries.set({ list: _queries, selected: null }) database.set(application.instance) diff --git a/packages/builder/src/stores/backend/datasources.js b/packages/builder/src/stores/backend/datasources.js index 31d34cec32..1a64955ba7 100644 --- a/packages/builder/src/stores/backend/datasources.js +++ b/packages/builder/src/stores/backend/datasources.js @@ -12,9 +12,12 @@ export function createDatasourcesStore(_api = api) { return { subscribe, - set, 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 () => { const response = await api.get(`/api/datasources`) const json = await response.json() diff --git a/packages/builder/src/stores/backend/tests/datasources.spec.js b/packages/builder/src/stores/backend/tests/datasources.spec.js index c70104f74d..4c29206c17 100644 --- a/packages/builder/src/stores/backend/tests/datasources.spec.js +++ b/packages/builder/src/stores/backend/tests/datasources.spec.js @@ -3,31 +3,7 @@ import api from 'builderStore/api' jest.mock('builderStore/api'); -const SOME_DATASOURCE = [ - { - "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 { SOME_DATASOURCE, SAVE_DATASOURCE} from './fixtures/datasources' import { createDatasourcesStore } from "../datasources" import { queries } from '../queries' @@ -36,27 +12,27 @@ describe("Datasources Store", () => { let store = createDatasourcesStore() beforeEach(() => { - api.get.mockReturnValueOnce({ json: () => SOME_DATASOURCE}) store.init() }) - it("Inits properly", () => { - const value = get(store) - expect(value).toEqual({ list: [], selected: null}) + it("Initialises correctly", async () => { + api.get.mockReturnValue({ json: () => [SOME_DATASOURCE]}) + + await store.init() + expect(get(store)).toEqual({ list: [SOME_DATASOURCE], selected: null}) }) it("fetches all the datasources and updates the store", async () => { + api.get.mockReturnValue({ json: () => [SOME_DATASOURCE]}) await store.fetch() - const value = get(store) - expect(value).toEqual({ list: SOME_DATASOURCE, selected: null}) + expect(get(store)).toEqual({ list: [SOME_DATASOURCE], selected: null}) }) it("selects a datasource", async () => { store.select(SOME_DATASOURCE._id) - - const value = get(store) - expect(value.select).toEqual(SOME_DATASOURCE._id) + + expect(get(store).select).toEqual(SOME_DATASOURCE._id) }) 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 () => { - api.post.mockReturnValueOnce({ json: () => SAVE_DATASOURCE}) + api.post.mockReturnValue({ json: () => SAVE_DATASOURCE}) await store.save({ name: 'CoolDB', @@ -75,17 +51,17 @@ describe("Datasources Store", () => { 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 () => { - api.delete.mockReturnValueOnce({status: 200, message: 'Datasource deleted.'}) + api.get.mockReturnValue({ json: () => [SOME_DATASOURCE]}) await store.fetch() + + api.delete.mockReturnValue({status: 200, message: 'Datasource deleted.'}) + await store.delete(SOME_DATASOURCE[0]) - const value = await get(store) - - expect(value).toEqual({ list: [], selected: null}) + expect(get(store)).toEqual({ list: [], selected: null}) }) }) \ No newline at end of file diff --git a/packages/builder/src/stores/backend/tests/fixtures/datasources.js b/packages/builder/src/stores/backend/tests/fixtures/datasources.js new file mode 100644 index 0000000000..582c7fc574 --- /dev/null +++ b/packages/builder/src/stores/backend/tests/fixtures/datasources.js @@ -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" + } + \ No newline at end of file diff --git a/packages/builder/src/stores/backend/tests/queries.spec.js b/packages/builder/src/stores/backend/tests/queries.spec.js index e69de29bb2..4e7a54d0b5 100644 --- a/packages/builder/src/stores/backend/tests/queries.spec.js +++ b/packages/builder/src/stores/backend/tests/queries.spec.js @@ -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) + +// }) +// }) \ No newline at end of file