Skip to content

Commit

Permalink
🐛Convert arg 'index' to number. (#26533)
Browse files Browse the repository at this point in the history
* Convert arg 'index' to number.

* switch to userAssert and add unit tests
  • Loading branch information
jyn15 committed Jan 31, 2020
1 parent dd33fa5 commit 307c885
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
11 changes: 9 additions & 2 deletions extensions/amp-carousel/0.2/amp-carousel.js
Expand Up @@ -24,7 +24,7 @@ import {Services} from '../../../src/services';
import {closestAncestorElementBySelector} from '../../../src/dom';
import {computedStyle} from '../../../src/style';
import {createCustomEvent, getDetail} from '../../../src/event-helper';
import {dev, devAssert} from '../../../src/log';
import {dev, devAssert, userAssert} from '../../../src/log';
import {dict} from '../../../src/utils/object';
import {htmlFor} from '../../../src/static-template';
import {isLayoutSizeDefined} from '../../../src/layout';
Expand All @@ -47,7 +47,14 @@ class AmpCarousel extends AMP.BaseElement {
'goToSlide',
actionInvocation => {
const {args, trust} = actionInvocation;
this.carousel_.goToSlide(args['index'] || 0, {
const slide = Number(args['index'] || 0);
userAssert(
!isNaN(slide),
'Unexpected slide index for goToSlide action: %s. %s',
args['index'],
this.element
);
this.carousel_.goToSlide(slide, {
actionSource: this.getActionSource_(trust),
});
},
Expand Down
43 changes: 43 additions & 0 deletions extensions/amp-carousel/0.2/test/test-type-slides.js
Expand Up @@ -264,6 +264,49 @@ describes.realWin(
ActionTrust.LOW
);
});

it('should allow string-valued index', async () => {
const carousel = await getCarousel({loop: false});
const impl = carousel.implementation_;
const triggerSpy = env.sandbox.spy(impl.action_, 'trigger');

impl.executeAction({
method: 'goToSlide',
args: {index: '1'},
trust: ActionTrust.LOW,
satisfiesTrust: () => true,
});
await afterIndexUpdate(carousel);

expect(triggerSpy).to.have.been.calledWith(
carousel,
'slideChange',
/* CustomEvent */ env.sandbox.match.has('detail', {index: 1}),
ActionTrust.LOW
);
});

it('should cause error with invalid index', async () => {
const carousel = await getCarousel({loop: false});
const impl = carousel.implementation_;
const triggerSpy = env.sandbox.spy(impl.action_, 'trigger');

try {
allowConsoleError(() => {
impl.executeAction({
method: 'goToSlide',
args: {index: 'one'},
trust: ActionTrust.LOW,
satisfiesTrust: () => true,
});
});
await afterIndexUpdate(carousel);
} catch (expected) {
expect(triggerSpy).to.not.have.been.called;
return;
}
expect.fail();
});
});

describe('layout direction', () => {
Expand Down

0 comments on commit 307c885

Please sign in to comment.