Skip to content

Commit

Permalink
Instantiate the amp-story element only once per test to avoid side ef…
Browse files Browse the repository at this point in the history
…fects. (ampproject#18142)
  • Loading branch information
gmajoulet authored and Enriqe committed Nov 28, 2018
1 parent 93149ce commit 1f3f258
Showing 1 changed file with 3 additions and 56 deletions.
59 changes: 3 additions & 56 deletions extensions/amp-story/1.0/test/test-amp-story.js
Expand Up @@ -109,11 +109,9 @@ describes.realWin('amp-story', {

AmpStory.isBrowserSupported = () => true;

story = new AmpStory(element);
// TODO(alanorozco): Test active page event triggers once the stubbable
// `Services` module is part of the amphtml-story repo.
// sandbox.stub(element.implementation_,
// 'triggerActiveEventForPage_').callsFake(NOOP);
return element.getImpl().then(impl => {
story = impl;
});
});

afterEach(() => {
Expand Down Expand Up @@ -294,7 +292,6 @@ describes.realWin('amp-story', {

it('lock body when amp-story is initialized', () => {
createPages(story.element, 2, ['cover', 'page-1']);
story.buildCallback();

return story.layoutCallback()
.then(() => {
Expand All @@ -309,7 +306,6 @@ describes.realWin('amp-story', {

it('builds and attaches pagination buttons ', () => {
createPages(story.element, 2, ['cover', 'page-1']);
story.buildCallback();

return story.layoutCallback()
.then(() => {
Expand Down Expand Up @@ -373,7 +369,6 @@ describes.realWin('amp-story', {
const pageCount = 2;
createPages(story.element, pageCount, [firstPageId, 'page-1']);

story.buildCallback();
return story.layoutCallback()
.then(() => {
return expect(replaceStateStub).to.have.been.calledWith(
Expand Down Expand Up @@ -432,7 +427,6 @@ describes.realWin('amp-story', {
const firstPage = pages[0];
firstPage.setAttribute('ad', '');

story.buildCallback();
return story.layoutCallback()
.then(() => {
return expect(replaceStateStub).to.not.have.been.called;
Expand All @@ -443,7 +437,6 @@ describes.realWin('amp-story', {
createPages(story.element, 2, ['cover', 'page-1']);

story.storeService_.dispatch(Action.TOGGLE_PAUSED, true);
story.buildCallback();

return story.layoutCallback()
.then(() => {
Expand Down Expand Up @@ -473,8 +466,6 @@ describes.realWin('amp-story', {
const promise = new Promise(() => {});
sandbox.stub(consent, 'getConsentPolicyState').returns(promise);

story.buildCallback();

const coverEl = element.querySelector('amp-story-page');
let setStateStub;

Expand Down Expand Up @@ -517,8 +508,6 @@ describes.realWin('amp-story', {

sandbox.stub(consent, 'getConsentPolicyState').returns(promise);

story.buildCallback();

const coverEl = element.querySelector('amp-story-page');
let setStateStub;

Expand Down Expand Up @@ -560,8 +549,6 @@ describes.realWin('amp-story', {
// rejected the consent in a previous session.
sandbox.stub(consent, 'getConsentPolicyState').resolves();

story.buildCallback();

const coverEl = element.querySelector('amp-story-page');
let setStateStub;

Expand Down Expand Up @@ -632,8 +619,6 @@ describes.realWin('amp-story', {
it('should pause the story when viewer becomes inactive', () => {
createPages(story.element, 2, ['cover', 'page-1']);

story.buildCallback();

return story.layoutCallback()
.then(() => {
story.pauseCallback();
Expand All @@ -647,8 +632,6 @@ describes.realWin('amp-story', {

story.storeService_.dispatch(Action.TOGGLE_PAUSED, true);

story.buildCallback();

return story.layoutCallback()
.then(() => {
story.resumeCallback();
Expand All @@ -662,8 +645,6 @@ describes.realWin('amp-story', {

story.storeService_.dispatch(Action.TOGGLE_PAUSED, true);

story.buildCallback();

return story.layoutCallback()
.then(() => {
story.pauseCallback();
Expand All @@ -680,7 +661,6 @@ describes.realWin('amp-story', {
story = new AmpStory(element);
const dispatchStub = sandbox.stub(story.storeService_, 'dispatch');
createPages(story.element, 2, ['cover', 'page-4']);
story.buildCallback();
return story.layoutCallback()
.then(() => {
expect(dispatchStub).to.have.been.calledWith(
Expand All @@ -690,17 +670,13 @@ describes.realWin('amp-story', {
});

it('should display the story after clicking "continue" button', () => {

AmpStory.isBrowserSupported = () => false;
story = new AmpStory(element);
const dispatchStub = sandbox.stub(
story.unsupportedBrowserLayer_.storeService_, 'dispatch');
createPages(story.element, 2, ['cover', 'page-1']);

story.buildCallback();
//story.layoutCallback();

//story.unsupportedBrowserLayer_.continueButton_.click();

return story.layoutCallback()
.then(() => {
Expand All @@ -722,7 +698,6 @@ describes.realWin('amp-story', {

const pages = createPages(story.element, 2, ['page-0', 'page-1']);
const page1 = pages[1];
story.buildCallback();
return story.layoutCallback()
.then(() => {
expect(page1.hasAttribute('i-amphtml-next-page')).to.be.true;
Expand All @@ -736,7 +711,6 @@ describes.realWin('amp-story', {

const pages = createPages(story.element, 2, ['page-0', 'page-1']);
const page0 = pages[0];
story.buildCallback();
return story.layoutCallback()
.then(() => story.switchTo_('page-1'))
.then(() => {
Expand All @@ -751,7 +725,6 @@ describes.realWin('amp-story', {

const pages = createPages(story.element, 2, ['page-0', 'page-1']);
const page0 = pages[0];
story.buildCallback();
return story.layoutCallback()
.then(() => story.switchTo_('page-1'))
.then(() => {
Expand All @@ -769,8 +742,6 @@ describes.realWin('amp-story', {

createPages(story.element, 2, ['cover', 'page-1']);

story.buildCallback();

return story.layoutCallback()
.then(() => {
expect(story.backgroundAudioEl_).to.exist;
Expand All @@ -785,7 +756,6 @@ describes.realWin('amp-story', {
sandbox.stub(story.mediaPool_, 'blessAll').resolves();

createPages(story.element, 2, ['cover', 'page-1']);
story.buildCallback();

return story.layoutCallback()
.then(() => {
Expand All @@ -800,7 +770,6 @@ describes.realWin('amp-story', {
story.backgroundAudioEl_ = backgroundAudioEl;

createPages(story.element, 2, ['cover', 'page-1']);
story.buildCallback();
return story.layoutCallback()
.then(() => {
const pauseStub = sandbox.stub(story.mediaPool_, 'pause');
Expand All @@ -819,7 +788,6 @@ describes.realWin('amp-story', {
story.backgroundAudioEl_ = backgroundAudioEl;

createPages(story.element, 2, ['cover', 'page-1']);
story.buildCallback();

return story.layoutCallback()
.then(() => {
Expand All @@ -845,7 +813,6 @@ describes.realWin('amp-story', {
story.backgroundAudioEl_ = backgroundAudioEl;

createPages(story.element, 2, ['cover', 'page-1']);
story.buildCallback();

return story.layoutCallback()
.then(() => {
Expand All @@ -865,7 +832,6 @@ describes.realWin('amp-story', {
createPages(story.element, 4, ['cover', 'page-1', 'page-2', 'page-3']);

story.storeService_.dispatch(Action.TOGGLE_MUTED, false);
story.buildCallback();

let coverMuteStub;
let firstPageUnmuteStub;
Expand All @@ -888,7 +854,6 @@ describes.realWin('amp-story', {
describe('#getMaxMediaElementCounts', () => {
it('should create 2 audio & video elements when no elements found', () => {
createPages(story.element, 2, ['cover', 'page-1']);
story.buildCallback();

return story.layoutCallback()
.then(() => {
Expand All @@ -904,7 +869,6 @@ describes.realWin('amp-story', {

it('should create 2 extra audio & video elements for ads', () => {
createPages(story.element, 2, ['cover', 'page-1']);
story.buildCallback();

return story.layoutCallback()
.then(() => {
Expand All @@ -922,7 +886,6 @@ describes.realWin('amp-story', {

it('never have more than the defined maximums', () => {
createPages(story.element, 2, ['cover', 'page-1']);
story.buildCallback();

return story.layoutCallback()
.then(() => {
Expand All @@ -945,8 +908,6 @@ describes.realWin('amp-story', {
it('should set the access state to true if next page blocked', () => {
createPages(story.element, 4, ['cover', 'page-1', 'page-2', 'page-3']);

story.buildCallback();

return story.layoutCallback()
.then(() => {
story.getPageById('page-1')
Expand All @@ -962,8 +923,6 @@ describes.realWin('amp-story', {
it('should not navigate if next page is blocked by paywall', () => {
createPages(story.element, 4, ['cover', 'page-1', 'page-2', 'page-3']);

story.buildCallback();

return story.layoutCallback()
.then(() => {
story.getPageById('page-1')
Expand All @@ -986,8 +945,6 @@ describes.realWin('amp-story', {
sandbox.stub(Services, 'accessServiceForDocOrNull')
.resolves(fakeAccessService);

story.buildCallback();

// Navigates to a paywall protected page, and waits until the document
// is successfuly reauthorized to navigate.
return story.layoutCallback()
Expand Down Expand Up @@ -1016,8 +973,6 @@ describes.realWin('amp-story', {
sandbox.stub(Services, 'accessServiceForDocOrNull')
.resolves(fakeAccessService);

story.buildCallback();

// Navigates to a paywall protected page, and waits until the document
// is successfuly reauthorized to hide the access UI.
return story.layoutCallback()
Expand Down Expand Up @@ -1048,8 +1003,6 @@ describes.realWin('amp-story', {
sandbox.stub(Services, 'accessServiceForDocOrNull')
.resolves(fakeAccessService);

story.buildCallback();

// Navigates to a paywall protected page, and does not navigate to that
// page if the document has been reauthorized with insuficient rights.
return story.layoutCallback()
Expand All @@ -1076,8 +1029,6 @@ describes.realWin('amp-story', {
sandbox.stub(Services, 'accessServiceForDocOrNull')
.resolves(fakeAccessService);

story.buildCallback();

// Navigates to a paywall protected page, and does not hide the access UI
// if the document has been reauthorized with insuficient rights.
return story.layoutCallback()
Expand All @@ -1104,8 +1055,6 @@ describes.realWin('amp-story', {
sandbox.stub(Services, 'accessServiceForDocOrNull')
.resolves(fakeAccessService);

story.buildCallback();

// Navigates to a maybe protected page (has amp-access="" rule), but the
// document authorizations are still pending. Asserts that it blocks the
// navigation.
Expand All @@ -1131,8 +1080,6 @@ describes.realWin('amp-story', {
sandbox.stub(Services, 'accessServiceForDocOrNull')
.resolves(fakeAccessService);

story.buildCallback();

// Navigation to a maybe protected page (has amp-access="" rule) is
// blocked until the authorizations are completed.
return story.layoutCallback()
Expand Down

0 comments on commit 1f3f258

Please sign in to comment.