Skip to content

Commit 0e695f2

Browse files
authored
chore(connection-form): typecheck packages/connection-form (#6143)
typecheck packages/connection-form
1 parent cadde7d commit 0e695f2

18 files changed

+282
-73
lines changed

packages/connection-form/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,12 @@
3232
"bootstrap": "npm run compile",
3333
"prepublishOnly": "npm run compile && compass-scripts check-exports-exist",
3434
"compile": "tsc -p tsconfig.json",
35+
"typecheck": "tsc -p tsconfig-lint.json --noEmit",
3536
"eslint": "eslint",
3637
"prettier": "prettier",
3738
"lint": "npm run eslint . && npm run prettier -- --check .",
3839
"depcheck": "compass-scripts check-peer-deps && depcheck",
39-
"check": "npm run lint && npm run depcheck",
40+
"check": "npm run typecheck && npm run lint && npm run depcheck",
4041
"check-ci": "npm run check",
4142
"test": "mocha",
4243
"test-electron": "xvfb-maybe electron-mocha --no-sandbox",

packages/connection-form/src/components/advanced-options-tabs/authentication-tab/authentication-default.spec.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ describe('AuthenticationDefault Component', function () {
172172
{
173173
fieldName: 'username',
174174
message: 'username error',
175+
fieldTab: 'general',
175176
},
176177
],
177178
updateConnectionFormField: updateConnectionFormFieldSpy,
@@ -186,6 +187,7 @@ describe('AuthenticationDefault Component', function () {
186187
{
187188
fieldName: 'password',
188189
message: 'password error',
190+
fieldTab: 'general',
189191
},
190192
],
191193
updateConnectionFormField: updateConnectionFormFieldSpy,

packages/connection-form/src/components/advanced-options-tabs/authentication-tab/authentication-gssapi.spec.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ describe('AuthenticationGssapi Component', function () {
118118
.getByTestId('gssapi-canonicalize-host-name-none')
119119
.closest('input');
120120

121-
expect(radio.checked).to.be.true;
121+
expect(radio?.checked).to.be.true;
122122
});
123123

124124
it('updates the form field with CANONICALIZE_HOST_NAME forward', function () {
@@ -188,7 +188,7 @@ describe('AuthenticationGssapi Component', function () {
188188
it('allows to edit the password when enter password directly is enabled', function () {
189189
expect(screen.queryByTestId('gssapi-password-input')).to.not.exist;
190190
const checkbox = screen.getByTestId('gssapi-password-checkbox');
191-
expect(checkbox.closest('input').checked).to.be.false;
191+
expect(checkbox.closest('input')?.checked).to.be.false;
192192

193193
fireEvent.click(checkbox);
194194

@@ -220,15 +220,17 @@ describe('AuthenticationGssapi Component', function () {
220220

221221
it('enables the checkbox and shows the password input', function () {
222222
const checkbox = screen.getByTestId('gssapi-password-checkbox');
223-
expect(checkbox.closest('input').checked).to.be.true;
223+
expect(checkbox.closest('input')?.checked).to.be.true;
224224
const passwordInput = screen.queryByTestId('gssapi-password-input');
225225
expect(passwordInput).to.exist;
226-
expect(passwordInput.closest('input').value).to.equal('password');
226+
expect(passwordInput && passwordInput.closest('input')?.value).to.equal(
227+
'password'
228+
);
227229
});
228230

229231
it('resets the password when the checkbox is unchecked', function () {
230232
const checkbox = screen.getByTestId('gssapi-password-checkbox');
231-
expect(checkbox.closest('input').checked).to.be.true;
233+
expect(checkbox.closest('input')?.checked).to.be.true;
232234
fireEvent.click(checkbox);
233235

234236
expect(updateConnectionFormFieldSpy.callCount).to.equal(1);

packages/connection-form/src/components/advanced-options-tabs/authentication-tab/authentication-oidc.spec.tsx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ import ConnectionForm from '../../../';
1515
const deviceAuthFlowText = 'Enable Device Authentication Flow';
1616

1717
async function renderConnectionForm(
18-
connectSpy,
18+
connectSpy: (
19+
expected: ConnectionOptions | ((expected: ConnectionOptions) => void)
20+
) => Promise<void>,
1921
{ showOIDCDeviceAuthFlow }: { showOIDCDeviceAuthFlow: boolean }
2022
) {
2123
render(
@@ -27,9 +29,12 @@ async function renderConnectionForm(
2729
},
2830
}}
2931
onConnectClicked={(connectionInfo) => {
30-
connectSpy(connectionInfo.connectionOptions);
32+
void connectSpy(connectionInfo.connectionOptions);
3133
}}
3234
preferences={{ enableOidc: true, showOIDCDeviceAuthFlow }}
35+
onSaveClicked={() => {
36+
return Promise.resolve();
37+
}}
3338
/>
3439
);
3540

@@ -57,13 +62,15 @@ const openOptionsAccordion = () =>
5762
fireEvent.click(screen.getByText('OIDC Options'));
5863

5964
describe('Authentication OIDC Connection Form', function () {
60-
let expectToConnectWith;
65+
let expectToConnectWith: (
66+
expected: ConnectionOptions | ((expected: ConnectionOptions) => void)
67+
) => Promise<void>;
6168
let connectSpy: sinon.SinonSpy;
6269

6370
beforeEach(function () {
6471
connectSpy = sinon.spy();
6572
expectToConnectWith = async (
66-
expected: ConnectionOptions | ((ConnectionOptions) => void)
73+
expected: ConnectionOptions | ((expected: ConnectionOptions) => void)
6774
): Promise<void> => {
6875
connectSpy.resetHistory();
6976
fireEvent.click(screen.getByTestId('connect-button'));

packages/connection-form/src/components/advanced-options-tabs/authentication-tab/authentication-plain.spec.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ describe('AuthenticationAws Component', function () {
8383
{
8484
fieldName: 'username',
8585
message: 'username error',
86+
fieldTab: 'general',
8687
},
8788
],
8889
updateConnectionFormField: updateConnectionFormFieldSpy,
@@ -97,6 +98,7 @@ describe('AuthenticationAws Component', function () {
9798
{
9899
fieldName: 'password',
99100
message: 'password error',
101+
fieldTab: 'general',
100102
},
101103
],
102104
updateConnectionFormField: updateConnectionFormFieldSpy,

packages/connection-form/src/components/advanced-options-tabs/csfle-tab/csfle-tab.spec.tsx

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,16 @@ const setFileInputValue = (testId: string, value: string) =>
4444
});
4545

4646
describe('In-Use Encryption', function () {
47-
let expectToConnectWith;
48-
let expectConnectionError;
47+
let expectToConnectWith: (
48+
expected: ConnectionOptions | ((opts: ConnectionOptions) => void)
49+
) => Promise<void>;
50+
let expectConnectionError: (expectedErrorText: string) => Promise<void>;
4951

5052
beforeEach(async function () {
5153
const connectSpy = sinon.spy();
5254

5355
expectToConnectWith = async (
54-
expected: ConnectionOptions | ((ConnectionOptions) => void)
56+
expected: ConnectionOptions | ((opts: ConnectionOptions) => void)
5557
): Promise<void> => {
5658
connectSpy.resetHistory();
5759
fireEvent.click(screen.getByTestId('connect-button'));
@@ -90,6 +92,9 @@ describe('In-Use Encryption', function () {
9092
onConnectClicked={(connectionInfo) => {
9193
connectSpy(connectionInfo.connectionOptions);
9294
}}
95+
onSaveClicked={() => {
96+
return Promise.resolve();
97+
}}
9398
/>
9499
);
95100

@@ -192,6 +197,10 @@ describe('In-Use Encryption', function () {
192197
.getByTestId('csfle-kms-local-key')
193198
.closest('input')?.value;
194199

200+
if (!generatedLocalKey) {
201+
throw new Error('expected generatedLocalKey');
202+
}
203+
195204
expect(generatedLocalKey).to.match(/^[a-zA-Z0-9+/-_=]{128}$/);
196205

197206
await expectToConnectWith({

packages/connection-form/src/components/advanced-options-tabs/ssh-tunnel-tab/socks.spec.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,23 @@ const formFields = [
2222
key: 'proxyPassword',
2323
value: 'password',
2424
},
25-
];
25+
] as const;
2626

2727
const proxyParams = {
2828
proxyHost: 'hello-world.com',
29-
proxyPort: 1080,
29+
proxyPort: '1080',
3030
proxyUsername: 'cosmo',
3131
proxyPassword: 'kramer',
32-
};
32+
} as const;
3333
const connectionStringUrl = new ConnectionStringUrl(
3434
'mongodb+srv://0ranges:p!neapp1es@localhost/'
3535
);
3636

3737
for (const key in proxyParams) {
38-
connectionStringUrl.searchParams.set(key, proxyParams[key]);
38+
connectionStringUrl.searchParams.set(
39+
key,
40+
proxyParams[key as keyof typeof proxyParams]
41+
);
3942
}
4043

4144
describe('TunnelSocks', function () {

packages/connection-form/src/components/advanced-options-tabs/ssh-tunnel-tab/ssh-tunnel-identity.spec.tsx

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ import SSHTunnelIdentity from './ssh-tunnel-identity';
88
import type { ConnectionFormError } from '../../../utils/validation';
99
import { errorMessageByFieldName } from '../../../utils/validation';
1010

11-
const formFields = [
11+
const formFields: {
12+
key: keyof SSHConnectionOptions;
13+
value: string;
14+
}[] = [
1215
{
1316
key: 'host',
1417
value: 'host',
@@ -61,7 +64,7 @@ describe('SSHTunnelIdentity', function () {
6164

6265
if (key !== 'identityKeyFile') {
6366
expect(el.getAttribute('value'), `renders ${key} value`).to.equal(
64-
sshTunnelOptions[key].toString()
67+
sshTunnelOptions[key]?.toString()
6568
);
6669
}
6770
});
@@ -92,14 +95,17 @@ describe('SSHTunnelIdentity', function () {
9295
{
9396
fieldName: 'sshHostname',
9497
message: 'Invalid host',
98+
fieldTab: 'authentication',
9599
},
96100
{
97101
fieldName: 'sshUsername',
98102
message: 'Invalid username',
103+
fieldTab: 'authentication',
99104
},
100105
{
101106
fieldName: 'sshIdentityKeyFile',
102107
message: 'Invalid file',
108+
fieldTab: 'authentication',
103109
},
104110
];
105111

@@ -112,17 +118,23 @@ describe('SSHTunnelIdentity', function () {
112118
);
113119

114120
expect(
115-
screen.getByText(errorMessageByFieldName(errors, 'sshHostname')),
121+
screen.getByText(
122+
errorMessageByFieldName(errors, 'sshHostname') as string
123+
),
116124
'renders sshHostname field error'
117125
).to.exist;
118126

119127
expect(
120-
screen.getByText(errorMessageByFieldName(errors, 'sshUsername')),
128+
screen.getByText(
129+
errorMessageByFieldName(errors, 'sshUsername') as string
130+
),
121131
'renders sshUsername field error'
122132
).to.exist;
123133

124134
expect(
125-
screen.getByText(errorMessageByFieldName(errors, 'sshIdentityKeyFile')),
135+
screen.getByText(
136+
errorMessageByFieldName(errors, 'sshIdentityKeyFile') as string
137+
),
126138
'renders sshIdentityKeyFile field error'
127139
).to.exist;
128140
});

packages/connection-form/src/components/advanced-options-tabs/ssh-tunnel-tab/ssh-tunnel-password.spec.tsx

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ const formFields = [
2525
key: 'password',
2626
value: 'password',
2727
},
28-
];
28+
] as const;
2929

3030
const sshTunnelOptions: SSHConnectionOptions = {
3131
host: 'old host',
3232
port: 22,
3333
username: 'old username',
3434
password: 'old password',
35-
};
35+
} as const;
3636

3737
describe('SSHTunnelPassword', function () {
3838
let updateConnectionFormFieldSpy: sinon.SinonSpy;
@@ -54,7 +54,7 @@ describe('SSHTunnelPassword', function () {
5454
const el = screen.getByTestId(key);
5555
expect(el, `renders ${key} field`).to.exist;
5656
expect(el.getAttribute('value'), `renders ${key} value`).to.equal(
57-
sshTunnelOptions[key].toString()
57+
sshTunnelOptions[key]?.toString()
5858
);
5959
});
6060
});
@@ -97,17 +97,23 @@ describe('SSHTunnelPassword', function () {
9797
);
9898

9999
expect(
100-
screen.getByText(errorMessageByFieldName(errors, 'sshHostname')),
100+
screen.getByText(
101+
errorMessageByFieldName(errors, 'sshHostname') as string
102+
),
101103
'renders sshHostname field error'
102104
).to.exist;
103105

104106
expect(
105-
screen.getByText(errorMessageByFieldName(errors, 'sshUsername')),
107+
screen.getByText(
108+
errorMessageByFieldName(errors, 'sshUsername') as string
109+
),
106110
'renders sshUsername field error'
107111
).to.exist;
108112

109113
expect(
110-
screen.getByText(errorMessageByFieldName(errors, 'sshPassword')),
114+
screen.getByText(
115+
errorMessageByFieldName(errors, 'sshPassword') as string
116+
),
111117
'renders sshPassword field error'
112118
).to.exist;
113119
});

packages/connection-form/src/components/connection-form.spec.tsx

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,10 @@ describe('ConnectionForm Component', function () {
303303

304304
expect(screen.queryByText('Save connection to favorites')).to.not.exist;
305305

306-
fireEvent.click(screen.getByText(favoriteText).closest('button'));
306+
const button = screen.getByText(favoriteText).closest('button');
307+
if (button) {
308+
fireEvent.click(button);
309+
}
307310

308311
expect(screen.getByText('Save connection to favorites')).to.be.visible;
309312
});
@@ -420,7 +423,9 @@ describe('ConnectionForm Component', function () {
420423

421424
describe('name input', function () {
422425
it('should sync with the href of the connection string unless it has been edited', async function () {
423-
const connectionString = screen.getByTestId('connectionString');
426+
const connectionString = screen.getByTestId(
427+
'connectionString'
428+
) as HTMLInputElement;
424429
userEvent.clear(connectionString);
425430

426431
await waitFor(() => expect(connectionString.value).to.equal(''));
@@ -433,15 +438,17 @@ describe('ConnectionForm Component', function () {
433438

434439
const personalizationName = screen.getByTestId(
435440
'personalization-name-input'
436-
);
441+
) as HTMLInputElement;
437442
expect(personalizationName.value).to.equal('myserver:27017');
438443
});
439444

440445
it('should not sync with the href of the connection string when it has been edited', async function () {
441-
const connectionString = screen.getByTestId('connectionString');
446+
const connectionString = screen.getByTestId(
447+
'connectionString'
448+
) as HTMLInputElement;
442449
const personalizationName = screen.getByTestId(
443450
'personalization-name-input'
444-
);
451+
) as HTMLInputElement;
445452

446453
userEvent.clear(personalizationName);
447454
userEvent.clear(connectionString);

0 commit comments

Comments
 (0)