Skip to content

Commit b8db1ec

Browse files
committed
test: fix add and remove test
1 parent 4e98376 commit b8db1ec

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

packages/compass-components/src/components/document-list/element.spec.tsx

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ import { render, screen, userEvent } from '@mongodb-js/testing-library-compass';
33
import { expect } from 'chai';
44
import sinon from 'sinon';
55
import HadronDocument from 'hadron-document';
6-
import { HadronElement, getNestedKeyPathForElement } from './element';
6+
import {
7+
HadronElement,
8+
type QueryBarController,
9+
getNestedKeyPathForElement,
10+
} from './element';
711
import type { Element } from 'hadron-document';
812

913
describe('HadronElement', function () {
@@ -32,21 +36,18 @@ describe('HadronElement', function () {
3236

3337
// Mock queryBar controller
3438
const mockQueryBar = {
35-
isInQuery: sinon.stub(),
39+
isInQuery: sinon.stub().returns(false),
3640
toggleQueryFilter: sinon.spy(),
3741
};
3842

39-
// Initially not in query
40-
mockQueryBar.isInQuery.returns(false);
41-
4243
const { rerender } = render(
4344
<HadronElement
4445
value={nestedElement}
4546
editable={true}
4647
editingEnabled={true}
4748
lineNumberSize={1}
4849
onAddElement={() => {}}
49-
queryBar={mockQueryBar}
50+
queryBar={mockQueryBar as unknown as QueryBarController}
5051
/>
5152
);
5253

@@ -57,12 +58,10 @@ describe('HadronElement', function () {
5758
expect(screen.getByText('Add to query')).to.exist;
5859
expect(screen.queryByText('Remove from query')).to.not.exist;
5960

60-
// Click "Add to query"
6161
userEvent.click(screen.getByText('Add to query'), undefined, {
6262
skipPointerEventsCheck: true,
6363
});
6464

65-
// Verify toggleQueryFilter was called with correct parameters
6665
expect(mockQueryBar.toggleQueryFilter).to.have.been.calledWith(
6766
'user.name',
6867
nestedElement.generateObject()
@@ -79,7 +78,10 @@ describe('HadronElement', function () {
7978
editingEnabled={true}
8079
lineNumberSize={1}
8180
onAddElement={() => {}}
82-
queryBar={mockQueryBar}
81+
queryBar={{
82+
...mockQueryBar,
83+
isInQuery: sinon.stub().returns(true),
84+
}}
8385
/>
8486
);
8587

@@ -89,12 +91,10 @@ describe('HadronElement', function () {
8991
expect(screen.getByText('Remove from query')).to.exist;
9092
expect(screen.queryByText('Add to query')).to.not.exist;
9193

92-
// Click "Remove from query"
9394
userEvent.click(screen.getByText('Remove from query'), undefined, {
9495
skipPointerEventsCheck: true,
9596
});
9697

97-
// Verify toggleQueryFilter was called again
9898
expect(mockQueryBar.toggleQueryFilter).to.have.been.calledTwice;
9999
expect(mockQueryBar.toggleQueryFilter.secondCall).to.have.been.calledWith(
100100
'user.name',

packages/compass-components/src/components/document-list/element.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -504,20 +504,22 @@ export const HadronElement: React.FunctionComponent<{
504504
collapse,
505505
} = useHadronElement(element);
506506

507+
const { isInQuery, toggleQueryFilter } = queryBar || {};
508+
507509
// Add context menu hook for the field
508510
const fieldContextMenuRef = useContextMenuItems(
509511
() => [
510-
...(queryBar
512+
...(isInQuery && toggleQueryFilter
511513
? [
512514
{
513-
label: queryBar.isInQuery(
515+
label: isInQuery(
514516
getNestedKeyPathForElement(element),
515517
element.generateObject()
516518
)
517519
? 'Remove from query'
518520
: 'Add to query',
519521
onAction: () => {
520-
queryBar.toggleQueryFilter(
522+
toggleQueryFilter(
521523
getNestedKeyPathForElement(element),
522524
element.generateObject()
523525
);
@@ -544,7 +546,7 @@ export const HadronElement: React.FunctionComponent<{
544546
]
545547
: []),
546548
],
547-
[element, key.value, value.value, type.value, queryBar]
549+
[element, key.value, value.value, type.value, isInQuery, toggleQueryFilter]
548550
);
549551

550552
const toggleExpanded = () => {

0 commit comments

Comments
 (0)