-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(native): add CheckboxField component
- Loading branch information
Showing
5 changed files
with
93 additions
and
21 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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
jest.mock('react-native/Libraries/Components/Touchable/TouchableWithoutFeedback', | ||
() => 'TouchableWithoutFeedback'); |
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
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
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
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,10 +1,63 @@ | ||
import React from 'react'; | ||
import { mount } from 'enzyme'; | ||
import React, { createRef } from 'react'; | ||
import sinon from 'sinon'; | ||
import { render, wait, fireEvent } from '@testing-library/react-native'; | ||
|
||
import CheckboxField from './'; | ||
|
||
describe('test', () => { | ||
it('should render', () => { | ||
expect(mount(<CheckboxField />).html()).toBe('<div></div>'); | ||
describe('<CheckboxField />', () => { | ||
|
||
it('should render', async () => { | ||
const ref = createRef(); | ||
const { getByTestId } = render( | ||
<CheckboxField ref={ref}>Check this</CheckboxField> | ||
); | ||
|
||
await wait(() => getByTestId('CheckboxField/Main')); | ||
expect(getByTestId('CheckboxField/Main')).toBeDefined(); | ||
fireEvent.press(getByTestId('CheckboxField/Main')); | ||
expect(ref.current.internalValue).toBe(true); | ||
fireEvent.focus(getByTestId('CheckboxField/Main')); | ||
expect(ref.current.focused).toBe(true); | ||
fireEvent.blur(getByTestId('CheckboxField/Main')); | ||
expect(ref.current.focused).toBe(false); | ||
}); | ||
|
||
it('should correctly fire onChange event', async () => { | ||
const onChange = sinon.spy(); | ||
const { getByTestId } = render(<CheckboxField onChange={onChange} />); | ||
await wait(() => getByTestId('CheckboxField/Main')); | ||
fireEvent.press(getByTestId('CheckboxField/Main')); | ||
expect(onChange.calledWith(sinon.match({ checked: true }))) | ||
.toBe(true); | ||
fireEvent.press(getByTestId('CheckboxField/Main')); | ||
expect(onChange.calledWith(sinon.match({ checked: false }))) | ||
.toBe(true); | ||
}); | ||
|
||
it('should toggle checkbox active state on click', async () => { | ||
const ref = createRef(); | ||
const { getByTestId } = render(<CheckboxField ref={ref} />); | ||
await wait(() => getByTestId('CheckboxField/Main')); | ||
fireEvent.pressIn(getByTestId('CheckboxField/Main')); | ||
expect(ref.current.active).toBe(true); | ||
fireEvent.pressOut(getByTestId('CheckboxField/Main')); | ||
expect(ref.current.active).toBe(false); | ||
}); | ||
|
||
it('should toggle checkbox focused state on focus', async () => { | ||
const ref = createRef(); | ||
const onFocus = sinon.spy(); | ||
const onBlur = sinon.spy(); | ||
const { getByTestId } = render( | ||
<CheckboxField onFocus={onFocus} onBlur={onBlur} ref={ref} /> | ||
); | ||
await wait(() => getByTestId('CheckboxField/Main')); | ||
fireEvent.focus(getByTestId('CheckboxField/Main')); | ||
expect(ref.current.focused).toBe(true); | ||
expect(onFocus.called).toBe(true); | ||
fireEvent.blur(getByTestId('CheckboxField/Main')); | ||
expect(ref.current.focused).toBe(false); | ||
expect(onBlur.called).toBe(true); | ||
}); | ||
|
||
}); |