Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Find sizer and apply slot before layoutCallback #35784

Merged
merged 5 commits into from
Aug 31, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Apply sizer slot in buildInternal
  • Loading branch information
caroqliu committed Aug 24, 2021
commit 719e8f5cd16a3963233ba995bbcee657c53e916b
2 changes: 2 additions & 0 deletions src/custom-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,7 @@ function createBaseCustomElementClass(win, elementConnectedCallback) {

// Create the instance.
const implPromise = this.createImpl_();
this.getSizer_();

// Wait for consent.
const consentPromise = implPromise.then(() => {
Expand Down Expand Up @@ -962,6 +963,7 @@ function createBaseCustomElementClass(win, elementConnectedCallback) {
) {
// Expect sizer to exist, just not yet discovered.
this.sizerElement = this.querySelector('i-amphtml-sizer');
this.sizerElement?.setAttribute('slot', 'i-amphtml-svc');
}
return this.sizerElement || null;
}
Expand Down
10 changes: 10 additions & 0 deletions test/unit/test-custom-element-v1.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ describes.realWin('CustomElement V1', {amp: true}, (env) => {
);

const element = new ElementClass();
const getSizerStub = env.sandbox.stub(element, 'getSizer_');
doc.body.appendChild(element);

const promise = element.buildInternal();
Expand All @@ -259,6 +260,7 @@ describes.realWin('CustomElement V1', {amp: true}, (env) => {
expect(element).to.not.have.class('i-amphtml-built');
expect(element.signals().get(CommonSignals.BUILT)).to.be.null;
expect(attachedCallbackStub).to.not.be.called;
expect(getSizerStub).to.be.calledOnce;

await promise;
expect(getImplSyncForTesting(element)).to.be.instanceOf(TestElement);
Expand All @@ -280,6 +282,7 @@ describes.realWin('CustomElement V1', {amp: true}, (env) => {
);

const element = new ElementClass();
const getSizerStub = env.sandbox.stub(element, 'getSizer_');
doc.body.appendChild(element);

const promise = element.mountInternal();
Expand All @@ -293,6 +296,7 @@ describes.realWin('CustomElement V1', {amp: true}, (env) => {
expect(element).to.not.have.class('i-amphtml-built');
expect(element.signals().get(CommonSignals.BUILT)).to.be.null;
expect(attachedCallbackStub).to.not.be.called;
expect(getSizerStub).to.be.calledOnce;

await promise;
expect(getImplSyncForTesting(element)).to.be.instanceOf(TestElement);
Expand All @@ -314,6 +318,7 @@ describes.realWin('CustomElement V1', {amp: true}, (env) => {
);

const element = new StubElementClass();
const getSizerStub = env.sandbox.stub(element, 'getSizer_');
doc.body.appendChild(element);
element.upgrade(TestElement);

Expand All @@ -324,6 +329,7 @@ describes.realWin('CustomElement V1', {amp: true}, (env) => {
expect(element.isBuilt()).to.be.false;
expect(element.readyState).to.equal('building');
expect(attachedCallbackStub).to.not.be.called;
expect(getSizerStub).to.be.calledOnce;

await promise;
expect(getImplSyncForTesting(element)).to.be.instanceOf(TestElement);
Expand All @@ -345,6 +351,7 @@ describes.realWin('CustomElement V1', {amp: true}, (env) => {
);

const element = new StubElementClass();
const getSizerStub = env.sandbox.stub(element, 'getSizer_');
doc.body.appendChild(element);
element.upgrade(TestElement);

Expand All @@ -355,6 +362,7 @@ describes.realWin('CustomElement V1', {amp: true}, (env) => {
expect(element.isBuilt()).to.be.false;
expect(element.readyState).to.equal('building');
expect(attachedCallbackStub).to.not.be.called;
expect(getSizerStub).to.be.calledOnce;

await promise;
expect(getImplSyncForTesting(element)).to.be.instanceOf(TestElement);
Expand All @@ -373,11 +381,13 @@ describes.realWin('CustomElement V1', {amp: true}, (env) => {
env.sandbox.stub(TestElement, 'usesLoading').returns(true);

const element = new ElementClass();
const getSizerStub = env.sandbox.stub(element, 'getSizer_');
doc.body.appendChild(element);

await element.mountInternal();
expect(buildCallbackStub).to.be.calledOnce;
expect(element.readyState).to.equal('loading');
expect(getSizerStub).to.be.calledOnce;
});

it('should continue in a state if modified by buildCallback', async () => {
Expand Down
2 changes: 2 additions & 0 deletions test/unit/test-custom-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,7 @@ describes.realWin('CustomElement', {amp: true}, (env) => {

it('Element - build allowed', () => {
const element = new ElementClass();
const getSizerStub = env.sandbox.stub(element, 'getSizer_');

expect(element.isBuilt()).to.equal(false);
expect(testElementBuildCallback).to.have.not.been.called;
Expand All @@ -585,6 +586,7 @@ describes.realWin('CustomElement', {amp: true}, (env) => {
expect(element).to.not.have.class('i-amphtml-notbuilt');
expect(element).to.not.have.class('amp-notbuilt');
expect(element).to.have.class('i-amphtml-built');
expect(getSizerStub).to.be.calledOnce;
expect(testElementBuildCallback).to.be.calledOnce;
expect(element.signals().get(CommonSignals.BUILT)).to.be.ok;
return element.whenBuilt(); // Should eventually resolve.
Expand Down