From daeeacba8f0b6986d11e807c2cf30588e8f01d57 Mon Sep 17 00:00:00 2001 From: Jake Fried Date: Mon, 27 Jan 2020 16:10:05 -0500 Subject: [PATCH] simplify fetch expectations and make more consistent with xhr --- examples/bind/basic.amp.html | 4 +-- extensions/amp-list/0.1/amp-list.js | 25 ++++++------------- extensions/amp-list/0.1/test/test-amp-list.js | 6 +++-- 3 files changed, 14 insertions(+), 21 deletions(-) diff --git a/examples/bind/basic.amp.html b/examples/bind/basic.amp.html index bf634e2d36d2..771214641fc7 100644 --- a/examples/bind/basic.amp.html +++ b/examples/bind/basic.amp.html @@ -44,9 +44,9 @@

"amp-state:" url example:

diff --git a/extensions/amp-list/0.1/amp-list.js b/extensions/amp-list/0.1/amp-list.js index ccffc15698d6..5f1ac20c4c56 100644 --- a/extensions/amp-list/0.1/amp-list.js +++ b/extensions/amp-list/0.1/amp-list.js @@ -264,11 +264,6 @@ export class AmpList extends AMP.BaseElement { this.initializeLoadMoreElements_(); } - const src = this.element.getAttribute('src'); - if (this.isAmpStateSrc_(src)) { - return this.renderAmpStateJson_(src); - } - return this.fetchList_(); } @@ -364,14 +359,14 @@ export class AmpList extends AMP.BaseElement { } /** - * Render an amp-list that has an "amp-state:" uri. For example, + * Gets the json an amp-list that has an "amp-state:" uri. For example, * src="amp-state:json.path". * * @param {string} src - * @return {Promise} + * @return {Promise} * @private */ - renderAmpStateJson_(src) { + getAmpStateJson_(src) { return Services.bindForDocOrNull(this.element) .then(bind => { userAssert(bind, '"amp-state:" URLs require amp-bind to be installed.'); @@ -388,14 +383,7 @@ export class AmpList extends AMP.BaseElement { typeof json !== 'undefined', `[amp-list] No data was found at provided uri: ${src}` ); - - const array = /** @type {!Array} */ (isArray(json) ? json : [json]); - return this.scheduleRender_(array, /* append */ false); - }) - .catch(error => { - this.triggerFetchErrorEvent_(error); - this.showFallback_(); - throw error; + return json; }); } @@ -615,7 +603,10 @@ export class AmpList extends AMP.BaseElement { if (this.ssrTemplateHelper_.isEnabled()) { fetch = this.ssrTemplate_(opt_refresh); } else { - fetch = this.prepareAndSendFetch_(opt_refresh).then(data => { + fetch = this.isAmpStateSrc_(elementSrc) + ? this.getAmpStateJson_(elementSrc) + : this.prepareAndSendFetch_(opt_refresh); + fetch = fetch.then(data => { // Bail if the src has changed while resolving the xhr request. if (elementSrc !== this.element.getAttribute('src')) { return; diff --git a/extensions/amp-list/0.1/test/test-amp-list.js b/extensions/amp-list/0.1/test/test-amp-list.js index 85fef7f56b8e..46cc71f6f4d9 100644 --- a/extensions/amp-list/0.1/test/test-amp-list.js +++ b/extensions/amp-list/0.1/test/test-amp-list.js @@ -1264,7 +1264,7 @@ describes.repeated( it('should render a list using local data', async () => { toggleExperiment(win, experimentName, true); - bind.getState = () => [1, 2, 3]; + bind.getState = () => ({items: [1, 2, 3]}); const ampStateEl = doc.createElement('amp-state'); ampStateEl.setAttribute('id', 'okapis'); @@ -1275,8 +1275,10 @@ describes.repeated( listMock .expects('scheduleRender_') - .withExactArgs([1, 2, 3], /*append*/ false) + .withExactArgs([1, 2, 3], /*append*/ false, {items: [1, 2, 3]}) + .returns(Promise.resolve()) .once(); + await list.layoutCallback(); }); });