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

Meta providers accept number or string keys #721

Merged
merged 2 commits into from
Oct 16, 2017
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
16 changes: 11 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/meta/Dimensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const defaultDimensions = {

export class Dimensions extends Base {

public get(key: string): Readonly<DimensionResults> {
public get(key: string | number): Readonly<DimensionResults> {
const node = this.getNode(key);

if (!node) {
Expand Down
2 changes: 1 addition & 1 deletion src/meta/Drag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ const controller = new DragController();
export class Drag extends Base {
private _boundInvalidate: () => void = this.invalidate.bind(this);

public get(key: string): Readonly<DragResults> {
public get(key: string | number): Readonly<DragResults> {
const node = this.getNode(key);

// if we don't have a reference to the node yet, return an empty set of results
Expand Down
2 changes: 1 addition & 1 deletion src/meta/Intersection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class Intersection extends Base {
* @param key The key to return the intersection meta for
* @param options The options for the request
*/
public get(key: string, options: IntersectionGetOptions = {}): IntersectionResult {
public get(key: string | number, options: IntersectionGetOptions = {}): IntersectionResult {
let rootNode: HTMLElement | undefined;
if (options.root) {
rootNode = this.getNode(options.root);
Expand Down
2 changes: 1 addition & 1 deletion src/meta/Matches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default class Matches extends Base {
* @param key The virtual DOM key
* @param event The event object
*/
public get(key: string, event: Event): boolean {
public get(key: string | number, event: Event): boolean {
return this.getNode(key) === event.target;
}
}
10 changes: 10 additions & 0 deletions tests/unit/meta/Dimensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ registerSuite({

assert.deepEqual(dimensions.get('foo'), defaultDimensions);
},
'Will accept a number key'() {
const nodeHandler = new NodeHandler();

const dimensions = new Dimensions({
invalidate: () => {},
nodeHandler
});

assert.deepEqual(dimensions.get(1234), defaultDimensions);
},
'Will create event listener for node if not yet loaded'() {
const nodeHandler = new NodeHandler();
const onSpy = spy(nodeHandler, 'on');
Expand Down
28 changes: 28 additions & 0 deletions tests/unit/meta/Drag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,34 @@ registerSuite({
document.body.removeChild(div);
},

'standard rendering with a number key'() {
const dragResults: DragResults[] = [];

class TestWidget extends ProjectorMixin(ThemeableMixin(WidgetBase)) {
render() {
dragResults.push(this.meta(Drag).get(1234));
return v('div', {
innerHTML: 'hello world',
key: 1234
});
}
}

const div = document.createElement('div');

document.body.appendChild(div);

const widget = new TestWidget();
widget.append(div);

resolveRAF();

assert.deepEqual(dragResults, [ emptyResults, emptyResults ], 'should have been called twice, both empty results');

widget.destroy();
document.body.removeChild(div);
},

'pointer dragging a node'() {
const dragResults: DragResults[] = [];

Expand Down
13 changes: 13 additions & 0 deletions tests/unit/meta/Intersection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,19 @@ registerSuite({
assert.isTrue(onSpy.calledOnce);
assert.isTrue(onSpy.firstCall.calledWith('root'));
},
'intersections with number key'() {
const nodeHandler = new NodeHandler();
const onSpy = spy(nodeHandler, 'on');

const intersection = new Intersection({
invalidate: () => {},
nodeHandler
});

intersection.get(1234);
assert.isTrue(onSpy.calledOnce);
assert.isTrue(onSpy.firstCall.calledWith(1234));
},
'intersection calls invalidate when node available'() {
const nodeHandler = new NodeHandler();
const onSpy = spy(nodeHandler, 'on');
Expand Down
34 changes: 34 additions & 0 deletions tests/unit/meta/Matches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,40 @@ registerSuite({
document.body.removeChild(div);
},

'node matches with number key'() {
const results: boolean[] = [];

class TestWidget extends ProjectorMixin(ThemeableMixin(WidgetBase)) {
private _onclick(evt: MouseEvent) {
results.push(this.meta(Matches).get(1234, evt));
}

render() {
return v('div', {
innerHTML: 'hello world',
key: 1234,
onclick: this._onclick
});
}
}

const div = document.createElement('div');

document.body.appendChild(div);

const widget = new TestWidget();
widget.append(div);

resolveRAF();

sendEvent(div.firstChild as Element, 'click');

assert.deepEqual(results, [ true ], 'should have been called and the target matched');

widget.destroy();
document.body.removeChild(div);
},

'node does not match'() {
const results: boolean[] = [];

Expand Down