From cfacd80d75de8004e85206c16f530617300ea0e2 Mon Sep 17 00:00:00 2001 From: Le Roux Bodenstein Date: Wed, 26 Mar 2025 13:47:28 +0000 Subject: [PATCH] use switch-exhaustiveness-check, add updatemanu to fixtures --- .../.eslintrc.js | 8 +++++ .../src/hooks/use-grid-filters.spec.tsx | 4 +-- .../src/index.spec.tsx | 16 +++++++--- .../src/stores/delete-item.ts | 13 +++++--- .../src/stores/edit-item.ts | 19 +++++++----- .../src/stores/open-item.ts | 17 +++++----- .../test/fixtures.ts | 31 +++++++++++++++++++ 7 files changed, 82 insertions(+), 26 deletions(-) diff --git a/packages/compass-saved-aggregations-queries/.eslintrc.js b/packages/compass-saved-aggregations-queries/.eslintrc.js index b54d314ab18..7f3f9408aa1 100644 --- a/packages/compass-saved-aggregations-queries/.eslintrc.js +++ b/packages/compass-saved-aggregations-queries/.eslintrc.js @@ -6,4 +6,12 @@ module.exports = { tsconfigRootDir: __dirname, project: ['./tsconfig-lint.json'], }, + overrides: [ + { + files: ['**/*.ts'], + rules: { + '@typescript-eslint/switch-exhaustiveness-check': 'error', + }, + }, + ], }; diff --git a/packages/compass-saved-aggregations-queries/src/hooks/use-grid-filters.spec.tsx b/packages/compass-saved-aggregations-queries/src/hooks/use-grid-filters.spec.tsx index b5cbf3dcfcf..9c76660edc7 100644 --- a/packages/compass-saved-aggregations-queries/src/hooks/use-grid-filters.spec.tsx +++ b/packages/compass-saved-aggregations-queries/src/hooks/use-grid-filters.spec.tsx @@ -83,7 +83,7 @@ describe('use-grid-header', function () { result.current.search ) ).result.current.map((x) => x.item); - expect(gridItems).to.have.length(4); + expect(gridItems).to.have.length(5); }); it('should filter items by search text - collection name', function () { @@ -113,7 +113,7 @@ describe('use-grid-header', function () { result.current.search ) ).result.current.map((x) => x.item); - expect(gridItems).to.have.length(4); + expect(gridItems).to.have.length(5); }); it('should not filter items by search text - sort key (num_of_host_spaces)', function () { diff --git a/packages/compass-saved-aggregations-queries/src/index.spec.tsx b/packages/compass-saved-aggregations-queries/src/index.spec.tsx index 2547dd18d96..82ed378a06c 100644 --- a/packages/compass-saved-aggregations-queries/src/index.spec.tsx +++ b/packages/compass-saved-aggregations-queries/src/index.spec.tsx @@ -41,7 +41,7 @@ function getConnection() { }; } -describe('AggregationsQueriesList', function () { +describe('AggregationsAndQueriesAndUpdatemanyList', function () { const sandbox = Sinon.createSandbox(); const query = { _id: '123', @@ -51,6 +51,14 @@ describe('AggregationsQueriesList', function () { filter: { foo: 'bar' }, sort: { bar: -1 }, } as any; + const updatemany = { + _id: '5667', + _name: 'Updatemany', + _ns: 'bar.baz', + _dateSaved: new Date(), + filter: { foo: 'baz' }, + sort: { baz: -1 }, + } as any; const aggregation = { id: '123', name: 'Aggregation', @@ -153,7 +161,7 @@ describe('AggregationsQueriesList', function () { }); it('should load queries and display them in the list', async function () { - sandbox.stub(queryStorage, 'loadAll').resolves([query]); + sandbox.stub(queryStorage, 'loadAll').resolves([query, updatemany]); renderPlugin(); expect(await screen.findByText('Query')).to.exist; await waitFor(() => expect(screen.findByText(query._name)).to.exist); @@ -168,7 +176,7 @@ describe('AggregationsQueriesList', function () { describe('copy to clipboard', function () { it('should copy query to the clipboard', async function () { - sandbox.stub(queryStorage, 'loadAll').resolves([query]); + sandbox.stub(queryStorage, 'loadAll').resolves([query, updatemany]); renderPlugin(); expect(await screen.findByText(query._name)).to.exist; @@ -386,7 +394,7 @@ describe('AggregationsQueriesList', function () { }; beforeEach(function () { - sandbox.stub(queryStorage, 'loadAll').resolves([query]); + sandbox.stub(queryStorage, 'loadAll').resolves([query, updatemany]); }); context('when not connected to any connection', function () { diff --git a/packages/compass-saved-aggregations-queries/src/stores/delete-item.ts b/packages/compass-saved-aggregations-queries/src/stores/delete-item.ts index ba8e1b646de..61ff9a39e06 100644 --- a/packages/compass-saved-aggregations-queries/src/stores/delete-item.ts +++ b/packages/compass-saved-aggregations-queries/src/stores/delete-item.ts @@ -55,11 +55,14 @@ export const confirmDeleteItem = ( undefined // this event is connection scoped when triggered from the aggregation or query screen ); - if (item.type === 'aggregation') { - await pipelineStorage?.delete(item.id); - } else { - // query or updatemany - await queryStorage?.delete(item.id); + switch (item.type) { + case 'aggregation': + await pipelineStorage?.delete(item.id); + break; + case 'query': + case 'updatemany': + await queryStorage?.delete(item.id); + break; } dispatch({ type: ActionTypes.DeleteItemConfirm, id: item.id }); diff --git a/packages/compass-saved-aggregations-queries/src/stores/edit-item.ts b/packages/compass-saved-aggregations-queries/src/stores/edit-item.ts index a78c09fff6a..317508dc776 100644 --- a/packages/compass-saved-aggregations-queries/src/stores/edit-item.ts +++ b/packages/compass-saved-aggregations-queries/src/stores/edit-item.ts @@ -87,14 +87,17 @@ export const updateItem = return; } - if (item.type === 'aggregation') { - await pipelineStorage?.updateAttributes(id, attributes); - } else { - // query or updatemany - await queryStorage?.updateAttributes(id, { - _name: attributes.name, - _dateModified: new Date(), - }); + switch (item.type) { + case 'aggregation': + await pipelineStorage?.updateAttributes(id, attributes); + break; + case 'query': + case 'updatemany': + await queryStorage?.updateAttributes(id, { + _name: attributes.name, + _dateModified: new Date(), + }); + break; } dispatch({ diff --git a/packages/compass-saved-aggregations-queries/src/stores/open-item.ts b/packages/compass-saved-aggregations-queries/src/stores/open-item.ts index 8a9589f4204..0b82b8614d8 100644 --- a/packages/compass-saved-aggregations-queries/src/stores/open-item.ts +++ b/packages/compass-saved-aggregations-queries/src/stores/open-item.ts @@ -568,13 +568,16 @@ export const openSelectedItem = const id = selectedItem.id; const newNamespace = `${selectedDatabase}.${selectedCollection}`; - if (selectedItem.type === 'aggregation') { - await pipelineStorage?.updateAttributes(id, { - namespace: newNamespace, - }); - } else { - // query or updatemany - await queryStorage?.updateAttributes(id, { _ns: newNamespace }); + switch (selectedItem.type) { + case 'aggregation': + await pipelineStorage?.updateAttributes(id, { + namespace: newNamespace, + }); + break; + case 'query': + case 'updatemany': + await queryStorage?.updateAttributes(id, { _ns: newNamespace }); + break; } } diff --git a/packages/compass-saved-aggregations-queries/test/fixtures.ts b/packages/compass-saved-aggregations-queries/test/fixtures.ts index 4525bdec8c7..aef28748f3f 100644 --- a/packages/compass-saved-aggregations-queries/test/fixtures.ts +++ b/packages/compass-saved-aggregations-queries/test/fixtures.ts @@ -97,6 +97,37 @@ export const queries = [ }, }, }, + { + id: '9999', + name: 'update some things', + database: 'airbnb', + collection: 'hosts', + lastModified: 13, + type: 'updatemany' as const, + query: { + _id: '9012', + _name: 'update some things', + _ns: 'airbnb.hosts', + _dateSaved: DATE, + _dateModified: DATE, + _lastExecuted: DATE, + filter: { + host_location: RegExp('berlin'), + }, + project: { + id: 1, + name: 1, + }, + sort: { + reviews: -1, + }, + skip: 0, + limit: 10, + collation: { + locale: 'simple', + }, + }, + }, ]; export const pipelines = [