-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## Summary It was brought to our attention that `inline styles` do not work with `Jest`. This PR aims to fix this problem. ## Test plan Run tests
- Loading branch information
Showing
7 changed files
with
374 additions
and
15 deletions.
There are no files selected for viewing
91 changes: 91 additions & 0 deletions
91
packages/react-native-reanimated/__tests__/hooks.useAnimatedStyle.test.tsx
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,91 @@ | ||
import { Button, View } from 'react-native'; | ||
import { fireEvent, render, screen } from '@testing-library/react-native'; | ||
import Animated, { | ||
useSharedValue, | ||
getAnimatedStyle, | ||
useAnimatedStyle, | ||
withTiming, | ||
} from '../src'; | ||
|
||
jest.useFakeTimers(); | ||
|
||
describe('Tests of inline styles', () => { | ||
beforeEach(() => { | ||
jest.useFakeTimers(); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.runOnlyPendingTimers(); | ||
jest.useRealTimers(); | ||
}); | ||
|
||
test('useAnimatedStyle', () => { | ||
function UseAnimatedStyle() { | ||
const width = useSharedValue(100); | ||
|
||
const handlePress = () => { | ||
width.value = width.value + 50; | ||
}; | ||
|
||
const animatedStyle = useAnimatedStyle(() => { | ||
return { | ||
width: width.value, | ||
}; | ||
}, [width]); | ||
|
||
return ( | ||
<View style={{ flex: 1, alignItems: 'center' }}> | ||
<Animated.View testID="view" style={animatedStyle} /> | ||
<Button testID="button" onPress={handlePress} title="Click me" /> | ||
</View> | ||
); | ||
} | ||
|
||
render(<UseAnimatedStyle />); | ||
const view = screen.getByTestId('view'); | ||
const button = screen.getByTestId('button'); | ||
|
||
expect(getAnimatedStyle(view)).toEqual({ width: 100 }); | ||
|
||
fireEvent.press(button); | ||
|
||
jest.runAllTimers(); | ||
|
||
expect(getAnimatedStyle(view)).toEqual({ width: 150 }); | ||
}); | ||
|
||
test('useAnimatedStyle with withTiming', () => { | ||
function UseAnimatedStyle() { | ||
const width = useSharedValue(100); | ||
|
||
const handlePress = () => { | ||
width.value = withTiming(width.value + 50, { duration: 500 }); | ||
}; | ||
|
||
const animatedStyle = useAnimatedStyle(() => { | ||
return { | ||
width: width.value, | ||
}; | ||
}, [width]); | ||
|
||
return ( | ||
<View style={{ flex: 1, alignItems: 'center' }}> | ||
<Animated.View testID="view" style={animatedStyle} /> | ||
<Button testID="button" onPress={handlePress} title="Click me" /> | ||
</View> | ||
); | ||
} | ||
|
||
render(<UseAnimatedStyle />); | ||
const view = screen.getByTestId('view'); | ||
const button = screen.getByTestId('button'); | ||
|
||
expect(getAnimatedStyle(view)).toEqual({ width: 100 }); | ||
|
||
fireEvent.press(button); | ||
|
||
jest.runAllTimers(); | ||
|
||
expect(getAnimatedStyle(view)).toEqual({ width: 150 }); | ||
}); | ||
}); |
197 changes: 197 additions & 0 deletions
197
packages/react-native-reanimated/__tests__/inlineStyles.test.tsx
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,197 @@ | ||
import { Button, View } from 'react-native'; | ||
import { fireEvent, render, screen } from '@testing-library/react-native'; | ||
import Animated, { | ||
useSharedValue, | ||
getAnimatedStyle, | ||
useAnimatedStyle, | ||
withTiming, | ||
} from '../src'; | ||
|
||
jest.useFakeTimers(); | ||
|
||
describe('Tests of inline styles', () => { | ||
beforeEach(() => { | ||
jest.useFakeTimers(); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.runOnlyPendingTimers(); | ||
jest.useRealTimers(); | ||
}); | ||
|
||
test('Inline styles', () => { | ||
function InlineStyle() { | ||
const width = useSharedValue(100); | ||
|
||
const handlePress = () => { | ||
width.value = width.value + 50; | ||
}; | ||
|
||
return ( | ||
<View style={{ flex: 1, alignItems: 'center' }}> | ||
<Animated.View | ||
testID="view" | ||
style={{ | ||
width, | ||
}} | ||
/> | ||
<Button testID="button" onPress={handlePress} title="Click me" /> | ||
</View> | ||
); | ||
} | ||
|
||
render(<InlineStyle />); | ||
const view = screen.getByTestId('view'); | ||
const button = screen.getByTestId('button'); | ||
|
||
expect(getAnimatedStyle(view)).toEqual({ width: 100 }); | ||
|
||
fireEvent.press(button); | ||
jest.runAllTimers(); | ||
|
||
expect(view).toHaveAnimatedStyle({ width: 150 }); | ||
}); | ||
|
||
test('Inline styles with withTiming', () => { | ||
function InlineStyle() { | ||
const width = useSharedValue(100); | ||
|
||
const handlePress = () => { | ||
width.value = withTiming(width.value + 50, { duration: 500 }); | ||
}; | ||
|
||
return ( | ||
<View style={{ flex: 1, alignItems: 'center' }}> | ||
<Animated.View | ||
testID="view" | ||
style={{ | ||
width, | ||
height: 100, | ||
backgroundColor: 'violet', | ||
}} | ||
/> | ||
<Button testID="button" onPress={handlePress} title="Click me" /> | ||
</View> | ||
); | ||
} | ||
|
||
render(<InlineStyle />); | ||
const view = screen.getByTestId('view'); | ||
const button = screen.getByTestId('button'); | ||
|
||
expect(getAnimatedStyle(view)).toEqual({ | ||
width: 100, | ||
height: 100, | ||
backgroundColor: 'violet', | ||
}); | ||
|
||
fireEvent.press(button); | ||
|
||
jest.runAllTimers(); | ||
|
||
expect(getAnimatedStyle(view)).toEqual({ | ||
width: 150, | ||
height: 100, | ||
backgroundColor: 'violet', | ||
}); | ||
}); | ||
|
||
test('Double inline styles (single object)', () => { | ||
function UseAnimatedStyle() { | ||
const width = useSharedValue(100); | ||
const height = useSharedValue(100); | ||
|
||
const handlePress = () => { | ||
width.value = width.value + 50; | ||
height.value = height.value + 50; | ||
}; | ||
|
||
return ( | ||
<View style={{ flex: 1, alignItems: 'center' }}> | ||
<Animated.View testID="view" style={{ width, height }} /> | ||
<Button testID="button" onPress={handlePress} title="Click me" /> | ||
</View> | ||
); | ||
} | ||
|
||
render(<UseAnimatedStyle />); | ||
const view = screen.getByTestId('view'); | ||
const button = screen.getByTestId('button'); | ||
|
||
expect(getAnimatedStyle(view)).toEqual({ width: 100, height: 100 }); | ||
|
||
fireEvent.press(button); | ||
|
||
jest.runAllTimers(); | ||
|
||
expect(getAnimatedStyle(view)).toEqual({ width: 150, height: 150 }); | ||
}); | ||
|
||
test('Double inline styles (array)', () => { | ||
function UseAnimatedStyle() { | ||
const width = useSharedValue(100); | ||
const height = useSharedValue(100); | ||
|
||
const handlePress = () => { | ||
width.value = width.value + 50; | ||
height.value = height.value + 50; | ||
}; | ||
|
||
return ( | ||
<View style={{ flex: 1, alignItems: 'center' }}> | ||
<Animated.View testID="view" style={[{ width }, { height }]} /> | ||
<Button testID="button" onPress={handlePress} title="Click me" /> | ||
</View> | ||
); | ||
} | ||
|
||
render(<UseAnimatedStyle />); | ||
const view = screen.getByTestId('view'); | ||
const button = screen.getByTestId('button'); | ||
|
||
expect(getAnimatedStyle(view)).toEqual({ width: 100, height: 100 }); | ||
|
||
fireEvent.press(button); | ||
|
||
jest.runAllTimers(); | ||
|
||
expect(getAnimatedStyle(view)).toEqual({ width: 150, height: 150 }); | ||
}); | ||
|
||
test('Inline & useAnimatedStyle()', () => { | ||
function UseAnimatedStyle() { | ||
const width = useSharedValue(100); | ||
const height = useSharedValue(100); | ||
|
||
const handlePress = () => { | ||
width.value = width.value + 50; | ||
height.value = height.value + 50; | ||
}; | ||
|
||
const animatedStyle = useAnimatedStyle(() => { | ||
return { | ||
width: width.value, | ||
}; | ||
}, [width]); | ||
|
||
return ( | ||
<View style={{ flex: 1, alignItems: 'center' }}> | ||
<Animated.View testID="view" style={[animatedStyle, { height }]} /> | ||
<Button testID="button" onPress={handlePress} title="Click me" /> | ||
</View> | ||
); | ||
} | ||
|
||
render(<UseAnimatedStyle />); | ||
const view = screen.getByTestId('view'); | ||
const button = screen.getByTestId('button'); | ||
|
||
expect(getAnimatedStyle(view)).toEqual({ width: 100, height: 100 }); | ||
|
||
fireEvent.press(button); | ||
|
||
jest.runAllTimers(); | ||
|
||
expect(getAnimatedStyle(view)).toEqual({ width: 150, height: 150 }); | ||
}); | ||
}); |
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
Oops, something went wrong.