From a1f242a6289256ebd5f2dcb464ae0c833a0bd13d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Keviin=20=C3=85berg=20Kultalahti?= Date: Mon, 29 Mar 2021 12:03:34 +0200 Subject: [PATCH] adds fetch and delete tests to datasources store --- .../builder/src/stores/backend/datasources.js | 7 ++- .../stores/backend/tests/datasources.spec.js | 44 ++++++++++++++----- 2 files changed, 38 insertions(+), 13 deletions(-) diff --git a/packages/builder/src/stores/backend/datasources.js b/packages/builder/src/stores/backend/datasources.js index 6317d32e41..6fdafc1394 100644 --- a/packages/builder/src/stores/backend/datasources.js +++ b/packages/builder/src/stores/backend/datasources.js @@ -45,13 +45,16 @@ export function createDatasourcesStore(_api = api) { return json }, delete: async datasource => { - 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 => { const sources = state.list.filter( existing => existing._id !== datasource._id ) - return { sources, selected: null } + return { list: sources, selected: null } }) + + return json }, } } diff --git a/packages/builder/src/stores/backend/tests/datasources.spec.js b/packages/builder/src/stores/backend/tests/datasources.spec.js index 79913c24ac..ea9ea572fb 100644 --- a/packages/builder/src/stores/backend/tests/datasources.spec.js +++ b/packages/builder/src/stores/backend/tests/datasources.spec.js @@ -3,31 +3,53 @@ import api from 'builderStore/api' jest.mock('builderStore/api'); +const FETCH_RESPONSE = [ + { + "type": "datasource", + "name": "erterter", + "source": "REST", + "config": { + "url": "localhost", + "defaultHeaders": {} + }, + "_id": "datasource_04b003a7b4a8428eadd3bb2f7eae0255", + "_rev": "1-4e72002f1011e9392e655948469b7908" + } +] + import { createDatasourcesStore } from "../datasources" describe("Automation Data Object", () => { let store = createDatasourcesStore() beforeEach(() => { + api.get.mockReturnValueOnce({ json: () => FETCH_RESPONSE}) + api.delete.mockReturnValueOnce({ json: () => ({status: 200, message: 'Datasource deleted.'})}) store.init() }) it("Inits properly", () => { - const value = get(store) expect(value).toEqual({ list: [], selected: null}) }) - it("Fetch returns and updates store", async () => { - api.get.mockReturnValueOnce({ json: () => 'some-cool-value'}) - - store.fetch() - - + 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(get(store)).toEqual({ list: [], 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) + 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) + + expect(value).toEqual({ list: [], selected: null}) + }) +}) \ No newline at end of file