Skip to content

Commit 4db143a

Browse files
committed
Update existing tests to handle dialog elements remaining in the DOM
1 parent 24c5eed commit 4db143a

File tree

7 files changed

+104
-29
lines changed

7 files changed

+104
-29
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { Assertion, util } from 'chai';
2+
3+
declare global {
4+
// eslint-disable-next-line @typescript-eslint/no-namespace
5+
export namespace Chai {
6+
interface Assertion {
7+
/** Asserts that a dialog is open */
8+
get open(): Assertion;
9+
/** Asserts that a dialog is closed */
10+
get closed(): Assertion;
11+
}
12+
}
13+
}
14+
15+
util.addProperty(
16+
Assertion.prototype,
17+
'open',
18+
function (this: typeof Assertion) {
19+
const obj = util.flag(this, 'object');
20+
new Assertion(obj).to.be.instanceof(HTMLDialogElement);
21+
new Assertion(obj as HTMLDialogElement).has.property(
22+
'open',
23+
true,
24+
'Expected dialog to be open'
25+
);
26+
}
27+
);
28+
29+
util.addProperty(
30+
Assertion.prototype,
31+
'closed',
32+
function (this: typeof Assertion) {
33+
const obj = util.flag(this, 'object');
34+
new Assertion(obj).to.be.instanceof(HTMLDialogElement);
35+
new Assertion(obj as HTMLDialogElement).has.property(
36+
'open',
37+
false,
38+
'Expected dialog to be closed'
39+
);
40+
}
41+
);

configs/testing-library-compass/src/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ import { expect } from 'chai';
7373
import { Provider } from 'react-redux';
7474
import ConnectionString from 'mongodb-connection-string-url';
7575

76+
import './assertions';
77+
7678
function wait(ms: number) {
7779
return new Promise((resolve) => {
7880
setTimeout(resolve, ms);

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

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
screen,
66
userEvent,
77
waitFor,
8-
waitForElementToBeRemoved,
98
within,
109
} from '@mongodb-js/testing-library-compass';
1110
import {
@@ -564,8 +563,9 @@ describe('CompassAssistantProvider', function () {
564563
userEvent.click(clearButton);
565564

566565
await waitFor(() => {
567-
expect(screen.getByTestId('assistant-confirm-clear-chat-modal')).to
568-
.exist;
566+
expect(
567+
screen.getByTestId('assistant-confirm-clear-chat-modal').firstChild
568+
).to.exist;
569569
});
570570

571571
// There should be messages in the chat
@@ -576,9 +576,11 @@ describe('CompassAssistantProvider', function () {
576576
const confirmButton = within(modal).getByText('Clear chat');
577577
userEvent.click(confirmButton);
578578

579-
await waitForElementToBeRemoved(() =>
580-
screen.getByTestId('assistant-confirm-clear-chat-modal')
581-
);
579+
await waitFor(() => {
580+
expect(
581+
screen.getByTestId('assistant-confirm-clear-chat-modal').firstChild
582+
).to.not.exist;
583+
});
582584

583585
expect(mockChat.messages).to.be.empty;
584586
expect(screen.queryByTestId('assistant-message-1')).to.not.exist;
@@ -594,8 +596,9 @@ describe('CompassAssistantProvider', function () {
594596
userEvent.click(clearButton);
595597

596598
await waitFor(() => {
597-
expect(screen.getByTestId('assistant-confirm-clear-chat-modal')).to
598-
.exist;
599+
expect(
600+
screen.getByTestId('assistant-confirm-clear-chat-modal').firstChild
601+
).to.exist;
599602
});
600603

601604
// There should be messages in the chat
@@ -606,9 +609,11 @@ describe('CompassAssistantProvider', function () {
606609
const cancelButton = within(modal).getByText('Cancel');
607610
userEvent.click(cancelButton);
608611

609-
await waitForElementToBeRemoved(() =>
610-
screen.getByTestId('assistant-confirm-clear-chat-modal')
611-
);
612+
await waitFor(() => {
613+
expect(
614+
screen.getByTestId('assistant-confirm-clear-chat-modal').firstChild
615+
).to.not.exist;
616+
});
612617

613618
expect(mockChat.messages).to.deep.equal(mockMessages);
614619
expect(screen.getByTestId('assistant-message-1')).to.exist;
@@ -626,8 +631,9 @@ describe('CompassAssistantProvider', function () {
626631
userEvent.click(clearButton);
627632

628633
await waitFor(() => {
629-
expect(screen.getByTestId('assistant-confirm-clear-chat-modal')).to
630-
.exist;
634+
expect(
635+
screen.getByTestId('assistant-confirm-clear-chat-modal').firstChild
636+
).to.exist;
631637
});
632638

633639
// There should be messages in the chat
@@ -640,9 +646,11 @@ describe('CompassAssistantProvider', function () {
640646
const confirmButton = within(modal).getByText('Clear chat');
641647
userEvent.click(confirmButton);
642648

643-
await waitForElementToBeRemoved(() =>
644-
screen.getByTestId('assistant-confirm-clear-chat-modal')
645-
);
649+
await waitFor(() => {
650+
expect(
651+
screen.getByTestId('assistant-confirm-clear-chat-modal').firstChild
652+
).to.not.exist;
653+
});
646654

647655
// The non-genuine warning message should still be in the chat
648656
expect(screen.getByTestId('assistant-message-non-genuine-warning')).to

packages/compass-collection/src/components/mock-data-generator-modal/mock-data-generator-modal.spec.tsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -134,23 +134,23 @@ describe('MockDataGeneratorModal', () => {
134134
it('renders the modal when isOpen is true', async () => {
135135
await renderModal();
136136

137-
expect(screen.getByTestId('generate-mock-data-modal')).to.exist;
137+
expect(screen.getByTestId('generate-mock-data-modal')).to.be.open;
138138
});
139139

140140
it('does not render the modal when isOpen is false', async () => {
141141
await renderModal({ isOpen: false });
142142

143-
expect(screen.queryByTestId('generate-mock-data-modal')).to.not.exist;
143+
expect(screen.getByTestId('generate-mock-data-modal')).to.be.closed;
144144
});
145145

146146
it('closes the modal when the close button is clicked', async () => {
147147
await renderModal();
148148

149-
expect(screen.getByTestId('generate-mock-data-modal')).to.exist;
149+
expect(screen.getByTestId('generate-mock-data-modal')).to.be.open;
150150
userEvent.click(screen.getByLabelText('Close modal'));
151151
await waitFor(
152152
() =>
153-
expect(screen.queryByTestId('generate-mock-data-modal')).to.not.exist
153+
expect(screen.getByTestId('generate-mock-data-modal')).to.be.closed
154154
);
155155
});
156156

@@ -172,11 +172,11 @@ describe('MockDataGeneratorModal', () => {
172172
it('closes the modal when the cancel button is clicked', async () => {
173173
await renderModal();
174174

175-
expect(screen.getByTestId('generate-mock-data-modal')).to.exist;
175+
expect(screen.getByTestId('generate-mock-data-modal')).to.be.open;
176176
userEvent.click(screen.getByText('Cancel'));
177177
await waitFor(
178178
() =>
179-
expect(screen.queryByTestId('generate-mock-data-modal')).to.not.exist
179+
expect(screen.getByTestId('generate-mock-data-modal')).to.be.closed
180180
);
181181
});
182182

@@ -1062,11 +1062,11 @@ describe('MockDataGeneratorModal', () => {
10621062
},
10631063
});
10641064

1065-
expect(screen.getByTestId('generate-mock-data-modal')).to.exist;
1065+
expect(screen.getByTestId('generate-mock-data-modal')).to.be.open;
10661066
userEvent.click(screen.getByText('Done'));
10671067
await waitFor(
10681068
() =>
1069-
expect(screen.queryByTestId('generate-mock-data-modal')).to.not.exist
1069+
expect(screen.getByTestId('generate-mock-data-modal')).to.be.closed
10701070
);
10711071
});
10721072

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import React from 'react';
2+
import { render, screen } from '@mongodb-js/testing-library-compass';
3+
import { expect } from 'chai';
4+
5+
import { Modal } from './modal';
6+
7+
describe('Modal Component', function () {
8+
it('opens and closes', () => {
9+
const { rerender } = render(
10+
<Modal data-testid="modal" open={false}>
11+
<p data-testid="modal-content">The content!</p>
12+
</Modal>
13+
);
14+
15+
expect(screen.getByTestId('modal')).to.be.closed;
16+
17+
rerender(
18+
<Modal data-testid="modal" open={true}>
19+
<p data-testid="modal-content">The content!</p>
20+
</Modal>
21+
);
22+
23+
expect(screen.getByTestId('modal')).to.be.open;
24+
});
25+
});

packages/compass-settings/src/components/modal.spec.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ describe('SettingsModal', function () {
6060
renderSettingsModal({ isOpen: false });
6161

6262
expect(fetchSettingsSpy.called).to.be.false;
63-
const container = screen.queryByTestId('settings-modal');
64-
expect(container).to.not.exist;
63+
expect(screen.getByTestId('settings-modal')).to.be.closed;
6564
});
6665

6766
it('modal footer actions', async function () {

packages/compass-welcome/src/components/modal.spec.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ describe('WelcomeModal', function () {
2525
expect(startButton).to.be.visible;
2626
userEvent.click(startButton);
2727
await waitFor(() => {
28-
expect(() => screen.getByTestId('welcome-modal')).to.throw();
28+
expect(screen.getByTestId('welcome-modal')).be.closed;
2929
});
3030
});
3131

@@ -36,7 +36,7 @@ describe('WelcomeModal', function () {
3636
const closeButton = screen.getByLabelText('Close modal');
3737
userEvent.click(closeButton);
3838
await waitFor(() => {
39-
expect(() => screen.getByTestId('welcome-modal')).to.throw();
39+
expect(screen.getByTestId('welcome-modal')).be.closed;
4040
});
4141
});
4242

@@ -48,7 +48,7 @@ describe('WelcomeModal', function () {
4848
const settingsLink = screen.getByText('Settings');
4949
userEvent.click(settingsLink);
5050
await waitFor(() => {
51-
expect(() => screen.getByTestId('welcome-modal')).to.throw();
51+
expect(screen.getByTestId('welcome-modal')).be.closed;
5252
});
5353
expect(emitSpy).to.have.been.called;
5454
});

0 commit comments

Comments
 (0)