diff --git a/src/Array/additional.test.tsx b/src/Array/additional.test.tsx new file mode 100644 index 00000000..8c6f97d4 --- /dev/null +++ b/src/Array/additional.test.tsx @@ -0,0 +1,81 @@ +import {render, screen} from '@testing-library/react' +import Field from '../Field' +import Form from '../Form' +import ArrayField from './index' +import {FieldProps} from '../types' +import '@testing-library/jest-dom' + +jest.useFakeTimers() + +function DummyInput(props: FieldProps) { + return ( + props.onChange(e.target.value)} + /> + ) +} + +test('autoAddItem renders an empty item when value is empty', () => { + const {container} = render( +
, + ) + expect(container.querySelectorAll('input')).toHaveLength(1) +}) + +test('renderProps provides the current index', () => { + render( + , + ) + expect(screen.getByTestId('item-0')).toBeInTheDocument() +}) + +test('renderItem customizes each item element', () => { + render( + , + ) + expect(screen.getByTestId('custom-0')).toHaveTextContent('A') +}) + +test('honors showAddButton and custom addLabel', () => { + const {container} = render( + , + ) + expect(screen.queryByText('Plus')).not.toBeInTheDocument() + expect(container.querySelector('.srf_addButton')).not.toBeInTheDocument() +}) + +test('honors showRemoveButton and custom removeLabel', () => { + const {container} = render( + , + ) + expect(screen.queryByText('Del')).not.toBeInTheDocument() + expect(container.querySelector('.srf_removeButton')).not.toBeInTheDocument() +}) diff --git a/src/Form/getNewValue.additional.test.ts b/src/Form/getNewValue.additional.test.ts new file mode 100644 index 00000000..d135e35d --- /dev/null +++ b/src/Form/getNewValue.additional.test.ts @@ -0,0 +1,17 @@ +import getNewValue from './getNewValue' + +test('supports updater functions', () => { + const original = {counter: 1} + const result = getNewValue(original, 'counter', n => (n || 0) + 1) + expect(result).toEqual({counter: 2}) + expect(original).toEqual({counter: 1}) +}) + +test('creates nested objects when missing', () => { + const result = getNewValue({}, 'person.name.first', 'John') + expect(result).toEqual({person: {name: {first: 'John'}}}) +}) + +test('throws if path expects object but finds primitive', () => { + expect(() => getNewValue({person: 5}, 'person.name', 'John')).toThrow('Expected plain object for key person') +}) diff --git a/src/Form/index.test.tsx b/src/Form/index.test.tsx index 9b097a1b..1ccb9684 100644 --- a/src/Form/index.test.tsx +++ b/src/Form/index.test.tsx @@ -163,3 +163,38 @@ test('should allow to reset state', () => { expect(form.getValue()).toEqual({name: 'Nico'}) }) + +test('does not render form tag in React Native environment', () => { + const original: any = (global as any).navigator + Object.defineProperty(global, 'navigator', { + value: {product: 'ReactNative'}, + configurable: true, + enumerable: true, + writable: true, + }) + + const {container} = render( + , + ) + + expect(container.querySelector('form')).not.toBeInTheDocument() + + Object.defineProperty(global, 'navigator', { + value: original, + configurable: true, + enumerable: true, + writable: true, + }) +}) + +test('passes dom props to the form element', () => { + const {container} = render( + , + ) + + expect(container.querySelector('form').getAttribute('target')).toBe('/submit-here') +}) diff --git a/src/WithValue/index.test.tsx b/src/WithValue/index.test.tsx index 5f458896..7c262a10 100644 --- a/src/WithValue/index.test.tsx +++ b/src/WithValue/index.test.tsx @@ -1,4 +1,4 @@ -import {render, screen} from '@testing-library/react' +import {render, screen, fireEvent, act} from '@testing-library/react' import React from 'react' import Field from '../Field' import Form from '../Form' @@ -7,6 +7,8 @@ import {FieldProps} from '../types' import WithValue from './index' import '@testing-library/jest-dom' +jest.useFakeTimers() + class DummyInput extends React.Component