@@ -197,6 +197,22 @@ describe('<Timer />', () => {
197197 component . unmount ( ) ;
198198 } ) ;
199199
200+ test ( 'onStop - called' , ( ) => {
201+ expect . assertions ( 2 ) ;
202+
203+ const onStop = jest . fn ( ) ;
204+
205+ component = mount ( < Timer active onStop = { onStop } /> , {
206+ attachTo : document . getElementById ( 'root' ) ,
207+ } ) ;
208+
209+ expect ( onStop ) . not . toBeCalled ( ) ;
210+ component . setProps ( { active : false } ) ;
211+ expect ( onStop ) . toBeCalled ( ) ;
212+
213+ component . unmount ( ) ;
214+ } ) ;
215+
200216 test ( 'time offset supported - onStart' , ( ) => {
201217 const onStart = jest . fn ( ) ;
202218
@@ -248,18 +264,117 @@ describe('<Timer />', () => {
248264 component . unmount ( ) ;
249265 } ) ;
250266
251- test ( 'onStop - called ' , ( ) => {
267+ test ( 'time prop changed ' , ( ) => {
252268 expect . assertions ( 2 ) ;
269+ const onTimeUpdate = jest . fn ( ) ;
253270
271+ component = mount ( < Timer active onTimeUpdate = { onTimeUpdate } /> , {
272+ attachTo : document . getElementById ( 'root' ) ,
273+ } ) ;
274+
275+ clock . tick ( 16 ) ;
276+ expect ( onTimeUpdate ) . toHaveBeenLastCalledWith ( {
277+ duration : 10000 ,
278+ progress : 0.0016 ,
279+ time : 16 ,
280+ } ) ;
281+ component . setProps ( { time : 5000 } ) ;
282+ clock . tick ( 16 ) ;
283+ expect ( onTimeUpdate ) . toHaveBeenLastCalledWith ( {
284+ duration : 10000 ,
285+ progress : 0.5016 ,
286+ time : 5016 ,
287+ } ) ;
288+
289+ component . unmount ( ) ;
290+ } ) ;
291+
292+ test ( 'active prop changed - after timer complete' , ( ) => {
293+ expect . assertions ( 4 ) ;
294+
295+ const onStart = jest . fn ( ) ;
254296 const onStop = jest . fn ( ) ;
297+ const onTimeUpdate = jest . fn ( ) ;
255298
256- component = mount ( < Timer active onStop = { onStop } /> , {
299+ component = mount ( (
300+ < Timer
301+ active
302+ onStart = { onStart }
303+ onStop = { onStop }
304+ onTimeUpdate = { onTimeUpdate }
305+ />
306+ ) , {
257307 attachTo : document . getElementById ( 'root' ) ,
258308 } ) ;
259309
260- expect ( onStop ) . not . toBeCalled ( ) ;
310+ expect ( onStart ) . toHaveBeenLastCalledWith ( {
311+ duration : 10000 ,
312+ progress : 0 ,
313+ time : 0 ,
314+ } ) ;
315+ clock . tick ( 10000 ) ;
316+ expect ( onTimeUpdate ) . toHaveBeenLastCalledWith ( {
317+ duration : 10000 ,
318+ progress : 1 ,
319+ time : 10000 ,
320+ } ) ;
261321 component . setProps ( { active : false } ) ;
262- expect ( onStop ) . toBeCalled ( ) ;
322+ expect ( onStop ) . toHaveBeenLastCalledWith ( {
323+ duration : 10000 ,
324+ progress : 1 ,
325+ time : 10000 ,
326+ } ) ;
327+ component . setProps ( { active : true } ) ;
328+ expect ( onStart ) . toHaveBeenLastCalledWith ( {
329+ duration : 10000 ,
330+ progress : 0 ,
331+ time : 0 ,
332+ } ) ;
333+
334+ component . unmount ( ) ;
335+ } ) ;
336+
337+ test ( 'active prop changed - before timer complete' , ( ) => {
338+ expect . assertions ( 4 ) ;
339+
340+ const onStart = jest . fn ( ) ;
341+ const onStop = jest . fn ( ) ;
342+ const onTimeUpdate = jest . fn ( ) ;
343+
344+ component = mount ( (
345+ < Timer
346+ active
347+ onStart = { onStart }
348+ onStop = { onStop }
349+ onTimeUpdate = { onTimeUpdate }
350+ />
351+ ) , {
352+ attachTo : document . getElementById ( 'root' ) ,
353+ } ) ;
354+
355+ expect ( onStart ) . toHaveBeenLastCalledWith ( {
356+ duration : 10000 ,
357+ progress : 0 ,
358+ time : 0 ,
359+ } ) ;
360+ clock . tick ( 2000 ) ;
361+ expect ( onTimeUpdate ) . toHaveBeenLastCalledWith ( {
362+ duration : 10000 ,
363+ progress : 0.2 ,
364+ time : 2000 ,
365+ } ) ;
366+ component . setProps ( { active : false } ) ;
367+ expect ( onStop ) . toHaveBeenLastCalledWith ( {
368+ duration : 10000 ,
369+ progress : 0.2 ,
370+ time : 2000 ,
371+ } ) ;
372+ component . setProps ( { active : true } ) ;
373+ expect ( onStart ) . toHaveBeenLastCalledWith ( {
374+ duration : 10000 ,
375+ progress : 0.2 ,
376+ time : 2000 ,
377+ } ) ;
263378
264379 component . unmount ( ) ;
265380 } ) ;
0 commit comments