Skip to content

Commit f4c6471

Browse files
authored
chore(saved-aggregations-queries): Use proxyquire to mock dependencies that cause misc issues for running tests in the package (#2745)
* chore(mocha-config-compass): Use tsconfig-patch package as a way to automatically mock dependencies This configuration will resolve anything with the matching path in the __mocks__ folder first before trying to do a real import * test(saved-aggregations-queries): Add a simple test that checks that connected list renders items from the store * chore(saved-aggregations-queries): Change test setup to use proxyquire * test(saved-aggregations-queries): Increase test timeout * chore: Prettier
1 parent c49de91 commit f4c6471

File tree

8 files changed

+152
-24
lines changed

8 files changed

+152
-24
lines changed

package-lock.json

Lines changed: 22 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1 @@
1-
const base = require('@mongodb-js/mocha-config-compass/compass-plugin');
2-
const path = require('path');
3-
4-
module.exports = {
5-
...base,
6-
require: (base.require || []).concat(
7-
[path.resolve(__dirname, 'tests', 'setup.ts')].filter(Boolean)
8-
),
9-
};
1+
module.exports = require('@mongodb-js/mocha-config-compass/compass-plugin');

packages/compass-saved-aggregations-queries/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
"test-electron": "xvfb-maybe electron-mocha --no-sandbox",
4747
"test-cov": "nyc -x \"**/*.spec.*\" --reporter=lcov --reporter=text --reporter=html npm run test",
4848
"test-watch": "npm run test -- --watch",
49-
"test-ci": "npm run test-electron",
49+
"test-ci": "npm run test-cov && npm run test-electron",
5050
"reformat": "npm run prettier -- --write ."
5151
},
5252
"peerDependencies": {
@@ -78,6 +78,7 @@
7878
"@types/chai": "^4.2.21",
7979
"@types/chai-dom": "^0.0.10",
8080
"@types/mocha": "^9.0.0",
81+
"@types/proxyquire": "^1.3.28",
8182
"@types/react": "^17.0.5",
8283
"@types/react-dom": "^17.0.10",
8384
"@types/sinon-chai": "^3.2.5",
@@ -90,6 +91,7 @@
9091
"mongodb-instance-model": "^11.18.0",
9192
"nyc": "^15.1.0",
9293
"prettier": "2.3.2",
94+
"proxyquire": "^2.1.3",
9395
"rimraf": "^3.0.2",
9496
"sinon": "^9.2.3",
9597
"xvfb-maybe": "^0.2.1"

packages/compass-saved-aggregations-queries/src/components/aggregations-queries-list.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ const row = css({
6161
const AggregationsQueriesList = ({
6262
loading,
6363
items,
64-
fetchItems,
64+
onMount,
6565
}: AggregationsQueriesListProps) => {
6666
useEffect(() => {
67-
void fetchItems();
68-
}, [fetchItems]);
67+
void onMount();
68+
}, [onMount]);
6969

7070
if (loading) {
7171
return null;
@@ -102,7 +102,7 @@ const mapState = ({ savedItems: { items, loading } }: RootState) => ({
102102
loading,
103103
});
104104

105-
const mapDispatch = { fetchItems };
105+
const mapDispatch = { onMount: fetchItems };
106106

107107
const connector = connect(mapState, mapDispatch);
108108

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import React from 'react';
2+
import { render, screen, cleanup } from '@testing-library/react';
3+
import { expect } from 'chai';
4+
import proxyquire from 'proxyquire';
5+
import { createProxyquireMockForQueriesAndAggregationsPlugins } from '../../test/mock';
6+
7+
describe('AggregationsQueriesList', function () {
8+
// Even though we are mocking dependencies, the code is still processed by
9+
// ts-node (we're just not running it) so running this suite takes more time
10+
// than is allowed by our default configuration
11+
this.timeout(30000);
12+
13+
afterEach(cleanup);
14+
15+
it('should load queries and display them in the list', async function () {
16+
const { default: ConnectedList }: any = proxyquire.load('./index', {
17+
...(createProxyquireMockForQueriesAndAggregationsPlugins(
18+
[],
19+
[
20+
{
21+
_id: '123',
22+
_name: 'Query',
23+
_ns: 'bar.foo',
24+
_dateSaved: 0,
25+
},
26+
]
27+
) as any),
28+
// XXX: It's important that the proxyquire required module has the same
29+
// instance of react that the code in this scope has, otherwise we will
30+
// get a "multiple React instances" error while trying to render the
31+
// component
32+
react: Object.assign(React, {
33+
'@global': true,
34+
'@noCallThru': true,
35+
}),
36+
});
37+
38+
render(<ConnectedList></ConnectedList>);
39+
40+
expect(await screen.findByText('Query')).to.exist;
41+
});
42+
43+
it('should load aggregations and display them in the list', async function () {
44+
const { default: ConnectedList }: any = proxyquire.load('./index', {
45+
...(createProxyquireMockForQueriesAndAggregationsPlugins(
46+
[
47+
{
48+
id: '123',
49+
name: 'Aggregation',
50+
namespace: 'foo.bar',
51+
lastModified: 0,
52+
},
53+
],
54+
[]
55+
) as any),
56+
// XXX: It's important that the proxyquire required module has the same
57+
// instance of react that the code in this scope has, otherwise we will
58+
// get a "multiple React instances" error while trying to render the
59+
// component
60+
react: Object.assign(React, {
61+
'@global': true,
62+
'@noCallThru': true,
63+
}),
64+
});
65+
66+
render(<ConnectedList></ConnectedList>);
67+
68+
expect(await screen.findByText('Aggregation')).to.exist;
69+
});
70+
});

packages/compass-saved-aggregations-queries/src/index.spec.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import { expect } from 'chai';
2-
import * as CompassPlugin from './index';
2+
import proxyquire from 'proxyquire';
3+
import { createProxyquireMockForQueriesAndAggregationsPlugins } from '../test/mock';
4+
5+
const CompassPlugin: any = proxyquire.load(
6+
'./index',
7+
createProxyquireMockForQueriesAndAggregationsPlugins([], [])
8+
);
39

410
describe('Compass Plugin', function () {
511
it('exports activate, deactivate, and metadata', function () {
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
export function createCompassAggregationsMock(pipelines: unknown[]): {
2+
readPipelinesFromStorage(): Promise<typeof pipelines>;
3+
} {
4+
return {
5+
readPipelinesFromStorage(): Promise<typeof pipelines> {
6+
return Promise.resolve(pipelines);
7+
},
8+
};
9+
}
10+
11+
interface FavoriteQueryStorageClass<T> {
12+
new (): {
13+
loadAll(): Promise<T>;
14+
};
15+
}
16+
17+
export function createCompassQueryHistoryMock(queries: unknown[]): {
18+
FavoriteQueryStorage: FavoriteQueryStorageClass<typeof queries>;
19+
} {
20+
return {
21+
FavoriteQueryStorage: class FavoriteQueryStorage {
22+
loadAll(): Promise<typeof queries> {
23+
return Promise.resolve(queries);
24+
}
25+
},
26+
};
27+
}
28+
29+
export function createProxyquireMockForQueriesAndAggregationsPlugins(
30+
pipelines: unknown[],
31+
queries: unknown[]
32+
): unknown {
33+
return {
34+
'@mongodb-js/compass-query-history': {
35+
...createCompassQueryHistoryMock(queries),
36+
'@global': true,
37+
'@noCallThru': true,
38+
},
39+
'@mongodb-js/compass-aggregations': {
40+
...createCompassAggregationsMock(pipelines),
41+
'@global': true,
42+
'@noCallThru': true,
43+
},
44+
};
45+
}

packages/compass-saved-aggregations-queries/tests/setup.ts

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

0 commit comments

Comments
 (0)