Skip to content

Commit

Permalink
🐛 [amp-carousel 0.1] Fix snapping and closing race (#32695)
Browse files Browse the repository at this point in the history
* Init

* init

* Fix race
  • Loading branch information
Micajuine Ho committed Feb 23, 2021
1 parent 5cb3446 commit 8fa011f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
5 changes: 5 additions & 0 deletions extensions/amp-carousel/0.1/slidescroll.js
Expand Up @@ -521,6 +521,11 @@ export class AmpSlideScroll extends BaseSlides {
* @return {number} a number representing the next slide index.
*/
getNextSlideIndex_(currentScrollLeft) {
// Addresses race where slideWidth is 0, due to being hidden
// while snapping is occuring.
if (!currentScrollLeft && !this.slideWidth_) {
return 0;
}
// This can be only 0, 1 or 2, since only a max of 3 slides are shown at
// a time.
const scrolledSlideIndex = Math.round(currentScrollLeft / this.slideWidth_);
Expand Down
11 changes: 11 additions & 0 deletions extensions/amp-carousel/0.1/test/test-slidescroll.js
Expand Up @@ -1271,6 +1271,17 @@ describes.realWin(
expect(showSlideSpy).to.have.been.calledWith(4);
});

it('should handle carousel snapping & hiding race', async () => {
const ampSlideScroll = await getAmpSlideScroll(true);
const impl = await ampSlideScroll.getImpl();

// simluate carousel hidding
impl.slideWidth_ = 0;

// simulate snapping
expect(impl.getNextSlideIndex_(0)).to.equal(0);
});

it('should NOT call showSlide_ before layout', async () => {
const ampSlideScroll = await getAmpSlideScroll(
true,
Expand Down

0 comments on commit 8fa011f

Please sign in to comment.