Skip to content

Commit

Permalink
test: 💍 Adding tests for input data
Browse files Browse the repository at this point in the history
  • Loading branch information
aviemet committed May 24, 2024
1 parent cf1c777 commit 165a639
Show file tree
Hide file tree
Showing 10 changed files with 411 additions and 206 deletions.
45 changes: 30 additions & 15 deletions src/Inputs/Input.tsx
Original file line number Diff line number Diff line change
@@ -1,39 +1,54 @@
import React from 'react'
import useInertiaInput from '../useInertiaInput'
import { NestedObject } from '../useInertiaForm'
import { BaseFormInputProps, InputConflicts } from '.'

interface InputProps extends React.InputHTMLAttributes<HTMLInputElement> {
name: string
model?: string
interface InputProps<TForm extends NestedObject, T = string>
extends
Omit<React.InputHTMLAttributes<HTMLInputElement>, InputConflicts>,
BaseFormInputProps<T, TForm>
{
component?: React.ElementType
}

const Input = React.forwardRef<HTMLInputElement, InputProps>((
{ name, component = 'input', model, onChange, ...props },
ref,
const Input = <TForm extends NestedObject, T = string>(
{ name,
component = 'input',
type = 'text',
model,
onChange,
errorKey,
defaultValue,
clearErrorsOnChange,
...props
}: InputProps<TForm, T>,
) => {
const { inputName, inputId, value, setValue } = useInertiaInput({ name, model })
const { form, inputName, inputId, value, setValue } = useInertiaInput<T, TForm>({
name,
model,
errorKey,
defaultValue,
clearErrorsOnChange,
})

const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
if(onChange) {
onChange(e)
return
}

setValue(e.target.value)
const value = (e.target?.checked || e.target.value) as T
setValue(value)
onChange?.(value, form)
}

const Element = component

return (
<Element
type={ type }
name={ inputName }
id={ inputId }
value={ value }
onChange={ handleChange }
ref={ ref }
{ ...props }
/>
)
})
}

export default Input
19 changes: 19 additions & 0 deletions src/Inputs/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,21 @@
import { UseFormProps } from '../Form'
import { NestedObject } from '../useInertiaForm'
import { UseInertiaInputProps } from '../useInertiaInput'

export { default as Input } from './Input'
export { default as Submit } from './Submit'

export type InputConflicts = 'name'|'onChange'|'onBlur'|'onFocus'|'value'|'defaultValue'
export interface BaseFormInputProps<T, TForm extends NestedObject = NestedObject>
extends UseInertiaInputProps<T>
{
model?: string
errorKey?: string
field?: boolean
required?: boolean
hidden?: boolean
onChange?: (value: T, form: UseFormProps<TForm>) => void
onBlur?: (value: T, form: UseFormProps<TForm>) => void
onFocus?: (value: T, form: UseFormProps<TForm>) => void
wrapperProps?: Record<string, any>
}
3 changes: 1 addition & 2 deletions src/useInertiaInput/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ const useInertiaInput = <T = string|number|boolean, TForm = NestedObject>({

// Add a valid default value to the data object
const initializingRef = useRef(true)
if(usedModel === 'values') {
}

useEffect(() => {
if(!initializingRef.current) return

Expand Down
4 changes: 1 addition & 3 deletions tests/.eslintrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
{
"rules": {
"no-unused-vars": "off",
"@typescript-eslint/member-delimiter-style": "off",
"@typescript-eslint/indent": "off"
"no-unused-vars": "off"
}
}
29 changes: 0 additions & 29 deletions tests/TestInput.tsx

This file was deleted.

28 changes: 28 additions & 0 deletions tests/components/ContextTest.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import React, { useEffect } from 'react'
import { NestedObject, useForm, UseFormProps } from '../../src'

interface ContextTestProps<T = NestedObject> {
cb?: (form: UseFormProps<T>) => void
}

const ContextTest = <T = NestedObject>({ cb }: ContextTestProps<T>) => {
const form = useForm<T>()

useEffect(() => {
cb?.(form)
}, [cb])

return (
<>
<div data-testid="data">
{ JSON.stringify(form.data) }
</div>
<div data-testid="errors">
{ JSON.stringify(form.errors) }
</div>
</>
)

}

export default ContextTest
40 changes: 40 additions & 0 deletions tests/components/data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
export const multiRootData = {
user: {
username: 'some name',
},
person: {
first_name: 'first',
last_name: 'last',
middle_name: undefined,
nested: {
key: 'value',
},
},
contact: {
phones: [
{ number: '1234567890' },
{ number: '2234567890' },
{ number: '3234567890' },
],
},
}

export const singleRootData = {
person: {
first_name: 'first',
last_name: 'last',
middle_name: undefined,
nested: {
key: 'value',
},
},
}

export const flatData = {
first_name: 'first',
last_name: 'last',
middle_name: undefined,
nested: {
key: 'value',
},
}
Loading

0 comments on commit 165a639

Please sign in to comment.