-
-
Notifications
You must be signed in to change notification settings - Fork 100
feat: jest integration #102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
83ef4f7
refactor: jest mocks
kirillzyusko b07b886
refactor: continue implementation
kirillzyusko 620f323
refactor: removed unused file
kirillzyusko cc6cd12
refactor: fixed TS support for mock
kirillzyusko 9c7f327
refactor: add a sample of API for testing
kirillzyusko 7c93292
refactor: more tests coming
kirillzyusko 0ab7e2e
refactor: added more tests
kirillzyusko 0401943
refactor: save intermediate changes
kirillzyusko d1273ab
refactor: keep working
kirillzyusko 2fa588f
refactor: removed test kit
kirillzyusko 46091b9
refactor: keyboard handler test
kirillzyusko d875fc2
fix: run test in lib stage only against src folder
kirillzyusko 76038ef
fix: added jest to globals (eslint)
kirillzyusko 9850e2f
refactor: fixed unit test
kirillzyusko 5cfef18
refactor: changes after code review
kirillzyusko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,67 @@ | ||
--- | ||
sidebar_position: 3 | ||
--- | ||
|
||
# Jest testing guide | ||
|
||
## Setting up a mock | ||
|
||
This library includes a built in mock for Jest. To use it, add the following code to the [jest setup](https://jestjs.io/docs/configuration#setupfiles-array) file: | ||
|
||
```js | ||
jest.mock('react-native-keyboard-controller', () => | ||
require('react-native-keyboard-controller/jest') | ||
); | ||
``` | ||
|
||
## Test case example | ||
|
||
Once you've set up mock - you can write your first test 😊. A sample of test case is shown below. For more test cases please see [this](https://github.com/kirillzyusko/react-native-keyboard-controller/tree/main/example/__tests__) link. | ||
|
||
```tsx | ||
import '@testing-library/jest-native/extend-expect'; | ||
import React from 'react'; | ||
import { Animated } from 'react-native'; | ||
import { render } from '@testing-library/react-native'; | ||
|
||
import { useKeyboardAnimation } from 'react-native-keyboard-controller'; | ||
|
||
function TestComponent() { | ||
const { height } = useKeyboardAnimation(); | ||
|
||
return ( | ||
<Animated.View | ||
testID="view" | ||
style={{ transform: [{ translateY: height }] }} | ||
/> | ||
); | ||
} | ||
|
||
describe('basic keyboard interaction', () => { | ||
it('should have different styles depends on position', () => { | ||
const { getByTestId, update } = render(<TestComponent />); | ||
|
||
expect(getByTestId('view')).toHaveStyle({ transform: [{ translateY: 0 }] }); | ||
|
||
(useKeyboardAnimation as jest.Mock).mockReturnValue({ | ||
height: new Animated.Value(150), | ||
progress: new Animated.Value(0.5), | ||
}); | ||
update(<TestComponent />); | ||
|
||
expect(getByTestId('view')).toHaveStyle({ | ||
transform: [{ translateY: 150 }], | ||
}); | ||
|
||
(useKeyboardAnimation as jest.Mock).mockReturnValue({ | ||
height: new Animated.Value(300), | ||
progress: new Animated.Value(1), | ||
}); | ||
update(<TestComponent />); | ||
|
||
expect(getByTestId('view')).toHaveStyle({ | ||
transform: [{ translateY: 300 }], | ||
}); | ||
}); | ||
}); | ||
``` |
67 changes: 67 additions & 0 deletions
67
docs/versioned_docs/version-1.4.0/recipes/jest-testing-guide.md
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,67 @@ | ||
--- | ||
sidebar_position: 3 | ||
--- | ||
|
||
# Jest testing guide | ||
|
||
## Setting up a mock | ||
|
||
This library includes a built in mock for Jest. To use it, add the following code to the [jest setup](https://jestjs.io/docs/configuration#setupfiles-array) file: | ||
|
||
```js | ||
jest.mock('react-native-keyboard-controller', () => | ||
require('react-native-keyboard-controller/jest') | ||
); | ||
``` | ||
|
||
## Test case example | ||
|
||
Once you've set up mock - you can write your first test 😊. A sample of test case is shown below. For more test cases please see [this](https://github.com/kirillzyusko/react-native-keyboard-controller/tree/main/example/__tests__) link. | ||
|
||
```tsx | ||
import '@testing-library/jest-native/extend-expect'; | ||
import React from 'react'; | ||
import { Animated } from 'react-native'; | ||
import { render } from '@testing-library/react-native'; | ||
|
||
import { useKeyboardAnimation } from 'react-native-keyboard-controller'; | ||
|
||
function TestComponent() { | ||
const { height } = useKeyboardAnimation(); | ||
|
||
return ( | ||
<Animated.View | ||
testID="view" | ||
style={{ transform: [{ translateY: height }] }} | ||
/> | ||
); | ||
} | ||
|
||
describe('basic keyboard interaction', () => { | ||
it('should have different styles depends on position', () => { | ||
const { getByTestId, update } = render(<TestComponent />); | ||
|
||
expect(getByTestId('view')).toHaveStyle({ transform: [{ translateY: 0 }] }); | ||
|
||
(useKeyboardAnimation as jest.Mock).mockReturnValue({ | ||
height: new Animated.Value(150), | ||
progress: new Animated.Value(0.5), | ||
}); | ||
update(<TestComponent />); | ||
|
||
expect(getByTestId('view')).toHaveStyle({ | ||
transform: [{ translateY: 150 }], | ||
}); | ||
|
||
(useKeyboardAnimation as jest.Mock).mockReturnValue({ | ||
height: new Animated.Value(300), | ||
progress: new Animated.Value(1), | ||
}); | ||
update(<TestComponent />); | ||
|
||
expect(getByTestId('view')).toHaveStyle({ | ||
transform: [{ translateY: 300 }], | ||
}); | ||
}); | ||
}); | ||
``` |
23 changes: 23 additions & 0 deletions
23
example/__tests__/__snapshots__/components-rendering.spec.tsx.snap
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,23 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`components rendering should render \`KeyboardControllerView\` 1`] = ` | ||
<KeyboardControllerView | ||
statusBarTranslucent={true} | ||
/> | ||
`; | ||
|
||
exports[`components rendering should render \`KeyboardProvider\` 1`] = ` | ||
<KeyboardProvider | ||
statusBarTranslucent={true} | ||
> | ||
<View | ||
style={ | ||
Object { | ||
"backgroundColor": "black", | ||
"height": 20, | ||
"width": 20, | ||
} | ||
} | ||
/> | ||
</KeyboardProvider> | ||
`; |
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,45 @@ | ||
import '@testing-library/jest-native/extend-expect'; | ||
import React from 'react'; | ||
import { Animated } from 'react-native'; | ||
import { render } from '@testing-library/react-native'; | ||
|
||
import { useKeyboardAnimation } from 'react-native-keyboard-controller'; | ||
|
||
function TestComponent() { | ||
const { height } = useKeyboardAnimation(); | ||
|
||
return ( | ||
<Animated.View | ||
testID="view" | ||
style={{ transform: [{ translateY: height }] }} | ||
/> | ||
); | ||
} | ||
|
||
describe('basic keyboard interaction', () => { | ||
it('should have different styles depends on position', () => { | ||
const { getByTestId, update } = render(<TestComponent />); | ||
|
||
expect(getByTestId('view')).toHaveStyle({ transform: [{ translateY: 0 }] }); | ||
|
||
(useKeyboardAnimation as jest.Mock).mockReturnValue({ | ||
height: new Animated.Value(150), | ||
progress: new Animated.Value(0.5), | ||
}); | ||
update(<TestComponent />); | ||
|
||
expect(getByTestId('view')).toHaveStyle({ | ||
transform: [{ translateY: 150 }], | ||
}); | ||
|
||
(useKeyboardAnimation as jest.Mock).mockReturnValue({ | ||
height: new Animated.Value(300), | ||
progress: new Animated.Value(1), | ||
}); | ||
update(<TestComponent />); | ||
|
||
expect(getByTestId('view')).toHaveStyle({ | ||
transform: [{ translateY: 300 }], | ||
}); | ||
}); | ||
}); |
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,29 @@ | ||
import React from 'react'; | ||
import { View } from 'react-native'; | ||
import { | ||
KeyboardControllerView, | ||
KeyboardProvider, | ||
} from 'react-native-keyboard-controller'; | ||
import { render } from '@testing-library/react-native'; | ||
|
||
function KeyboardControllerViewTest() { | ||
return <KeyboardControllerView statusBarTranslucent />; | ||
} | ||
|
||
function KeyboardProviderTest() { | ||
return ( | ||
<KeyboardProvider statusBarTranslucent> | ||
<View style={{ width: 20, height: 20, backgroundColor: 'black' }} /> | ||
</KeyboardProvider> | ||
); | ||
} | ||
|
||
describe('components rendering', () => { | ||
it('should render `KeyboardControllerView`', () => { | ||
expect(render(<KeyboardControllerViewTest />)).toMatchSnapshot(); | ||
}); | ||
|
||
it('should render `KeyboardProvider`', () => { | ||
expect(render(<KeyboardProviderTest />)).toMatchSnapshot(); | ||
}); | ||
}); |
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,88 @@ | ||
jest.unmock('react-native-reanimated'); | ||
jest.useFakeTimers(); | ||
|
||
global.ReanimatedDataMock = { | ||
now: () => 0, | ||
}; | ||
|
||
import '@testing-library/jest-native/extend-expect'; | ||
import React from 'react'; | ||
import Reanimated, { | ||
useAnimatedStyle, | ||
useSharedValue, | ||
} from 'react-native-reanimated'; | ||
import { render } from '@testing-library/react-native'; | ||
|
||
import { | ||
KeyboardHandler, | ||
NativeEvent, | ||
useKeyboardHandler, | ||
} from 'react-native-keyboard-controller'; | ||
|
||
function TestComponent() { | ||
const height = useSharedValue(0); | ||
useKeyboardHandler( | ||
{ | ||
onStart: (e) => { | ||
'worklet'; | ||
height.value = e.height; | ||
}, | ||
onMove: (e) => { | ||
'worklet'; | ||
height.value = e.height; | ||
}, | ||
onEnd: (e) => { | ||
'worklet'; | ||
height.value = e.height; | ||
}, | ||
}, | ||
[] | ||
); | ||
const style = useAnimatedStyle( | ||
() => ({ | ||
width: 20, | ||
height: 20, | ||
backgroundColor: 'red', | ||
transform: [{ translateY: height.value }], | ||
}), | ||
[] | ||
); | ||
|
||
return <Reanimated.View testID="view" style={style} />; | ||
} | ||
|
||
describe('keyboard handler specification', () => { | ||
it('should execute all handlers and change corresponding style properties', () => { | ||
let handlers: KeyboardHandler = {}; | ||
(useKeyboardHandler as jest.Mock).mockImplementation( | ||
(handler) => (handlers = handler) | ||
); | ||
const onStart = (e: NativeEvent) => handlers.onStart?.(e); | ||
const onMove = (e: NativeEvent) => handlers.onMove?.(e); | ||
const onEnd = (e: NativeEvent) => handlers.onEnd?.(e); | ||
const { getByTestId } = render(<TestComponent />); | ||
|
||
expect(getByTestId('view')).toHaveStyle({ transform: [{ translateY: 0 }] }); | ||
|
||
onStart({ height: 100, progress: 1 }); | ||
jest.advanceTimersByTime(100); | ||
|
||
expect(getByTestId('view')).toHaveAnimatedStyle({ | ||
transform: [{ translateY: 100 }], | ||
}); | ||
|
||
onMove({ height: 20, progress: 0.2 }); | ||
jest.advanceTimersByTime(100); | ||
|
||
expect(getByTestId('view')).toHaveAnimatedStyle({ | ||
transform: [{ translateY: 20 }], | ||
}); | ||
|
||
onEnd({ height: 100, progress: 1 }); | ||
jest.advanceTimersByTime(100); | ||
|
||
expect(getByTestId('view')).toHaveAnimatedStyle({ | ||
transform: [{ translateY: 100 }], | ||
}); | ||
}); | ||
}); |
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,49 @@ | ||
import '@testing-library/jest-native/extend-expect'; | ||
import React from 'react'; | ||
import Reanimated, { useAnimatedStyle } from 'react-native-reanimated'; | ||
import { render } from '@testing-library/react-native'; | ||
|
||
import { useReanimatedKeyboardAnimation } from 'react-native-keyboard-controller'; | ||
|
||
function TestComponent() { | ||
const { height } = useReanimatedKeyboardAnimation(); | ||
const style = useAnimatedStyle( | ||
() => ({ | ||
width: 20, | ||
height: 20, | ||
backgroundColor: 'red', | ||
transform: [{ translateY: height.value }], | ||
}), | ||
[] | ||
); | ||
|
||
return <Reanimated.View testID="view" style={style} />; | ||
} | ||
|
||
describe('basic keyboard interaction', () => { | ||
it('should have different styles depends on position', () => { | ||
const { getByTestId, update } = render(<TestComponent />); | ||
|
||
expect(getByTestId('view')).toHaveStyle({ transform: [{ translateY: 0 }] }); | ||
|
||
(useReanimatedKeyboardAnimation as jest.Mock).mockReturnValue({ | ||
height: { value: 150 }, | ||
progress: { progress: 0.5 }, | ||
}); | ||
update(<TestComponent />); | ||
|
||
expect(getByTestId('view')).toHaveStyle({ | ||
transform: [{ translateY: 150 }], | ||
}); | ||
|
||
(useReanimatedKeyboardAnimation as jest.Mock).mockReturnValue({ | ||
height: { value: 300 }, | ||
progress: { progress: 1 }, | ||
}); | ||
update(<TestComponent />); | ||
|
||
expect(getByTestId('view')).toHaveStyle({ | ||
transform: [{ translateY: 300 }], | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe it's better to split these object to fisrtPosition, secondPosition, thirdPosition?