Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 [amp-carousel 0.1] Fix snapping and closing race #32695

Merged
merged 6 commits into from
Feb 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions extensions/amp-carousel/0.1/slidescroll.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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