From f8475bfdc22078fbe743fd7600877dd98847ce87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Keviin=20=C3=85berg=20Kultalahti?= Date: Mon, 29 Mar 2021 12:54:41 +0200 Subject: [PATCH] finishes datasources stores --- .../builder/src/stores/backend/datasources.js | 3 +- .../stores/backend/tests/datasources.spec.js | 73 ++++++++++++++----- 2 files changed, 55 insertions(+), 21 deletions(-) diff --git a/packages/builder/src/stores/backend/datasources.js b/packages/builder/src/stores/backend/datasources.js index 6fdafc1394..31d34cec32 100644 --- a/packages/builder/src/stores/backend/datasources.js +++ b/packages/builder/src/stores/backend/datasources.js @@ -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 }, } } diff --git a/packages/builder/src/stores/backend/tests/datasources.spec.js b/packages/builder/src/stores/backend/tests/datasources.spec.js index ea9ea572fb..5eb7e8cd05 100644 --- a/packages/builder/src/stores/backend/tests/datasources.spec.js +++ b/packages/builder/src/stores/backend/tests/datasources.spec.js @@ -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,23 +46,45 @@ 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}) - - await store.fetch() - value = get(store) - expect(api.get).toBeCalledWith(`/api/datasources`) - expect(value).toEqual({ list: FETCH_RESPONSE, selected: null}) - }) - it("Delete - calls delete endpoint, updates store and returns status", async () => { - let value = get(store) - const { _id, _rev } = FETCH_RESPONSE[0] - await store.fetch() - await store.delete(FETCH_RESPONSE[0]) - expect(api.delete).toBeCalledWith(`/api/datasources/${_id}/${_rev}`) - value = await get(store) + it("fetches all the datasources and updates the store", async () => { + await store.fetch() + const value = get(store) + expect(value).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) + }) + + 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(SOME_DATASOURCE[0]) + const value = await get(store) + expect(value).toEqual({ list: [], selected: null}) }) }) \ No newline at end of file