@@ -114,7 +114,7 @@ test("should remove target from the observer targets' list on umount", () => {
114114} ) ;
115115
116116describe ( 'update' , ( ) => {
117- test ( 'componentWillUpdate determines whether the observer should restart ' , ( ) => {
117+ test ( 'componentDidUpdate reobserves the target with observer prop changes ' , ( ) => {
118118 const component = (
119119 < IntersectionObserver onChange = { noop } >
120120 < span />
@@ -141,7 +141,7 @@ describe('update', () => {
141141 ) ;
142142 let called = false ;
143143 const tree = renderer . create ( component , {
144- createNodeMock : ( ) => {
144+ createNodeMock ( ) {
145145 if ( called ) {
146146 return target ;
147147 }
@@ -160,7 +160,7 @@ describe('update', () => {
160160 ) ;
161161
162162 tree . update (
163- < IntersectionObserver onChange = { noop } rootMargin = "1%" >
163+ < IntersectionObserver onChange = { noop } >
164164 < div />
165165 </ IntersectionObserver > ,
166166 ) ;
@@ -171,70 +171,81 @@ describe('update', () => {
171171 } ) ;
172172
173173 test ( 'should reobserve with new root, rootMargin and/or threshold props' , ( ) => {
174- const winElement = Object . assign ( { id : 'window' } , target ) ;
175- const docElement = Object . assign ( { id : 'document' } , target ) ;
174+ const root1 = Object . assign ( { id : 'window' } , target ) ;
175+ const root2 = Object . assign ( { id : 'document' } , target ) ;
176176 const initialProps = {
177177 onChange : noop ,
178- root : winElement ,
178+ root : root1 ,
179179 rootMargin : '10% 20%' ,
180180 threshold : 0.5 ,
181181 } ;
182- const children = < span /> ;
183- const component = < IntersectionObserver { ...initialProps } > { children } </ IntersectionObserver > ;
184- const instance = renderer . create ( component , { createNodeMock : ( ) => target } ) . getInstance ( ) ;
185- const nextProps = { ...initialProps , children } ;
186-
187- instance . componentWillUpdate ( nextProps ) ;
188- expect ( instance . shouldResetObserver ) . toBeFalsy ( ) ;
189-
190- instance . componentWillUpdate ( {
191- ...nextProps ,
192- children : < div /> ,
193- } ) ;
194- expect ( instance . shouldResetObserver ) . toBeFalsy ( ) ;
195-
196- instance . componentWillUpdate ( {
197- ...nextProps ,
198- root : docElement ,
199- } ) ;
200- expect ( instance . shouldResetObserver ) . toBeTruthy ( ) ;
201-
202- instance . componentWillUpdate ( {
203- ...nextProps ,
204- root : winElement ,
205- } ) ;
206- expect ( instance . shouldResetObserver ) . toBeFalsy ( ) ;
207-
208- instance . componentWillUpdate ( {
209- ...nextProps ,
210- root : winElement ,
211- rootMargin : '20% 10%' ,
212- } ) ;
213- expect ( instance . shouldResetObserver ) . toBeTruthy ( ) ;
214-
215- instance . componentWillUpdate ( {
216- ...nextProps ,
217- rootMargin : '20% 10%' ,
218- } ) ;
219- expect ( instance . shouldResetObserver ) . toBeTruthy ( ) ;
220-
221- instance . componentWillUpdate ( {
222- ...nextProps ,
223- threshold : [ 0.5 , 1 ] ,
224- } ) ;
225- expect ( instance . shouldResetObserver ) . toBeTruthy ( ) ;
182+ const component = (
183+ < IntersectionObserver { ...initialProps } >
184+ < span />
185+ </ IntersectionObserver >
186+ ) ;
187+ const tree = renderer . create ( component , { createNodeMock : ( ) => target } ) ;
188+ const instance = tree . getInstance ( ) ;
189+ const spy1 = jest . spyOn ( instance , 'unobserve' ) ;
190+ const spy2 = jest . spyOn ( instance , 'observe' ) ;
226191
227- instance . componentWillUpdate ( {
228- ...nextProps ,
229- threshold : [ 0 , 0.25 , 0.5 , 0.75 , 1 ] ,
230- } ) ;
231- expect ( instance . shouldResetObserver ) . toBeTruthy ( ) ;
192+ // none of the props updating
193+ tree . update (
194+ < IntersectionObserver { ...initialProps } >
195+ < span />
196+ </ IntersectionObserver > ,
197+ ) ;
198+ // only children updating
199+ tree . update (
200+ < IntersectionObserver { ...initialProps } >
201+ < div />
202+ </ IntersectionObserver > ,
203+ ) ;
204+ // only root updating (document)
205+ tree . update (
206+ < IntersectionObserver { ...initialProps } root = { root2 } >
207+ < div />
208+ </ IntersectionObserver > ,
209+ ) ;
210+ // only root updating (window)
211+ tree . update (
212+ < IntersectionObserver { ...initialProps } root = { root1 } >
213+ < div />
214+ </ IntersectionObserver > ,
215+ ) ;
216+ // only rootMargin updating
217+ tree . update (
218+ < IntersectionObserver { ...initialProps } root = { root1 } rootMargin = "20% 10%" >
219+ < div />
220+ </ IntersectionObserver > ,
221+ ) ;
222+ // only root updating (null)
223+ tree . update (
224+ < IntersectionObserver { ...initialProps } rootMargin = "20% 10%" >
225+ < div />
226+ </ IntersectionObserver > ,
227+ ) ;
228+ // only threshold updating (non-scalar)
229+ tree . update (
230+ < IntersectionObserver { ...initialProps } threshold = { [ 0.5 , 1 ] } >
231+ < div />
232+ </ IntersectionObserver > ,
233+ ) ;
234+ // only threshold updating (length changed)
235+ tree . update (
236+ < IntersectionObserver { ...initialProps } threshold = { [ 0 , 0.25 , 0.5 , 0.75 , 1 ] } >
237+ < div />
238+ </ IntersectionObserver > ,
239+ ) ;
240+ // only threshold updating (scalar)
241+ tree . update (
242+ < IntersectionObserver { ...initialProps } threshold = { 1 } >
243+ < div />
244+ </ IntersectionObserver > ,
245+ ) ;
232246
233- instance . componentWillUpdate ( {
234- ...nextProps ,
235- threshold : 1 ,
236- } ) ;
237- expect ( instance . shouldResetObserver ) . toBeTruthy ( ) ;
247+ expect ( spy1 ) . toHaveBeenCalledTimes ( 6 ) ;
248+ expect ( spy2 ) . toHaveBeenCalledTimes ( 6 ) ;
238249 } ) ;
239250
240251 test ( 'should be defensive against unobserving nullified nodes' , ( ) => {
0 commit comments