Skip to content

Commit

Permalink
Add unlayout function and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cathyxz committed Jun 29, 2018
1 parent 8380ae2 commit f0b81d1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
22 changes: 20 additions & 2 deletions builtins/amp-img.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ export class AmpImg extends BaseElement {
/** @private {?Element} */
this.img_ = null;

/** @private {?UnlistenDef} */
this.unlistenLoad_ = null;

/** @private {?UnlistenDef} */
this.unlistenError_ = null;
}

/** @override */
Expand Down Expand Up @@ -140,14 +145,27 @@ export class AmpImg extends BaseElement {
layoutCallback() {
this.initialize_();
const img = dev().assertElement(this.img_);
listen(img, 'load', () => this.hideFallbackImg_());
listen(img, 'error', () => this.onImgLoadingError_());
this.unlistenLoad_ = listen(img, 'load', () => this.hideFallbackImg_());
this.unlistenError_ = listen(img, 'error', () => this.onImgLoadingError_());
if (this.getLayoutWidth() <= 0) {
return Promise.resolve();
}
return this.loadPromise(img);
}

/** @override */
unlayoutCallback() {
if (this.unlistenError_) {
this.unlistenError_();
this.unlistenError_ = null;
}
if (this.unlistenLoad_) {
this.unlistenLoad_();
this.unlistenLoad_ = null;
}
return true;
}

/**
* Sets the img src to the first url in the srcset if srcset is defined but
* src is not.
Expand Down
6 changes: 6 additions & 0 deletions test/functional/test-amp-img.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,10 @@ describe('amp-img', () => {
};
});

afterEach(() => {
impl.unlayoutCallback();
});

it('should not display fallback if loading succeeds', () => {
impl.buildCallback();
expect(errorSpy).to.have.not.been.called;
Expand Down Expand Up @@ -367,5 +371,7 @@ describe('amp-img', () => {
expect(img.getAttribute('aria-label')).to.equal('Hello');
expect(img.getAttribute('aria-labelledby')).to.equal('id2');
expect(img.getAttribute('aria-describedby')).to.equal('id3');
impl.unlayoutCallback();
});

});

0 comments on commit f0b81d1

Please sign in to comment.