Skip to content

Commit f4d651d

Browse files
authored
test(connections): add functional tests for CSFLE (#3113)
* test(connections): add functional tests for CSFLE * fix tests * address PR feedback * add aws kms tests * add gcp tests * added rest of tests for CSFLE * move to connection-form * test bson values * fix ace selector * don't wrap editor without data-testid * fix ace editor selector * change test id
1 parent 6fa8647 commit f4d651d

File tree

9 files changed

+389
-6
lines changed

9 files changed

+389
-6
lines changed

package-lock.json

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/compass-components/src/components/editor.tsx

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React from 'react';
2+
import { useId } from '@react-aria/utils';
23

34
if (typeof window === 'undefined' && typeof globalThis !== 'undefined') {
45
// ace-builds wants to install itself on `window`, which
@@ -51,6 +52,7 @@ type EditorProps = {
5152
options?: Omit<IAceOptions, 'readOnly'>;
5253
readOnly?: boolean;
5354
completer?: unknown;
55+
'data-testid'?: string;
5456
onChangeText?: (text: string, event?: any) => void;
5557
} & Omit<IAceEditorProps, 'onChange' | 'value'>;
5658

@@ -62,6 +64,7 @@ function Editor({
6264
onChangeText,
6365
completer,
6466
onFocus,
67+
'data-testid': dataTestId,
6568
...aceProps
6669
}: EditorProps): React.ReactElement {
6770
const setOptions: IAceOptions = {
@@ -72,7 +75,9 @@ function Editor({
7275
...(!!completer && { enableLiveAutocompletion: true }),
7376
};
7477

75-
return (
78+
const editorId = useId();
79+
80+
const editor = (
7681
<AceEditor
7782
mode={
7883
variant === 'Generic'
@@ -88,6 +93,8 @@ function Editor({
8893
editorProps={{ $blockScrolling: Infinity }}
8994
setOptions={setOptions}
9095
readOnly={readOnly}
96+
// name should be unique since it gets translated to an id
97+
name={aceProps.name ?? editorId}
9198
{...aceProps}
9299
onFocus={(ev: any) => {
93100
if (completer) {
@@ -97,7 +104,28 @@ function Editor({
97104
}}
98105
/>
99106
);
107+
108+
// NOTE: we wrap the editor in a div only to add data-testid.
109+
// Doing so everywhere caused the styles to break in the query bar,
110+
// and so we add the div conditionally based on the data-testid prop.
111+
return dataTestId ? <div data-testid={dataTestId}>{editor}</div> : editor;
112+
}
113+
114+
/**
115+
* Sets the editor value, use this with RTL like this:
116+
*
117+
* ```
118+
* render(<Editor data-testid='my-editor' />);
119+
* setEditorValue(screen.getByTestId('editor-test-id'), 'my text');
120+
* ```
121+
*/
122+
function setEditorValue(element: HTMLElement, value: string): void {
123+
const container = element.querySelector('.ace_editor');
124+
if (!container) {
125+
throw new Error('Cannot find editor container');
126+
}
127+
(window as any).ace.edit(container.id).setValue(value);
100128
}
101129

102130
const EditorTextCompleter = tools.textCompleter;
103-
export { Editor, EditorVariant, EditorTextCompleter };
131+
export { Editor, EditorVariant, EditorTextCompleter, setEditorValue };

packages/compass-components/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export {
3333
Editor,
3434
EditorVariant,
3535
EditorTextCompleter,
36+
setEditorValue,
3637
} from './components/editor';
3738
export { FavoriteIcon } from './components/icons/favorite-icon';
3839
export { Variant as BadgeVariant } from '@leafygreen-ui/badge';

packages/compass-e2e-tests/helpers/selectors.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ export const ConfirmDeleteDocumentButton =
498498
export const InsertDialog = '.insert-document-dialog';
499499
export const InsertDialogErrorMessage =
500500
'[data-testid="insert_document_modal"] .document-footer.document-footer-is-error .document-footer-message';
501-
export const InsertJSONEditor = '.insert-document-dialog #ace-editor';
501+
export const InsertJSONEditor = '.insert-document-dialog .ace_editor';
502502
export const InsertConfirm =
503503
'.insert-document-dialog [role=dialog] > div:nth-child(2) button:first-child';
504504
export const InsertCancel =

packages/compass-e2e-tests/tests/collection-documents-tab.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ FindIterable<Document> result = collection.find(filter);`);
379379
const newjson = JSON.stringify({ ...JSON.parse(json), j: 1234 });
380380

381381
await browser.setAceValue(
382-
'[data-test-id="editable-json"] #ace-editor',
382+
'[data-test-id="editable-json"] .ace_editor',
383383
newjson
384384
);
385385

@@ -426,7 +426,7 @@ FindIterable<Document> result = collection.find(filter);`);
426426
});
427427

428428
await browser.setAceValue(
429-
'[data-test-id="editable-json"] #ace-editor',
429+
'[data-test-id="editable-json"] .ace_editor',
430430
newjson
431431
);
432432

packages/connection-form/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
"@types/react": "^17.0.5",
7575
"@types/react-dom": "^17.0.10",
7676
"@types/sinon-chai": "^3.2.5",
77+
"bson": "^4.4.1",
7778
"chai": "^4.3.4",
7879
"depcheck": "^1.4.1",
7980
"eslint": "^7.25.0",

0 commit comments

Comments
 (0)