Skip to content
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

fix: cleanup native SVG animations #232

Merged
merged 1 commit into from
Mar 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/native/Svg.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class NativeSvg extends Component<IContentLoaderProps> {

idGradient = `${this.fixedId}-animated-diff`

unmounted = false

setAnimation = () => {
// props.speed is in seconds as it is compatible with web
// convert to milliseconds
Expand All @@ -44,7 +46,7 @@ class NativeSvg extends Component<IContentLoaderProps> {
duration: durMs,
useNativeDriver: true,
}).start(() => {
if (this.props.animate) {
if (!this.unmounted && this.props.animate) {
this.animatedValue.setValue(-1)
this.setAnimation()
}
Expand All @@ -63,6 +65,10 @@ class NativeSvg extends Component<IContentLoaderProps> {
}
}

componentWillUnmount() {
this.unmounted = true
}

render() {
const {
children,
Expand Down
27 changes: 27 additions & 0 deletions src/native/__tests__/ContentLoader.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as React from 'react'
import { Animated } from 'react-native'
import * as renderer from 'react-test-renderer'
import * as ShallowRenderer from 'react-test-renderer/shallow'

Expand Down Expand Up @@ -121,4 +122,30 @@ describe('ContentLoader', () => {
expect(propsFromFullField.rtl).toBe(true)
})
})

describe('when using SVG', () => {
describe('cleanup', () => {
afterAll(() => {
jest.useRealTimers()
})

it('cleans up animations when unmounted', () => {
jest.useFakeTimers()
const animationSpy = jest.spyOn(Animated, 'timing')

const mockSpeed = 10
const { unmount } = renderer.create(
<ContentLoader animate={true} height={200} speed={mockSpeed}>
<Rect />
</ContentLoader>
)

jest.runTimersToTime(mockSpeed)
unmount()
jest.runTimersToTime(mockSpeed)

expect(animationSpy).toHaveBeenCalledTimes(1)
})
})
})
})