Skip to content

Commit 00ccee3

Browse files
committed
chore: minimize side effects from scroll to override
1 parent 76c6025 commit 00ccee3

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

packages/compass-assistant/src/assistant-chat.spec.tsx

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,22 @@ import {
77
} from '@mongodb-js/testing-library-compass';
88
import { AssistantChat } from './assistant-chat';
99
import { expect } from 'chai';
10-
import { createMockChat } from '../test/utils';
10+
import { createMockChat, withMockedScrollTo } from '../test/utils';
1111
import type { AssistantMessage } from './compass-assistant-provider';
1212

1313
describe('AssistantChat', function () {
14+
withMockedScrollTo();
15+
16+
let originalScrollTo: typeof Element.prototype.scrollTo;
1417
// Mock scrollTo method for DOM elements to prevent test failures
1518
before(function () {
16-
if (!Element.prototype.scrollTo) {
17-
Element.prototype.scrollTo = function () {
18-
// No-op implementation for testing
19-
};
20-
}
19+
originalScrollTo = Element.prototype.scrollTo.bind(Element.prototype);
20+
Element.prototype.scrollTo = () => {};
21+
});
22+
after(function () {
23+
Element.prototype.scrollTo = originalScrollTo;
2124
});
25+
2226
const mockMessages: AssistantMessage[] = [
2327
{
2428
id: 'user',

packages/compass-assistant/src/compass-assistant-provider.spec.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
} from '@mongodb-js/compass-components';
2323
import type { AtlasService } from '@mongodb-js/atlas-service/provider';
2424
import { CompassAssistantDrawer } from './compass-assistant-drawer';
25-
import { createMockChat } from '../test/utils';
25+
import { createMockChat, withMockedScrollTo } from '../test/utils';
2626

2727
// Test component that renders AssistantProvider with children
2828
const TestComponent: React.FunctionComponent<{
@@ -85,6 +85,7 @@ describe('AssistantProvider', function () {
8585
});
8686

8787
describe('with existing chat instance', function () {
88+
withMockedScrollTo();
8889
before(function () {
8990
// TODO(COMPASS-9618): skip in electron runtime for now, drawer has issues rendering
9091
if ((process as any).type === 'renderer') {
@@ -117,7 +118,7 @@ describe('AssistantProvider', function () {
117118
);
118119

119120
expect(screen.getByTestId('assistant-message-2')).to.exist;
120-
expect(screen.getByTestId('assistant-message-2')).to.have.text(
121+
expect(screen.getByTestId('assistant-message-2')).to.contain.text(
121122
'Test assistant message'
122123
);
123124
});

packages/compass-assistant/test/utils.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,17 @@ export const createMockChat = ({
1515
sendMessage: sinon.SinonStub;
1616
};
1717
};
18+
19+
export function withMockedScrollTo() {
20+
let originalScrollTo: typeof Element.prototype.scrollTo;
21+
// Mock scrollTo method for DOM elements to prevent test failures
22+
before(function () {
23+
originalScrollTo = Element.prototype.scrollTo;
24+
if (!Element.prototype.scrollTo) {
25+
Element.prototype.scrollTo = () => {};
26+
}
27+
});
28+
after(function () {
29+
Element.prototype.scrollTo = originalScrollTo;
30+
});
31+
}

0 commit comments

Comments
 (0)