From 3b8e28d12d0d2590813b18bb9a647444b020d6d5 Mon Sep 17 00:00:00 2001 From: Kevin Mas Ruiz Date: Mon, 2 Oct 2023 13:25:20 +0200 Subject: [PATCH 1/5] chore: only poll in online mode --- .../search-indexes-table.spec.tsx | 26 +++++++++++++++++-- .../search-indexes-table.tsx | 12 +++++++-- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/packages/compass-indexes/src/components/search-indexes-table/search-indexes-table.spec.tsx b/packages/compass-indexes/src/components/search-indexes-table/search-indexes-table.spec.tsx index 5aee98274fb..290be818e27 100644 --- a/packages/compass-indexes/src/components/search-indexes-table/search-indexes-table.spec.tsx +++ b/packages/compass-indexes/src/components/search-indexes-table/search-indexes-table.spec.tsx @@ -5,13 +5,14 @@ import { screen, fireEvent, within, + waitFor, } from '@testing-library/react'; import { expect } from 'chai'; import sinon from 'sinon'; import userEvent from '@testing-library/user-event'; import type { Document } from 'mongodb'; -import { SearchIndexesTable } from './search-indexes-table'; +import { POLLING_INTERVAL, SearchIndexesTable } from './search-indexes-table'; import { SearchIndexesStatuses } from '../../modules/search-indexes'; import { searchIndexes as indexes } from './../../../test/fixtures/search-indexes'; @@ -36,7 +37,7 @@ const renderIndexList = ( ); }; -describe('SearchIndexesTable Component', function () { +describe.only('SearchIndexesTable Component', function () { before(cleanup); afterEach(cleanup); @@ -184,4 +185,25 @@ describe('SearchIndexesTable Component', function () { expect(onEditIndexSpy.callCount).to.equal(1); }); }); + + describe('connectivity', function () { + it('does not poll the index for changes in offline mode', function () { + const onPollIndexesSpy = sinon.spy(); + renderIndexList({ onPollIndexes: onPollIndexesSpy, isWritable: false }); + + expect(onPollIndexesSpy.callCount).to.equal(0); + }); + + it('does poll the index for changes in online mode', async function () { + const onPollIndexesSpy = sinon.spy(); + renderIndexList({ onPollIndexes: onPollIndexesSpy, isWritable: true }); + + await waitFor( + () => { + expect(onPollIndexesSpy.callCount).to.be.greaterThanOrEqual(1); + }, + { timeout: POLLING_INTERVAL * 2 } + ); + }); + }); }); diff --git a/packages/compass-indexes/src/components/search-indexes-table/search-indexes-table.tsx b/packages/compass-indexes/src/components/search-indexes-table/search-indexes-table.tsx index 28dca9effc9..d364ad3d4d6 100644 --- a/packages/compass-indexes/src/components/search-indexes-table/search-indexes-table.tsx +++ b/packages/compass-indexes/src/components/search-indexes-table/search-indexes-table.tsx @@ -31,7 +31,11 @@ import { IndexesTable } from '../indexes-table'; import IndexActions from './search-index-actions'; import { ZeroGraphic } from './zero-graphic'; -const POLLING_INTERVAL = 5000; +export const POLLING_INTERVAL = 5000; + +function noop() { + return; +} type SearchIndexesTableProps = { indexes: SearchIndex[]; @@ -174,11 +178,15 @@ export const SearchIndexesTable: React.FunctionComponent< onPollIndexes, }) => { useEffect(() => { + if (!isWritable) { + return noop; + } + const id = setInterval(onPollIndexes, POLLING_INTERVAL); return () => { clearInterval(id); }; - }, [onPollIndexes]); + }, [onPollIndexes, isWritable]); if (!isReadyStatus(status)) { // If there's an error or the search indexes are still pending or search From 52a57d90bc97af458d02e95490e1414ac802ba3b Mon Sep 17 00:00:00 2001 From: Kevin Mas Ruiz Date: Mon, 2 Oct 2023 13:39:14 +0200 Subject: [PATCH 2/5] chore: remove .only --- .../search-indexes-table/search-indexes-table.spec.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/compass-indexes/src/components/search-indexes-table/search-indexes-table.spec.tsx b/packages/compass-indexes/src/components/search-indexes-table/search-indexes-table.spec.tsx index 290be818e27..0d650306f30 100644 --- a/packages/compass-indexes/src/components/search-indexes-table/search-indexes-table.spec.tsx +++ b/packages/compass-indexes/src/components/search-indexes-table/search-indexes-table.spec.tsx @@ -37,7 +37,7 @@ const renderIndexList = ( ); }; -describe.only('SearchIndexesTable Component', function () { +describe('SearchIndexesTable Component', function () { before(cleanup); afterEach(cleanup); From 73975cc1ae2a2c8ab1a0338b836cbb49e815886f Mon Sep 17 00:00:00 2001 From: Kevin Mas Ruiz Date: Mon, 2 Oct 2023 15:33:20 +0200 Subject: [PATCH 3/5] chore: remove unnecessary function --- .../search-indexes-table/search-indexes-table.tsx | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/packages/compass-indexes/src/components/search-indexes-table/search-indexes-table.tsx b/packages/compass-indexes/src/components/search-indexes-table/search-indexes-table.tsx index d364ad3d4d6..99f561f4803 100644 --- a/packages/compass-indexes/src/components/search-indexes-table/search-indexes-table.tsx +++ b/packages/compass-indexes/src/components/search-indexes-table/search-indexes-table.tsx @@ -33,10 +33,6 @@ import { ZeroGraphic } from './zero-graphic'; export const POLLING_INTERVAL = 5000; -function noop() { - return; -} - type SearchIndexesTableProps = { indexes: SearchIndex[]; isWritable?: boolean; @@ -179,7 +175,7 @@ export const SearchIndexesTable: React.FunctionComponent< }) => { useEffect(() => { if (!isWritable) { - return noop; + return; } const id = setInterval(onPollIndexes, POLLING_INTERVAL); From 0123c5c3ea39e4e578ccd2ce160f37374ba7aad5 Mon Sep 17 00:00:00 2001 From: Kevin Mas Ruiz Date: Wed, 4 Oct 2023 14:58:03 +0200 Subject: [PATCH 4/5] chore: refactor, so offline mode is handled in the action --- .../search-indexes-table/search-indexes-table.tsx | 6 +----- .../src/modules/search-indexes.spec.ts | 13 +++++++++++++ .../compass-indexes/src/modules/search-indexes.ts | 3 ++- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/packages/compass-indexes/src/components/search-indexes-table/search-indexes-table.tsx b/packages/compass-indexes/src/components/search-indexes-table/search-indexes-table.tsx index 99f561f4803..abbe2beaea7 100644 --- a/packages/compass-indexes/src/components/search-indexes-table/search-indexes-table.tsx +++ b/packages/compass-indexes/src/components/search-indexes-table/search-indexes-table.tsx @@ -174,15 +174,11 @@ export const SearchIndexesTable: React.FunctionComponent< onPollIndexes, }) => { useEffect(() => { - if (!isWritable) { - return; - } - const id = setInterval(onPollIndexes, POLLING_INTERVAL); return () => { clearInterval(id); }; - }, [onPollIndexes, isWritable]); + }, [onPollIndexes]); if (!isReadyStatus(status)) { // If there's an error or the search indexes are still pending or search diff --git a/packages/compass-indexes/src/modules/search-indexes.spec.ts b/packages/compass-indexes/src/modules/search-indexes.spec.ts index f343337356f..9bcf3941554 100644 --- a/packages/compass-indexes/src/modules/search-indexes.spec.ts +++ b/packages/compass-indexes/src/modules/search-indexes.spec.ts @@ -20,6 +20,7 @@ import { readonlyViewChanged } from './is-readonly-view'; // Importing this to stub showConfirmation import * as searchIndexesSlice from './search-indexes'; +import { writeStateChanged } from './is-writable'; describe('search-indexes module', function () { let store: ReturnType; @@ -68,6 +69,18 @@ describe('search-indexes module', function () { expect(store.getState().searchIndexes.status).to.equal('NOT_READY'); }); + it('does nothing if isWritable is false (offline mode)', function () { + store.dispatch(writeStateChanged(false)); + + expect(store.getState().isWritable).to.equal(false); + expect(getSearchIndexesStub.callCount).to.equal(0); + + store.dispatch(fetchSearchIndexes); + + expect(getSearchIndexesStub.callCount).to.equal(0); + expect(store.getState().searchIndexes.status).to.equal('NOT_READY'); + }); + it('does nothing if there is no dataService', function () { store.getState().dataService = null; store.dispatch(fetchSearchIndexes); diff --git a/packages/compass-indexes/src/modules/search-indexes.ts b/packages/compass-indexes/src/modules/search-indexes.ts index 96d0ae85ef9..9a0c378f043 100644 --- a/packages/compass-indexes/src/modules/search-indexes.ts +++ b/packages/compass-indexes/src/modules/search-indexes.ts @@ -518,12 +518,13 @@ const fetchIndexes = ( return async (dispatch, getState) => { const { isReadonlyView, + isWritable, dataService, namespace, searchIndexes: { sortColumn, sortOrder, status }, } = getState(); - if (isReadonlyView) { + if (isReadonlyView || !isWritable) { return; } From bb1251fd87a3b2f70899a9281c2c62250dbc9c80 Mon Sep 17 00:00:00 2001 From: Kevin Mas Ruiz Date: Wed, 4 Oct 2023 15:06:36 +0200 Subject: [PATCH 5/5] chore: removed unused text / moved logic to the action --- .../search-indexes-table/search-indexes-table.spec.tsx | 7 ------- 1 file changed, 7 deletions(-) diff --git a/packages/compass-indexes/src/components/search-indexes-table/search-indexes-table.spec.tsx b/packages/compass-indexes/src/components/search-indexes-table/search-indexes-table.spec.tsx index 0d650306f30..10baef42329 100644 --- a/packages/compass-indexes/src/components/search-indexes-table/search-indexes-table.spec.tsx +++ b/packages/compass-indexes/src/components/search-indexes-table/search-indexes-table.spec.tsx @@ -187,13 +187,6 @@ describe('SearchIndexesTable Component', function () { }); describe('connectivity', function () { - it('does not poll the index for changes in offline mode', function () { - const onPollIndexesSpy = sinon.spy(); - renderIndexList({ onPollIndexes: onPollIndexesSpy, isWritable: false }); - - expect(onPollIndexesSpy.callCount).to.equal(0); - }); - it('does poll the index for changes in online mode', async function () { const onPollIndexesSpy = sinon.spy(); renderIndexList({ onPollIndexes: onPollIndexesSpy, isWritable: true });