From 0e695f2a56457eac99a636dabc06060068bf7a4a Mon Sep 17 00:00:00 2001 From: Le Roux Bodenstein Date: Mon, 19 Aug 2024 16:53:07 +0100 Subject: [PATCH] chore(connection-form): typecheck packages/connection-form (#6143) typecheck packages/connection-form --- packages/connection-form/package.json | 3 +- .../authentication-default.spec.tsx | 2 + .../authentication-gssapi.spec.tsx | 12 +- .../authentication-oidc.spec.tsx | 15 +- .../authentication-plain.spec.tsx | 2 + .../csfle-tab/csfle-tab.spec.tsx | 15 +- .../ssh-tunnel-tab/socks.spec.tsx | 11 +- .../ssh-tunnel-identity.spec.tsx | 22 ++- .../ssh-tunnel-password.spec.tsx | 18 +- .../src/components/connection-form.spec.tsx | 17 +- .../connection-string-input.spec.tsx | 8 +- .../components/save-connection-modal.spec.tsx | 2 +- .../src/hooks/use-connect-form.spec.ts | 168 +++++++++++++++++- .../src/hooks/use-connection-color.spec.tsx | 11 +- ...heck-for-invalid-character-in-host.spec.ts | 6 +- .../src/utils/connection-ssh-handler.spec.ts | 32 ++-- .../utils/connection-string-helpers.spec.ts | 8 +- .../src/utils/csfle-handler.spec.ts | 3 + 18 files changed, 282 insertions(+), 73 deletions(-) diff --git a/packages/connection-form/package.json b/packages/connection-form/package.json index 65643f019a7..62a34101baa 100644 --- a/packages/connection-form/package.json +++ b/packages/connection-form/package.json @@ -32,11 +32,12 @@ "bootstrap": "npm run compile", "prepublishOnly": "npm run compile && compass-scripts check-exports-exist", "compile": "tsc -p tsconfig.json", + "typecheck": "tsc -p tsconfig-lint.json --noEmit", "eslint": "eslint", "prettier": "prettier", "lint": "npm run eslint . && npm run prettier -- --check .", "depcheck": "compass-scripts check-peer-deps && depcheck", - "check": "npm run lint && npm run depcheck", + "check": "npm run typecheck && npm run lint && npm run depcheck", "check-ci": "npm run check", "test": "mocha", "test-electron": "xvfb-maybe electron-mocha --no-sandbox", diff --git a/packages/connection-form/src/components/advanced-options-tabs/authentication-tab/authentication-default.spec.tsx b/packages/connection-form/src/components/advanced-options-tabs/authentication-tab/authentication-default.spec.tsx index 183d84008df..f88616d19b9 100644 --- a/packages/connection-form/src/components/advanced-options-tabs/authentication-tab/authentication-default.spec.tsx +++ b/packages/connection-form/src/components/advanced-options-tabs/authentication-tab/authentication-default.spec.tsx @@ -172,6 +172,7 @@ describe('AuthenticationDefault Component', function () { { fieldName: 'username', message: 'username error', + fieldTab: 'general', }, ], updateConnectionFormField: updateConnectionFormFieldSpy, @@ -186,6 +187,7 @@ describe('AuthenticationDefault Component', function () { { fieldName: 'password', message: 'password error', + fieldTab: 'general', }, ], updateConnectionFormField: updateConnectionFormFieldSpy, diff --git a/packages/connection-form/src/components/advanced-options-tabs/authentication-tab/authentication-gssapi.spec.tsx b/packages/connection-form/src/components/advanced-options-tabs/authentication-tab/authentication-gssapi.spec.tsx index ffbe3284c12..411cd2ff9c6 100644 --- a/packages/connection-form/src/components/advanced-options-tabs/authentication-tab/authentication-gssapi.spec.tsx +++ b/packages/connection-form/src/components/advanced-options-tabs/authentication-tab/authentication-gssapi.spec.tsx @@ -118,7 +118,7 @@ describe('AuthenticationGssapi Component', function () { .getByTestId('gssapi-canonicalize-host-name-none') .closest('input'); - expect(radio.checked).to.be.true; + expect(radio?.checked).to.be.true; }); it('updates the form field with CANONICALIZE_HOST_NAME forward', function () { @@ -188,7 +188,7 @@ describe('AuthenticationGssapi Component', function () { it('allows to edit the password when enter password directly is enabled', function () { expect(screen.queryByTestId('gssapi-password-input')).to.not.exist; const checkbox = screen.getByTestId('gssapi-password-checkbox'); - expect(checkbox.closest('input').checked).to.be.false; + expect(checkbox.closest('input')?.checked).to.be.false; fireEvent.click(checkbox); @@ -220,15 +220,17 @@ describe('AuthenticationGssapi Component', function () { it('enables the checkbox and shows the password input', function () { const checkbox = screen.getByTestId('gssapi-password-checkbox'); - expect(checkbox.closest('input').checked).to.be.true; + expect(checkbox.closest('input')?.checked).to.be.true; const passwordInput = screen.queryByTestId('gssapi-password-input'); expect(passwordInput).to.exist; - expect(passwordInput.closest('input').value).to.equal('password'); + expect(passwordInput && passwordInput.closest('input')?.value).to.equal( + 'password' + ); }); it('resets the password when the checkbox is unchecked', function () { const checkbox = screen.getByTestId('gssapi-password-checkbox'); - expect(checkbox.closest('input').checked).to.be.true; + expect(checkbox.closest('input')?.checked).to.be.true; fireEvent.click(checkbox); expect(updateConnectionFormFieldSpy.callCount).to.equal(1); diff --git a/packages/connection-form/src/components/advanced-options-tabs/authentication-tab/authentication-oidc.spec.tsx b/packages/connection-form/src/components/advanced-options-tabs/authentication-tab/authentication-oidc.spec.tsx index 99cae4de99f..e069a3f33b2 100644 --- a/packages/connection-form/src/components/advanced-options-tabs/authentication-tab/authentication-oidc.spec.tsx +++ b/packages/connection-form/src/components/advanced-options-tabs/authentication-tab/authentication-oidc.spec.tsx @@ -15,7 +15,9 @@ import ConnectionForm from '../../../'; const deviceAuthFlowText = 'Enable Device Authentication Flow'; async function renderConnectionForm( - connectSpy, + connectSpy: ( + expected: ConnectionOptions | ((expected: ConnectionOptions) => void) + ) => Promise, { showOIDCDeviceAuthFlow }: { showOIDCDeviceAuthFlow: boolean } ) { render( @@ -27,9 +29,12 @@ async function renderConnectionForm( }, }} onConnectClicked={(connectionInfo) => { - connectSpy(connectionInfo.connectionOptions); + void connectSpy(connectionInfo.connectionOptions); }} preferences={{ enableOidc: true, showOIDCDeviceAuthFlow }} + onSaveClicked={() => { + return Promise.resolve(); + }} /> ); @@ -57,13 +62,15 @@ const openOptionsAccordion = () => fireEvent.click(screen.getByText('OIDC Options')); describe('Authentication OIDC Connection Form', function () { - let expectToConnectWith; + let expectToConnectWith: ( + expected: ConnectionOptions | ((expected: ConnectionOptions) => void) + ) => Promise; let connectSpy: sinon.SinonSpy; beforeEach(function () { connectSpy = sinon.spy(); expectToConnectWith = async ( - expected: ConnectionOptions | ((ConnectionOptions) => void) + expected: ConnectionOptions | ((expected: ConnectionOptions) => void) ): Promise => { connectSpy.resetHistory(); fireEvent.click(screen.getByTestId('connect-button')); diff --git a/packages/connection-form/src/components/advanced-options-tabs/authentication-tab/authentication-plain.spec.tsx b/packages/connection-form/src/components/advanced-options-tabs/authentication-tab/authentication-plain.spec.tsx index 8359134e684..d0a0b10f99c 100644 --- a/packages/connection-form/src/components/advanced-options-tabs/authentication-tab/authentication-plain.spec.tsx +++ b/packages/connection-form/src/components/advanced-options-tabs/authentication-tab/authentication-plain.spec.tsx @@ -83,6 +83,7 @@ describe('AuthenticationAws Component', function () { { fieldName: 'username', message: 'username error', + fieldTab: 'general', }, ], updateConnectionFormField: updateConnectionFormFieldSpy, @@ -97,6 +98,7 @@ describe('AuthenticationAws Component', function () { { fieldName: 'password', message: 'password error', + fieldTab: 'general', }, ], updateConnectionFormField: updateConnectionFormFieldSpy, diff --git a/packages/connection-form/src/components/advanced-options-tabs/csfle-tab/csfle-tab.spec.tsx b/packages/connection-form/src/components/advanced-options-tabs/csfle-tab/csfle-tab.spec.tsx index 589f2fa46ca..87e4dbcadd8 100644 --- a/packages/connection-form/src/components/advanced-options-tabs/csfle-tab/csfle-tab.spec.tsx +++ b/packages/connection-form/src/components/advanced-options-tabs/csfle-tab/csfle-tab.spec.tsx @@ -44,14 +44,16 @@ const setFileInputValue = (testId: string, value: string) => }); describe('In-Use Encryption', function () { - let expectToConnectWith; - let expectConnectionError; + let expectToConnectWith: ( + expected: ConnectionOptions | ((opts: ConnectionOptions) => void) + ) => Promise; + let expectConnectionError: (expectedErrorText: string) => Promise; beforeEach(async function () { const connectSpy = sinon.spy(); expectToConnectWith = async ( - expected: ConnectionOptions | ((ConnectionOptions) => void) + expected: ConnectionOptions | ((opts: ConnectionOptions) => void) ): Promise => { connectSpy.resetHistory(); fireEvent.click(screen.getByTestId('connect-button')); @@ -90,6 +92,9 @@ describe('In-Use Encryption', function () { onConnectClicked={(connectionInfo) => { connectSpy(connectionInfo.connectionOptions); }} + onSaveClicked={() => { + return Promise.resolve(); + }} /> ); @@ -192,6 +197,10 @@ describe('In-Use Encryption', function () { .getByTestId('csfle-kms-local-key') .closest('input')?.value; + if (!generatedLocalKey) { + throw new Error('expected generatedLocalKey'); + } + expect(generatedLocalKey).to.match(/^[a-zA-Z0-9+/-_=]{128}$/); await expectToConnectWith({ diff --git a/packages/connection-form/src/components/advanced-options-tabs/ssh-tunnel-tab/socks.spec.tsx b/packages/connection-form/src/components/advanced-options-tabs/ssh-tunnel-tab/socks.spec.tsx index 3b23ebce5a3..aafe97c9254 100644 --- a/packages/connection-form/src/components/advanced-options-tabs/ssh-tunnel-tab/socks.spec.tsx +++ b/packages/connection-form/src/components/advanced-options-tabs/ssh-tunnel-tab/socks.spec.tsx @@ -22,20 +22,23 @@ const formFields = [ key: 'proxyPassword', value: 'password', }, -]; +] as const; const proxyParams = { proxyHost: 'hello-world.com', - proxyPort: 1080, + proxyPort: '1080', proxyUsername: 'cosmo', proxyPassword: 'kramer', -}; +} as const; const connectionStringUrl = new ConnectionStringUrl( 'mongodb+srv://0ranges:p!neapp1es@localhost/' ); for (const key in proxyParams) { - connectionStringUrl.searchParams.set(key, proxyParams[key]); + connectionStringUrl.searchParams.set( + key, + proxyParams[key as keyof typeof proxyParams] + ); } describe('TunnelSocks', function () { diff --git a/packages/connection-form/src/components/advanced-options-tabs/ssh-tunnel-tab/ssh-tunnel-identity.spec.tsx b/packages/connection-form/src/components/advanced-options-tabs/ssh-tunnel-tab/ssh-tunnel-identity.spec.tsx index b7270c442e7..2035e02abed 100644 --- a/packages/connection-form/src/components/advanced-options-tabs/ssh-tunnel-tab/ssh-tunnel-identity.spec.tsx +++ b/packages/connection-form/src/components/advanced-options-tabs/ssh-tunnel-tab/ssh-tunnel-identity.spec.tsx @@ -8,7 +8,10 @@ import SSHTunnelIdentity from './ssh-tunnel-identity'; import type { ConnectionFormError } from '../../../utils/validation'; import { errorMessageByFieldName } from '../../../utils/validation'; -const formFields = [ +const formFields: { + key: keyof SSHConnectionOptions; + value: string; +}[] = [ { key: 'host', value: 'host', @@ -61,7 +64,7 @@ describe('SSHTunnelIdentity', function () { if (key !== 'identityKeyFile') { expect(el.getAttribute('value'), `renders ${key} value`).to.equal( - sshTunnelOptions[key].toString() + sshTunnelOptions[key]?.toString() ); } }); @@ -92,14 +95,17 @@ describe('SSHTunnelIdentity', function () { { fieldName: 'sshHostname', message: 'Invalid host', + fieldTab: 'authentication', }, { fieldName: 'sshUsername', message: 'Invalid username', + fieldTab: 'authentication', }, { fieldName: 'sshIdentityKeyFile', message: 'Invalid file', + fieldTab: 'authentication', }, ]; @@ -112,17 +118,23 @@ describe('SSHTunnelIdentity', function () { ); expect( - screen.getByText(errorMessageByFieldName(errors, 'sshHostname')), + screen.getByText( + errorMessageByFieldName(errors, 'sshHostname') as string + ), 'renders sshHostname field error' ).to.exist; expect( - screen.getByText(errorMessageByFieldName(errors, 'sshUsername')), + screen.getByText( + errorMessageByFieldName(errors, 'sshUsername') as string + ), 'renders sshUsername field error' ).to.exist; expect( - screen.getByText(errorMessageByFieldName(errors, 'sshIdentityKeyFile')), + screen.getByText( + errorMessageByFieldName(errors, 'sshIdentityKeyFile') as string + ), 'renders sshIdentityKeyFile field error' ).to.exist; }); diff --git a/packages/connection-form/src/components/advanced-options-tabs/ssh-tunnel-tab/ssh-tunnel-password.spec.tsx b/packages/connection-form/src/components/advanced-options-tabs/ssh-tunnel-tab/ssh-tunnel-password.spec.tsx index e542b46a5d7..6a03e01c89d 100644 --- a/packages/connection-form/src/components/advanced-options-tabs/ssh-tunnel-tab/ssh-tunnel-password.spec.tsx +++ b/packages/connection-form/src/components/advanced-options-tabs/ssh-tunnel-tab/ssh-tunnel-password.spec.tsx @@ -25,14 +25,14 @@ const formFields = [ key: 'password', value: 'password', }, -]; +] as const; const sshTunnelOptions: SSHConnectionOptions = { host: 'old host', port: 22, username: 'old username', password: 'old password', -}; +} as const; describe('SSHTunnelPassword', function () { let updateConnectionFormFieldSpy: sinon.SinonSpy; @@ -54,7 +54,7 @@ describe('SSHTunnelPassword', function () { const el = screen.getByTestId(key); expect(el, `renders ${key} field`).to.exist; expect(el.getAttribute('value'), `renders ${key} value`).to.equal( - sshTunnelOptions[key].toString() + sshTunnelOptions[key]?.toString() ); }); }); @@ -97,17 +97,23 @@ describe('SSHTunnelPassword', function () { ); expect( - screen.getByText(errorMessageByFieldName(errors, 'sshHostname')), + screen.getByText( + errorMessageByFieldName(errors, 'sshHostname') as string + ), 'renders sshHostname field error' ).to.exist; expect( - screen.getByText(errorMessageByFieldName(errors, 'sshUsername')), + screen.getByText( + errorMessageByFieldName(errors, 'sshUsername') as string + ), 'renders sshUsername field error' ).to.exist; expect( - screen.getByText(errorMessageByFieldName(errors, 'sshPassword')), + screen.getByText( + errorMessageByFieldName(errors, 'sshPassword') as string + ), 'renders sshPassword field error' ).to.exist; }); diff --git a/packages/connection-form/src/components/connection-form.spec.tsx b/packages/connection-form/src/components/connection-form.spec.tsx index 42c61f90cfb..8aa1d2e7de3 100644 --- a/packages/connection-form/src/components/connection-form.spec.tsx +++ b/packages/connection-form/src/components/connection-form.spec.tsx @@ -303,7 +303,10 @@ describe('ConnectionForm Component', function () { expect(screen.queryByText('Save connection to favorites')).to.not.exist; - fireEvent.click(screen.getByText(favoriteText).closest('button')); + const button = screen.getByText(favoriteText).closest('button'); + if (button) { + fireEvent.click(button); + } expect(screen.getByText('Save connection to favorites')).to.be.visible; }); @@ -420,7 +423,9 @@ describe('ConnectionForm Component', function () { describe('name input', function () { it('should sync with the href of the connection string unless it has been edited', async function () { - const connectionString = screen.getByTestId('connectionString'); + const connectionString = screen.getByTestId( + 'connectionString' + ) as HTMLInputElement; userEvent.clear(connectionString); await waitFor(() => expect(connectionString.value).to.equal('')); @@ -433,15 +438,17 @@ describe('ConnectionForm Component', function () { const personalizationName = screen.getByTestId( 'personalization-name-input' - ); + ) as HTMLInputElement; expect(personalizationName.value).to.equal('myserver:27017'); }); it('should not sync with the href of the connection string when it has been edited', async function () { - const connectionString = screen.getByTestId('connectionString'); + const connectionString = screen.getByTestId( + 'connectionString' + ) as HTMLInputElement; const personalizationName = screen.getByTestId( 'personalization-name-input' - ); + ) as HTMLInputElement; userEvent.clear(personalizationName); userEvent.clear(connectionString); diff --git a/packages/connection-form/src/components/connection-string-input.spec.tsx b/packages/connection-form/src/components/connection-string-input.spec.tsx index 1cfeda49c73..8d7a7e51de3 100644 --- a/packages/connection-form/src/components/connection-string-input.spec.tsx +++ b/packages/connection-form/src/components/connection-string-input.spec.tsx @@ -222,7 +222,9 @@ describe('ConnectionStringInput Component', function () { screen.getByRole('switch').click(); // Click confirm on the modal that opens. - const confirmButton = screen.getByText('Confirm').closest('button'); + const confirmButton = screen + .getByText('Confirm') + .closest('button') as HTMLButtonElement; fireEvent( confirmButton, new MouseEvent('click', { @@ -248,7 +250,9 @@ describe('ConnectionStringInput Component', function () { screen.getByRole('switch').click(); // Click cancel on the modal that opens. - const cancelButton = screen.getByText('Cancel').closest('button'); + const cancelButton = screen + .getByText('Cancel') + .closest('button') as HTMLButtonElement; fireEvent( cancelButton, new MouseEvent('click', { diff --git a/packages/connection-form/src/components/save-connection-modal.spec.tsx b/packages/connection-form/src/components/save-connection-modal.spec.tsx index fd3e1f0f5ff..7c66ee95950 100644 --- a/packages/connection-form/src/components/save-connection-modal.spec.tsx +++ b/packages/connection-form/src/components/save-connection-modal.spec.tsx @@ -7,7 +7,7 @@ import SaveConnectionModal from './save-connection-modal'; describe('SaveConnectionModal Component', function () { let onSaveSpy: sinon.SinonSpy; - let onCancelSpy; + let onCancelSpy: sinon.SinonSpy; beforeEach(function () { onSaveSpy = sinon.spy(); diff --git a/packages/connection-form/src/hooks/use-connect-form.spec.ts b/packages/connection-form/src/hooks/use-connect-form.spec.ts index f9d06d7d567..c8d7c63afc0 100644 --- a/packages/connection-form/src/hooks/use-connect-form.spec.ts +++ b/packages/connection-form/src/hooks/use-connect-form.spec.ts @@ -104,6 +104,12 @@ describe('use-connect-form hook', function () { }, { connectionString: connectionStringUrl.toString(), + }, + { + name: 'does not matter', + color: 'color1', + isFavorite: false, + isNameDirty: false, } ); }); @@ -141,6 +147,12 @@ describe('use-connect-form hook', function () { }, { connectionString: connectionStringUrl.toString(), + }, + { + name: 'does not matter', + color: 'color1', + isFavorite: false, + isNameDirty: false, } ); }); @@ -180,6 +192,12 @@ describe('use-connect-form hook', function () { }, { connectionString: connectionStringUrl.toString(), + }, + { + name: 'does not matter', + color: 'color1', + isFavorite: false, + isNameDirty: false, } ); }); @@ -215,6 +233,12 @@ describe('use-connect-form hook', function () { }, { connectionString: connectionStringUrl.toString(), + }, + { + name: 'does not matter', + color: 'color1', + isFavorite: false, + isNameDirty: false, } ); }); @@ -248,6 +272,12 @@ describe('use-connect-form hook', function () { }, { connectionString: connectionStringUrl.toString(), + }, + { + name: 'does not matter', + color: 'color1', + isFavorite: false, + isNameDirty: false, } ); }); @@ -284,6 +314,12 @@ describe('use-connect-form hook', function () { }, { connectionString: connectionStringUrl.toString(), + }, + { + name: 'does not matter', + color: 'color1', + isFavorite: false, + isNameDirty: false, } ); }); @@ -327,6 +363,12 @@ describe('use-connect-form hook', function () { }, { connectionString: connectionStringUrl.toString(), + }, + { + name: 'does not matter', + color: 'color1', + isFavorite: false, + isNameDirty: false, } ); }); @@ -365,6 +407,12 @@ describe('use-connect-form hook', function () { }, { connectionString: connectionStringUrl.toString(), + }, + { + name: 'does not matter', + color: 'color1', + isFavorite: false, + isNameDirty: false, } ); }); @@ -403,6 +451,12 @@ describe('use-connect-form hook', function () { }, { connectionString: connectionStringUrl.toString(), + }, + { + name: 'does not matter', + color: 'color1', + isFavorite: false, + isNameDirty: false, } ); }); @@ -439,6 +493,12 @@ describe('use-connect-form hook', function () { }, { connectionString: connectionStringUrl.toString(), + }, + { + name: 'does not matter', + color: 'color1', + isFavorite: false, + isNameDirty: false, } ); }); @@ -481,6 +541,12 @@ describe('use-connect-form hook', function () { }, { connectionString: connectionStringUrl.toString(), + }, + { + name: 'does not matter', + color: 'color1', + isFavorite: false, + isNameDirty: false, } ); }); @@ -523,6 +589,12 @@ describe('use-connect-form hook', function () { }, { connectionString: connectionStringUrl.toString(), + }, + { + name: 'does not matter', + color: 'color1', + isFavorite: false, + isNameDirty: false, } ); }); @@ -566,6 +638,12 @@ describe('use-connect-form hook', function () { }, { connectionString: connectionStringUrl.toString(), + }, + { + name: 'does not matter', + color: 'color1', + isFavorite: false, + isNameDirty: false, } ); }); @@ -611,6 +689,12 @@ describe('use-connect-form hook', function () { }, { connectionString: connectionStringUrl.toString(), + }, + { + name: 'does not matter', + color: 'color1', + isFavorite: false, + isNameDirty: false, } ); }); @@ -652,6 +736,12 @@ describe('use-connect-form hook', function () { }, { connectionString: connectionStringUrl.toString(), + }, + { + name: 'does not matter', + color: 'color1', + isFavorite: false, + isNameDirty: false, } ); }); @@ -699,6 +789,12 @@ describe('use-connect-form hook', function () { }, { connectionString: connectionStringUrl.toString(), + }, + { + name: 'does not matter', + color: 'color1', + isFavorite: false, + isNameDirty: false, } ); @@ -720,6 +816,12 @@ describe('use-connect-form hook', function () { }, { connectionString: connectionStringUrl.toString(), + }, + { + name: 'does not matter', + color: 'color1', + isFavorite: false, + isNameDirty: false, } ); expect( @@ -740,6 +842,12 @@ describe('use-connect-form hook', function () { }, { connectionString: connectionStringUrl.toString(), + }, + { + name: 'does not matter', + color: 'color1', + isFavorite: false, + isNameDirty: false, } ); expect( @@ -766,6 +874,12 @@ describe('use-connect-form hook', function () { }, { connectionString: connectionStringUrl.toString(), + }, + { + name: 'does not matter', + color: 'color1', + isFavorite: false, + isNameDirty: false, } ); expect( @@ -788,6 +902,12 @@ describe('use-connect-form hook', function () { }, { connectionString: connectionStringUrl.toString(), + }, + { + name: 'does not matter', + color: 'color1', + isFavorite: false, + isNameDirty: false, } ); expect( @@ -815,6 +935,12 @@ describe('use-connect-form hook', function () { }, { connectionString: connectionStringUrl.toString(), + }, + { + name: 'does not matter', + color: 'color1', + isFavorite: false, + isNameDirty: false, } ); expect( @@ -833,6 +959,12 @@ describe('use-connect-form hook', function () { }, { connectionString: connectionStringUrl.toString(), + }, + { + name: 'does not matter', + color: 'color1', + isFavorite: false, + isNameDirty: false, } ); expect( @@ -853,6 +985,12 @@ describe('use-connect-form hook', function () { }, { connectionString: connectionStringUrl.toString(), + }, + { + name: 'does not matter', + color: 'color1', + isFavorite: false, + isNameDirty: false, } ); expect( @@ -873,6 +1011,12 @@ describe('use-connect-form hook', function () { }, { connectionString: connectionStringUrl.toString(), + }, + { + name: 'does not matter', + color: 'color1', + isFavorite: false, + isNameDirty: false, } ); expect( @@ -893,6 +1037,12 @@ describe('use-connect-form hook', function () { }, { connectionString: connectionStringUrl.toString(), + }, + { + name: 'does not matter', + color: 'color1', + isFavorite: false, + isNameDirty: false, } ); expect( @@ -915,7 +1065,7 @@ describe('use-connect-form hook', function () { color: 'color4', }, savedConnectionType: 'favorite', - }; + } as const; it('should inherit the initial information from the connection information', function () { const { result } = renderHook(() => @@ -945,8 +1095,8 @@ describe('use-connect-form hook', function () { initialState.current[0].personalizationOptions ); - expect(result.personalizationOptions.name).to.equal('turtles'); - expect(result.personalizationOptions.isNameDirty).to.be.true; + expect(result.personalizationOptions?.name).to.equal('turtles'); + expect(result.personalizationOptions?.isNameDirty).to.be.true; }); }); @@ -985,8 +1135,8 @@ describe('use-connect-form hook', function () { initialState.current[0].personalizationOptions ); - expect(result.personalizationOptions.name).to.equal('localhost:27019'); - expect(result.personalizationOptions.isNameDirty).to.be.false; + expect(result.personalizationOptions?.name).to.equal('localhost:27019'); + expect(result.personalizationOptions?.isNameDirty).to.be.false; }); it('should not be inferred when the name is dirty', function () { @@ -1007,8 +1157,8 @@ describe('use-connect-form hook', function () { } ); - expect(result.personalizationOptions.name).to.equal('webscale'); - expect(result.personalizationOptions.isNameDirty).to.be.true; + expect(result.personalizationOptions?.name).to.equal('webscale'); + expect(result.personalizationOptions?.isNameDirty).to.be.true; }); it('should support empty names', function () { @@ -1029,8 +1179,8 @@ describe('use-connect-form hook', function () { } ); - expect(result.personalizationOptions.name).to.equal(''); - expect(result.personalizationOptions.isNameDirty).to.be.true; + expect(result.personalizationOptions?.name).to.equal(''); + expect(result.personalizationOptions?.isNameDirty).to.be.true; }); }); }); diff --git a/packages/connection-form/src/hooks/use-connection-color.spec.tsx b/packages/connection-form/src/hooks/use-connection-color.spec.tsx index 29111be74ec..d928517c85c 100644 --- a/packages/connection-form/src/hooks/use-connection-color.spec.tsx +++ b/packages/connection-form/src/hooks/use-connection-color.spec.tsx @@ -27,7 +27,7 @@ describe('useConnectionColor', function () { it('converts a color code to hex', function () { for (const colorCode of CONNECTION_COLOR_CODES) { const { container } = render(); - expect(container.firstChild.textContent).to.match( + expect(container.firstChild?.textContent).to.match( /^#([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/ ); } @@ -54,8 +54,9 @@ describe('useConnectionColor', function () { const { container: container2 } = render( ); - expect(container1.firstChild.textContent).to.equal( - container2.firstChild.textContent + expect(container1.firstChild?.textContent).to.be.not.be.undefined; + expect(container1.firstChild?.textContent).to.equal( + container2.firstChild?.textContent ); } }); @@ -64,12 +65,12 @@ describe('useConnectionColor', function () { const { container } = render( ); - expect(container.firstChild.textContent).to.be.empty; + expect(container.firstChild?.textContent).to.be.empty; }); it('does not convert an unknown hex code', function () { const { container } = render(); - expect(container.firstChild.textContent).to.be.empty; + expect(container.firstChild?.textContent).to.be.empty; }); describe('connection color names', function () { diff --git a/packages/connection-form/src/utils/check-for-invalid-character-in-host.spec.ts b/packages/connection-form/src/utils/check-for-invalid-character-in-host.spec.ts index 68e7e692544..a444dd053e4 100644 --- a/packages/connection-form/src/utils/check-for-invalid-character-in-host.spec.ts +++ b/packages/connection-form/src/utils/check-for-invalid-character-in-host.spec.ts @@ -11,7 +11,7 @@ describe('#checkForInvalidCharacterInHost', function () { let errorThrown; try { checkForInvalidCharacterInHost('aaAA@@aa', false); - } catch (e) { + } catch (e: any) { // Expected to throw. errorThrown = e.message; } @@ -23,7 +23,7 @@ describe('#checkForInvalidCharacterInHost', function () { let errorThrown; try { checkForInvalidCharacterInHost('localhost,,', true); - } catch (e) { + } catch (e: any) { // Expected to throw. errorThrown = e.message; } @@ -35,7 +35,7 @@ describe('#checkForInvalidCharacterInHost', function () { let errorThrown; try { checkForInvalidCharacterInHost('localhost:222', true); - } catch (e) { + } catch (e: any) { // Expected to throw. errorThrown = e.message; } diff --git a/packages/connection-form/src/utils/connection-ssh-handler.spec.ts b/packages/connection-form/src/utils/connection-ssh-handler.spec.ts index 82606ae0aa3..3d6d4234a96 100644 --- a/packages/connection-form/src/utils/connection-ssh-handler.spec.ts +++ b/packages/connection-form/src/utils/connection-ssh-handler.spec.ts @@ -20,16 +20,16 @@ describe('#handleUpdateSshOptions', function () { expect(response.connectionOptions.connectionString).to.equal( connectionString ); - expect(response.connectionOptions.sshTunnel.host).to.equal('localhost'); - expect(response.connectionOptions.sshTunnel.port).to.equal(22); - expect(response.connectionOptions.sshTunnel.username).to.equal(''); - expect(response.connectionOptions.sshTunnel.password).to.equal(undefined); - expect(response.connectionOptions.sshTunnel.identityKeyFile).to.equal( - undefined - ); - expect(response.connectionOptions.sshTunnel.identityKeyPassphrase).to.equal( + expect(response.connectionOptions.sshTunnel?.host).to.equal('localhost'); + expect(response.connectionOptions.sshTunnel?.port).to.equal(22); + expect(response.connectionOptions.sshTunnel?.username).to.equal(''); + expect(response.connectionOptions.sshTunnel?.password).to.equal(undefined); + expect(response.connectionOptions.sshTunnel?.identityKeyFile).to.equal( undefined ); + expect( + response.connectionOptions.sshTunnel?.identityKeyPassphrase + ).to.equal(undefined); }); it('should handle tab update with initial options', function () { @@ -52,15 +52,15 @@ describe('#handleUpdateSshOptions', function () { expect(response.connectionOptions.connectionString).to.equal( connectionString ); - expect(response.connectionOptions.sshTunnel.host).to.equal('localhosted'); - expect(response.connectionOptions.sshTunnel.port).to.equal(22); - expect(response.connectionOptions.sshTunnel.username).to.equal('root'); - expect(response.connectionOptions.sshTunnel.password).to.equal(undefined); - expect(response.connectionOptions.sshTunnel.identityKeyFile).to.equal( - undefined - ); - expect(response.connectionOptions.sshTunnel.identityKeyPassphrase).to.equal( + expect(response.connectionOptions.sshTunnel?.host).to.equal('localhosted'); + expect(response.connectionOptions.sshTunnel?.port).to.equal(22); + expect(response.connectionOptions.sshTunnel?.username).to.equal('root'); + expect(response.connectionOptions.sshTunnel?.password).to.equal(undefined); + expect(response.connectionOptions.sshTunnel?.identityKeyFile).to.equal( undefined ); + expect( + response.connectionOptions.sshTunnel?.identityKeyPassphrase + ).to.equal(undefined); }); }); diff --git a/packages/connection-form/src/utils/connection-string-helpers.spec.ts b/packages/connection-form/src/utils/connection-string-helpers.spec.ts index 55f5671fb50..dff395cf392 100644 --- a/packages/connection-form/src/utils/connection-string-helpers.spec.ts +++ b/packages/connection-form/src/utils/connection-string-helpers.spec.ts @@ -38,10 +38,10 @@ describe('connection-string-helpers', function () { 'mongodb://outerspace:27099?directConnection=true' ); - expect(connectionString.toString()).to.equal( + expect(connectionString?.toString()).to.equal( 'mongodb://outerspace:27099/?directConnection=true' ); - expect(connectionString.hosts[0]).to.equal('outerspace:27099'); + expect(connectionString?.hosts[0]).to.equal('outerspace:27099'); }); it('should return without an error when successfully parsed', function () { @@ -59,7 +59,7 @@ describe('connection-string-helpers', function () { ); expect(connectionString).to.equal(undefined); - expect(error.message).to.equal( + expect(error?.message).to.equal( 'Invalid scheme, expected connection string to start with "mongodb://" or "mongodb+srv://"' ); }); @@ -69,7 +69,7 @@ describe('connection-string-helpers', function () { 'mongos://pineapple:27099/?directConnection=true' ); - expect(connectionString.href).to.equal( + expect(connectionString?.href).to.equal( 'mongos://pineapple:27099/?directConnection=true' ); expect(error).to.equal(undefined); diff --git a/packages/connection-form/src/utils/csfle-handler.spec.ts b/packages/connection-form/src/utils/csfle-handler.spec.ts index 4b5eeb5d9bc..79772cbdcb7 100644 --- a/packages/connection-form/src/utils/csfle-handler.spec.ts +++ b/packages/connection-form/src/utils/csfle-handler.spec.ts @@ -262,6 +262,9 @@ describe('csfle-handler', function () { const obj = textToEncryptedFieldConfig( encryptedFieldConfigToText(exampleObject) ); + if (!obj) { + throw new Error('expected obj'); + } expect(obj).to.deep.equal({ ...exampleObject, '$compass.error': null,