Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
Micajuine Ho committed Feb 19, 2021
1 parent 2205cef commit 9e77182
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
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)) {
dev().error('Attempted to show slide that is not a number');
return;
}
if (
newIndex < 0 ||
newIndex >= noOfSlides_ ||
Expand Down
21 changes: 20 additions & 1 deletion extensions/amp-carousel/0.1/test/test-slidescroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
whenUpgradedToCustomElement,
} from '../../../../src/dom';
import {installResizeObserverStub} from '../../../../testing/resize-observer-stub';
import {user} from '../../../../src/log';
import {dev, user} from '../../../../src/log';

describes.realWin(
'SlideScroll',
Expand Down Expand Up @@ -1271,6 +1271,25 @@ 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][0]).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

0 comments on commit 9e77182

Please sign in to comment.