diff --git a/packages/builder/src/builderStore/store/frontend.js b/packages/builder/src/builderStore/store/frontend.js index 88a280a006..9db23d3001 100644 --- a/packages/builder/src/builderStore/store/frontend.js +++ b/packages/builder/src/builderStore/store/frontend.js @@ -69,16 +69,14 @@ export const getFrontendStore = () => { // Initialise backend stores const [ _integrations, - _queries, _tables, ] = await Promise.all([ 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.init() integrations.set(_integrations) - queries.set({ list: _queries, selected: null }) + queries.init() database.set(application.instance) tables.set({ list: _tables, diff --git a/packages/builder/src/components/integration/QueryViewer.svelte b/packages/builder/src/components/integration/QueryViewer.svelte index 063bbf2309..c37158d93f 100644 --- a/packages/builder/src/components/integration/QueryViewer.svelte +++ b/packages/builder/src/components/integration/QueryViewer.svelte @@ -15,7 +15,7 @@ import IntegrationQueryEditor from "components/integration/index.svelte" import ExternalDataSourceTable from "components/backend/DataTable/ExternalDataSourceTable.svelte" import ParameterBuilder from "components/integration/QueryParameterBuilder.svelte" - import { datasources, integrations } from 'stores/backend/' + import { datasources, integrations, queries } from 'stores/backend/' const PREVIEW_HEADINGS = [ { diff --git a/packages/builder/src/stores/backend/queries.js b/packages/builder/src/stores/backend/queries.js index 8bda870179..a23cc4d644 100644 --- a/packages/builder/src/stores/backend/queries.js +++ b/packages/builder/src/stores/backend/queries.js @@ -10,6 +10,11 @@ export function createQueriesStore() { subscribe, set, update, + init: async () => { + const response = await api.get(`/api/queries`) + const json = await response.json() + set({ list: json, selected: null }) + }, fetch: async () => { const response = await api.get(`/api/queries`) const json = await response.json() @@ -54,7 +59,7 @@ export function createQueriesStore() { datasources.update(state => ({ ...state, selected: query.datasourceId })) }, delete: async query => { - await api.delete(`/api/queries/${query._id}/${query._rev}`) + const response = await api.delete(`/api/queries/${query._id}/${query._rev}`) update(state => { state.list = state.list.filter(existing => existing._id !== query._id) if (state.selected === query._id) { @@ -63,6 +68,7 @@ export function createQueriesStore() { return state }) + console.log(response) }, } } diff --git a/packages/builder/src/stores/backend/tests/datasources.spec.js b/packages/builder/src/stores/backend/tests/datasources.spec.js index 0d1d15777d..f4e10b6aa3 100644 --- a/packages/builder/src/stores/backend/tests/datasources.spec.js +++ b/packages/builder/src/stores/backend/tests/datasources.spec.js @@ -11,8 +11,9 @@ import { queries } from '../queries' describe("Datasources Store", () => { let store = createDatasourcesStore() - beforeEach(() => { - store.init() + beforeEach(async () => { + api.get.mockReturnValue({ json: () => [SOME_DATASOURCE]}) + await store.init() }) it("Initialises correctly", async () => { @@ -35,9 +36,9 @@ describe("Datasources Store", () => { expect(get(store).select).toEqual(SOME_DATASOURCE._id) }) - it("resets the queries store when it a new datasource is selected", async () => { + it("resets the queries store when new datasource is selected", async () => { - store.select(SOME_DATASOURCE._id) + await store.select(SOME_DATASOURCE._id) const queriesValue = get(queries) expect(queriesValue.selected).toEqual(null) }) diff --git a/packages/builder/src/stores/backend/tests/fixtures/queries.js b/packages/builder/src/stores/backend/tests/fixtures/queries.js new file mode 100644 index 0000000000..97807cdefc --- /dev/null +++ b/packages/builder/src/stores/backend/tests/fixtures/queries.js @@ -0,0 +1,80 @@ +export const SOME_QUERY = { + "datasourceId": "datasource_04b003a7b4a8428eadd3bb2f7eae0255", + "parameters": [], + "fields": { + "headers": {}, + "queryString": "", + "path": "Speakers" + }, + "queryVerb": "read", + "schema": {}, + "name": "Speakers", + "_id": "query_datasource_04b003a7b4a8428eadd3bb2f7eae0255_bcb8ffc6fcbc484e8d63121fc0bf986f", + "_rev": "2-941f8699eb0adf995f8bd59c99203b26", + "readable": true + } + +export const SAVE_QUERY_RESPONSE = { + "datasourceId": "datasource_04b003a7b4a8428eadd3bb2f7eae0255", + "parameters": [], + "fields": { + "headers": {}, + "queryString": "", + "path": "Speakers" + }, + "queryVerb": "read", + "schema": { + "id": { + "name": "id", + "type": "string" + }, + "firstName": { + "name": "firstName", + "type": "string" + }, + "lastName": { + "name": "lastName", + "type": "string" + }, + "fullName": { + "name": "fullName", + "type": "string" + }, + "bio": { + "name": "bio", + "type": "string" + }, + "tagLine": { + "name": "tagLine", + "type": "string" + }, + "profilePicture": { + "name": "profilePicture", + "type": "string" + }, + "sessions": { + "name": "sessions", + "type": "string" + }, + "isTopSpeaker": { + "name": "isTopSpeaker", + "type": "string" + }, + "links": { + "name": "links", + "type": "string" + }, + "questionAnswers": { + "name": "questionAnswers", + "type": "string" + }, + "categories": { + "name": "categories", + "type": "string" + } + }, + "name": "Speakers", + "_id": "query_datasource_04b003a7b4a8428eadd3bb2f7eae0255_bcb8ffc6fcbc484e8d63121fc0bf986f", + "_rev": "3-5a64adef494b1e9c793dc91b51ce73c6", + "readable": true +} \ No newline at end of file diff --git a/packages/builder/src/stores/backend/tests/permissions.spec.js b/packages/builder/src/stores/backend/tests/permissions.spec.js index 54938c3c6f..ab5aebb284 100644 --- a/packages/builder/src/stores/backend/tests/permissions.spec.js +++ b/packages/builder/src/stores/backend/tests/permissions.spec.js @@ -21,6 +21,5 @@ describe("Permissions Store", () => { expect(api.get).toBeCalledWith(`/api/permission/${resourceId}`) expect(permissions).toEqual(PERMISSIONS_FOR_RESOURCE) - }) }) \ 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 4e7a54d0b5..c9d4a51c73 100644 --- a/packages/builder/src/stores/backend/tests/queries.spec.js +++ b/packages/builder/src/stores/backend/tests/queries.spec.js @@ -1,26 +1,57 @@ -// import api from 'builderStore/api' +import { get } from 'svelte/store' +import api from 'builderStore/api' -// jest.mock('builderStore/api'); +jest.mock('builderStore/api'); -// const PERMISSIONS_FOR_RESOURCE = { -// "write": "BASIC", -// "read": "BASIC" -// } +import { SOME_QUERY, SAVE_QUERY_RESPONSE } from './fixtures/queries' -// import { createQueriesStore } from "../queries" +import { createQueriesStore } from "../queries" +import { datasources } from '../datasources' -// describe("Queries Store", () => { -// const store = createQueriesStore() +describe("Queries Store", () => { + let store = createQueriesStore() -// it("fetches permissions for specific resource", async () => { -// api.get.mockReturnValueOnce({ json: () => PERMISSIONS_FOR_RESOURCE}) + beforeEach(async () => { + api.get.mockReturnValue({ json: () => [SOME_QUERY]}) + await store.init() + }) -// const resourceId = "ta_013657543b4043b89dbb17e9d3a4723a" + it("Initialises correctly", async () => { + api.get.mockReturnValue({ json: () => [SOME_QUERY]}) + + await store.init() + expect(get(store)).toEqual({ list: [SOME_QUERY], selected: null}) + }) -// const permissions = await store.forResource(resourceId) + it("fetches all the queries", async () => { + api.get.mockReturnValue({ json: () => [SOME_QUERY]}) -// expect(api.get).toBeCalledWith(`/api/permission/${resourceId}`) -// expect(permissions).toEqual(PERMISSIONS_FOR_RESOURCE) - -// }) -// }) \ No newline at end of file + await store.fetch() + expect(get(store)).toEqual({ list: [SOME_QUERY], selected: null}) + }) + + it("selects a query and updates selected datasource", async () => { + await store.select(SOME_QUERY) + + expect(get(store).selected).toEqual(SOME_QUERY._id) + expect(get(datasources).selected).toEqual(SOME_QUERY.datasourceId) + }) + + it("saves the datasource, updates the store and returns status message", async () => { + api.post.mockReturnValue({ json: () => SAVE_QUERY_RESPONSE}) + + await store.select(SOME_QUERY.datasourceId, SOME_QUERY) + + expect(get(store).list).toEqual(expect.arrayContaining([SOME_QUERY])) + }) + it("deletes a datasource, updates the store and returns status message", async () => { + api.get.mockReturnValue({ json: () => SOME_QUERY}) + + await store.fetch() + + api.delete.mockReturnValue({status: 200, message: 'Datasource deleted.'}) + + await store.delete(SOME_QUERY) + expect(get(store)).toEqual({ list: [], selected: null}) + }) +}) \ No newline at end of file