-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Migrate input * Migrate textarea * Remove unused import * Migrate switch * Remove unused import * Migrate range-input * Migrate select * Migrate masked input * Migrate replace empty string value hoc * Migrate blur dirty * Migrate radio group input * Migrate cloudinary uploader * Be more explicit * Use toHaveAttribute * Do not reference id directly
- Loading branch information
Showing
10 changed files
with
242 additions
and
221 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,52 @@ | ||
import { blurDirty } from '../../../src/' | ||
import { noop } from '../../../src/utils' | ||
import React from 'react' | ||
import { mount } from 'enzyme' | ||
import { render, screen } from '@testing-library/react' | ||
import userEvent from '@testing-library/user-event' | ||
|
||
const InputToWrap = ({ input: { value, onBlur }}) => ( | ||
<input value={value} onBlur={onBlur} /> | ||
) | ||
|
||
test('has correct displayName', () => { | ||
const MyInput = () => <input /> | ||
const WrappedInput = blurDirty()(MyInput) | ||
expect(WrappedInput.displayName).toEqual('blurDirty(MyInput)') | ||
const WrappedInput = blurDirty()(InputToWrap) | ||
expect(WrappedInput.displayName).toEqual('blurDirty(InputToWrap)') | ||
}) | ||
|
||
test('when input is pristine, blurDirty replaces onBlur with empty function', () => { | ||
const MyInput = () => <input /> | ||
const WrappedInput = blurDirty()(MyInput) | ||
const onBlur = () => console.log('I blurred!') | ||
const wrapper = mount( | ||
test('when input is pristine, blurDirty replaces onBlur with empty function', async () => { | ||
const user = userEvent.setup() | ||
const WrappedInput = blurDirty()(InputToWrap) | ||
const onBlur = jest.fn() | ||
const { container, rerender } = render( | ||
<WrappedInput {...{ input: { onBlur }, meta: { pristine: true } }} /> | ||
) | ||
expect(wrapper.find(MyInput).props().input.onBlur).toEqual(noop) | ||
wrapper.setProps({ meta: { pristine: false } }) | ||
expect(wrapper.find(MyInput).props().input.onBlur).toEqual(onBlur) | ||
|
||
await user.click(screen.getByRole('textbox')) | ||
await user.click(container) | ||
|
||
expect(onBlur).not.toHaveBeenCalled() | ||
|
||
rerender( | ||
<WrappedInput {...{ input: { onBlur }, meta: { pristine: false } }} /> | ||
) | ||
|
||
await user.click(screen.getByRole('textbox')) | ||
await user.click(container) | ||
|
||
expect(onBlur).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
test('when alwaysBlur is set to true, blurDirty does not replace onBlur', () => { | ||
const MyInput = () => <input /> | ||
const WrappedInput = blurDirty()(MyInput) | ||
const onBlur = () => console.log('I blurred!') | ||
const wrapper = mount( | ||
test('when alwaysBlur is set to true, blurDirty does not replace onBlur', async () => { | ||
const user = userEvent.setup() | ||
const WrappedInput = blurDirty()(InputToWrap) | ||
const onBlur = jest.fn() | ||
const { container } = render( | ||
<WrappedInput | ||
{...{ input: { onBlur }, meta: { pristine: true }, alwaysBlur: true }} | ||
/> | ||
) | ||
expect(wrapper.find(MyInput).props().input.onBlur).toEqual(onBlur) | ||
wrapper.setProps({ meta: { pristine: false } }) | ||
expect(wrapper.find(MyInput).props().input.onBlur).toEqual(onBlur) | ||
|
||
await user.click(screen.getByRole('textbox')) | ||
await user.click(container) | ||
|
||
expect(onBlur).toHaveBeenCalled() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,26 @@ | ||
import { replaceEmptyStringValue } from '../../../src/' | ||
import React from 'react' | ||
import { mount } from 'enzyme' | ||
import { render, screen } from '@testing-library/react' | ||
|
||
const InputToWrap = ({ input: { value } }) => ( | ||
<input value={value} readOnly={true} /> | ||
) | ||
|
||
test('has correct displayName', () => { | ||
const MyInput = () => <input /> | ||
const WrappedInput = replaceEmptyStringValue()(MyInput) | ||
expect(WrappedInput.displayName).toEqual('replaceEmptyStringValue(MyInput)') | ||
const WrappedInput = replaceEmptyStringValue()(InputToWrap) | ||
expect(WrappedInput.displayName).toEqual('replaceEmptyStringValue(InputToWrap)') | ||
}) | ||
|
||
test('replaces empty string with given value', () => { | ||
const MyInput = () => <input /> | ||
const WrappedInput = replaceEmptyStringValue('foo')(MyInput) | ||
const wrapper = mount( | ||
<WrappedInput {...{ input: { value: '' }, meta: {} }} /> | ||
) | ||
expect(wrapper.find(MyInput).props().input.value).toEqual('foo') | ||
const WrappedInput = replaceEmptyStringValue('foo')(InputToWrap) | ||
render(<WrappedInput {...{ input: { value: '' }, meta: {} }} />) | ||
expect(screen.getByRole('textbox')).toHaveValue('foo') | ||
}) | ||
|
||
test("doesn't replace other values", () => { | ||
const MyInput = () => <input /> | ||
const WrappedInput = replaceEmptyStringValue('foo')(MyInput) | ||
const wrapper = mount( | ||
const WrappedInput = replaceEmptyStringValue('foo')(InputToWrap) | ||
render( | ||
<WrappedInput {...{ input: { value: 'other' }, meta: {} }} /> | ||
) | ||
expect(wrapper.find(MyInput).props().input.value).toEqual('other') | ||
expect(screen.getByRole('textbox')).toHaveValue('other') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.