Skip to content

Commit aaa06de

Browse files
iterianiSepand Parhami
authored and
Sepand Parhami
committed
Add getKey method with tests (#384)
1 parent 8b96c8c commit aaa06de

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
export {applyAttr, applyProp, attributes,} from './src/attributes';
1919
export {close, currentElement, currentPointer, open, patchInner as patch, patchInner, patchOuter, skip, skipNode} from './src/core';
20-
export {clearCache, importNode} from './src/node_data';
20+
export {getKey, clearCache, importNode} from './src/node_data';
2121
export {notifications} from './src/notifications';
2222
export {symbols} from './src/symbols';
2323
export {attr, elementClose, elementOpen, elementOpenEnd, elementOpenStart, elementVoid, text, key} from './src/virtual_elements';

src/node_data.ts

+6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717

1818
import {Key, NameOrCtorDef} from './types';
19+
import {assert} from './assertions';
1920
import {isElement, isText} from './dom_util';
2021

2122

@@ -88,6 +89,10 @@ function getData(node: Node, key?: Key) {
8889
return importSingleNode(node, key);
8990
}
9091

92+
function getKey(node: Node) {
93+
assert(node['__incrementalDOMData']);
94+
return getData(node).key;
95+
}
9196

9297
/**
9398
* Imports single node and its subtree, initializing caches.
@@ -163,6 +168,7 @@ function recordAttributes(node: Element, data: NodeData) {
163168
/** */
164169
export {
165170
getData,
171+
getKey,
166172
initData,
167173
importNode,
168174
clearCache,

test/functional/keyed_items.ts

+26-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
// taze: mocha from //third_party/javascript/typings/mocha
1919
// taze: chai from //third_party/javascript/typings/chai
2020

21-
import {currentElement, elementClose, elementOpen, elementVoid, patch, skip, text, clearCache} from '../../index';
21+
import {currentElement, currentPointer, elementClose, elementOpen, elementVoid, getKey, patch, skip, text, clearCache} from '../../index';
2222
import {assertHTMLElement, attachShadow, BROWSER_SUPPORTS_SHADOW_DOM,} from '../util/dom';
2323
const {expect} = chai;
2424

@@ -373,3 +373,28 @@ describe('rendering with keys', () => {
373373
}
374374
});
375375
});
376+
377+
describe('getKey', () => {
378+
it('should fail if the element has no node data', () => {
379+
const div = document.createElement('div');
380+
expect(() => {
381+
getKey(div);
382+
}).to.throw('Expected value to be defined');
383+
});
384+
385+
it('should return undefined if the element has no key', () => {
386+
const div = document.createElement('div');
387+
patch(div, () => {
388+
elementVoid('div');
389+
});
390+
expect(getKey(div.firstChild!)).to.be.undefined;
391+
});
392+
393+
it('should return a key if the element has a key', () => {
394+
const div = document.createElement('div');
395+
patch(div, () => {
396+
elementVoid('div', '3');
397+
});
398+
expect(getKey(div.firstChild!)).to.equal('3')
399+
});
400+
});

0 commit comments

Comments
 (0)