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 4 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: 4 additions & 1 deletion extensions/amp-carousel/0.1/slidescroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,10 @@ export class AmpSlideScroll extends BaseSlides {
*/
showSlide_(newIndex) {
const {noOfSlides_} = this;
newIndex = dev().assertNumber(newIndex);
if (isNaN(newIndex)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

From the stack trace, this looks like we're fixing the issue in the wrong place. We're getting a NaN from getNextSlideIndex_ (called from updateOnScroll_).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the catch. I'll add the check there as well.

dev().error(TAG, 'Attempted to show slide that is not a number');
return;
}
if (
newIndex < 0 ||
newIndex >= noOfSlides_ ||
micajuine-ho marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
23 changes: 22 additions & 1 deletion extensions/amp-carousel/0.1/test/test-slidescroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import {
createElementWithAttributes,
whenUpgradedToCustomElement,
} from '../../../../src/dom';
import {dev, user} from '../../../../src/log';
import {installResizeObserverStub} from '../../../../testing/resize-observer-stub';
import {user} from '../../../../src/log';

describes.realWin(
'SlideScroll',
Expand Down Expand Up @@ -1271,6 +1271,27 @@ describes.realWin(
expect(showSlideSpy).to.have.been.calledWith(4);
});

it('should error on a non-number goToSlide', async () => {
const ampSlideScroll = await getAmpSlideScroll(true);
const impl = await ampSlideScroll.getImpl();
const devErrorSpy = env.sandbox.spy(dev(), 'error');

impl.showSlideAndTriggerAction_(NaN);
expect(devErrorSpy).to.be.calledOnce;
expect(devErrorSpy.args[0][1]).to.match(
/Attempted to show slide that is not a number/
);
});

it('should return false for invalid slide number', async () => {
const ampSlideScroll = await getAmpSlideScroll(true);
const impl = await ampSlideScroll.getImpl();

impl.noOfSlides_ = 10;
expect(impl.showSlide_(Infinity)).to.be.false;
expect(impl.showSlide_(-Infinity)).to.be.false;
});

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