-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
94 additions
and
0 deletions.
There are no files selected for viewing
94 changes: 94 additions & 0 deletions
94
packages/react-native-aztec/src/test/AztecInputState.test.js
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,94 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import TextInputState from 'react-native/Libraries/Components/TextInput/TextInputState'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { | ||
addBlurListener, | ||
addFocusListener, | ||
getCurrentFocusedElement, | ||
isFocused, | ||
focus, | ||
blur, | ||
notifyBlur, | ||
notifyFocus, | ||
removeFocusListener, | ||
removeBlurListener, | ||
} from '../AztecInputState'; | ||
|
||
jest.mock( | ||
'react-native/Libraries/Components/TextInput/TextInputState', | ||
() => ( { | ||
focusTextInput: jest.fn(), | ||
blurTextInput: jest.fn(), | ||
} ) | ||
); | ||
|
||
const ref = { current: null }; | ||
|
||
describe( 'Aztec Input State', () => { | ||
it( 'listens to focus event', () => { | ||
const listener = jest.fn(); | ||
addFocusListener( listener ); | ||
notifyFocus( ref ); | ||
expect( listener ).toHaveBeenCalledWith( ref ); | ||
} ); | ||
|
||
it( 'listens to blur event', () => { | ||
const listener = jest.fn(); | ||
addBlurListener( listener ); | ||
notifyBlur( ref ); | ||
expect( listener ).toHaveBeenCalledWith( ref ); | ||
} ); | ||
|
||
it( 'does not call focus listener if removed', () => { | ||
const listener = jest.fn(); | ||
addFocusListener( listener ); | ||
removeFocusListener( listener ); | ||
notifyFocus( ref ); | ||
expect( listener ).not.toHaveBeenCalled(); | ||
} ); | ||
|
||
it( 'does not call blur listener if removed', () => { | ||
const listener = jest.fn(); | ||
addBlurListener( listener ); | ||
removeBlurListener( listener ); | ||
notifyBlur( ref ); | ||
expect( listener ).not.toHaveBeenCalled(); | ||
} ); | ||
|
||
it( 'returns true if an element is focused', () => { | ||
notifyFocus( ref ); | ||
expect( isFocused() ).toBeTruthy(); | ||
} ); | ||
|
||
it( 'returns false if an element is unfocused', () => { | ||
notifyFocus( ref ); | ||
notifyBlur( ref ); | ||
expect( isFocused() ).toBeFalsy(); | ||
} ); | ||
|
||
it( 'returns current focused element', () => { | ||
notifyFocus( ref ); | ||
expect( getCurrentFocusedElement() ).toBe( ref ); | ||
} ); | ||
|
||
it( 'returns null if focused element is unfocused', () => { | ||
notifyFocus( ref ); | ||
notifyBlur( ref ); | ||
expect( getCurrentFocusedElement() ).toBe( null ); | ||
} ); | ||
|
||
it( 'focus an element', () => { | ||
focus( ref ); | ||
expect( TextInputState.focusTextInput ).toHaveBeenCalledWith( ref ); | ||
} ); | ||
|
||
it( 'unfocuses an element', () => { | ||
blur( ref ); | ||
expect( TextInputState.blurTextInput ).toHaveBeenCalledWith( ref ); | ||
} ); | ||
} ); |