Skip to content

Commit dcea229

Browse files
authored
Refactoring stories tests to use async/await. (ampproject#24721)
* Refactoring stories tests to use async/await. * Lint.
1 parent f67d800 commit dcea229

11 files changed

+1046
-1189
lines changed

extensions/amp-story/1.0/test/test-amp-story-bookend.js

Lines changed: 38 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ describes.realWin('amp-story-bookend', {amp: true}, env => {
330330
}
331331
);
332332

333-
it('should forward the correct target when clicking on an element', () => {
333+
it('should forward the correct target when clicking on an element', async () => {
334334
const userJson = {
335335
'bookendVersion': 'v1.0',
336336
'shareProviders': [
@@ -357,22 +357,21 @@ describes.realWin('amp-story-bookend', {amp: true}, env => {
357357
win.document.addEventListener('click', clickSpy);
358358

359359
bookend.build();
360-
return bookend.loadConfigAndMaybeRenderBookend().then(() => {
361-
const ctaLinks = bookend.bookendEl_.querySelector(
362-
'.i-amphtml-story-bookend-cta-link-wrapper'
363-
);
364-
ctaLinks.children[0].onclick = function(e) {
365-
e.preventDefault(); // Make the test not actually navigate.
366-
};
367-
ctaLinks.children[0].click();
360+
await bookend.loadConfigAndMaybeRenderBookend();
361+
const ctaLinks = bookend.bookendEl_.querySelector(
362+
'.i-amphtml-story-bookend-cta-link-wrapper'
363+
);
364+
ctaLinks.children[0].onclick = function(e) {
365+
e.preventDefault(); // Make the test not actually navigate.
366+
};
367+
ctaLinks.children[0].click();
368368

369-
expect(clickSpy.getCall(0).args[0]).to.contain({
370-
'__AMP_CUSTOM_LINKER_TARGET__': ctaLinks.children[0],
371-
});
369+
expect(clickSpy.getCall(0).args[0]).to.contain({
370+
'__AMP_CUSTOM_LINKER_TARGET__': ctaLinks.children[0],
372371
});
373372
});
374373

375-
it('should fire analytics event when clicking on a link', () => {
374+
it('should fire analytics event when clicking on a link', async () => {
376375
const userJson = {
377376
'bookendVersion': 'v1.0',
378377
'shareProviders': [
@@ -398,31 +397,28 @@ describes.realWin('amp-story-bookend', {amp: true}, env => {
398397
const analyticsSpy = sandbox.spy(analytics, 'triggerEvent');
399398

400399
bookend.build();
401-
return bookend.loadConfigAndMaybeRenderBookend().then(() => {
402-
const ctaLinks = bookend.bookendEl_.querySelector(
403-
'.i-amphtml-story-bookend-cta-link-wrapper'
404-
);
405-
ctaLinks.children[0].onclick = function(e) {
406-
e.preventDefault(); // Make the test not actually navigate.
407-
};
408-
ctaLinks.children[0].click();
409-
410-
expect(analyticsSpy).to.have.been.calledWith(
411-
AnalyticsEvent.BOOKEND_CLICK
412-
);
413-
expect(
414-
analyticsVariables.get()[AnalyticsVariable.BOOKEND_TARGET_HREF]
415-
).to.equal('http://localhost:9876/google.com');
416-
expect(
417-
analyticsVariables.get()[AnalyticsVariable.BOOKEND_COMPONENT_TYPE]
418-
).to.equal('cta-link');
419-
expect(
420-
analyticsVariables.get()[AnalyticsVariable.BOOKEND_COMPONENT_POSITION]
421-
).to.equal(1);
422-
});
400+
await bookend.loadConfigAndMaybeRenderBookend();
401+
const ctaLinks = bookend.bookendEl_.querySelector(
402+
'.i-amphtml-story-bookend-cta-link-wrapper'
403+
);
404+
ctaLinks.children[0].onclick = function(e) {
405+
e.preventDefault(); // Make the test not actually navigate.
406+
};
407+
ctaLinks.children[0].click();
408+
409+
expect(analyticsSpy).to.have.been.calledWith(AnalyticsEvent.BOOKEND_CLICK);
410+
expect(
411+
analyticsVariables.get()[AnalyticsVariable.BOOKEND_TARGET_HREF]
412+
).to.equal('http://localhost:9876/google.com');
413+
expect(
414+
analyticsVariables.get()[AnalyticsVariable.BOOKEND_COMPONENT_TYPE]
415+
).to.equal('cta-link');
416+
expect(
417+
analyticsVariables.get()[AnalyticsVariable.BOOKEND_COMPONENT_POSITION]
418+
).to.equal(1);
423419
});
424420

425-
it('should not fire analytics event when clicking non-clickable components', () => {
421+
it('should not fire analytics event when clicking non-clickable components', async () => {
426422
const userJson = {
427423
'bookendVersion': 'v1.0',
428424
'shareProviders': [
@@ -447,15 +443,14 @@ describes.realWin('amp-story-bookend', {amp: true}, env => {
447443
const analyticsSpy = sandbox.spy(analytics, 'triggerEvent');
448444

449445
bookend.build();
450-
return bookend.loadConfigAndMaybeRenderBookend().then(() => {
451-
const textEl = bookend.bookendEl_.querySelector(
452-
'.i-amphtml-story-bookend-text'
453-
);
446+
await bookend.loadConfigAndMaybeRenderBookend();
447+
const textEl = bookend.bookendEl_.querySelector(
448+
'.i-amphtml-story-bookend-text'
449+
);
454450

455-
textEl.click();
451+
textEl.click();
456452

457-
expect(analyticsSpy).to.not.have.been.called;
458-
});
453+
expect(analyticsSpy).to.not.have.been.called;
459454
});
460455

461456
it(

extensions/amp-story/1.0/test/test-amp-story-consent.js

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ describes.realWin('amp-story-consent', {amp: true}, env => {
296296
);
297297
});
298298

299-
it('should set the consent ID in the store if right amp-geo group', () => {
299+
it('should set the consent ID in the store if right amp-geo group', async () => {
300300
const config = {consents: {ABC: {promptIfUnknownForGeoGroup: 'eea'}}};
301301
consentConfigEl.textContent = JSON.stringify(config);
302302

@@ -306,14 +306,13 @@ describes.realWin('amp-story-consent', {amp: true}, env => {
306306

307307
storyConsent.buildCallback();
308308

309-
return Promise.resolve().then(() => {
310-
expect(storyConsent.storeService_.get(StateProperty.CONSENT_ID)).to.equal(
311-
CONSENT_ID
312-
);
313-
});
309+
await Promise.resolve();
310+
expect(storyConsent.storeService_.get(StateProperty.CONSENT_ID)).to.equal(
311+
CONSENT_ID
312+
);
314313
});
315314

316-
it('should not set consent ID in the store if wrong amp-geo group', () => {
315+
it('should not set consent ID in the store if wrong amp-geo group', async () => {
317316
const config = {consents: {ABC: {promptIfUnknownForGeoGroup: 'eea'}}};
318317
consentConfigEl.textContent = JSON.stringify(config);
319318

@@ -323,10 +322,8 @@ describes.realWin('amp-story-consent', {amp: true}, env => {
323322

324323
storyConsent.buildCallback();
325324

326-
return Promise.resolve().then(() => {
327-
expect(storyConsent.storeService_.get(StateProperty.CONSENT_ID)).to.be
328-
.null;
329-
});
325+
await Promise.resolve();
326+
expect(storyConsent.storeService_.get(StateProperty.CONSENT_ID)).to.be.null;
330327
});
331328

332329
it('should set the font color to black if background is white', () => {

extensions/amp-story/1.0/test/test-amp-story-cta-layer.js

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -41,34 +41,31 @@ describes.realWin(
4141
ampStoryCtaLayer = new AmpStoryCtaLayer(ampStoryCtaLayerEl);
4242
});
4343

44-
it('should build the cta layer', () => {
44+
it('should build the cta layer', async () => {
4545
ampStoryCtaLayer.buildCallback();
46-
return ampStoryCtaLayer.layoutCallback().then(() => {
47-
expect(ampStoryCtaLayer.element).to.have.class('i-amphtml-story-layer');
48-
});
46+
await ampStoryCtaLayer.layoutCallback();
47+
expect(ampStoryCtaLayer.element).to.have.class('i-amphtml-story-layer');
4948
});
5049

51-
it('should add or overwrite target attribute to links', () => {
50+
it('should add or overwrite target attribute to links', async () => {
5251
const ctaLink = win.document.createElement('a');
5352
expect(ctaLink).to.not.have.attribute('target');
5453

5554
ampStoryCtaLayer.element.appendChild(ctaLink);
5655
ampStoryCtaLayer.buildCallback();
5756

58-
return ampStoryCtaLayer.layoutCallback().then(() => {
59-
expect(ctaLink).to.have.attribute('target');
60-
expect(ctaLink.getAttribute('target')).to.equal('_blank');
61-
});
57+
await ampStoryCtaLayer.layoutCallback();
58+
expect(ctaLink).to.have.attribute('target');
59+
expect(ctaLink.getAttribute('target')).to.equal('_blank');
6260
});
6361

64-
it('should not add target attribute to other elements', () => {
62+
it('should not add target attribute to other elements', async () => {
6563
const elem = win.document.createElement('span');
6664
ampStoryCtaLayer.element.appendChild(elem);
6765
ampStoryCtaLayer.buildCallback();
6866

69-
return ampStoryCtaLayer.layoutCallback().then(() => {
70-
expect(elem).to.not.have.attribute('target');
71-
});
67+
await ampStoryCtaLayer.layoutCallback();
68+
expect(elem).to.not.have.attribute('target');
7269
});
7370

7471
it('should not allow a cta layer on the first page', () => {
@@ -111,40 +108,37 @@ describes.realWin(
111108
});
112109
});
113110

114-
it('should add or overwrite role attribute to links', () => {
111+
it('should add or overwrite role attribute to links', async () => {
115112
const ctaLink = win.document.createElement('a');
116113
expect(ctaLink).to.not.have.attribute('role');
117114

118115
ampStoryCtaLayer.element.appendChild(ctaLink);
119116
ampStoryCtaLayer.buildCallback();
120117

121-
return ampStoryCtaLayer.layoutCallback().then(() => {
122-
expect(ctaLink).to.have.attribute('role');
123-
expect(ctaLink.getAttribute('role')).to.equal('link');
124-
});
118+
await ampStoryCtaLayer.layoutCallback();
119+
expect(ctaLink).to.have.attribute('role');
120+
expect(ctaLink.getAttribute('role')).to.equal('link');
125121
});
126122

127-
it('should add or overwrite role attribute to buttons', () => {
123+
it('should add or overwrite role attribute to buttons', async () => {
128124
const ctaButton = win.document.createElement('button');
129125
expect(ctaButton).to.not.have.attribute('role');
130126

131127
ampStoryCtaLayer.element.appendChild(ctaButton);
132128
ampStoryCtaLayer.buildCallback();
133129

134-
return ampStoryCtaLayer.layoutCallback().then(() => {
135-
expect(ctaButton).to.have.attribute('role');
136-
expect(ctaButton.getAttribute('role')).to.equal('button');
137-
});
130+
await ampStoryCtaLayer.layoutCallback();
131+
expect(ctaButton).to.have.attribute('role');
132+
expect(ctaButton.getAttribute('role')).to.equal('button');
138133
});
139134

140-
it('should not add role attribute to other elements', () => {
135+
it('should not add role attribute to other elements', async () => {
141136
const elem = win.document.createElement('span');
142137
ampStoryCtaLayer.element.appendChild(elem);
143138
ampStoryCtaLayer.buildCallback();
144139

145-
return ampStoryCtaLayer.layoutCallback().then(() => {
146-
expect(elem).to.not.have.attribute('role');
147-
});
140+
await ampStoryCtaLayer.layoutCallback();
141+
expect(elem).to.not.have.attribute('role');
148142
});
149143
}
150144
);

extensions/amp-story/1.0/test/test-amp-story-embedded-component.js

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -96,17 +96,16 @@ describes.realWin('amp-story-embedded-component', {amp: true}, env => {
9696
}
9797
);
9898

99-
it('should show the tooltip on store property update', () => {
99+
it('should show the tooltip on store property update', async () => {
100100
fakePage.appendChild(clickableEl);
101101

102102
storeService.dispatch(Action.TOGGLE_INTERACTIVE_COMPONENT, fakeComponent);
103103

104104
// Wait for TOOLTIP_CLOSE_ANIMATION_MS is finished before showing tooltip.
105-
return timeout(150).then(() => {
106-
expect(component.focusedStateOverlay_).to.not.have.class(
107-
'i-amphtml-hidden'
108-
);
109-
});
105+
await timeout(150);
106+
expect(component.focusedStateOverlay_).to.not.have.class(
107+
'i-amphtml-hidden'
108+
);
110109
});
111110

112111
it('should hide the tooltip when switching page', () => {
@@ -166,7 +165,7 @@ describes.realWin('amp-story-embedded-component', {amp: true}, env => {
166165
}
167166
);
168167

169-
it('should append icon when icon attribute is present', () => {
168+
it('should append icon when icon attribute is present', async () => {
170169
addAttributesToElement(clickableEl, {'data-tooltip-icon': '/my-icon'});
171170
fakePage.appendChild(clickableEl);
172171
storeService.dispatch(Action.TOGGLE_INTERACTIVE_COMPONENT, fakeComponent);
@@ -176,11 +175,10 @@ describes.realWin('amp-story-embedded-component', {amp: true}, env => {
176175
);
177176

178177
// Wait for TOOLTIP_CLOSE_ANIMATION_MS is finished before building tooltip.
179-
return timeout(150).then(() => {
180-
expect(tooltipIconEl.style['background-image']).to.equal(
181-
'url("http://localhost:9876/my-icon")'
182-
);
183-
});
178+
await timeout(150);
179+
expect(tooltipIconEl.style['background-image']).to.equal(
180+
'url("http://localhost:9876/my-icon")'
181+
);
184182
});
185183

186184
it('should find invalid urls', () => {
@@ -201,7 +199,7 @@ describes.realWin('amp-story-embedded-component', {amp: true}, env => {
201199
expect(tooltipIconEl.style['background-image']).to.equal('');
202200
});
203201

204-
it('should append text when text attribute is present', () => {
202+
it('should append text when text attribute is present', async () => {
205203
addAttributesToElement(clickableEl, {'data-tooltip-text': 'my cool text'});
206204
fakePage.appendChild(clickableEl);
207205
storeService.dispatch(Action.TOGGLE_INTERACTIVE_COMPONENT, fakeComponent);
@@ -211,12 +209,11 @@ describes.realWin('amp-story-embedded-component', {amp: true}, env => {
211209
);
212210

213211
// Wait for TOOLTIP_CLOSE_ANIMATION_MS is finished before building tooltip.
214-
return timeout(150).then(() => {
215-
expect(tooltipTextEl.textContent).to.equal('my cool text');
216-
});
212+
await timeout(150);
213+
expect(tooltipTextEl.textContent).to.equal('my cool text');
217214
});
218215

219-
it('should append href url when text attribute is not present', () => {
216+
it('should append href url when text attribute is not present', async () => {
220217
fakePage.appendChild(clickableEl);
221218
storeService.dispatch(Action.TOGGLE_INTERACTIVE_COMPONENT, fakeComponent);
222219

@@ -225,9 +222,8 @@ describes.realWin('amp-story-embedded-component', {amp: true}, env => {
225222
);
226223

227224
// Wait for TOOLTIP_CLOSE_ANIMATION_MS is finished before building tooltip.
228-
return timeout(150).then(() => {
229-
expect(tooltipTextEl.textContent).to.equal('google.com');
230-
});
225+
await timeout(150);
226+
expect(tooltipTextEl.textContent).to.equal('google.com');
231227
});
232228
});
233229

0 commit comments

Comments
 (0)