Skip to content

Commit c82c4eb

Browse files
authored
Merge pull request #148 from jviide/subtree-staticness
Add a test for forcing subtree staticness
2 parents 7623ca1 + 63b86ea commit c82c4eb

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

test/statics-caching.test.mjs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,39 @@ describe('htm', () => {
8989
expect(html`<div x=${1}><a y=${2} /><b /></div>`).toBe(3);
9090
});
9191
});
92+
93+
describe('the h function should be able to modify `this[0]`', () => {
94+
function wrapH(h) {
95+
return function(type, props, ...children) {
96+
if (type === '@static') {
97+
this[0] &= ~3;
98+
return children;
99+
}
100+
if (props['@static']) {
101+
this[0] &= ~3;
102+
}
103+
return h(type, props, ...children);
104+
};
105+
}
106+
107+
test('should be able to force subtrees to be static via a prop', () => {
108+
const html = htm.bind(wrapH(h));
109+
const x = () => html`<div @static>${'a'}</div>`;
110+
const a = x();
111+
const b = x();
112+
expect(a).toEqual({ tag: 'div', props: { '@static': true }, children: ['a'] });
113+
expect(b).toEqual({ tag: 'div', props: { '@static': true }, children: ['a'] });
114+
expect(a).toBe(b);
115+
});
116+
117+
test('should be able to force subtrees to be static via a special tag', () => {
118+
const html = htm.bind(wrapH(h));
119+
const x = () => html`<@static>${'a'}<//>`;
120+
const a = x();
121+
const b = x();
122+
expect(a).toEqual(['a']);
123+
expect(b).toEqual(['a']);
124+
expect(a).toBe(b);
125+
});
126+
});
92127
});

0 commit comments

Comments
 (0)