Skip to content

Commit

Permalink
#setOwner should rewrite resource.owner_ value if it exist (#5898)
Browse files Browse the repository at this point in the history
* fix

* add test

* add instance method to update owner value
  • Loading branch information
zhouyx authored and lannka committed Oct 31, 2016
1 parent c723386 commit 79a4253
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/service/resource.js
Expand Up @@ -99,6 +99,9 @@ export class Resource {
*/
static setOwner(element, owner) {
dev().assert(owner.contains(element), 'Owner must contain the element');
if (Resource.forElementOptional(element)) {
Resource.forElementOptional(element).updateOwner(owner);
}
element[OWNER_PROP_] = owner;
}

Expand Down Expand Up @@ -185,6 +188,14 @@ export class Resource {
return this.id_;
}

/**
* Update owner element
* @param {!AmpElement} owner
*/
updateOwner(owner) {
this.owner_ = owner;
}

/**
* Returns an owner element or null.
* @return {?AmpElement}
Expand Down
35 changes: 35 additions & 0 deletions test/functional/test-resource.js
Expand Up @@ -513,6 +513,41 @@ describe('Resource', () => {
});
});

describe('Resource set/get ownership', () => {
let child;
let parentResource;
let resources;
beforeEach(() => {
const parent = {
ownerDocument: {defaultView: window},
tagName: 'AMP-STICKY-AD',
isBuilt: () => false,
contains: () => true,
};
child = {
ownerDocument: {defaultView: window},
tagName: 'AMP-AD',
isBuilt: () => false,
contains: () => true,
};
resources = new Resources(new AmpDocSingle(window));
parentResource = new Resource(1, parent, resources);
});

it('should set resource before Resource created for child element', () => {
resources.setOwner(child, parentResource.element);
const childResource = new Resource(1, child, resources);
expect(childResource.getOwner()).to.equal(parentResource.element);
});

it('should always get the lastest owner value', () => {
const childResource = new Resource(1, child, resources);
expect(childResource.getOwner()).to.be.null;
resources.setOwner(childResource.element, parentResource.element);
expect(childResource.owner_).to.equal(parentResource.element);
expect(childResource.getOwner()).to.equal(parentResource.element);
});
});

describe('unlayoutCallback', () => {
it('should NOT call unlayoutCallback on unbuilt element', () => {
Expand Down

0 comments on commit 79a4253

Please sign in to comment.