Skip to content

Commit

Permalink
Test type-strictness of PasswordField
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaw3d committed Oct 19, 2022
1 parent 96e1d7d commit 6b4a28f
Showing 1 changed file with 96 additions and 0 deletions.
96 changes: 96 additions & 0 deletions src/app/components/PasswordField/__tests__/type-only.test.tsx
Original file line number Diff line number Diff line change
@@ -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('<PasswordField /> with `name`, `validate`, and `FormValue`', () => {
it('is strictly typed', () => {
expect(
<Form<FormValue>
onSubmit={({ value }) => {
// @ts-expect-error Detect incorrect type
expect(value.privateKey !== 5).toBeTruthy()
expect(value.privateKey !== '5').toBeTruthy()
}}
>
<FormField name="name">
<TextInput name="name" value="name" />
</FormField>
<PasswordField<FormValue>
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"
/>
<PasswordField<FormValue>
inputElementId="privateKey3"
// @ts-expect-error Detect missing field
name="privateKey3"
autoComplete="current-password"
showTip="show"
hideTip="hide"
/>
<input type="submit" />
</Form>,
).toBeDefined()
})

it('less type-safe without `FormValue`', () => {
expect(
<Form
onSubmit={({ value }) => {
// @ts-expect-error Doesn't know about any fields
expect(value.privateKey !== '5').toBeTruthy()
}}
>
<FormField name="name">
<TextInput name="name" value="name" />
</FormField>
<PasswordField
inputElementId="privateKey"
name="privateKey"
label="privateKey"
autoComplete="off"
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()
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"
/>
<input type="submit" />
</Form>,
).toBeDefined()
})
})
})

0 comments on commit 6b4a28f

Please sign in to comment.