File tree Expand file tree Collapse file tree 2 files changed +34
-1
lines changed Expand file tree Collapse file tree 2 files changed +34
-1
lines changed Original file line number Diff line number Diff line change @@ -32,6 +32,8 @@ class NativeSvg extends Component<IContentLoaderProps> {
3232
3333 idGradient = `${ this . fixedId } -animated-diff`
3434
35+ unmounted = false
36+
3537 setAnimation = ( ) => {
3638 // props.speed is in seconds as it is compatible with web
3739 // convert to milliseconds
@@ -44,7 +46,7 @@ class NativeSvg extends Component<IContentLoaderProps> {
4446 duration : durMs ,
4547 useNativeDriver : true ,
4648 } ) . start ( ( ) => {
47- if ( this . props . animate ) {
49+ if ( ! this . unmounted && this . props . animate ) {
4850 this . animatedValue . setValue ( - 1 )
4951 this . setAnimation ( )
5052 }
@@ -63,6 +65,10 @@ class NativeSvg extends Component<IContentLoaderProps> {
6365 }
6466 }
6567
68+ componentWillUnmount ( ) {
69+ this . unmounted = true
70+ }
71+
6672 render ( ) {
6773 const {
6874 children,
Original file line number Diff line number Diff line change 11import * as React from 'react'
2+ import { Animated } from 'react-native'
23import * as renderer from 'react-test-renderer'
34import * as ShallowRenderer from 'react-test-renderer/shallow'
45
@@ -121,4 +122,30 @@ describe('ContentLoader', () => {
121122 expect ( propsFromFullField . rtl ) . toBe ( true )
122123 } )
123124 } )
125+
126+ describe ( 'when using SVG' , ( ) => {
127+ describe ( 'cleanup' , ( ) => {
128+ afterAll ( ( ) => {
129+ jest . useRealTimers ( )
130+ } )
131+
132+ it ( 'cleans up animations when unmounted' , ( ) => {
133+ jest . useFakeTimers ( )
134+ const animationSpy = jest . spyOn ( Animated , 'timing' )
135+
136+ const mockSpeed = 10
137+ const { unmount } = renderer . create (
138+ < ContentLoader animate = { true } height = { 200 } speed = { mockSpeed } >
139+ < Rect />
140+ </ ContentLoader >
141+ )
142+
143+ jest . runTimersToTime ( mockSpeed )
144+ unmount ( )
145+ jest . runTimersToTime ( mockSpeed )
146+
147+ expect ( animationSpy ) . toHaveBeenCalledTimes ( 1 )
148+ } )
149+ } )
150+ } )
124151} )
You can’t perform that action at this time.
0 commit comments