Skip to content

Commit

Permalink
Explicitly specify the version of automatic legacy extensions (#33048)
Browse files Browse the repository at this point in the history
* Explicitly specify the version of automatic legacy extensions

* fix tests
  • Loading branch information
Dima Voytenko committed Mar 4, 2021
1 parent 402e06d commit bb49466
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/custom-element.js
Expand Up @@ -964,11 +964,13 @@ function createBaseCustomElementClass(win) {
const ampdoc = ampdocService.getAmpDoc(this);
this.ampdoc_ = ampdoc;
// Load the pre-stubbed extension if needed.
const extensionId = this.tagName.toLowerCase();
const extensionId = this.localName;
if (!this.implClass_ && !ampdoc.declaresExtension(extensionId)) {
Services.extensionsFor(win).installExtensionForDoc(
ampdoc,
extensionId
extensionId,
// The legacy auto-extensions are always 0.1.
'0.1'
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/unit/test-custom-element-registry.js
Expand Up @@ -153,7 +153,7 @@ describes.realWin('CustomElement register', {amp: true}, (env) => {
const element = doc.createElement('amp-element2');
doc.body.appendChild(element);
expect(stub).to.be.calledOnce;
expect(stub).to.be.calledWithExactly(ampdoc, 'amp-element2');
expect(stub).to.be.calledWithExactly(ampdoc, 'amp-element2', '0.1');
});

it('should not install declared pre-stubbed element extension', () => {
Expand Down
37 changes: 37 additions & 0 deletions test/unit/test-custom-element.js
Expand Up @@ -243,6 +243,43 @@ describes.realWin('CustomElement', {amp: true}, (env) => {
// expect(build.calledOnce).to.equal(true);
});

it('StubElement - should try to install an unregistered legacy extensions', () => {
const LegacyElementClass = createAmpElementForTesting(win, ElementStub);
win.customElements.define('amp-legacy', LegacyElementClass);
win.__AMP_EXTENDED_ELEMENTS['amp-legacy'] = ElementStub;

const extensions = Services.extensionsFor(win);
env.sandbox.stub(extensions, 'installExtensionForDoc');

const element = new LegacyElementClass();
env.sandbox.stub(element, 'buildInternal');

container.appendChild(element);
expect(element.readyState).to.equal('upgrading');
expect(extensions.installExtensionForDoc).to.be.calledOnce.calledWith(
ampdoc,
'amp-legacy',
'0.1'
);
});

it('StubElement - should not try to install a pre-registered legacy extensions', () => {
const LegacyElementClass = createAmpElementForTesting(win, ElementStub);
win.customElements.define('amp-legacy', LegacyElementClass);
win.__AMP_EXTENDED_ELEMENTS['amp-legacy'] = ElementStub;
ampdoc.declareExtension('amp-legacy');

const extensions = Services.extensionsFor(win);
env.sandbox.stub(extensions, 'installExtensionForDoc');

const element = new LegacyElementClass();
env.sandbox.stub(element, 'buildInternal');

container.appendChild(element);
expect(element.readyState).to.equal('upgrading');
expect(extensions.installExtensionForDoc).to.not.be.called;
});

it('Element - should only add classes on first attachedCallback', () => {
const element = new ElementClass();
const buildPromise = Promise.resolve();
Expand Down

0 comments on commit bb49466

Please sign in to comment.