Skip to content

Commit 33c1940

Browse files
committed
Update existing tests to handle dialog elements remaining in the DOM
1 parent bf23395 commit 33c1940

File tree

6 files changed

+103
-27
lines changed

6 files changed

+103
-27
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 {
@@ -568,8 +567,9 @@ describe('CompassAssistantProvider', function () {
568567
userEvent.click(clearButton);
569568

570569
await waitFor(() => {
571-
expect(screen.getByTestId('assistant-confirm-clear-chat-modal')).to
572-
.exist;
570+
expect(
571+
screen.getByTestId('assistant-confirm-clear-chat-modal').firstChild
572+
).to.exist;
573573
});
574574

575575
// There should be messages in the chat
@@ -580,9 +580,11 @@ describe('CompassAssistantProvider', function () {
580580
const confirmButton = within(modal).getByText('Clear chat');
581581
userEvent.click(confirmButton);
582582

583-
await waitForElementToBeRemoved(() =>
584-
screen.getByTestId('assistant-confirm-clear-chat-modal')
585-
);
583+
await waitFor(() => {
584+
expect(
585+
screen.getByTestId('assistant-confirm-clear-chat-modal').firstChild
586+
).to.not.exist;
587+
});
586588

587589
expect(mockChat.messages).to.be.empty;
588590
expect(screen.queryByTestId('assistant-message-1')).to.not.exist;
@@ -598,8 +600,9 @@ describe('CompassAssistantProvider', function () {
598600
userEvent.click(clearButton);
599601

600602
await waitFor(() => {
601-
expect(screen.getByTestId('assistant-confirm-clear-chat-modal')).to
602-
.exist;
603+
expect(
604+
screen.getByTestId('assistant-confirm-clear-chat-modal').firstChild
605+
).to.exist;
603606
});
604607

605608
// There should be messages in the chat
@@ -610,9 +613,11 @@ describe('CompassAssistantProvider', function () {
610613
const cancelButton = within(modal).getByText('Cancel');
611614
userEvent.click(cancelButton);
612615

613-
await waitForElementToBeRemoved(() =>
614-
screen.getByTestId('assistant-confirm-clear-chat-modal')
615-
);
616+
await waitFor(() => {
617+
expect(
618+
screen.getByTestId('assistant-confirm-clear-chat-modal').firstChild
619+
).to.not.exist;
620+
});
616621

617622
expect(mockChat.messages).to.deep.equal(mockMessages);
618623
expect(screen.getByTestId('assistant-message-1')).to.exist;
@@ -630,8 +635,9 @@ describe('CompassAssistantProvider', function () {
630635
userEvent.click(clearButton);
631636

632637
await waitFor(() => {
633-
expect(screen.getByTestId('assistant-confirm-clear-chat-modal')).to
634-
.exist;
638+
expect(
639+
screen.getByTestId('assistant-confirm-clear-chat-modal').firstChild
640+
).to.exist;
635641
});
636642

637643
// There should be messages in the chat
@@ -644,9 +650,11 @@ describe('CompassAssistantProvider', function () {
644650
const confirmButton = within(modal).getByText('Clear chat');
645651
userEvent.click(confirmButton);
646652

647-
await waitForElementToBeRemoved(() =>
648-
screen.getByTestId('assistant-confirm-clear-chat-modal')
649-
);
653+
await waitFor(() => {
654+
expect(
655+
screen.getByTestId('assistant-confirm-clear-chat-modal').firstChild
656+
).to.not.exist;
657+
});
650658

651659
// The non-genuine warning message should still be in the chat
652660
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
@@ -133,23 +133,23 @@ describe('MockDataGeneratorModal', () => {
133133
it('renders the modal when isOpen is true', async () => {
134134
await renderModal();
135135

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

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

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

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

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

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

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

@@ -1097,11 +1097,11 @@ describe('MockDataGeneratorModal', () => {
10971097
},
10981098
});
10991099

1100-
expect(screen.getByTestId('generate-mock-data-modal')).to.exist;
1100+
expect(screen.getByTestId('generate-mock-data-modal')).to.be.open;
11011101
userEvent.click(screen.getByText('Done'));
11021102
await waitFor(
11031103
() =>
1104-
expect(screen.queryByTestId('generate-mock-data-modal')).to.not.exist
1104+
expect(screen.getByTestId('generate-mock-data-modal')).to.be.closed
11051105
);
11061106
});
11071107

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-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')).to.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')).to.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')).to.be.closed;
5252
});
5353
expect(emitSpy).to.have.been.called;
5454
});

0 commit comments

Comments
 (0)