From 449b223e3e370c976b3749c31fb0ed841ad5bbc6 Mon Sep 17 00:00:00 2001 From: Anugerah Erlaut Date: Wed, 4 May 2022 12:25:55 +0100 Subject: [PATCH 1/7] load cell sets v2 --- src/redux/actions/cellSets/loadCellSets.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/redux/actions/cellSets/loadCellSets.js b/src/redux/actions/cellSets/loadCellSets.js index 6c287bc81f..38a4cfb891 100644 --- a/src/redux/actions/cellSets/loadCellSets.js +++ b/src/redux/actions/cellSets/loadCellSets.js @@ -5,6 +5,9 @@ import { } from 'redux/actionTypes/cellSets'; import endUserMessages from 'utils/endUserMessages'; +import config from 'config'; +import { api } from 'utils/constants'; + const loadCellSets = (experimentId, forceReload = false) => async (dispatch, getState) => { const { loading, error, updatingClustering, @@ -20,7 +23,13 @@ const loadCellSets = (experimentId, forceReload = false) => async (dispatch, get }); } - const url = `/v1/experiments/${experimentId}/cellSets`; + let url; + + if (config.currentApiVersion === api.V1) { + url = `/v1/experiments/${experimentId}/cellSets`; + } else if (config.currentApiVersion === api.V2) { + url = `/v2/experiments/${experimentId}/cellSets`; + } try { const data = await fetchAPI(url); dispatch({ From 41e0989a8890cefc3bef161d9e6fc6cdeb0a887e Mon Sep 17 00:00:00 2001 From: Anugerah Erlaut Date: Wed, 4 May 2022 12:35:36 +0100 Subject: [PATCH 2/7] update cell sets v2 endpoint --- src/redux/actions/cellSets/createCellSet.js | 11 ++++++++++- src/redux/actions/cellSets/deleteCellSet.js | 11 ++++++++++- src/redux/actions/cellSets/reorderCellSet.js | 12 +++++++++++- src/redux/actions/cellSets/updateCellSetProperty.js | 12 +++++++++++- 4 files changed, 42 insertions(+), 4 deletions(-) diff --git a/src/redux/actions/cellSets/createCellSet.js b/src/redux/actions/cellSets/createCellSet.js index cb014ecdb7..31f94a5fdf 100644 --- a/src/redux/actions/cellSets/createCellSet.js +++ b/src/redux/actions/cellSets/createCellSet.js @@ -8,6 +8,9 @@ import endUserMessages from 'utils/endUserMessages'; import pushNotificationMessage from 'utils/pushNotificationMessage'; import handleError from 'utils/http/handleError'; +import config from 'config'; +import { api } from 'utils/constants'; + const createCellSetJsonMerger = (newCellSet, cellClassKey) => ( [{ $match: { @@ -64,7 +67,13 @@ const createCellSet = (experimentId, name, color, cellIdsSet) => async (dispatch payload: dataForRedux, }); - const url = `/v1/experiments/${experimentId}/cellSets`; + let url; + + if (config.currentApiVersion === api.V1) { + url = `/v1/experiments/${experimentId}/cellSets`; + } else if (config.currentApiVersion === api.V2) { + url = `/v2/experiments/${experimentId}/cellSets`; + } try { await fetchAPI( diff --git a/src/redux/actions/cellSets/deleteCellSet.js b/src/redux/actions/cellSets/deleteCellSet.js index cbd36c0ee0..f26931b72f 100644 --- a/src/redux/actions/cellSets/deleteCellSet.js +++ b/src/redux/actions/cellSets/deleteCellSet.js @@ -4,6 +4,9 @@ import endUserMessages from 'utils/endUserMessages'; import fetchAPI from 'utils/http/fetchAPI'; import handleError from 'utils/http/handleError'; +import config from 'config'; +import { api } from 'utils/constants'; + const deleteCellSetJsonMerger = (cellSetKey, cellClasskey) => ( [{ $match: { @@ -33,7 +36,13 @@ const deleteCellSet = (experimentId, key) => async (dispatch, getState) => { return null; } - const url = `/v1/experiments/${experimentId}/cellSets`; + let url; + + if (config.currentApiVersion === api.V1) { + url = `/v1/experiments/${experimentId}/cellSets`; + } else if (config.currentApiVersion === api.V2) { + url = `/v2/experiments/${experimentId}/cellSets`; + } try { await fetchAPI( diff --git a/src/redux/actions/cellSets/reorderCellSet.js b/src/redux/actions/cellSets/reorderCellSet.js index 0595ea82f1..6f994fb18f 100644 --- a/src/redux/actions/cellSets/reorderCellSet.js +++ b/src/redux/actions/cellSets/reorderCellSet.js @@ -4,6 +4,9 @@ import fetchAPI from 'utils/http/fetchAPI'; import handleError from 'utils/http/handleError'; import endUserMessages from 'utils/endUserMessages'; +import config from 'config'; +import { api } from 'utils/constants'; + const reorderCellSetJsonMerger = (cellSetKey, newPosition, cellClassKey) => ( [{ $match: { @@ -27,9 +30,16 @@ const reorderCellSetJsonMerger = (cellSetKey, newPosition, cellClassKey) => ( const reorderCellSet = ( experimentId, cellSetKey, newPosition, ) => async (dispatch, getState) => { - const url = `/v1/experiments/${experimentId}/cellSets`; const { parentNodeKey } = getState().cellSets.properties[cellSetKey]; + let url; + + if (config.currentApiVersion === api.V1) { + url = `/v1/experiments/${experimentId}/cellSets`; + } else if (config.currentApiVersion === api.V2) { + url = `/v2/experiments/${experimentId}/cellSets`; + } + try { await fetchAPI( url, diff --git a/src/redux/actions/cellSets/updateCellSetProperty.js b/src/redux/actions/cellSets/updateCellSetProperty.js index e8ae203310..466b8c9d87 100644 --- a/src/redux/actions/cellSets/updateCellSetProperty.js +++ b/src/redux/actions/cellSets/updateCellSetProperty.js @@ -6,6 +6,9 @@ import fetchAPI from 'utils/http/fetchAPI'; import endUserMessages from 'utils/endUserMessages'; import handleError from 'utils/http/handleError'; +import config from 'config'; +import { api } from 'utils/constants'; + const updateCellSetPropertyJsonMerger = (cellSetKey, dataUpdated, cellClassKey) => ( [{ $match: { @@ -65,7 +68,14 @@ const updateCellSetProperty = ( ? updateCellClassPropertyJsonMerger(key, dataUpdated) : updateCellSetPropertyJsonMerger(key, dataUpdated, parentNodeKey); - const url = `/v1/experiments/${experimentId}/cellSets`; + let url; + + if (config.currentApiVersion === api.V1) { + url = `/v1/experiments/${experimentId}/cellSets`; + } else if (config.currentApiVersion === api.V2) { + url = `/v2/experiments/${experimentId}/cellSets`; + } + try { await fetchAPI( url, From 163b150ef9d43cb1789e81d3d02abed0d4b9ae1e Mon Sep 17 00:00:00 2001 From: Anugerah Erlaut Date: Thu, 5 May 2022 10:39:52 +0100 Subject: [PATCH 3/7] add tests for load cellsets --- .../__snapshots__/loadCellSets.test.js.snap | 23 ++++++++++- .../actions/cellSets/loadCellSets.test.js | 38 ++++++++++++++----- 2 files changed, 49 insertions(+), 12 deletions(-) diff --git a/src/__test__/redux/actions/cellSets/__snapshots__/loadCellSets.test.js.snap b/src/__test__/redux/actions/cellSets/__snapshots__/loadCellSets.test.js.snap index 08b1e4ea6d..624142b2d6 100644 --- a/src/__test__/redux/actions/cellSets/__snapshots__/loadCellSets.test.js.snap +++ b/src/__test__/redux/actions/cellSets/__snapshots__/loadCellSets.test.js.snap @@ -1,6 +1,18 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`loadCellSets action Dispatches a loaded action when run with the initial state. 1`] = `undefined`; +exports[`loadCellSets action Dispatches a loaded action when run with the initial state. 1`] = ` +Object { + "payload": Object { + "data": Object { + "cellIds": Object {}, + "color": "#ff0000", + "name": "one", + }, + "experimentId": "1234", + }, + "type": "cellSets/loaded", +} +`; exports[`loadCellSets action Dispatches a loading action when run after an error condition. 1`] = ` Object { @@ -8,4 +20,11 @@ Object { } `; -exports[`loadCellSets action Dispatches an error condition if fetch fails 1`] = `undefined`; +exports[`loadCellSets action Dispatches an error condition if fetch fails 1`] = ` +Object { + "payload": Object { + "error": "We couldn't get the list of cell sets.", + }, + "type": "cellSets/error", +} +`; diff --git a/src/__test__/redux/actions/cellSets/loadCellSets.test.js b/src/__test__/redux/actions/cellSets/loadCellSets.test.js index bbe2521cca..7dc699696e 100644 --- a/src/__test__/redux/actions/cellSets/loadCellSets.test.js +++ b/src/__test__/redux/actions/cellSets/loadCellSets.test.js @@ -4,15 +4,20 @@ import fetchMock, { enableFetchMocks } from 'jest-fetch-mock'; import loadCellSets from 'redux/actions/cellSets/loadCellSets'; import initialState from 'redux/reducers/cellSets/initialState'; +import config from 'config'; +import { api } from 'utils/constants'; + import '__test__/test-utils/setupTests'; +jest.mock('config'); + enableFetchMocks(); const mockStore = configureStore([thunk]); describe('loadCellSets action', () => { const experimentId = '1234'; - const cellSet = { + const cellSets = { name: 'one', color: '#ff0000', cellIds: new Set([1, 2, 3]), }; @@ -23,7 +28,9 @@ describe('loadCellSets action', () => { }; beforeEach(() => { - const response = new Response(JSON.stringify({})); + const response = new Response(JSON.stringify({ cellSets })); + + config.currentApiVersion = api.V1; fetchMock.resetMocks(); fetchMock.doMock(); @@ -32,14 +39,14 @@ describe('loadCellSets action', () => { it('Does not dispatch on normal operation', async () => { const store = mockStore({ cellSets: { loading: false, error: false }, experimentSettings }); - store.dispatch(loadCellSets(experimentId, cellSet.name, cellSet.color, cellSet.cellIds)); + await store.dispatch(loadCellSets(experimentId)); expect(store.getActions().length).toEqual(0); }); - it('Dispatches on loading state', async () => { - const store = mockStore({ cellSets: { loading: true, error: false }, experimentSettings }); - await store.dispatch(loadCellSets(experimentId)); - expect(store.getActions().length).toBeGreaterThan(0); + it('Dispatches on force reload ', async () => { + const store = mockStore({ cellSets: { loading: false, error: false }, experimentSettings }); + await store.dispatch(loadCellSets(experimentId, true)); + expect(store.getActions().length).toEqual(1); }); it('Dispatches on loading and error state', async () => { @@ -50,7 +57,7 @@ describe('loadCellSets action', () => { it('Dispatches a loading action when run after an error condition.', async () => { const store = mockStore({ cellSets: { loading: false, error: true }, experimentSettings }); - store.dispatch(loadCellSets(experimentId)); + await store.dispatch(loadCellSets(experimentId)); const firstAction = store.getActions()[0]; @@ -59,7 +66,7 @@ describe('loadCellSets action', () => { it('Dispatches a loaded action when run with the initial state.', async () => { const store = mockStore({ cellSets: initialState, experimentSettings }); - store.dispatch(loadCellSets(experimentId)); + await store.dispatch(loadCellSets(experimentId)); const firstAction = store.getActions()[0]; @@ -72,9 +79,20 @@ describe('loadCellSets action', () => { fetchMock.resetMocks(); fetchMock.mockReject(new Error('some weird error that happened')); - store.dispatch(loadCellSets(experimentId)); + await store.dispatch(loadCellSets(experimentId)); const firstAction = store.getActions()[0]; expect(firstAction).toMatchSnapshot(); }); + + it('Uses V2 URL when using API version V2', async () => { + config.currentApiVersion = api.V2; + + const store = mockStore({ cellSets: initialState, experimentSettings }); + await store.dispatch(loadCellSets(experimentId)); + + const fetchUrl = fetchMock.mock.calls[0][0]; + + expect(fetchUrl).toMatch(/v2\/experiments\/.*\/cellSets/); + }); }); From d470f9a5ab2050f904c853d8a40e2182ba58cb57 Mon Sep 17 00:00:00 2001 From: Anugerah Erlaut Date: Thu, 5 May 2022 10:58:06 +0100 Subject: [PATCH 4/7] add tests for patch --- .../__snapshots__/createCellSet.test.js.snap | 10 +++++++ .../__snapshots__/deleteCellSet.test.js.snap | 10 +++++++ .../__snapshots__/reorderCellSet.test.js.snap | 10 +++++++ .../updateCellSetProperty.test.js.snap | 10 +++++++ .../actions/cellSets/createCellSet.test.js | 25 ++++++++++++++-- .../actions/cellSets/deleteCellSet.test.js | 24 ++++++++++++++- .../actions/cellSets/reorderCellSet.test.js | 28 +++++++++++++++-- .../cellSets/updateCellSetProperty.test.js | 30 +++++++++++++++++-- 8 files changed, 138 insertions(+), 9 deletions(-) diff --git a/src/__test__/redux/actions/cellSets/__snapshots__/createCellSet.test.js.snap b/src/__test__/redux/actions/cellSets/__snapshots__/createCellSet.test.js.snap index 9766af1146..6dfdb96618 100644 --- a/src/__test__/redux/actions/cellSets/__snapshots__/createCellSet.test.js.snap +++ b/src/__test__/redux/actions/cellSets/__snapshots__/createCellSet.test.js.snap @@ -26,3 +26,13 @@ Object { "method": "PATCH", } `; + +exports[`createCellSet action Uses V2 URL when using API version V2 1`] = ` +Object { + "body": "[{\\"$match\\":{\\"query\\":\\"$[?(@.key == \\\\\\"scratchpad\\\\\\")]\\",\\"value\\":{\\"children\\":[{\\"$insert\\":{\\"index\\":\\"-\\",\\"value\\":{\\"key\\":\\"some-uuid\\",\\"name\\":\\"one\\",\\"color\\":\\"#ff0000\\",\\"type\\":\\"cellSets\\",\\"cellIds\\":[1,2,3]}}}]}}}]", + "headers": Object { + "Content-Type": "application/boschni-json-merger+json", + }, + "method": "PATCH", +} +`; diff --git a/src/__test__/redux/actions/cellSets/__snapshots__/deleteCellSet.test.js.snap b/src/__test__/redux/actions/cellSets/__snapshots__/deleteCellSet.test.js.snap index e8776c94f8..f08e2ca00a 100644 --- a/src/__test__/redux/actions/cellSets/__snapshots__/deleteCellSet.test.js.snap +++ b/src/__test__/redux/actions/cellSets/__snapshots__/deleteCellSet.test.js.snap @@ -18,3 +18,13 @@ Object { "method": "PATCH", } `; + +exports[`deleteCellSet action Uses V2 URL when using API version V2 1`] = ` +Object { + "body": "[{\\"$match\\":{\\"query\\":\\"$[?(@.key == \\\\\\"scratchpad\\\\\\")]\\",\\"value\\":{\\"children\\":[{\\"$match\\":{\\"query\\":\\"$[?(@.key == \\\\\\"my-key\\\\\\")]\\",\\"value\\":{\\"$remove\\":true}}}]}}}]", + "headers": Object { + "Content-Type": "application/boschni-json-merger+json", + }, + "method": "PATCH", +} +`; diff --git a/src/__test__/redux/actions/cellSets/__snapshots__/reorderCellSet.test.js.snap b/src/__test__/redux/actions/cellSets/__snapshots__/reorderCellSet.test.js.snap index 081e79c900..e33fdfd5de 100644 --- a/src/__test__/redux/actions/cellSets/__snapshots__/reorderCellSet.test.js.snap +++ b/src/__test__/redux/actions/cellSets/__snapshots__/reorderCellSet.test.js.snap @@ -1,5 +1,15 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`reorderCellSet action Uses V2 URL when using API version V2 1`] = ` +Object { + "body": "[{\\"$match\\":{\\"query\\":\\"$[?(@.key == \\\\\\"root\\\\\\")]\\",\\"value\\":{\\"children\\":[{\\"$match\\":{\\"query\\":\\"$[?(@.key == \\\\\\"child\\\\\\")]\\",\\"value\\":{\\"$move\\":5}}}]}}}]", + "headers": Object { + "Content-Type": "application/boschni-json-merger+json", + }, + "method": "PATCH", +} +`; + exports[`reorderCellSet action Works correctly 1`] = ` Object { "payload": Object { diff --git a/src/__test__/redux/actions/cellSets/__snapshots__/updateCellSetProperty.test.js.snap b/src/__test__/redux/actions/cellSets/__snapshots__/updateCellSetProperty.test.js.snap index 109e078083..99cf17a9ed 100644 --- a/src/__test__/redux/actions/cellSets/__snapshots__/updateCellSetProperty.test.js.snap +++ b/src/__test__/redux/actions/cellSets/__snapshots__/updateCellSetProperty.test.js.snap @@ -33,3 +33,13 @@ Object { "method": "PATCH", } `; + +exports[`updateCellSetProperty action Uses V2 URL when using API version V2 1`] = ` +Object { + "body": "[{\\"$match\\":{\\"query\\":\\"$[?(@.key == \\\\\\"root\\\\\\")]\\",\\"value\\":{\\"children\\":[{\\"$match\\":{\\"query\\":\\"$[?(@.key == \\\\\\"child\\\\\\")]\\",\\"value\\":{\\"name\\":\\"Some node!\\"}}}]}}}]", + "headers": Object { + "Content-Type": "application/boschni-json-merger+json", + }, + "method": "PATCH", +} +`; diff --git a/src/__test__/redux/actions/cellSets/createCellSet.test.js b/src/__test__/redux/actions/cellSets/createCellSet.test.js index 9d345ecdaa..1b2ce6bb55 100644 --- a/src/__test__/redux/actions/cellSets/createCellSet.test.js +++ b/src/__test__/redux/actions/cellSets/createCellSet.test.js @@ -4,12 +4,17 @@ import fetchMock, { enableFetchMocks } from 'jest-fetch-mock'; import createCellSet from 'redux/actions/cellSets/createCellSet'; import initialState from 'redux/reducers/cellSets/initialState'; +import config from 'config'; +import { api } from 'utils/constants'; + import uuid from 'uuid'; import '__test__/test-utils/setupTests'; enableFetchMocks(); +jest.mock('config'); + jest.mock('uuid', () => jest.fn()); uuid.v4 = jest.fn(() => 'some-uuid'); @@ -25,6 +30,8 @@ describe('createCellSet action', () => { beforeEach(() => { const response = new Response(JSON.stringify({})); + config.currentApiVersion = api.V1; + fetchMock.resetMocks(); fetchMock.doMock(); fetchMock.mockResolvedValueOnce(response); @@ -55,11 +62,25 @@ describe('createCellSet action', () => { const store = mockStore({ cellSets: { ...initialState, loading: false } }); await store.dispatch(createCellSet(experimentId, cellSet.name, cellSet.color, cellSet.cellIds)); - expect(fetch).toHaveBeenCalledTimes(1); + expect(fetchMock).toHaveBeenCalledTimes(1); + + const [url, body] = fetch.mock.calls[0]; + + expect(url).toMatch(/v1\/experiments\/.*\/cellSets/); + expect(body).toMatchSnapshot(); + }); + + it('Uses V2 URL when using API version V2', async () => { + config.currentApiVersion = api.V2; + + const store = mockStore({ cellSets: { ...initialState, loading: false } }); + await store.dispatch(createCellSet(experimentId, cellSet.name, cellSet.color, cellSet.cellIds)); + + expect(fetchMock).toHaveBeenCalledTimes(1); const [url, body] = fetch.mock.calls[0]; - expect(url).toEqual('http://localhost:3000/v1/experiments/1234/cellSets'); + expect(url).toMatch(/v2\/experiments\/.*\/cellSets/); expect(body).toMatchSnapshot(); }); }); diff --git a/src/__test__/redux/actions/cellSets/deleteCellSet.test.js b/src/__test__/redux/actions/cellSets/deleteCellSet.test.js index be7b4d3e12..bcf63c4246 100644 --- a/src/__test__/redux/actions/cellSets/deleteCellSet.test.js +++ b/src/__test__/redux/actions/cellSets/deleteCellSet.test.js @@ -2,6 +2,9 @@ import configureStore from 'redux-mock-store'; import thunk from 'redux-thunk'; import fetchMock, { enableFetchMocks } from 'jest-fetch-mock'; +import config from 'config'; +import { api } from 'utils/constants'; + import waitForActions from 'redux-mock-store-await-actions'; import { CELL_SETS_DELETE } from 'redux/actionTypes/cellSets'; @@ -11,6 +14,9 @@ import deleteCellSet from 'redux/actions/cellSets/deleteCellSet'; import '__test__/test-utils/setupTests'; enableFetchMocks(); + +jest.mock('config'); + const mockStore = configureStore([thunk]); describe('deleteCellSet action', () => { @@ -20,6 +26,8 @@ describe('deleteCellSet action', () => { beforeEach(() => { const response = new Response(JSON.stringify({})); + config.currentApiVersion = api.V1; + fetchMock.resetMocks(); fetchMock.doMock(); fetchMock.mockResolvedValueOnce(response); @@ -56,7 +64,21 @@ describe('deleteCellSet action', () => { const [url, body] = fetch.mock.calls[0]; - expect(url).toEqual('http://localhost:3000/v1/experiments/1234/cellSets'); + expect(url).toMatch(/v1\/experiments\/.*\/cellSets/); + expect(body).toMatchSnapshot(); + }); + + it('Uses V2 URL when using API version V2', async () => { + config.currentApiVersion = api.V2; + + const store = mockStore({ cellSets: { ...initialState, loading: false } }); + await store.dispatch(deleteCellSet(experimentId, key)); + + expect(fetchMock).toHaveBeenCalledTimes(1); + + const [url, body] = fetch.mock.calls[0]; + + expect(url).toMatch(/v2\/experiments\/.*\/cellSets/); expect(body).toMatchSnapshot(); }); }); diff --git a/src/__test__/redux/actions/cellSets/reorderCellSet.test.js b/src/__test__/redux/actions/cellSets/reorderCellSet.test.js index 34e7c46e76..05fd7bb382 100644 --- a/src/__test__/redux/actions/cellSets/reorderCellSet.test.js +++ b/src/__test__/redux/actions/cellSets/reorderCellSet.test.js @@ -4,6 +4,9 @@ import fetchMock, { enableFetchMocks } from 'jest-fetch-mock'; import waitForActions from 'redux-mock-store-await-actions'; import { waitFor } from '@testing-library/react'; +import config from 'config'; +import { api } from 'utils/constants'; + import reorderCellSet from 'redux/actions/cellSets/reorderCellSet'; import { CELL_SETS_REORDER } from 'redux/actionTypes/cellSets'; import pushNotificationMessage from 'utils/pushNotificationMessage'; @@ -11,6 +14,9 @@ import pushNotificationMessage from 'utils/pushNotificationMessage'; import initialState from 'redux/reducers/cellSets/initialState'; enableFetchMocks(); + +jest.mock('config'); + const mockStore = configureStore([thunk]); describe('reorderCellSet action', () => { @@ -36,6 +42,8 @@ describe('reorderCellSet action', () => { beforeEach(() => { jest.resetAllMocks(); + config.currentApiVersion = api.V1; + fetchMock.resetMocks(); fetchMock.doMock(); }); @@ -51,10 +59,10 @@ describe('reorderCellSet action', () => { expect(store.getActions()[0]).toMatchSnapshot(); // Fetch was called correctly - expect(fetch).toHaveBeenCalledTimes(1); + expect(fetchMock).toHaveBeenCalledTimes(1); - const [url, body] = fetch.mock.calls[0]; - expect(url).toEqual('http://localhost:3000/v1/experiments/1234/cellSets'); + const [url, body] = fetchMock.mock.calls[0]; + expect(url).toMatch(/v1\/experiments\/.*\/cellSets/); expect(body).toMatchSnapshot(); }); @@ -72,4 +80,18 @@ describe('reorderCellSet action', () => { expect(store.getActions()).toHaveLength(0); }); + + it('Uses V2 URL when using API version V2', async () => { + config.currentApiVersion = api.V2; + + const store = mockStore({ cellSets: { ...cellSetsNodeState, loading: false } }); + await store.dispatch(reorderCellSet(experimentId, cellSetKey, 5)); + + expect(fetchMock).toHaveBeenCalledTimes(1); + + const [url, body] = fetch.mock.calls[0]; + + expect(url).toMatch(/v2\/experiments\/.*\/cellSets/); + expect(body).toMatchSnapshot(); + }); }); diff --git a/src/__test__/redux/actions/cellSets/updateCellSetProperty.test.js b/src/__test__/redux/actions/cellSets/updateCellSetProperty.test.js index c5a387ecdd..cb8b7e42da 100644 --- a/src/__test__/redux/actions/cellSets/updateCellSetProperty.test.js +++ b/src/__test__/redux/actions/cellSets/updateCellSetProperty.test.js @@ -3,6 +3,9 @@ import thunk from 'redux-thunk'; import fetchMock, { enableFetchMocks } from 'jest-fetch-mock'; import waitForActions from 'redux-mock-store-await-actions'; +import config from 'config'; +import { api } from 'utils/constants'; + import updateCellSetProperty from 'redux/actions/cellSets/updateCellSetProperty'; import { CELL_SETS_UPDATE_PROPERTY } from 'redux/actionTypes/cellSets'; @@ -11,6 +14,9 @@ import initialState from 'redux/reducers/cellSets/initialState'; import '__test__/test-utils/setupTests'; enableFetchMocks(); + +jest.mock('config'); + const mockStore = configureStore([thunk]); describe('updateCellSetProperty action', () => { @@ -35,6 +41,8 @@ describe('updateCellSetProperty action', () => { beforeEach(() => { const response = new Response(JSON.stringify({})); + config.currentApiVersion = api.V1; + fetchMock.resetMocks(); fetchMock.doMock(); fetchMock.mockResolvedValueOnce(response); @@ -79,11 +87,11 @@ describe('updateCellSetProperty action', () => { await waitForActions(store, [CELL_SETS_UPDATE_PROPERTY]); - expect(fetch).toHaveBeenCalledTimes(1); + expect(fetchMock).toHaveBeenCalledTimes(1); - const [url, body] = fetch.mock.calls[0]; + const [url, body] = fetchMock.mock.calls[0]; - expect(url).toEqual('http://localhost:3000/v1/experiments/1234/cellSets'); + expect(url).toMatch(/v1\/experiments\/.*\/cellSets/); expect(body).toMatchSnapshot(); }); @@ -102,4 +110,20 @@ describe('updateCellSetProperty action', () => { await store.dispatch(updateCellSetProperty(experimentId, childKey, { parentNodeKey: 'someOtherParent' })); }).rejects.toThrow(); }); + + it('Uses V2 URL when using API version V2', async () => { + config.currentApiVersion = api.V2; + + const store = mockStore({ cellSets: { ...cellSetsNodeState, loading: false } }); + await store.dispatch(updateCellSetProperty(experimentId, childKey, property)); + + await waitForActions(store, [CELL_SETS_UPDATE_PROPERTY]); + + expect(fetchMock).toHaveBeenCalledTimes(1); + + const [url, body] = fetchMock.mock.calls[0]; + + expect(url).toMatch(/v2\/experiments\/.*\/cellSets/); + expect(body).toMatchSnapshot(); + }); }); From 812fbbf036f2c08fc22090a83663fc54f75187cf Mon Sep 17 00:00:00 2001 From: Anugerah Erlaut Date: Fri, 6 May 2022 11:35:59 +0100 Subject: [PATCH 5/7] properly format samples from v2 --- src/redux/actions/samples/loadSamples.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/redux/actions/samples/loadSamples.js b/src/redux/actions/samples/loadSamples.js index afbd03afdd..992397ce63 100644 --- a/src/redux/actions/samples/loadSamples.js +++ b/src/redux/actions/samples/loadSamples.js @@ -10,8 +10,6 @@ import { } from 'redux/actionTypes/samples'; const toApiV1 = (samples, experimentId) => { - const apiV1Samples = [{ samples: {} }]; - const buildApiv1Files = (files) => { const fileNames = []; const apiV1Files = {}; @@ -47,9 +45,9 @@ const toApiV1 = (samples, experimentId) => { throw new Error('Unknown sample technology'); }; - samples.forEach((sample) => { + const apiV1Samples = samples.reduce((acc, sample) => { const { apiV1Files, fileNames } = buildApiv1Files(sample.files); - apiV1Samples[0].samples[sample.id] = { + acc[sample.id] = { projectUuid: experimentId, metadata: sample.metadata, createdDate: sample.createdAt, @@ -60,7 +58,9 @@ const toApiV1 = (samples, experimentId) => { fileNames, uuid: sample.id, }; - }); + + return acc; + }, {}); return apiV1Samples; }; From 52e492dff4eec00c6bc6bff3e873ed2be7115ed2 Mon Sep 17 00:00:00 2001 From: Anugerah Erlaut Date: Fri, 6 May 2022 12:32:42 +0100 Subject: [PATCH 6/7] revert changes to loadSamples --- src/redux/actions/samples/loadSamples.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/redux/actions/samples/loadSamples.js b/src/redux/actions/samples/loadSamples.js index 992397ce63..afbd03afdd 100644 --- a/src/redux/actions/samples/loadSamples.js +++ b/src/redux/actions/samples/loadSamples.js @@ -10,6 +10,8 @@ import { } from 'redux/actionTypes/samples'; const toApiV1 = (samples, experimentId) => { + const apiV1Samples = [{ samples: {} }]; + const buildApiv1Files = (files) => { const fileNames = []; const apiV1Files = {}; @@ -45,9 +47,9 @@ const toApiV1 = (samples, experimentId) => { throw new Error('Unknown sample technology'); }; - const apiV1Samples = samples.reduce((acc, sample) => { + samples.forEach((sample) => { const { apiV1Files, fileNames } = buildApiv1Files(sample.files); - acc[sample.id] = { + apiV1Samples[0].samples[sample.id] = { projectUuid: experimentId, metadata: sample.metadata, createdDate: sample.createdAt, @@ -58,9 +60,7 @@ const toApiV1 = (samples, experimentId) => { fileNames, uuid: sample.id, }; - - return acc; - }, {}); + }); return apiV1Samples; }; From 9a1cfa17ce3b98d65541796db84fb17f1465ebe6 Mon Sep 17 00:00:00 2001 From: Anugerah Erlaut Date: Mon, 9 May 2022 13:52:14 +0100 Subject: [PATCH 7/7] use hardcoded urls for tests --- src/__test__/redux/actions/cellSets/createCellSet.test.js | 4 ++-- src/__test__/redux/actions/cellSets/deleteCellSet.test.js | 4 ++-- src/__test__/redux/actions/cellSets/loadCellSets.test.js | 2 +- src/__test__/redux/actions/cellSets/reorderCellSet.test.js | 4 ++-- .../redux/actions/cellSets/updateCellSetProperty.test.js | 4 ++-- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/__test__/redux/actions/cellSets/createCellSet.test.js b/src/__test__/redux/actions/cellSets/createCellSet.test.js index 1b2ce6bb55..5c988613ec 100644 --- a/src/__test__/redux/actions/cellSets/createCellSet.test.js +++ b/src/__test__/redux/actions/cellSets/createCellSet.test.js @@ -66,7 +66,7 @@ describe('createCellSet action', () => { const [url, body] = fetch.mock.calls[0]; - expect(url).toMatch(/v1\/experiments\/.*\/cellSets/); + expect(url).toEqual('http://localhost:3000/v1/experiments/1234/cellSets'); expect(body).toMatchSnapshot(); }); @@ -80,7 +80,7 @@ describe('createCellSet action', () => { const [url, body] = fetch.mock.calls[0]; - expect(url).toMatch(/v2\/experiments\/.*\/cellSets/); + expect(url).toEqual('http://localhost:3000/v2/experiments/1234/cellSets'); expect(body).toMatchSnapshot(); }); }); diff --git a/src/__test__/redux/actions/cellSets/deleteCellSet.test.js b/src/__test__/redux/actions/cellSets/deleteCellSet.test.js index bcf63c4246..b22763ab46 100644 --- a/src/__test__/redux/actions/cellSets/deleteCellSet.test.js +++ b/src/__test__/redux/actions/cellSets/deleteCellSet.test.js @@ -64,7 +64,7 @@ describe('deleteCellSet action', () => { const [url, body] = fetch.mock.calls[0]; - expect(url).toMatch(/v1\/experiments\/.*\/cellSets/); + expect(url).toEqual('http://localhost:3000/v1/experiments/1234/cellSets'); expect(body).toMatchSnapshot(); }); @@ -78,7 +78,7 @@ describe('deleteCellSet action', () => { const [url, body] = fetch.mock.calls[0]; - expect(url).toMatch(/v2\/experiments\/.*\/cellSets/); + expect(url).toEqual('http://localhost:3000/v2/experiments/1234/cellSets'); expect(body).toMatchSnapshot(); }); }); diff --git a/src/__test__/redux/actions/cellSets/loadCellSets.test.js b/src/__test__/redux/actions/cellSets/loadCellSets.test.js index 7dc699696e..40519ffa7a 100644 --- a/src/__test__/redux/actions/cellSets/loadCellSets.test.js +++ b/src/__test__/redux/actions/cellSets/loadCellSets.test.js @@ -93,6 +93,6 @@ describe('loadCellSets action', () => { const fetchUrl = fetchMock.mock.calls[0][0]; - expect(fetchUrl).toMatch(/v2\/experiments\/.*\/cellSets/); + expect(fetchUrl).toEqual('http://localhost:3000/v2/experiments/1234/cellSets'); }); }); diff --git a/src/__test__/redux/actions/cellSets/reorderCellSet.test.js b/src/__test__/redux/actions/cellSets/reorderCellSet.test.js index 05fd7bb382..22b41399c0 100644 --- a/src/__test__/redux/actions/cellSets/reorderCellSet.test.js +++ b/src/__test__/redux/actions/cellSets/reorderCellSet.test.js @@ -62,7 +62,7 @@ describe('reorderCellSet action', () => { expect(fetchMock).toHaveBeenCalledTimes(1); const [url, body] = fetchMock.mock.calls[0]; - expect(url).toMatch(/v1\/experiments\/.*\/cellSets/); + expect(url).toEqual('http://localhost:3000/v1/experiments/1234/cellSets'); expect(body).toMatchSnapshot(); }); @@ -91,7 +91,7 @@ describe('reorderCellSet action', () => { const [url, body] = fetch.mock.calls[0]; - expect(url).toMatch(/v2\/experiments\/.*\/cellSets/); + expect(url).toEqual('http://localhost:3000/v2/experiments/1234/cellSets'); expect(body).toMatchSnapshot(); }); }); diff --git a/src/__test__/redux/actions/cellSets/updateCellSetProperty.test.js b/src/__test__/redux/actions/cellSets/updateCellSetProperty.test.js index cb8b7e42da..e375176f6f 100644 --- a/src/__test__/redux/actions/cellSets/updateCellSetProperty.test.js +++ b/src/__test__/redux/actions/cellSets/updateCellSetProperty.test.js @@ -91,7 +91,7 @@ describe('updateCellSetProperty action', () => { const [url, body] = fetchMock.mock.calls[0]; - expect(url).toMatch(/v1\/experiments\/.*\/cellSets/); + expect(url).toEqual('http://localhost:3000/v1/experiments/1234/cellSets'); expect(body).toMatchSnapshot(); }); @@ -123,7 +123,7 @@ describe('updateCellSetProperty action', () => { const [url, body] = fetchMock.mock.calls[0]; - expect(url).toMatch(/v2\/experiments\/.*\/cellSets/); + expect(url).toEqual('http://localhost:3000/v2/experiments/1234/cellSets'); expect(body).toMatchSnapshot(); }); });