From 6b4a28f4ee1d6e13e5155cbe6ee9bf4f738f2e64 Mon Sep 17 00:00:00 2001 From: lukaw3d Date: Fri, 14 Oct 2022 06:15:09 +0200 Subject: [PATCH] Test type-strictness of PasswordField --- .../__tests__/type-only.test.tsx | 96 +++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 src/app/components/PasswordField/__tests__/type-only.test.tsx diff --git a/src/app/components/PasswordField/__tests__/type-only.test.tsx b/src/app/components/PasswordField/__tests__/type-only.test.tsx new file mode 100644 index 0000000000..2fd67213ae --- /dev/null +++ b/src/app/components/PasswordField/__tests__/type-only.test.tsx @@ -0,0 +1,96 @@ +import { Form, FormField, TextInput } from 'grommet' +import * as React from 'react' +import { PasswordField } from '..' + +interface FormValue { + name: string + privateKey: string +} +describe('type-only test', () => { + describe(' with `name`, `validate`, and `FormValue`', () => { + it('is strictly typed', () => { + expect( + + onSubmit={({ value }) => { + // @ts-expect-error Detect incorrect type + expect(value.privateKey !== 5).toBeTruthy() + expect(value.privateKey !== '5').toBeTruthy() + }} + > + + + + + inputElementId="privateKey" + name="privateKey" + label="privateKey" + // @ts-expect-error Detect incorrect value + autoComplete="password" + validate={(privateKey, form) => { + // @ts-expect-error Detect incorrect type + expect(privateKey !== 5).toBeTruthy() + expect(privateKey !== '5').toBeTruthy() + + // @ts-expect-error Detect missing field + expect(form.privateKey2 !== '5').toBeTruthy() + // @ts-expect-error Detect incorrect type + expect(form.privateKey !== 5).toBeTruthy() + expect(form.privateKey !== '5').toBeTruthy() + expect(form.name !== '5').toBeTruthy() + + return privateKey.length < 5 ? 'invalid' : undefined + }} + showTip="show" + hideTip="hide" + /> + + inputElementId="privateKey3" + // @ts-expect-error Detect missing field + name="privateKey3" + autoComplete="current-password" + showTip="show" + hideTip="hide" + /> + + , + ).toBeDefined() + }) + + it('less type-safe without `FormValue`', () => { + expect( +
{ + // @ts-expect-error Doesn't know about any fields + expect(value.privateKey !== '5').toBeTruthy() + }} + > + + + + { + // @ts-expect-error Detect incorrect type + expect(privateKey !== 5).toBeTruthy() + expect(privateKey !== '5').toBeTruthy() + + // @ts-expect-error Detect missing field + expect(form.privateKey2 !== '5').toBeTruthy() + expect(form.privateKey !== '5').toBeTruthy() + // @ts-expect-error Doesn't know about other fields + expect(form.name !== '5').toBeTruthy() + + return privateKey.length < 5 ? 'invalid' : undefined + }} + showTip="show" + hideTip="hide" + /> + + , + ).toBeDefined() + }) + }) +})