Skip to content

Commit b2db494

Browse files
authored
chore(compass-indexes): use dataService from thunk extra args, remove from store (#6192)
* use dataService from thunk extra args, remove from store * dataService always exists now * more flakyness * skip flaky test * remove unused debug * remove another unused debug
1 parent f4b48df commit b2db494

File tree

11 files changed

+72
-179
lines changed

11 files changed

+72
-179
lines changed

packages/compass-indexes/src/components/indexes/indexes.spec.tsx

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,11 @@ const DEFAULT_PROPS: Partial<RootState> = {
3232
},
3333
} as any;
3434

35-
const renderIndexes = (props: Partial<RootState> = {}) => {
36-
const store = setupStore();
35+
const renderIndexes = (
36+
props: Partial<RootState> = {},
37+
dataProvider: Partial<IndexesDataService> = {}
38+
) => {
39+
const store = setupStore(undefined, dataProvider);
3740

3841
const allProps: Partial<RootState> = {
3942
...DEFAULT_PROPS,
@@ -78,12 +81,14 @@ describe('Indexes Component', function () {
7881
});
7982

8083
it('renders indexes toolbar when there is a search indexes error', async function () {
81-
const store = renderIndexes();
82-
8384
// the component will load the search indexes the moment we switch to them
84-
store.getState()!.dataService!.getSearchIndexes = function () {
85-
return Promise.reject(new Error('This is an error.'));
85+
const getSearchIndexesStub = sinon
86+
.stub()
87+
.rejects(new Error('This is an error.'));
88+
const dataProvider = {
89+
getSearchIndexes: getSearchIndexesStub,
8690
};
91+
renderIndexes({}, dataProvider);
8792

8893
const toolbar = screen.getByTestId('indexes-toolbar');
8994
expect(toolbar).to.exist;
@@ -267,11 +272,11 @@ describe('Indexes Component', function () {
267272

268273
context('search indexes', function () {
269274
it('renders the search indexes table if the current view changes to search indexes', async function () {
270-
const store = renderIndexes();
271-
272-
store.getState()!.dataService!.getSearchIndexes = function () {
273-
return Promise.resolve(searchIndexes);
275+
const getSearchIndexesStub = sinon.stub().resolves(searchIndexes);
276+
const dataProvider = {
277+
getSearchIndexes: getSearchIndexesStub,
274278
};
279+
renderIndexes({}, dataProvider);
275280

276281
// switch to the Search Indexes tab
277282
const toolbar = screen.getByTestId('indexes-toolbar');
@@ -284,18 +289,17 @@ describe('Indexes Component', function () {
284289
});
285290

286291
it('refreshes the search indexes if the search indexes view is active', async function () {
287-
const store = renderIndexes();
288-
289-
const spy = sinon.spy(
290-
store.getState()?.dataService as IndexesDataService,
291-
'getSearchIndexes'
292-
);
292+
const getSearchIndexesStub = sinon.stub().resolves(searchIndexes);
293+
const dataProvider = {
294+
getSearchIndexes: getSearchIndexesStub,
295+
};
296+
renderIndexes({}, dataProvider);
293297

294298
// switch to the Search Indexes tab
295299
const toolbar = screen.getByTestId('indexes-toolbar');
296300
fireEvent.click(within(toolbar).getByText('Search Indexes'));
297301

298-
expect(spy.callCount).to.equal(1);
302+
expect(getSearchIndexesStub.callCount).to.equal(1);
299303

300304
// click the refresh button
301305
const refreshButton = within(toolbar).getByText('Refresh');
@@ -304,7 +308,7 @@ describe('Indexes Component', function () {
304308
);
305309
fireEvent.click(refreshButton);
306310

307-
expect(spy.callCount).to.equal(2);
311+
expect(getSearchIndexesStub.callCount).to.equal(2);
308312
});
309313

310314
it('switches to the search indexes table when a search index is created', async function () {

packages/compass-indexes/src/components/search-indexes-modals/base-search-index-modal.spec.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ describe('Base Search Index Modal', function () {
164164
});
165165
});
166166

167-
it('resets the template on type switch', async function () {
167+
// TODO(COMPASS-7557): super flaky even on mac
168+
it.skip('resets the template on type switch', async function () {
168169
userEvent.click(
169170
screen.getByTestId('search-index-type-vectorSearch-button'),
170171
undefined,

packages/compass-indexes/src/modules/data-service.spec.ts

Lines changed: 0 additions & 38 deletions
This file was deleted.

packages/compass-indexes/src/modules/data-service.ts

Lines changed: 0 additions & 29 deletions
This file was deleted.

packages/compass-indexes/src/modules/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { combineReducers } from 'redux';
22
import type { Action, AnyAction } from 'redux';
33
import type AppRegistry from 'hadron-app-registry';
4-
import dataService from './data-service';
54
import isWritable from './is-writable';
65
import indexView from './index-view';
76
import isReadonlyView from './is-readonly-view';
@@ -11,16 +10,17 @@ import searchIndexes from './search-indexes';
1110
import serverVersion from './server-version';
1211
import namespace from './namespace';
1312
import type { ThunkAction, ThunkDispatch } from 'redux-thunk';
13+
import type { DataService } from 'mongodb-data-service';
1414
import type { Logger } from '@mongodb-js/compass-logging';
1515
import type { TrackFunction } from '@mongodb-js/compass-telemetry';
1616
import type { ConnectionInfoAccess } from '@mongodb-js/compass-connections/provider';
17+
import type { IndexesDataServiceProps } from '../stores/store';
1718

1819
const reducer = combineReducers({
1920
isWritable,
2021
isReadonlyView,
2122
indexView,
2223
description,
23-
dataService,
2424
serverVersion,
2525
namespace,
2626
regularIndexes,
@@ -35,6 +35,7 @@ export type IndexesExtraArgs = {
3535
localAppRegistry: AppRegistry;
3636
logger: Logger;
3737
track: TrackFunction;
38+
dataService: Pick<DataService, IndexesDataServiceProps>;
3839
connectionInfoAccess: ConnectionInfoAccess;
3940
};
4041
export type IndexesThunkDispatch<A extends Action = AnyAction> = ThunkDispatch<

packages/compass-indexes/src/modules/regular-indexes.spec.ts

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -69,23 +69,6 @@ describe('regular-indexes module', function () {
6969
expect(indexesSpy.callCount).to.equal(0);
7070
});
7171

72-
it('when dataService is not connected, sets refreshing to false', async function () {
73-
const store = setupStore({}, {
74-
isConnected() {
75-
return false;
76-
},
77-
} as any);
78-
store.dispatch({
79-
type: ActionTypes.IndexesAdded,
80-
indexes: defaultSortedIndexes,
81-
});
82-
await store.dispatch(fetchIndexes());
83-
84-
const state = store.getState().regularIndexes;
85-
expect(state.indexes).to.deep.equal(defaultSortedIndexes);
86-
expect(state.isRefreshing).to.equal(false);
87-
});
88-
8972
it('sets indexes to empty array when there is an error', async function () {
9073
const error = new Error('failed to connect to server');
9174
const store = setupStore({}, {

packages/compass-indexes/src/modules/regular-indexes.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,9 @@ export const fetchIndexes = (): IndexesThunkAction<
209209
Promise<void>,
210210
RegularIndexesActions
211211
> => {
212-
return async (dispatch, getState, { logger: { debug } }) => {
212+
return async (dispatch, getState, { dataService }) => {
213213
const {
214214
isReadonlyView,
215-
dataService,
216215
namespace,
217216
regularIndexes: { inProgressIndexes },
218217
} = getState();
@@ -222,12 +221,6 @@ export const fetchIndexes = (): IndexesThunkAction<
222221
return;
223222
}
224223

225-
if (!dataService || !dataService.isConnected()) {
226-
dispatch(setIsRefreshing(false));
227-
debug('warning: trying to load indexes but dataService is disconnected');
228-
return;
229-
}
230-
231224
try {
232225
dispatch(setError(null));
233226
const indexes = await dataService.indexes(namespace);
@@ -304,8 +297,8 @@ export const showCreateModal = (): IndexesThunkAction<void> => {
304297
export const hideIndex = (
305298
indexName: string
306299
): IndexesThunkAction<Promise<void>> => {
307-
return async (dispatch, getState) => {
308-
const { dataService, namespace } = getState();
300+
return async (dispatch, getState, { dataService }) => {
301+
const { namespace } = getState();
309302
const confirmed = await showConfirmation({
310303
title: `Hiding \`${indexName}\``,
311304
description: hideModalDescription(indexName),
@@ -338,8 +331,8 @@ export const hideIndex = (
338331
export const unhideIndex = (
339332
indexName: string
340333
): IndexesThunkAction<Promise<void>> => {
341-
return async (dispatch, getState) => {
342-
const { namespace, dataService } = getState();
334+
return async (dispatch, getState, { dataService }) => {
335+
const { namespace } = getState();
343336
const confirmed = await showConfirmation({
344337
title: `Unhiding \`${indexName}\``,
345338
description: unhideModalDescription(indexName),

packages/compass-indexes/src/modules/search-indexes.spec.ts

Lines changed: 20 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,21 @@ import { writeStateChanged } from './is-writable';
2323
describe('search-indexes module', function () {
2424
let store: ReturnType<typeof setupStore>;
2525
let dataProvider: Partial<IndexesDataService>;
26-
let getSearchIndexesStub: any;
26+
let createSearchIndexStub: sinon.SinonStub;
27+
let updateSearchIndexStub: sinon.SinonStub;
28+
let getSearchIndexesStub: sinon.SinonStub;
29+
let dropSearchIndexStub: sinon.SinonStub;
2730

2831
beforeEach(function () {
32+
createSearchIndexStub = sinon.stub().resolves('foo');
33+
updateSearchIndexStub = sinon.stub().resolves();
34+
getSearchIndexesStub = sinon.stub().resolves(searchIndexes);
35+
dropSearchIndexStub = sinon.stub().resolves();
2936
dataProvider = {
30-
createSearchIndex: sinon.spy(),
31-
updateSearchIndex: sinon.spy(),
37+
createSearchIndex: createSearchIndexStub,
38+
updateSearchIndex: updateSearchIndexStub,
39+
getSearchIndexes: getSearchIndexesStub,
40+
dropSearchIndex: dropSearchIndexStub,
3241
};
3342

3443
store = setupStore(
@@ -38,13 +47,6 @@ describe('search-indexes module', function () {
3847
},
3948
dataProvider
4049
);
41-
42-
getSearchIndexesStub = sinon
43-
.stub(
44-
store.getState().dataService as IndexesDataService,
45-
'getSearchIndexes'
46-
)
47-
.resolves(searchIndexes);
4850
});
4951

5052
it('has not available search indexes state by default', function () {
@@ -79,12 +81,6 @@ describe('search-indexes module', function () {
7981
expect(store.getState().searchIndexes.status).to.equal('NOT_READY');
8082
});
8183

82-
it('does nothing if there is no dataService', function () {
83-
store.getState().dataService = null;
84-
store.dispatch(fetchSearchIndexes);
85-
// would throw if it tried to use it
86-
});
87-
8884
it('fetches the indexes', async function () {
8985
expect(getSearchIndexesStub.callCount).to.equal(0);
9086
expect(store.getState().searchIndexes.status).to.equal('NOT_READY');
@@ -105,17 +101,11 @@ describe('search-indexes module', function () {
105101
expect(store.getState().searchIndexes.status).to.equal('READY');
106102

107103
// replace the stub
108-
getSearchIndexesStub.restore();
109-
getSearchIndexesStub = sinon
110-
.stub(
111-
store.getState().dataService as IndexesDataService,
112-
'getSearchIndexes'
113-
)
114-
.callsFake(() => {
115-
return new Promise(() => {
116-
// never resolves
117-
});
104+
getSearchIndexesStub.callsFake(() => {
105+
return new Promise(() => {
106+
// never resolves
118107
});
108+
});
119109

120110
// not awaiting because REFRESHING happens during the action
121111
void store.dispatch(fetchSearchIndexes());
@@ -150,13 +140,7 @@ describe('search-indexes module', function () {
150140

151141
it('sets the status to ERROR if loading the indexes fails', async function () {
152142
// replace the stub
153-
getSearchIndexesStub.restore();
154-
getSearchIndexesStub = sinon
155-
.stub(
156-
store.getState().dataService as IndexesDataService,
157-
'getSearchIndexes'
158-
)
159-
.rejects(new Error('this is an error'));
143+
getSearchIndexesStub.rejects(new Error('this is an error'));
160144

161145
await store.dispatch(fetchSearchIndexes());
162146

@@ -257,29 +241,25 @@ describe('search-indexes module', function () {
257241
});
258242

259243
context('drop search index', function () {
260-
let dropSearchIndexStub: sinon.SinonStub;
261244
let showConfirmationStub: sinon.SinonStub;
262245
beforeEach(function () {
263-
dropSearchIndexStub = sinon.stub(
264-
store.getState().dataService as IndexesDataService,
265-
'dropSearchIndex'
266-
);
267246
showConfirmationStub = sinon.stub(searchIndexesSlice, 'showConfirmation');
268247
});
269248

270249
afterEach(function () {
271250
showConfirmationStub.restore();
272-
dropSearchIndexStub.restore();
273251
});
274252

275253
it('does not drop index when user does not confirm', async function () {
276-
showConfirmationStub.resolves(false);
254+
dropSearchIndexStub.resolves(false);
277255
await store.dispatch(dropSearchIndex('index_name'));
278256
expect(dropSearchIndexStub.callCount).to.equal(0);
279257
});
280258

281259
it('drops index successfully', async function () {
282260
showConfirmationStub.resolves(true);
261+
dropSearchIndexStub.resolves(true);
262+
283263
dropSearchIndexStub.resolves(true);
284264
await store.dispatch(dropSearchIndex('index_name'));
285265
expect(dropSearchIndexStub.firstCall.args).to.deep.equal([

0 commit comments

Comments
 (0)