@@ -213,35 +213,89 @@ async def test_cancel_widget_non_animation() -> None:
213213 assert not pilot .app .animator .is_being_animated (widget , "counter" )
214214
215215
216- async def test_double_animation_on_complete () -> None :
217- """Test that animating an attribute a second time, fires its `on_complete` callback."""
216+ async def test_double_animation_same_value_on_complete () -> None :
217+ """Test that animating an attribute a second time to the same value , fires its `on_complete` callback."""
218218
219- complete_count = 0
219+ completed : list [ str ] = []
220220
221221 class AnimApp (App ):
222222 x = var (0 )
223223
224- def on_key (self ) -> None :
224+ async def animate_a (self ) -> None :
225225
226226 def on_complete () -> None :
227- nonlocal complete_count
228- complete_count += 1
227+ completed .append ("a" )
229228
230229 self .animator .animate (
231230 self ,
232231 "x" ,
233- 100 + complete_count ,
232+ 100 ,
233+ duration = 0.1 ,
234+ on_complete = on_complete ,
235+ )
236+
237+ async def animate_b (self ) -> None :
238+
239+ def on_complete () -> None :
240+ completed .append ("b" )
241+
242+ self .animator .animate (
243+ self ,
244+ "x" ,
245+ 100 ,
246+ duration = 0.1 ,
247+ on_complete = on_complete ,
248+ )
249+
250+ app = AnimApp ()
251+ async with app .run_test () as pilot :
252+ await app .animate_a ()
253+ assert app .x != 100
254+ await app .animate_b ()
255+ await pilot .wait_for_animation ()
256+ assert completed == ["a" , "b" ]
257+ assert app .x == 100
258+
259+
260+ async def test_double_animation_different_value_on_complete () -> None :
261+ """Test that animating an attribute a second time to a different value, fires its `on_complete` callback."""
262+
263+ completed : list [str ] = []
264+
265+ class AnimApp (App ):
266+ x = var (0 )
267+
268+ async def animate_a (self ) -> None :
269+
270+ def on_complete () -> None :
271+ completed .append ("a" )
272+
273+ self .animator .animate (
274+ self ,
275+ "x" ,
276+ 100 ,
277+ duration = 0.1 ,
278+ on_complete = on_complete ,
279+ )
280+
281+ async def animate_b (self ) -> None :
282+
283+ def on_complete () -> None :
284+ completed .append ("b" )
285+
286+ self .animator .animate (
287+ self ,
288+ "x" ,
289+ 101 ,
234290 duration = 0.1 ,
235291 on_complete = on_complete ,
236292 )
237293
238294 app = AnimApp ()
239295 async with app .run_test () as pilot :
240- # Press space twice to initiate 2 animations
241- await pilot .press ("space" )
242- await pilot .press ("space" )
243- # Wait for animations to complete
296+ await app .animate_a ()
297+ assert app .x != 101
298+ await app .animate_b () # animate to different value
244299 await pilot .wait_for_animation ()
245- # Check that on_complete callback was invoked twice
246- assert complete_count == 2
300+ assert completed == ["a" , "b" ]
247301 assert app .x == 101
0 commit comments