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-base-carousel: Update offset and index together #30963

Merged
merged 2 commits into from
Nov 3, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion extensions/amp-base-carousel/0.1/carousel.js
Original file line number Diff line number Diff line change
Expand Up @@ -1180,10 +1180,11 @@ export class Carousel {
}

// We are updating during a programmatic scroll, so go to the correct
// index.
// index (and update offset accordingly).
if (this.requestedIndex_ !== null) {
this.currentIndex_ = this.requestedIndex_;
this.requestedIndex_ = null;
this.currentElementOffset_ = 0;
}

const totalLength = sum(this.getSlideLengths_());
Expand Down
66 changes: 66 additions & 0 deletions extensions/amp-base-carousel/0.1/test-e2e/test-advance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* Copyright 2020 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import {getNextArrow, getPrevArrow, getSlides} from './helpers';
import sleep from 'sleep-promise';

const pageWidth = 500;
const pageHeight = 800;

describes.endtoend(
'AMP carousel advance',
{
testUrl:
'http://localhost:8000/test/manual/amp-base-carousel/advance.amp.html',
experiments: ['amp-base-carousel'],
environments: ['single'],
initialRect: {width: pageWidth, height: pageHeight},
},
async (env) => {
let controller;
let nextArrow;
let prevArrow;

function css(handle, name) {
return controller.getElementCssValue(handle, name);
}

function rect(el) {
return controller.getElementRect(el);
}

beforeEach(async () => {
controller = env.controller;

nextArrow = await getNextArrow(controller);
});

it('should move forwards once', async () => {
await controller.click(nextArrow);
await sleep(500);
prevArrow = await getPrevArrow(controller);
await expect(css(prevArrow, 'opacity')).to.equal('1');
await expect(css(nextArrow, 'opacity')).to.equal('1');

const slides = await getSlides(controller);
const slideOne = await rect(slides[0]);
const slideTwo = await rect(slides[1]);

await expect(slideOne['x']).to.be.lessThan(0);
await expect(slideTwo['x']).to.be.at.least(0);
});
}
);
27 changes: 27 additions & 0 deletions extensions/amp-base-carousel/0.1/test/test-carousel.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,4 +236,31 @@ describes.realWin('carousel implementation', {}, (env) => {
expect(carousel.isAtStart()).to.be.false;
});
});

describe('resetScrollReferencePoint_', () => {
it(
'currentElementOffset_ & currentIndex_ should be set when it is a' +
' programmatic scroll',
async () => {
const carousel = await createCarousel({
slideCount: 12,
loop: false,
});

// Fake the scroll that ends short of the correct index.
// This is handled by scroll event listener.
carousel.touching_ = false;
carousel.requestedIndex_ = 1;
carousel.currentIndex_ = 0;
carousel.restingIndex_ = 0;
carousel.currentElementOffset_ = -0.99382;

carousel.resetScrollReferencePoint_();

expect(carousel.currentElementOffset_).to.equal(0);
expect(carousel.currentIndex_).to.equal(1);
expect(carousel.requestedIndex_).to.be.null;
}
);
});
});
42 changes: 42 additions & 0 deletions test/manual/amp-base-carousel/advance.amp.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<!doctype html>
<html ⚡>

<head>
<meta charset="utf-8">
<title>My AMP Page</title>
<link rel="canonical" href="self.html" />
<script async custom-element="amp-base-carousel" src="https://cdn.ampproject.org/v0/amp-base-carousel-0.1.js"></script>
<meta name="viewport" content="width=device-width,minimum-scale=1">
<script async src="https://cdn.ampproject.org/v0.js"></script>
<style amp-custom>
amp-base-carousel .item {
padding: 40px;
border: 1px solid black;
}
#wrapper {
width: 400px;
}
</style>
</head>

<body>
<div id="wrapper">
<amp-base-carousel class="gallery" layout="responsive" width="33" height="10" visible-count="3.3" advance-count="1" loop="false">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
<div class="item">5</div>
<div class="item">6</div>
<div class="item">7</div>
<div class="item">8</div>
<div class="item">9</div>
<div class="item">10</div>
<div class="item">11</div>
<div class="item">12</div>
<div class="item">13</div>
</amp-base-carousel>
</div>
</body>

</html>