Skip to content
This repository has been archived by the owner on Jul 30, 2018. It is now read-only.

Commit

Permalink
Fix behavior for multiple nodes with the same root in intersection me…
Browse files Browse the repository at this point in the history
…ta (#750)

Resolves #727
  • Loading branch information
maier49 committed Nov 8, 2017
1 parent 23fdc0e commit c93a258
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/meta/Intersection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ export class Intersection extends Base {
return defaultIntersection;
}

let details = this._getDetails(options);
if (!details) {
details = this._createDetails(options, rootNode);
let details = this._getDetails(options) || this._createDetails(options, rootNode);
if (!details.entries.get(node)) {
details.entries.set(node, defaultIntersection);
details.observer.observe(node);
}

Expand Down
83 changes: 83 additions & 0 deletions tests/unit/meta/Intersection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,89 @@ registerSuite('meta - Intersection', {
assert.lengthOf(observers, 2);
intersection.get('foo', { root: 'root'});
assert.lengthOf(observers, 2);
},
'observing multiple elements with the same root'() {
const nodeHandler = new NodeHandler();
const observeStub = stub();
const intersection = new Intersection({
invalidate: () => {},
nodeHandler,
bind: bindInstance
});
const root = document.createElement('div');
const bar = document.createElement('div');

intersectionObserver.restore();
intersectionObserver = stub(global, 'IntersectionObserver', function (callback: any) {
const observer = {
observe: observeStub,
takeRecords: stub().returns([])
};
observers.push([ observer, callback ]);
return observer;
});

nodeHandler.add(root, 'foo');
nodeHandler.add(bar, 'bar');
nodeHandler.add(root, 'baz');
nodeHandler.add(root, 'root');

intersection.get('foo');
assert.lengthOf(observers, 1);
assert.equal(observeStub.callCount, 1, 'Should have observed node');
intersection.get('foo', { root: 'root'});
assert.equal(observeStub.callCount, 2, 'Should have observed node with different options');
assert.lengthOf(observers, 2);
intersection.get('bar');
assert.equal(observeStub.callCount, 3, 'Should have observed new node');
assert.lengthOf(observers, 2);
intersection.get('bar', { root: 'root'});
assert.lengthOf(observers, 2);
assert.equal(observeStub.callCount, 4, 'Should have observed new node with different options');

intersection.get('bar', { root: 'root'});
intersection.get('bar');
intersection.get('foo');
intersection.get('foo', { root: 'root'});
assert.lengthOf(observers, 2);
assert.equal(observeStub.callCount, 4, 'Should not have observed the same nodes again');
},
'observation should be based on node, not key'() {
const nodeHandler = new NodeHandler();
const observeStub = stub();
const intersection = new Intersection({
invalidate: () => {},
nodeHandler,
bind: bindInstance
});
const root = document.createElement('div');

intersectionObserver.restore();
intersectionObserver = stub(global, 'IntersectionObserver', function (callback: any) {
const observer = {
observe: observeStub,
takeRecords: stub().returns([])
};
observers.push([ observer, callback ]);
return observer;
});

nodeHandler.add(root, 'foo');
nodeHandler.add(root, 'baz');
nodeHandler.add(root, 'root');

intersection.get('foo');
assert.lengthOf(observers, 1);
assert.equal(observeStub.callCount, 1, 'Should have observed node');
intersection.get('foo', { root: 'root'});
assert.equal(observeStub.callCount, 2, 'Should have observed node with different options');
assert.lengthOf(observers, 2);
intersection.get('baz');
assert.equal(observeStub.callCount, 2, 'Should not have observed with the same node and options');
assert.lengthOf(observers, 2);
intersection.get('baz', { root: 'root'});
assert.equal(observeStub.callCount, 2, 'Should not have observed with the same node and options');
assert.lengthOf(observers, 2);
}
}
}
Expand Down

0 comments on commit c93a258

Please sign in to comment.