Skip to content

Tests: Add unit tests for callback-only animation signatures#5776

Open
tommyhgunz14 wants to merge 2 commits intojquery:mainfrom
tommyhgunz14:test/callback-only-animation
Open

Tests: Add unit tests for callback-only animation signatures#5776
tommyhgunz14 wants to merge 2 commits intojquery:mainfrom
tommyhgunz14:test/callback-only-animation

Conversation

@tommyhgunz14
Copy link

Summary

Adds unit tests for the callback-only signatures of animation methods: show(callback), hide(callback), toggle(callback), and their slide* and fade* counterparts.

Problem

Passing only a callback function (without a duration) to show(), hide(), toggle(), slideDown(), slideUp(), slideToggle(), fadeIn(), fadeOut(), and fadeToggle() should trigger an animated transition with the default duration (jQuery.fx.speeds._default, 400ms) and invoke the callback on completion.

This behavior has been supported historically but was never explicitly tested.

Ref trac-12821

Changes

Added 3 new test cases to test/unit/effects.js with 12 total assertions:

  1. show/hide/toggle(callback) — verifies callback invocation and correct visibility state after animation completes
  2. slideDown/slideUp/slideToggle(callback) — verifies callback invocation for slide animations
  3. fadeIn/fadeOut/fadeToggle(callback) — verifies callback invocation for fade animations

All tests use the existing sinon.useFakeTimers pattern and clean up DOM elements after completion.

Closes gh-1738
Ref trac-12821

@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Feb 18, 2026

CLA Signed

The committers listed above are authorized under a signed CLA.

@mgol
Copy link
Member

mgol commented Feb 18, 2026

Thanks for the PR. Before we review it, please sign our CLA.

Copy link
Member

@mgol mgol left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! Please read my comments.

Comment on lines +861 to +874
div.fadeIn( function() {
assert.ok( true, "fadeIn(callback) invokes the callback" );
} );
this.clock.tick( 600 );

div.fadeOut( function() {
assert.ok( true, "fadeOut(callback) invokes the callback" );
} );
this.clock.tick( 600 );

div.fadeToggle( function() {
assert.ok( true, "fadeToggle(callback) invokes the callback" );
} );
this.clock.tick( 600 );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also have assertions that the callback is not called too early. You can do a tick by 500 first, check that it wasn't called and then by 100 and check that it was called.

Or, even better, don't hardcode the number & use jQuery.fx.speeds._default * 0.8, then jQuery.fx.speeds._default * 0.2.

For this to work, you need to change how you attach callbacks. Instead of a normal callback, attach a sinon spy (we're using a sinon sandbox here, see https://sinonjs.org/releases/latest/sandbox/ & the beforeEach hook in this file) and check if it was called or no via Sinon APIs.

Update all the tests this way.


var div = jQuery( "<div>" ).appendTo( "#qunit-fixture" );

// hide(callback) should animate with default duration
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These comments don't add a lot of value, please remove them.

@mgol
Copy link
Member

mgol commented Feb 23, 2026

Oh, and in the commit message & PR description reference gh-1738 instead of trac-12821. We should use the migrated issues whenever available.

@tommyhgunz14 tommyhgunz14 force-pushed the test/callback-only-animation branch from 35b0cad to b0e9578 Compare February 23, 2026 23:09
fadeToggleCb = this.sandbox.spy();

div.fadeIn( fadeInCb );
this.clock.tick( jQuery.fx.speeds._default * 0.8 );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes are needed in all tests, not just here.

Add tests for show/hide/toggle(callback), slideDown/slideUp/slideToggle(callback),
and fadeIn/fadeOut/fadeToggle(callback) to verify that passing only a callback
function as the first argument triggers an animated transition with the default
duration.

These signatures have been supported historically but were never explicitly tested.
The callback-only form is documented and should animate with jQuery.fx.speeds._default
(400ms) duration rather than toggling visibility immediately.

Ref jquerygh-1738
Closes jquerygh-1738
@tommyhgunz14 tommyhgunz14 force-pushed the test/callback-only-animation branch from b0e9578 to a29f145 Compare February 23, 2026 23:15
Copy link
Member

@mgol mgol left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! We could consider abstracting these three tests since they are almost identical, but 3 tests seem still manageable and debugging is a bit easier, so I'm fine with the way it is.

@mgol mgol added Tests Discuss in Meeting Reserved for Issues and PRs that anyone would like to discuss in the weekly meeting. labels Mar 4, 2026
Copy link
Member

@mgol mgol left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, actually, the tests are failing. Please run them locally and request re-review when they pass.

@mgol mgol removed the Discuss in Meeting Reserved for Issues and PRs that anyone would like to discuss in the weekly meeting. label Mar 4, 2026
The expected assertion counts were incorrect:
- show/hide/toggle test: 12 → 9 (3 groups × 3 assertions)
- slideDown/slideUp/slideToggle test: 9 → 6 (3 groups × 2 assertions)
- fadeIn/fadeOut/fadeToggle test: 9 → 6 (3 groups × 2 assertions)

Ref jquerygh-1738
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Development

Successfully merging this pull request may close these issues.

Create Unit test for toggle/show/hide( callback )

2 participants